-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
136 lines (94 loc) · 3.14 KB
/
grammar.txt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
start: function_declaration*
function_declaration: NAME "(" function_parameters? ")" function_return_type "{" statements_block "}"
function_parameters: function_parameter ("," function_parameter)*
// Inline
function_return_type: NAME
function_parameter: NAME type
type: NAME ("." NAME)*
// Inline
statement: assignment | for_statement | while_statement | expression | jump_statement
variable_declaration: (VAR | LET) NAME type
statements_block: statement*
assignment: directly_assignable_expression ASSIGNMENT_OPERATOR expression
// Inline
directly_assignable_expression: variable_declaration | NAME
// Inline
assignable_suffix: indexing_suffix
| navigation_suffix
for_statement: FOR NAME IN expression "{" statements_block "}"
while_statement: WHILE expression "{" statements_block "}"
// Inline
expression: disjunction
// Optional inline
disjunction: conjunction (OR conjunction)*
// Optional inline
conjunction: equality (AND equality)*
// Optional inline
equality: comparison (EQUALITY_OPERATOR comparison)?
// Optional inline
comparison: additive_expression (COMPARISON_OPERATOR additive_expression)*
// Optional inline
additive_expression: multiplicative_expression (ADDITIVE_OPERATOR multiplicative_expression)*
// Optional inline
multiplicative_expression: prefix_unary_expression (MULTIPLICATIVE_OPERATOR prefix_unary_expression)*
// Optional inline
prefix_unary_expression: prefix_operator? postfix_unary_expression
// Inline
prefix_operator: NEGATION | ADDITIVE_OPERATOR
// Optional inline
postfix_unary_expression: primary_expression postfix_unary_suffix*
// Inline
postfix_unary_suffix: call_suffix | indexing_suffix | navigation_suffix
call_suffix: "(" function_call_arguments? ")"
// Inline
function_call_arguments: expression ("," expression)*
indexing_suffix: "[" expression "]"
navigation_suffix: "." NAME
// Inline
primary_expression: parenthesized_expression
| NAME
| simple_literal
| collection_literal
| if_expression
parenthesized_expression: "(" expression ")"
collection_literal: "[" expression ("," expression)* "]" | "[" "]"
// Inline
jump_statement: return_statement
| break_statement
return_statement: RETURN expression?
break_statement: BREAK
if_expression: IF expression "{" statements_block "}"
| IF expression "{" statements_block "}" elseif_expression* else_expression?
elseif_expression: ELIF expression "{" statements_block "}"
else_expression: ELSE "{" statements_block "}"
// Inline
simple_literal: STRING | BOOLEAN | DEC_NUMBER | FLOAT_NUMBER
// Don't ignore
DEC_NUMBER: /0|[1-9][\d_]*/i
STRING: LEXER_IMP
EQUALITY_OPERATOR: "!=" | "=="
BOOLEAN: "true" | "false"
FLOAT_NUMBER: /((\d+\.[\d_]*|\.[\d_]+)(e[-+]?\d+)?|\d+(e[-+]?\d+))/i
COMPARISON_OPERATOR: "<" | ">" | "<=" | ">="
ADDITIVE_OPERATOR: "+" | "-"
NEGATION: "!"
MULTIPLICATIVE_OPERATOR: "*" | "/" | "%"
LET: "let"
VAR: "var"
NAME: /(?!(if|elif|or|and|for|while|true|false|ret|break|else|in|var|let)\b)[a-zA-Z_][a-zA-Z_0-9]*/
ASSIGNMENT_OPERATOR: "="
BREAK: "break"
COMMENT: /#[^\r\n]*/
//LETTER: /[a-zA-Z]/
WS: /[ \t\f]/+
NEWLINE: /[\r\n]/
// Non-terminals
RETURN: "ret"
ELSE: "else"
ELIF: "elif"
FOR: "for"
IN: "in"
WHILE: "while"
OR: "or"
AND: "and"
IF: "if"