-
Notifications
You must be signed in to change notification settings - Fork 4
/
Wiki.g4
213 lines (180 loc) · 4.34 KB
/
Wiki.g4
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
grammar Wiki;
/* shangshang pushed*/
/* Lennart just tried github push*/
/* The Start Production */
/* at some point I would like to add import statements */
prog: import_seq prog_seq
;
import_seq: import_stmt NL import_seq
| /* epsilon */
;
import_stmt: IMPORT ID ('.'ID)*;
prog_seq: seq prog_seq
| /* epsilon */
;
seq: func_seq
| main_func
| stmt_seq
| NL
;
main_func: MAIN '()' NL stmt_seq END
;
stmt_seq: stmt NL stmt_seq
| stmt (NL|)
;
func_seq: func NL func_seq
| func
;
stmt: PRINT '(' expr ')' # Print
| type ident # Declare
| type ident '=' expr # DecAssign
| ident '=' expr # Assign
| (ident u=(PP|MM) | u=(PP|MM) ident) # IncDec
| func_action # FuncAct
| if_stmt # IfStmt
| while_stmt # WhileStmt
| for_stmt # ForStmt
| comm # Comment
| static_fcall # StaticCall
| BRK # Break
| ret_stmt # Ret
;
comm: LCOM
| COMMENT
;
ident: ID ('['(int_expr|static_fcall)']'|'['']')*
;
/* Loop Types ***************/
for_stmt: FOR '('stmt ';'expr';'stmt')' NL stmt_seq END;
while_stmt: WHILE '('expr')' NL stmt_seq END;
/*****************************/
/* if statements *************/
if_stmt: IF '('expr')' NL stmt_seq else_stmt END;
else_stmt: ELSE NL stmt_seq
| /* epsilon */
;
/*****************************/
func_action: ident '=' ID '(' params ')'# FuncAssign
| type ident '=' ID '(' params ')' # FuncDecAssign
| ID '(' params ')' # FuncCall
;
/* maybe a funccall should be an expression, not sure yet */
expr: array_expr
| int_expr
| bool_expr
| str_expr
| static_fcall
;
array_expr: ident
| '{'expr (','expr)*'}'
;
static_fcall: ID '.' ID '('expr')'
| ID '.' ID '()'
| ID '.' ID
;
/* Function Definition *******/
func: FUNC (type|) ID'('args')' NL func_stmt (ret_stmt|) END # FuncDef
;
func_stmt: stmt NL func_stmt # FuncStmt
| /* epsilon */ # NoFunctStmt
| ID '(' params ')' # FCall
;
/* Function Arguments ********/
params: expr ',' params
| expr
| /* epsilon */
;
args: type ident',' args
| type ident
| /* epsilon */
;
/****************************/
/* Return Statements ********/
ret_stmt: RETURN expr NL # RetExpr
;
/****************************/
/* Boolean Expressions ******/
bool_expr: bool_expr '||' bool_term
| bool_term
;
bool_term: bool_term '&&' bool_fact
| bool_fact
;
bool_fact: '(' bool_expr ')'
| TRUE
| FALSE
| ident
| '!'bool_fact
| cond
;
cond: int_expr (LT|GT|LTE|GTE|EQ|NEQ) expr;
/****************************/
/* String Expressions *******/
str_expr: STRLIT '+' str_expr # ConcatStr
| ident '+' str_expr # ConcatId
| static_fcall '+' str_expr # ConcatFcall
| STRLIT # StrLit
| ident # IdString
| static_fcall # Fcall
;
/****************************/
/* Arithmetic Expressions ***/
int_expr: int_expr op=(ADD|SUB) term# AddSub
| term # TermExpr
;
term: term op=(MUL|DIV|MOD) fact # MulDiv
| fact # FactTerm
;
fact: '('expr')'
| NUM
| ident
| static_fcall
;
/****************************/
type: INT
| STRING
| BOOL
| PAGE
| TABLE
| ID /* this will be for user defined types or classes if we allow it*/
;
MUL: '*';
ADD: '+';
DIV: '/';
SUB: '-';
MOD: '%';
GT: '>';
LT: '<';
GTE:'>=';
LTE: '<=';
EQ: '==';
NEQ: '!=';
PP: '++';
MM: '--';
/* Keywords in Wikify *******/
INT: 'num';
STRING: 'string';
BOOL: 'bool';
TABLE: 'table';
PAGE: 'page';
TRUE: 'true';
FALSE: 'false';
PRINT: 'print';
FUNC: 'func';
END: 'end';
RETURN: 'return';
IF: 'if';
ELSE: 'else';
WHILE: 'while';
FOR: 'for';
BRK: 'break';
MAIN: 'main';
IMPORT: 'import';
/****************************/
ID : [a-zA-Z][a-zA-Z0-9]*;
NUM: [0-9]+;
COMMENT: '//'~[\r\n]*;
LCOM: '/*'.*?'*/';
STRLIT: '"'( '\\''"' |~[\r\n])*'"';
NL: [\n]+;
WS : [ \t]+ -> skip;