-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.bnf
67 lines (54 loc) · 2.31 KB
/
grammar.bnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Definition
# Lexer
# É de realçar que a gramática do Natrix foi alterada durante a implementação
# Sendo assim este ficheiro pode não ser 100% fiel
<INTEGER_SUFFIX> ::= "u8"|"u16"|"u32"|"u64"|"u128"|"usize"|"i8"|"i16"|"i32"|"i64"|"i128"|"isize"
<HEX_DIGIT> ::= ['0'-'9' 'a'-'f' 'A'-'F']
<DEC_DIGIT> ::= ['0'-'9']
<OCT_DIGIT> ::= ['0'-'7']
<BIN_DIGIT> ::= ['0'-'1']
<HEX_LITERAL> ::= ("0x"|"0X") (HEX_DIGIT|'_')*
<OCT_LITERAL> ::= ("0o"|"0O") (OCT_DIGIT|'_')*
<BIN_LITERAL> ::= ("0b"|"0B") (BIN_DIGIT|'_')*
<DEC_LITERAL> ::= DEC_DIGIT(DEC_DIGIT|'_')*
<INTEGER_LITERAL>::= (DEC_LITERAL|BIN_LITERAL|OCT_LITERAL|HEX_LITERAL)
<BOOLEAN_LITERAL> ::= "true"|"false"
<digit> ::= ['0'-'9']
<char> ::= ['a-zA-Z']
<id> ::= ('_' | <char>)(<char> | <digit>)*
<newline> ::= '\n'
<whitespace> ::= '\t' | ' '
<binop> ::= '+' | '-' | '/' | '*'
<booleanbop> ::= '>' | '>=' | '==' | '!=' | '<' | '<='
# Parser
<program> ::= <global_statements>
<global_statements> ::=
| 'use' <id>
| 'struct' <id> '{' (...) '}'
| 'impl' <id> '{' (...) '}'
| <function_def>
<statements> ::= ( <statement> | <function_def> )*
<type> ::= <id> | i8 | i16 | i32 | i64 | i128 | u8 | u16 | u32 | u64 | u128 | bool
<statement> ::= <statement>*
| 'if' <boolean_expression> '{' <statement> '}'
'else' '{' <statement> '}'
| 'loop' '{' <statement> '}'
| 'while' <boolean_expression> '{' <statement>'}'
| 'println!' '(' <expression> ')' ';'
| 'print!' '(' <expression> ')' ';'
| 'let' <id> (':' <type>)? '=' <expression> ';'
| 'return' <expr> ';'
| 'continue' ';'
| 'break' ';'
<expression> ::= '(' <expression> ')'
| <expression> <binop> <expression>
| <INTEGER_LITERAL>
| <BOOLEAN_LITERAL>
| <id>
<boolean_expression> ::= '('<expression> <booleanop> <expression> ')'
| <expression> <booleanop> <expression>
| '(' boolean_expression')'
| '!' <boolean_expression>
| <BOOLEAN_LITERAL>
<argument_list> ::= (<id> ':' <type>)*
<function_def> ::= 'fn' <id> '(' <argument_list> ')' '->' <type> '{' <statement> '}'