-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
162 lines (120 loc) · 3.75 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Grammar for a small subset of the language (incrementally added).
------------------------------------------------------------------
GRAMMAR TERMINOLOGY
------------------------------------------------------------------
terminal A literal to be matched and consumed
nonterminal A call to the production rule
------------------------------------------------------------------
GRAMMAR NOTATION
------------------------------------------------------------------
<rule name>: Production rule structure
| <rule definition>
<double quoted char sequence> Exact terminal
<uppercase char sequence> Varying terminal
<lowercase char sequence> Nonterminal
x* Match zero or more of x
x+ Match one or more of x
x? Match zero or one of x
x1 x2 Match x1 then x2
x1 | x2 Match x1 or x2
( x ) Match x (everything inside the parentheses)
\ Escape character
v1 ... v2 Any value in the range from v1 to v2 inclusive
< Start of comment describing the rule
> End of comment describing the rule
------------------------------------------------------------------
START RULES
------------------------------------------------------------------
file:
| statement* EOF
repl:
| statementRepl # TODO
------------------------------------------------------------------
STATEMENT RULES
------------------------------------------------------------------
statement:
| blockStatement
| expressionStatement
| foreachStatement
| ifStatement
| outStatement # Temporary until built-in function
| varStatement
| whileStatement
blockStatement:
| "block" standardBlock
expressionStatement:
| expression NEWLINE
foreachStatement:
| "foreach" IDENTIFIER "in" expression ".." expression ( "step" expression )? standardBlock
ifStatement:
| "if" expression selectionBlock ( "else" selectionBlock )? "end" NEWLINE
outStatement:
| "@out" expression NEWLINE
varStatement:
| "var" IDENTIFIER ":" expression NEWLINE
whileStatement:
| "while" expression ( "{" expression "}" )? standardBlock
------------------------------------------------------------------
HELPER RULES
------------------------------------------------------------------
standardBlock:
| NEWLINE statement* "end" NEWLINE
selectionBlock:
| NEWLINE statement*
------------------------------------------------------------------
EXPRESSION RULES
------------------------------------------------------------------
expression:
| assignment
| disjunction
# TODO: Prefix with `( call "." )?` when adding calls
assignment:
| IDENTIFIER ( ":" | "+:" | "-:" | "*:" | "/:" ) expression
disjunction:
| conjunction ( "or" conjunction )*
conjunction:
| equality ( "and" equality )*
equality:
| comparison ( ( "=" | "!=" ) comparison )*
comparison:
| term ( ( "<" | "<=" | ">" | ">=" ) term )*
term:
| factor ( ( "+" | "-" ) factor )*
factor:
| unary ( ( "*" | "/" | "mod" ) unary )*
unary:
| ( "not" | "-" ) unary
| atom # TODO: Change to `call` when adding calls
atom:
| IDENTIFIER
| NUMBER
| TEXT
| "true"
| "false"
| "none"
| group
group:
| "(" expression ")"
------------------------------------------------------------------
LEXICAL RULES
------------------------------------------------------------------
IDENTIFIER:
| ALPHA ( ALPHA | DIGIT )*
NATIVE_IDENTIFIER:
| "@" IDENTIFIER
NUMBER:
| DIGIT+ ( "." DIGIT+ )?
TEXT:
| "\"" <any ASCII character except "\"">* "\""
ALPHA:
| "a" ... "z"
| "A" ... "Z"
| "_"
DIGIT:
| "0" ... "9"
NEWLINE:
| "\n"
| "\r"
| "\r\n"
EOF:
| <end of file>