@@ -126,33 +126,54 @@ public class JJOnionParser implements Parser {
126
126
}
127
127
return new String(b);
128
128
}
129
+
130
+ private void enterSection() {
131
+ token_source.SwitchTo(IN_STATEMENT);
132
+ }
133
+
134
+ private void leaveSection() {
135
+ token_source.SwitchTo(DEFAULT);
136
+ }
129
137
}
130
138
PARSER_END(JJOnionParser)
131
139
132
- SKIP:
140
+ <DEFAULT> SKIP:
133
141
{
134
- " "
142
+ "\r\n "
135
143
| "\t"
136
- | "\n "
144
+ | "\f "
137
145
| "\r"
138
- | "\r\n"
146
+ | "\n"
147
+ | " "
139
148
}
140
149
150
+ <IN_STATEMENT> SKIP:
151
+ {
152
+ " "
153
+ | "\t"
154
+ | "\f"
155
+ }
141
156
157
+ <IN_STATEMENT> TOKEN:
158
+ {
159
+ <EOL: ("\r\n" | "\r" | "\n")>
160
+ }
142
161
143
162
/*
144
163
* COMMENTS
145
164
*/
165
+ <DEFAULT, IN_STATEMENT>
146
166
SPECIAL_TOKEN:
147
167
{
148
- <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" ("*" | (~["*", "/"] (~["*"])* "*"))* "/">
168
+ <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" ("*" | (~["*", "/"] (~["*"])* "*"))* "/">
149
169
| <LINE_COMMENT: "//" (~["\r", "\n"])*>
150
170
| <SHELL_COMMENT: "#!" (~["\r", "\n"])*>
151
171
}
152
172
153
173
/*
154
174
* KEYWORDS
155
175
*/
176
+ <DEFAULT, IN_STATEMENT>
156
177
TOKEN:
157
178
{
158
179
<K_ABSTRACT: "abstract" >
@@ -215,6 +236,7 @@ TOKEN:
215
236
/*
216
237
* OPERATORS
217
238
*/
239
+ <DEFAULT, IN_STATEMENT>
218
240
TOKEN : {
219
241
<PLUS: "+" >
220
242
| <MINUS: "-" >
@@ -269,6 +291,7 @@ TOKEN : {
269
291
/*
270
292
* LITERALS
271
293
*/
294
+ <DEFAULT, IN_STATEMENT>
272
295
TOKEN:
273
296
{
274
297
<INTEGER:
@@ -318,6 +341,7 @@ TOKEN:
318
341
/*
319
342
* ERROR TOKEN
320
343
*/
344
+ <DEFAULT, IN_STATEMENT>
321
345
TOKEN:{
322
346
<ERROR: ~[]>
323
347
}
@@ -334,7 +358,8 @@ AST.CompilationUnit unit() :{
334
358
}
335
359
336
360
AST.ModuleDeclaration module_decl() :{Token t1, t2; StringBuffer sb = new StringBuffer();}{
337
- t1="module" t2=<ID> {sb.append(t2.image);} ("." t2=<ID> {sb.append(t2.image);})* ";" {
361
+ {enterSection();}
362
+ t1="module" t2=<ID> {sb.append(t2.image);} ("." t2=<ID> {sb.append(t2.image);})* eos() {
338
363
return new AST.ModuleDeclaration(p(t1), new String(sb));
339
364
}
340
365
}
@@ -346,10 +371,10 @@ AST.ImportClause import_decl() :{
346
371
ArrayBuffer<Tuple2<String, String>> imports = new ArrayBuffer<Tuple2<String, String>>();
347
372
}{
348
373
t="import" "{"
349
- ( {sb = new StringBuffer();}
374
+ ( {sb = new StringBuffer();enterSection(); }
350
375
( LOOKAHEAD(2)
351
- (n=<ID> {s = n.image;} "=" n=<ID> {sb.append(n.image);} ("." n=<ID> {sb.append("."); sb.append(n.image);})+ ";" )
352
- | ((LOOKAHEAD(2) n=<ID> "." {sb.append(n.image); sb.append(".");})+ (n=<ID> | n="*") ";" {
376
+ (n=<ID> {s = n.image;} "=" n=<ID> {sb.append(n.image);} ("." n=<ID> {sb.append("."); sb.append(n.image);})+ eos() )
377
+ | ((LOOKAHEAD(2) n=<ID> "." {sb.append(n.image); sb.append(".");})+ (n=<ID> | n="*") eos() {
353
378
s = n.image; sb.append(s);
354
379
}
355
380
)
@@ -374,7 +399,8 @@ AST.GlobalVariableDeclaration var_decl(int modifiers) : {
374
399
AST.TypeNode ty = null;
375
400
AST.Expression e = null;
376
401
}{
377
- t1="var" t2=<FID> [":" ty=type()] ["=" e=term()] ";" {
402
+ {enterSection();}
403
+ t1="var" t2=<FID> [":" ty=type()] ["=" e=term()] eos() {
378
404
return new AST.GlobalVariableDeclaration(
379
405
p(t1), modifiers, t2.image.substring(1), ty, e
380
406
);
@@ -388,11 +414,12 @@ AST.FunctionDeclaration fun_decl(int modifiers) : {
388
414
AST.BlockExpression b = null;
389
415
AST.Expression e = null;
390
416
}{
417
+ {enterSection();}
391
418
t1="def" t2=<ID> ["(" args=args() ")"] [":" ty=return_type()]
392
- ( (b=block() | ";" ) {
419
+ ( (b=block() {leaveSection();} | eos() ) {
393
420
return new AST.FunctionDeclaration(p(t1), modifiers, t2.image, args, ty, b);
394
421
}
395
- | ("=" e=term() ";" ) {
422
+ | ("=" eols() e=term() eos() ) {
396
423
return new AST.FunctionDeclaration(
397
424
p(t1), modifiers, t1.image, args, ty,
398
425
new AST.BlockExpression(p(t1), asList(new AST.ReturnExpression(p(t1), e)))
@@ -534,11 +561,12 @@ AST.MethodDeclaration method_decl(int mset) : {
534
561
AST.BlockExpression b = null;
535
562
AST.Expression e = null;
536
563
}{
564
+ {enterSection();}
537
565
"def" t=<ID> ["(" args=args() ")"] [":" ty=return_type()]
538
- ( (b=block() | ";" ) {
566
+ ( ({leaveSection();} b=block() | eos() ) {
539
567
return new AST.MethodDeclaration(p(t), mset, t.image, args, ty, b);
540
568
}
541
- | ("=" e=term() ";" ) {
569
+ | ("=" eols() e=term() eos() ) {
542
570
return new AST.MethodDeclaration(
543
571
p(t), mset, t.image, args, ty,
544
572
new AST.BlockExpression(p(t), asList(new AST.ReturnExpression(p(t), e)))
@@ -552,7 +580,8 @@ AST.MethodDeclaration interface_method_decl() : {
552
580
List<AST.Argument> args = (List<AST.Argument>)AST.NIL();
553
581
AST.TypeNode ty = null;
554
582
}{
555
- "def" n=<ID> ["(" args=args() ")"] [":" ty=return_type()] ";" {
583
+ {enterSection();}
584
+ "def" n=<ID> ["(" args=args() ")"] [":" ty=return_type()] eos() {
556
585
return new AST.MethodDeclaration(p(n), AST.M_PUBLIC(), n.image, args, ty, null);
557
586
}
558
587
}
@@ -618,8 +647,9 @@ AST.DelegatedFieldDeclaration delegate_decl(int modifiers) : {
618
647
AST.TypeNode type = null;
619
648
AST.Expression init = null;
620
649
}{
650
+ {enterSection();}
621
651
start="forward" name=<FID> [":" type = type()]
622
- ["=" init=term()] ";" {
652
+ ["=" eols() init=term()] eos() {
623
653
return new AST.DelegatedFieldDeclaration(p(start), modifiers, name.image.substring(1), type, init);
624
654
}
625
655
}
@@ -629,8 +659,9 @@ AST.FieldDeclaration field_decl(int modifiers):{
629
659
AST.TypeNode type = null;
630
660
AST.Expression init = null;
631
661
}{
662
+ {enterSection();}
632
663
name=<FID> ":" [type=type()]
633
- ["=" init=term()] ";" {
664
+ ["=" eols() init=term()] eos() {
634
665
return new AST.FieldDeclaration(p(name), modifiers, name.image.substring(1), type, init);
635
666
}
636
667
}
@@ -654,20 +685,34 @@ AST.CompoundExpression statement():{AST.CompoundExpression s;}{
654
685
| s=foreach_statement() {return s;}
655
686
}
656
687
688
+ // End of section
689
+ void eos() :{} {
690
+ (";" | (<EOL>)) { leaveSection(); }
691
+ }
692
+
693
+ void eols() :{} {
694
+ (<EOL>)*
695
+ }
696
+
657
697
AST.LocalVariableDeclaration local_var_statement() :{Token t; AST.TypeNode ty; AST.Expression e = null;}{
658
- t=<ID> ":" ty=type() ["=" e=term()] ";" {return new AST.LocalVariableDeclaration(p(t), t.image, ty, e);}
698
+ {enterSection();}
699
+ t=<ID>
700
+ ":" ty=type() ["=" eols() e=term()] eos() {return new AST.LocalVariableDeclaration(p(t), t.image, ty, e);}
659
701
}
660
702
661
703
AST.BreakExpression break_statement() :{Token t;}{
662
- t="break" ";" {return new AST.BreakExpression(p(t));}
704
+ {enterSection();}
705
+ t="break" eos() {return new AST.BreakExpression(p(t));}
663
706
}
664
707
665
708
AST.ContinueExpression continue_statement() :{Token t; AST.ContinueExpression st;}{
666
- t="continue" ";" {return new AST.ContinueExpression(p(t));}
709
+ {enterSection();}
710
+ t="continue" eos() {return new AST.ContinueExpression(p(t));}
667
711
}
668
712
669
713
AST.ThrowExpression throw_statement() :{Token t; AST.Expression e;}{
670
- t="throw" e=term() ";" {return new AST.ThrowExpression(p(t), e);}
714
+ {enterSection();}
715
+ t="throw" e=term() eos() {return new AST.ThrowExpression(p(t), e);}
671
716
}
672
717
673
718
AST.TryExpression try_statement() :{
@@ -679,7 +724,8 @@ AST.TryExpression try_statement() :{
679
724
}
680
725
681
726
AST.ExpressionBox exp_statement() :{AST.Expression e;}{
682
- e=term() ";" {return new AST.ExpressionBox(e.location(), e);}
727
+ {enterSection();}
728
+ e=term() eos() {return new AST.ExpressionBox(e.location(), e);}
683
729
}
684
730
685
731
AST.EmptyExpression empty_statement() :{Token t;}{
@@ -708,7 +754,8 @@ AST.SelectExpression select_statement() :{
708
754
}
709
755
710
756
AST.ReturnExpression return_statement() :{Token t; AST.Expression e = null;}{
711
- t="return" [e=term()] ";" {return new AST.ReturnExpression(p(t), e);}
757
+ {enterSection();}
758
+ t="return" [e=term()] eos() {return new AST.ReturnExpression(p(t), e);}
712
759
}
713
760
714
761
AST.SynchronizedExpression synchronized_statement() :{Token t; AST.Expression e = null; AST.BlockExpression b;}{
0 commit comments