# Grammar

### Grammar Notation

```
Result  ::= component1 ( component2 | component3 ) | 
            component4 | [component5] component6
- /* comments */
- 'literal'
- (A|B): either of A or B
- A B: A then B
- [A]: Optional A
- (A)* : Repeating A zero or more times
- (A)+ : Repeating A one or more times
- {A}*  : Repeating A zero or more times, separated with comma
- {A}+  : Repeating A one or more times, separated with comma
```

### Top-level

A module is a series of types (named or alias) or bindings (value or function).

```
Module                   ::= ( TypeDecl | BindingDecl )*
```

### Naming basics

```
TypeName                ::= CAPITAL_LETTER (ALPHABET)*
BindingName             ::= ValueBindingName | TypeName | FunctionName
ValueBidningName        ::= (LOWERCASE) (LOWECASE | '_')*
FunctionName            ::= LOWERCASE (LOWERCASE | CAPITAL_LETTER)*
ModuleAlias             ::= ValueBidningName
```

### Type Declaration

Declaration of a named type or type alias

```
TypeDecl                ::= TypeName (':'|'=') TypeLiteral
TypeLiteral             ::= PrimitiveType | StructType | UnionType | 
                            FnType | SeqType | MapType | EnumType | 
                            TypeName
PrimitiveType           ::= 'int' | 'float' | 'string' | 'char' | 
                            'byte' | 'bool' | 'nothing'
StructType              ::= 'struct' '{' {StructField}* '}'
StructField             ::= { Identifier ':' TypeLiteral }
UnionType               ::= (TypeLiteral '|' TypeLiteral) | (TypeLiteral '|' UnionType)
FnType                  ::= 'fn' '(' { TypeLiteral }* '->' { TypeLiteral }* ')'
SeqType                 ::= '[' TypeLiteral ']'
MapType                 ::= '[' TypeLiteral ':' TypeLiteral ']'
EnumType                ::= 'enum' SeqLiteral
```

### Binding Declaration

Types of expressions you can define by combining other expressions (mathematical, struct access, boolean, a function call, ...).

```
BindingDecl             ::= { BindingNameItem }* '=' Expression
BindingNameItem         ::= BindingName [ ':' Type ] | '_'
Expression              ::= EqExpression     { ('and'|'or') EqExpression }*
EqExpression            ::= CmpExpression    { ('=='|'!=') CmpExpression }*
CmpExpression           ::= ShiftExpression  { ('>'|'<'|'>='|'<=') 
                                ShiftExpression }*
ShiftExpression         ::= AddExpression    { ('>>'|'<<'|'^') 
                                AddExpression }*
AddExpression           ::= MulExpression    { ('+'|'-') 
                                MulExpression }*
MulExpression           ::= UnaryExpression  { ('*'|'/'|'%'|'%%') 
                                UnaryExpression }*
UnaryExpression         ::= ['not'|'-']      BasicExpression
BasicExpression         ::= ['('] PrimaryExpression [')']
PrimaryExpression       ::= Literal | 
                                Identifier | 
                                StructAccessExpression | 
                                MapSeqAccessExpression | 
                                FnCallExpression | 
                                StructExpression | 
                                LambdaCreatorExpression | 
                                FnDeclaration |
                                IfExpression | 
                                ElseExpression | 
                                ErrorCheckExpression
StructAccessExpression  ::= Expression '.' Identifier
MapSeqAccessExpression  ::= Expression '[' Expression ']'
FnCallExpression        ::= Expression '(' { Expression }* ')'
StructExpression        ::= ( TypeName | StructType | '&' ) 
                                '{' {FieldValueList}* '}'
FieldValueList          ::= { [ Identifier ':' ] Expression }
LambdaCreatorExpression ::= Expression '(' { Expression | '_' }* ')'
FnDeclaration           ::= 'fn' ['(' { Identifier ':' Type } 
                                '->' {Type}+ ')'] '{' Expression+ '}'
IfExpression            ::= Expression '?' Expression
ElseExpression          ::= Expression '//' Expression
ErrorCheckExpression    ::= Expression '@' [ '{' Expression '}' ]
Literal                 ::= IntLiteral | 
                                FloatLiteral | 
                                CharLiterl | 
                                StringLiteral | 
                                NothingLiteral | 
                                BoolLiteral | 
                                SeqLiteral | 
                                MapLiteral | 
                                StructLiteral | 
                                TypeLiteral
```

## To Be Added Later

* Generics
* Concurrency and channels&#x20;
* Modules and import


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dotlang.org/grammar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
