-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOberonEmitter.g
390 lines (307 loc) · 10.5 KB
/
OberonEmitter.g
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
tree grammar OberonEmitter;
options
{
tokenVocab = Oberon07;
ASTLabelType = CommonTree;
output = template;
}
@header
{
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
import java.io.*;
}
@members
{
public static void main(String[] args) throws IOException, RecognitionException, FileNotFoundException
{
// CONST
String stg_load_path = ".gen"; // colon-separated string
// VAR
CommonTokenStream tokens;
CommonTreeNodeStream nodes;
CommonTree ast;
Oberon07Parser parser;
OberonEmitter emitter;
// BEGIN
// build the lexer:
tokens = new CommonTokenStream(
new Oberon07Lexer(
new ANTLRInputStream(System.in)));
// build the parser:
parser = new Oberon07Parser(tokens);
// parse the input stream:
ast = (CommonTree) parser.module().getTree();
// turn the ast into a stream of nodes:
nodes = new CommonTreeNodeStream(ast);
nodes.setTokenStream(tokens);
// hook the node stream up to the emitter:
emitter = new OberonEmitter(nodes);
// StringTemplate part:
StringTemplate result;
StringTemplateGroup stg;
StringTemplateGroupLoader loader;
String main_stg_path;
/*
// You need all this junk to allow template inheritance:
loader = new CommonGroupLoader(main_stg_path,
new StringTemplateErrorListener()
{
public void error(String msg, Throwable e)
{
System.out.println(msg + ":" + e.toString());
}
public void warning(String msg)
{
System.out.println(msg);
}
}
);
StringTemplateGroup.registerGroupLoader(loader);
// now we can load the main language template
// stg = StringTemplateGroup.loadGroup("Oberon");
*/
// configure the template group to use:
main_stg_path = (args.length == 1)? args[0] : ".gen/Oberon.stg";
stg = new StringTemplateGroup(new FileReader(main_stg_path));
// finally, hook the template to the emitter and generate output:
emitter.setTemplateLib(stg);
result = (StringTemplate) emitter.emit().getTemplate();
System.out.println(result.toString());
}
public String getErrorMessage(RecognitionException e,
String[] tokenNames)
{
List stack = getRuleInvocationStack(e, this.getClass().getName());
String msg = null;
if ( e instanceof NoViableAltException ) {
NoViableAltException nvae = (NoViableAltException)e;
msg = " no viable alt; token="+e.token+
" (decision="+nvae.decisionNumber+
" state "+nvae.stateNumber+")"+
" decision=";
}
else
{
msg = super.getErrorMessage(e, tokenNames);
}
return stack+" "+msg;
}
public String getTokenErrorDisplay(Token t)
{
return t.toString();
}
}
emit
: module -> emit(data={$module.st})
;
ident
: Ident -> ident(name={$Ident.text})
;
new_ident
: ^(PUBLIC pub=ident) -> new_public(ident={$pub.st})
| priv=ident -> {$priv.st}
;
qualident
: ^(DOT a=ident b=ident) -> qualident(mod={$a.st}, ident={$b.st})
| ident -> {$ident.st}
;
typed_idents
: ^(DECLARE type names+=new_ident+) -> declare(type={$type.st}, names={$names})
;
const_decl
: ^(EQ id=new_ident expr) -> declare_const(name={$id.st}, value={$expr.st})
;
type_decl
: ^(EQ new_ident structure) -> declare_type(name={$new_ident.st}, structure={$structure.st})
;
structure
: a=array_type -> {$a.st}
| r=record_type -> {$r.st}
| po=pointer_type -> {$po.st}
| pr=proc_type -> {$pr.st}
;
type
: q=qualident -> {$q.st}
| s=structure -> {$s.st}
;
array_type
: ^(ARRAY type dims+=expr+) -> array_type(dims={$dims}, of_type={$type.st})
;
record_type
: ^(RECORD base=base_type? fields+=typed_idents*)
-> record_type(base={$base.st}, fields={$fields})
;
base_type
: ^(EXTENDS qualident) -> based_on(qual={$qualident.st})
;
pointer_type
: ^(POINTER type) -> pointer_to(type={$type.st})
;
proc_type
: ^(PROCEDURE sig=signature? ret=return_type?) -> proc_type(sig={$sig.st}, ret={$ret.st})
;
var_decl
: ids=typed_idents -> {$ids.st}
;
designator
: chain -> {$chain.st}
;
head
: qualident -> {$qualident.st}
;
chain
: head -> {$head.st}
| ^(ATTR a=chain ident) -> getattr(obj={$a.st}, attr={$ident.st})
| ^(INDEX i=chain keys+=expr+) -> index(obj={$i.st}, keys={$keys})
| ^(DEREF d=chain) -> deref(obj={$d.st})
| ^(GUARD g=chain wanted=qualident) -> type_guard(obj={$g.st}, wanted={$wanted.st})
;
expr
: designator -> {$designator.st}
| literal -> {$literal.st}
| unary -> {$unary.st}
| binop -> {$binop.st}
| ^(GROUP exp=expr) -> grouped(expr={$exp.st})
| invoke -> {$invoke.st}
;
literal
: HexInt -> literal_hex(hex={$HexInt.text})
| Integer -> literal_int(int={$Integer.text})
| Real -> literal_num(num={$Real.text})
| NIL -> literal_null()
| TRUE -> literal_true()
| FALSE -> literal_false()
| ^(STRING Quoted) -> literal_str(str={$Quoted.text})
| ^(HEXCHAR HexChar) -> literal_chr(chr={$HexChar.text})
;
set_literal
: ^(SET_LIT members+=elem*) -> literal_set(members={$members})
;
elem
: ('..' lo=expr hi=expr) -> range(lo={$lo.st}, hi={$hi.st})
| expr -> {$expr.st}
;
unary
: ^(POS expr) -> positive(expr={$expr.st})
| ^(NEG expr) -> negative(expr={$expr.st})
| ^(NOT expr) -> inverted(expr={$expr.st})
;
binop
: ^(EQ a=expr b=expr) -> op_eq(a={$a.st}, b={$b.st})
| ^(NE a=expr b=expr) -> op_ne(a={$a.st}, b={$b.st})
| ^(LT a=expr b=expr) -> op_lt(a={$a.st}, b={$b.st})
| ^(GT a=expr b=expr) -> op_gt(a={$a.st}, b={$b.st})
| ^(LE a=expr b=expr) -> op_le(a={$a.st}, b={$b.st})
| ^(GE a=expr b=expr) -> op_ge(a={$a.st}, b={$b.st})
| ^(IN a=expr b=expr) -> op_in(a={$a.st}, b={$b.st})
| ^(IS a=expr b=expr) -> op_is(a={$a.st}, b={$b.st})
| ^(ADD a=expr b=expr) -> op_add(a={$a.st}, b={$b.st})
| ^(SUB a=expr b=expr) -> op_sub(a={$a.st}, b={$b.st})
| ^(RAT a=expr b=expr) -> op_rat(a={$a.st}, b={$b.st})
| ^(MUL a=expr b=expr) -> op_mul(a={$a.st}, b={$b.st})
| ^(DIV a=expr b=expr) -> op_div(a={$a.st}, b={$b.st})
| ^(MOD a=expr b=expr) -> op_mod(a={$a.st}, b={$b.st})
| ^(AND a=expr b=expr) -> op_and(a={$a.st}, b={$b.st})
| ^(OR a=expr b=expr) -> op_or(a={$a.st}, b={$b.st})
;
statement
: a=assign -> {$a.st}
| v=invoke_stmt -> {$v.st}
| i=if_stmt -> {$i.st}
| c=case_stmt -> {$c.st}
| w=while_stmt -> {$w.st}
| r=repeat_stmt -> {$r.st}
| f=for_stmt -> {$f.st}
;
assign
: ^(ASSIGN designator expr) -> assign(obj={$designator.st}, expr={$expr.st})
;
invoke
: ^(INVOKE designator args?) -> invoke(obj={$designator.st}, args={$args.st})
;
invoke_stmt // same as above, but a different template, because of semicolon issues in haxe
: ^(INVOKE designator args?) -> invoke_stmt(obj={$designator.st}, args={$args.st})
;
args
: ^(ARGS exps+=expr*) -> args(args={$exps})
;
block
: ^(BLOCK stmts+=statement*) -> block(stmts={$stmts})
| PASS -> pass()
;
if_stmt
: ^(IF conds+=if_cond+ el=else_block?)
-> if_stmt(conds={$conds}, else_block={$el.st})
;
if_cond
: ^(COND expr block) -> if_cond(expr={$expr.st}, block={$block.st})
;
else_block
: ^(ELSE block) -> {$block.st}
;
case_stmt
: ^(CASE expr cases+=a_case) -> case_stmt(expr={$expr.st}, cases={$cases})
;
a_case
: ^(':' labels+=elem+ block) -> a_case(labels={$labels}, block={$block.st})
;
while_stmt
: ^(WHILE expr block elifs+=elsif_do*)
-> while_stmt(cond={$expr.st}, block={$block.st}, elifs={$elifs})
;
elsif_do
: ^(ELSIF expr block) -> elsif_do(cond={$expr.st}, block={$block.st})
;
repeat_stmt
: ^(REPEAT block UNTIL expr) -> repeat_stmt(block={$block.st}, expr={$expr.st})
;
for_stmt
: ^(FOR id=ident beg=expr end=expr step=expr? block)
-> for_stmt(id={$id.st}, beg={$beg.st}, end={$end.st}, step={$step.st}, block={$block.st})
;
declaration
: ^(CONST consts+=const_decl+) -> consts(consts={$consts})
| ^(TYPE types+=type_decl+) -> types(types={$types})
| ^(VAR vars+=var_decl+) -> vars(vars={$vars})
| procedure -> {$procedure.st}
;
procedure
: ^(PROCEDURE id=new_ident sig=signature? rtyp=return_type?
decs+=declaration*
beg=begin?
ret=return_stmt?)
-> procedure(id={$id.st}, sig={$sig.st}, rtyp={$rtyp.st}, decs={$decs}, beg={$beg.st}, ret={$ret.st})
;
signature
: ^(SIGNATURE ps+=param*) -> signature(params={$ps})
;
param
: ^(VAR p=param) -> var_param(param={$p.st})
| ^(t=formal_type names+=ident+) -> param(type={$t.st}, names={$names})
;
formal_type
: (ARRAY OF t=formal_type) -> array_of(type={$t.st})
| qualident -> {$qualident.st}
;
return_type
: ^(RETURNS q=qualident) -> return_type(type={$q.st})
;
begin
: ^(BEGIN block) -> begin(block={$block.st})
;
return_stmt
: ^(RETURN expr) -> return_stmt(value={$expr.st})
;
module
: ^(MODULE ident imp=import_stmt? decs+=declaration* beg=begin?)
-> module(mod={$ident.st}, imp={$imp.st}, decs={$decs}, beg={$beg.st})
;
import_stmt
: ^(IMPORT mods+=module_name*) -> import_stmt(mods={$mods})
;
module_name
: ident -> {$ident.st}
| ^(':=' nick=ident mod=ident) -> import_as(mod={$mod.st}, nick={$nick.st})
;