antlr3 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/ANTLR-LICENSE.txt +26 -0
  2. data/History.txt +66 -0
  3. data/README.txt +139 -0
  4. data/bin/antlr4ruby +33 -0
  5. data/java/RubyTarget.java +524 -0
  6. data/java/antlr-full-3.2.1.jar +0 -0
  7. data/lib/antlr3.rb +176 -0
  8. data/lib/antlr3/constants.rb +88 -0
  9. data/lib/antlr3/debug.rb +701 -0
  10. data/lib/antlr3/debug/event-hub.rb +210 -0
  11. data/lib/antlr3/debug/record-event-listener.rb +25 -0
  12. data/lib/antlr3/debug/rule-tracer.rb +55 -0
  13. data/lib/antlr3/debug/socket.rb +360 -0
  14. data/lib/antlr3/debug/trace-event-listener.rb +92 -0
  15. data/lib/antlr3/dfa.rb +247 -0
  16. data/lib/antlr3/dot.rb +174 -0
  17. data/lib/antlr3/error.rb +657 -0
  18. data/lib/antlr3/main.rb +561 -0
  19. data/lib/antlr3/modes/ast-builder.rb +41 -0
  20. data/lib/antlr3/modes/filter.rb +56 -0
  21. data/lib/antlr3/profile.rb +322 -0
  22. data/lib/antlr3/recognizers.rb +1280 -0
  23. data/lib/antlr3/streams.rb +985 -0
  24. data/lib/antlr3/streams/interactive.rb +91 -0
  25. data/lib/antlr3/streams/rewrite.rb +412 -0
  26. data/lib/antlr3/test/call-stack.rb +57 -0
  27. data/lib/antlr3/test/config.rb +23 -0
  28. data/lib/antlr3/test/core-extensions.rb +269 -0
  29. data/lib/antlr3/test/diff.rb +165 -0
  30. data/lib/antlr3/test/functional.rb +207 -0
  31. data/lib/antlr3/test/grammar.rb +371 -0
  32. data/lib/antlr3/token.rb +592 -0
  33. data/lib/antlr3/tree.rb +1415 -0
  34. data/lib/antlr3/tree/debug.rb +163 -0
  35. data/lib/antlr3/tree/visitor.rb +84 -0
  36. data/lib/antlr3/tree/wizard.rb +481 -0
  37. data/lib/antlr3/util.rb +149 -0
  38. data/lib/antlr3/version.rb +27 -0
  39. data/samples/ANTLRv3Grammar.g +621 -0
  40. data/samples/Cpp.g +749 -0
  41. data/templates/AST.stg +335 -0
  42. data/templates/ASTDbg.stg +40 -0
  43. data/templates/ASTParser.stg +153 -0
  44. data/templates/ASTTreeParser.stg +272 -0
  45. data/templates/Dbg.stg +192 -0
  46. data/templates/Ruby.stg +1514 -0
  47. data/test/functional/ast-output/auto-ast.rb +797 -0
  48. data/test/functional/ast-output/construction.rb +555 -0
  49. data/test/functional/ast-output/hetero-nodes.rb +753 -0
  50. data/test/functional/ast-output/rewrites.rb +1327 -0
  51. data/test/functional/ast-output/tree-rewrite.rb +1662 -0
  52. data/test/functional/debugging/debug-mode.rb +689 -0
  53. data/test/functional/debugging/profile-mode.rb +165 -0
  54. data/test/functional/debugging/rule-tracing.rb +74 -0
  55. data/test/functional/delegation/import.rb +379 -0
  56. data/test/functional/lexer/basic.rb +559 -0
  57. data/test/functional/lexer/filter-mode.rb +245 -0
  58. data/test/functional/lexer/nuances.rb +47 -0
  59. data/test/functional/lexer/properties.rb +104 -0
  60. data/test/functional/lexer/syn-pred.rb +32 -0
  61. data/test/functional/lexer/xml.rb +206 -0
  62. data/test/functional/main/main-scripts.rb +245 -0
  63. data/test/functional/parser/actions.rb +224 -0
  64. data/test/functional/parser/backtracking.rb +244 -0
  65. data/test/functional/parser/basic.rb +282 -0
  66. data/test/functional/parser/calc.rb +98 -0
  67. data/test/functional/parser/ll-star.rb +143 -0
  68. data/test/functional/parser/nuances.rb +165 -0
  69. data/test/functional/parser/predicates.rb +103 -0
  70. data/test/functional/parser/properties.rb +242 -0
  71. data/test/functional/parser/rule-methods.rb +132 -0
  72. data/test/functional/parser/scopes.rb +274 -0
  73. data/test/functional/token-rewrite/basic.rb +318 -0
  74. data/test/functional/token-rewrite/via-parser.rb +100 -0
  75. data/test/functional/tree-parser/basic.rb +750 -0
  76. data/test/unit/sample-input/file-stream-1 +2 -0
  77. data/test/unit/sample-input/teststreams.input2 +2 -0
  78. data/test/unit/test-dfa.rb +52 -0
  79. data/test/unit/test-exceptions.rb +44 -0
  80. data/test/unit/test-recognizers.rb +55 -0
  81. data/test/unit/test-scheme.rb +62 -0
  82. data/test/unit/test-streams.rb +459 -0
  83. data/test/unit/test-tree-wizard.rb +535 -0
  84. data/test/unit/test-trees.rb +854 -0
  85. metadata +205 -0
@@ -0,0 +1,749 @@
1
+ grammar Cpp;
2
+
3
+ options {
4
+ language = Ruby;
5
+ memoize = true;
6
+ output = AST;
7
+ backtrack = true;
8
+ }
9
+
10
+ tokens
11
+ {
12
+ IFDEF;
13
+ IFNDEF;
14
+ IF;
15
+ ELIF;
16
+ ELSE;
17
+ ENDIF;
18
+ WARNING;
19
+ ERROR;
20
+ PRAGMA;
21
+ EXPR;
22
+ EXPR_DEF;
23
+ EXPR_NDEF;
24
+ UNARY_MINUS;
25
+ UNARY_PLUS;
26
+ REFERANCE;
27
+ TYPECAST;
28
+ SIZEOF_TYPE;
29
+ INDEX_OP;
30
+ POST_INC;
31
+ POST_DEC;
32
+ POINTER_AT;
33
+ POINTER;
34
+ EXPR_GROUP;
35
+ METHOD_CALL;
36
+ ARGS;
37
+ TEXT_LINE;
38
+ TEXT_GROUP;
39
+ TEXT_END;
40
+ EXPAND;
41
+ EXP_ARGS;
42
+ EXP_ARG;
43
+ EXEC_MACRO;
44
+ CONCATENATE;
45
+ LINE;
46
+ INCLUDE;
47
+ INCLUDE_EXPAND;
48
+ DEFINE;
49
+ UNDEF;
50
+ MAC_OBJECT;
51
+ MAC_FUNCTION;
52
+ MAC_FUNCTION_OBJECT;
53
+ MACRO_DEFINE;
54
+ }
55
+
56
+ scope Expression {
57
+ flavor;
58
+ }
59
+
60
+ @lexer::init {
61
+ @in_directive = @in_define_mode = false
62
+ @discard_space = true
63
+ @ltoken = End
64
+ @cpp = ?X
65
+ }
66
+
67
+ @lexer::members {
68
+ def in_directive?
69
+ @in_directive
70
+ end
71
+
72
+ def in_define_mode?
73
+ @in_define_mode
74
+ end
75
+
76
+ def discard_space?
77
+ @discard_space
78
+ end
79
+
80
+ attr_reader :include_search_paths
81
+ attr_accessor :cpp
82
+ attr_accessor :ltoken
83
+ }
84
+
85
+ preprocess
86
+ : proc_line+
87
+ ;
88
+
89
+ proc_line
90
+ :
91
+ ( DIRECTIVE
92
+ | text_line
93
+ | diagnostics
94
+ | file_inclusion
95
+ | macro_define
96
+ | macro_undef
97
+ | conditional_compilation
98
+ | line_control
99
+ | macro_execution
100
+ )? End
101
+ ;
102
+
103
+ file_inclusion
104
+ : INCLUDE -> ^(INCLUDE)
105
+ | INCLUDE_EXPAND -> ^(INCLUDE_EXPAND)
106
+ ;
107
+
108
+ macro_define
109
+ : DEFINE IDENTIFIER LPAREN WS? RPAREN macro_text? -> ^(MAC_FUNCTION_OBJECT IDENTIFIER macro_text?)
110
+ | DEFINE mac=IDENTIFIER LPAREN WS? (arg+=macro_param WS? (COMMA WS* arg+=macro_param WS*)*)? RPAREN macro_text? -> ^(MAC_FUNCTION $mac $arg+ macro_text? )
111
+ | DEFINE IDENTIFIER macro_text? -> ^(MAC_OBJECT IDENTIFIER macro_text?)
112
+ ;
113
+
114
+ macro_param
115
+ : IDENTIFIER ELLIPSIS ->^(ELLIPSIS IDENTIFIER)
116
+ | ELLIPSIS
117
+ | IDENTIFIER
118
+ ;
119
+
120
+ macro_execution
121
+ scope Expression;
122
+ : EXEC_MACRO { $Expression::flavor = :exec}
123
+ ifexpression -> ^(EXEC_MACRO ifexpression)
124
+ ;
125
+
126
+ macro_undef
127
+ : UNDEF mac=IDENTIFIER -> ^(UNDEF IDENTIFIER)
128
+ ;
129
+
130
+ conditional_compilation
131
+ scope Expression;
132
+ : t=IF { $Expression::flavor = $t.text.to_sym }
133
+ ifexp+=ifexpression ifstmt+=statement
134
+ (ELIF ifexp+=ifexpression ifstmt+=statement )*
135
+ (ELSE elsestmt=statement)?
136
+ ENDIF
137
+ -> ^(IF ($ifexp $ifstmt)+ (ELSE $elsestmt)?)
138
+ ;
139
+
140
+ line_control
141
+ : LINE n=DECIMAL_LITERAL (theFile=STRING_LITERAL)? ->^(LINE $n ($theFile)? )
142
+ ;
143
+
144
+ diagnostics
145
+ : WARNING -> ^(WARNING)
146
+ | ERROR -> ^(ERROR)
147
+ | PRAGMA -> ^(PRAGMA)
148
+ ;
149
+
150
+ text_line
151
+ : source_text+ -> ^(TEXT_LINE source_text+)
152
+ ;
153
+
154
+
155
+ statement
156
+ : proc_line+
157
+ ;
158
+
159
+ type_name
160
+ : IDENTIFIER
161
+ ;
162
+
163
+ ifexpression
164
+ @init {
165
+ ntype =
166
+ case ($Expression::flavor rescue nil)
167
+ when :ifndef then EXPR_NDEF
168
+ when :ifdef then EXPR_DEF
169
+ else EXPR
170
+ end
171
+ }
172
+ : assignment_expression -> ^({create_token(ntype)} assignment_expression)
173
+ ;
174
+
175
+ assignment_expression
176
+ : conditional_expression
177
+ ( ( ASSIGNEQUAL^ | TIMESEQUAL^ | DIVIDEEQUAL^ |
178
+ MODEQUAL^ | PLUSEQUAL^ | MINUSEQUAL^ |
179
+ SHIFTLEFTEQUAL^ | SHIFTRIGHTEQUAL^ | BITWISEANDEQUAL^ |
180
+ BITWISEXOREQUAL^ | BITWISEOREQUAL^
181
+ ) assignment_expression
182
+ )?
183
+ ;
184
+
185
+ conditional_expression
186
+ : logical_or_expression
187
+ ( QUESTIONMARK^ assignment_expression COLON conditional_expression )?
188
+ ;
189
+
190
+ logical_or_expression
191
+ : logical_and_expression (OR^ logical_and_expression)*
192
+ ;
193
+
194
+ logical_and_expression
195
+ : inclusive_or_expression (AND^ inclusive_or_expression)*
196
+ ;
197
+
198
+ inclusive_or_expression
199
+ : exclusive_or_expression (BITWISEOR^ exclusive_or_expression)*
200
+ ;
201
+
202
+ exclusive_or_expression
203
+ : and_expression (BITWISEXOR^ and_expression)*
204
+ ;
205
+
206
+ and_expression
207
+ : equality_expression (AMPERSAND^ equality_expression)*
208
+ ;
209
+
210
+ equality_expression
211
+ : relational_expression ((NOTEQUAL^ | EQUAL^) relational_expression)*
212
+ ;
213
+
214
+ relational_expression
215
+ : shift_expression
216
+ ( ( ( LESSTHAN^
217
+ | GREATERTHAN^
218
+ | LESSTHANOREQUALTO^
219
+ | GREATERTHANOREQUALTO^
220
+ )
221
+ shift_expression
222
+ )*
223
+ )
224
+ ;
225
+ shift_expression
226
+ : additive_expression ((SHIFTLEFT^ | SHIFTRIGHT^) additive_expression)*
227
+ ;
228
+
229
+ additive_expression
230
+ : multiplicative_expression ((PLUS^ | MINUS^) multiplicative_expression)*
231
+ ;
232
+
233
+ multiplicative_expression
234
+ : unary_expression ((STAR^ | DIVIDE^ | MOD^ ) unary_expression)*
235
+ ;
236
+
237
+ unary_expression
238
+ : PLUSPLUS unary_expression -> ^(PLUSPLUS unary_expression)
239
+ | MINUSMINUS unary_expression -> ^(MINUSMINUS unary_expression)
240
+ | SIZEOF unary_expression -> ^(SIZEOF unary_expression)
241
+ | SIZEOF LPAREN type_name RPAREN -> ^(SIZEOF_TYPE type_name)
242
+ | DEFINED type_name -> ^(DEFINED type_name)
243
+ | DEFINED LPAREN type_name RPAREN ->^(DEFINED type_name)
244
+ | unary_expression_not_plus_minus
245
+ ;
246
+
247
+ unary_expression_not_plus_minus
248
+ : NOT unary_expression -> ^(NOT unary_expression)
249
+ | TILDE unary_expression -> ^(TILDE unary_expression)
250
+ | AMPERSAND unary_expression -> ^(REFERANCE unary_expression)
251
+ | STAR unary_expression -> ^(POINTER_AT unary_expression)
252
+ | MINUS unary_expression -> ^(UNARY_MINUS unary_expression)
253
+ | PLUS unary_expression -> ^(UNARY_PLUS unary_expression)
254
+ | LPAREN type_name RPAREN unary_expression -> ^(TYPECAST type_name unary_expression)
255
+ | postfix_expression
256
+ ;
257
+
258
+ postfix_expression
259
+ : primary_expression
260
+ ( l=LSQUARE^ {l.type = INDEX_OP} assignment_expression RSQUARE!
261
+ | DOT^ IDENTIFIER
262
+ | s=STAR^ {s.type = POINTER} IDENTIFIER
263
+ | POINTERTO^ IDENTIFIER
264
+ | p=PLUSPLUS^ {p.type = POST_INC}
265
+ | m=MINUSMINUS^ {m.type = POST_DEC}
266
+ )*
267
+ ;
268
+
269
+ primary_expression
270
+ : (IDENTIFIER LPAREN) => function_call
271
+ | IDENTIFIER
272
+ | constant
273
+ | LPAREN assignment_expression RPAREN -> ^(EXPR_GROUP assignment_expression)
274
+ ;
275
+
276
+ function_call
277
+ : id=IDENTIFIER LPAREN arg_list? RPAREN -> ^(METHOD_CALL $id arg_list?)
278
+ ;
279
+
280
+ arg_list
281
+ : assignment_expression (COMMA assignment_expression)* -> ^(ARGS assignment_expression+)
282
+ ;
283
+
284
+ constant
285
+ : HEX_LITERAL
286
+ | OCTAL_LITERAL
287
+ | DECIMAL_LITERAL
288
+ | CHARACTER_LITERAL
289
+ | STRING_LITERAL
290
+ | FLOATING_POINT_LITERAL
291
+ ;
292
+
293
+
294
+ // | (IDENTIFIER { self.input.LT(1).getText() == '(' }?) => IDENTIFIER
295
+ source_text
296
+ : source_expression
297
+ | COMMA
298
+ | LPAREN
299
+ | RPAREN
300
+ | WS
301
+ ;
302
+
303
+ macro_expansion
304
+ : id=IDENTIFIER WS? LPAREN WS? RPAREN -> ^(EXPAND $id)
305
+ | id=IDENTIFIER WS? LPAREN WS? mac_args WS? RPAREN -> ^(EXPAND $id mac_args?)
306
+ ;
307
+
308
+ mac_args : marg+=m_arg ( WS? COMMA WS? marg+=m_arg)* -> ^(EXP_ARGS $marg+)
309
+ ;
310
+
311
+ m_arg : source_expression+ -> ^(EXP_ARG source_expression+)
312
+ | -> ^(EXP_ARG)
313
+ ;
314
+
315
+ source_expression
316
+ : (IDENTIFIER WS? LPAREN) => macro_expansion
317
+ | (primary_source WS? SHARPSHARP ) => concatenate
318
+ | STRINGIFICATION IDENTIFIER -> ^(STRINGIFICATION IDENTIFIER)
319
+ | primary_source
320
+ | STRING_OP
321
+ | SIZEOF
322
+ | LPAREN mac_args? RPAREN -> ^(TEXT_GROUP mac_args?)
323
+ | SEMICOLON
324
+ | TEXT_END
325
+ | WS
326
+ ;
327
+
328
+ concatenate
329
+ : prim+=primary_source (WS? SHARPSHARP WS? prim+=primary_source)+ -> ^(CONCATENATE $prim+)
330
+ ;
331
+
332
+ primary_source
333
+ : SHARPSHARP WS? IDENTIFIER -> ^(SHARPSHARP IDENTIFIER)
334
+ | IDENTIFIER
335
+ | constant
336
+ | CKEYWORD
337
+ | COPERATOR
338
+ ;
339
+
340
+ STRING_OP: { @in_define_mode }? '#' WS? id=IDENTIFIER
341
+ { self.text = $id.text}
342
+ ;
343
+
344
+ DIRECTIVE
345
+ : { !@in_directive && !@in_define_mode }? '#'
346
+ {
347
+ @in_directive = true
348
+ @cpp = ?X
349
+ @discard_space = false
350
+ } WS?
351
+ ( 'ifdef'
352
+ {
353
+ $type = IF
354
+ @discard_space = true
355
+ $text = "ifdef"
356
+ }
357
+ | 'ifndef'
358
+ {
359
+ $type = IF
360
+ @discard_space = true
361
+ $text = "ifndef"
362
+ }
363
+ | 'if'
364
+ {
365
+ $type = IF
366
+ @discard_space = true
367
+ $text = "if"
368
+ }
369
+ | 'elif'
370
+ {
371
+ $type = ELIF
372
+ @discard_space = true
373
+ }
374
+ | 'else'
375
+ {
376
+ $type = ELSE
377
+ @discard_space = true
378
+ }
379
+ | 'endif'
380
+ {
381
+ $type = ENDIF
382
+ @discard_space = true
383
+ }
384
+ | 'line'
385
+ {
386
+ $type = LINE
387
+ @discard_space = true
388
+ }
389
+ | 'undef' WS
390
+ {
391
+ $type = UNDEF
392
+ @discard_space = true
393
+ }
394
+ | 'define' WS
395
+ {
396
+ $type = DEFINE
397
+ @cpp = ?D
398
+ @discard_space = false
399
+ }
400
+ | 'exec_macro_expression'
401
+ {
402
+ $type = EXEC_MACRO
403
+ @discard_space = true
404
+ }
405
+ | ('include'|'include_next') WS f=IDENTIFIER
406
+ {
407
+ $type = INCLUDE_EXPAND
408
+ $text = $f.text
409
+ @discard_space = true
410
+ }
411
+ | ('include'|'include_next') WS f=INCLUDE_FILE
412
+ {
413
+ $type = INCLUDE
414
+ $text = $f.text
415
+ @discard_space = true
416
+ }
417
+ | 'warning' { $type = WARNING } m=MACRO_TEXT { $text = $m.text }
418
+ | 'error' { $type = ERROR } (m=MACRO_TEXT { $text = $m.text })?
419
+ | 'pragma' { $type = PRAGMA } m=MACRO_TEXT { $text = $m.text }
420
+ )
421
+ ;
422
+
423
+ fragment MACRO_TEXT
424
+ : ( ('/*' ) => '/*' .* '*/'
425
+ | ('\\\n') => ('\\\n')
426
+ | ('\\\r\n') => ('\\\n')
427
+ | ~'\n'
428
+ ) +
429
+ ;
430
+
431
+ SIZEOF : 'sizeof'
432
+ ;
433
+
434
+ DEFINED : 'defined'
435
+ { $type = CKEYWORD unless @in_directive }
436
+ ;
437
+
438
+ IDENTIFIER
439
+ : LETTER (LETTER|'0'..'9')*
440
+ {
441
+ if @in_directive and @cpp == ?D
442
+ if @input.peek == ?(
443
+ @cpp = ?(
444
+ else
445
+ @in_define_mode = true
446
+ @in_directive = false
447
+ @cpp = ?X
448
+ end
449
+ end
450
+ }
451
+ ;
452
+
453
+ fragment
454
+ LETTER : '$'
455
+ | 'A'..'Z'
456
+ | 'a'..'z'
457
+ | '_'
458
+ ;
459
+
460
+ /* Operators: */
461
+
462
+ STRINGIFICATION : '#_#_'
463
+ ;
464
+
465
+ SHARPSHARP : '##';
466
+
467
+ ASSIGNEQUAL : '=' ;
468
+
469
+ COLON : ':' ;
470
+
471
+ COMMA : ',' ;
472
+
473
+ QUESTIONMARK : '?' ;
474
+
475
+ SEMICOLON : ';' ;
476
+
477
+ POINTERTO : '->' ;
478
+
479
+ LPAREN : '(' ;
480
+
481
+ RPAREN : ')'
482
+ {
483
+ if @cpp == ?( and @input.peek != ?\n
484
+ @in_define_mode = true
485
+ @in_directive = false
486
+ @cpp = ?X
487
+ end
488
+ }
489
+ ;
490
+
491
+ LSQUARE : '[' ;
492
+
493
+ RSQUARE : ']' ;
494
+
495
+ LCURLY : '{' ;
496
+
497
+ RCURLY : '}' ;
498
+
499
+ EQUAL : '==';
500
+
501
+ NOTEQUAL : '!=';
502
+
503
+ LESSTHANOREQUALTO : '<=';
504
+
505
+ LESSTHAN : '<' ;
506
+
507
+ GREATERTHANOREQUALTO : '>=';
508
+
509
+ GREATERTHAN : '>' ;
510
+
511
+ DIVIDE : '/' ;
512
+
513
+ DIVIDEEQUAL : '/=';
514
+
515
+ PLUS : '+' ;
516
+
517
+ PLUSEQUAL : '+=';
518
+
519
+ PLUSPLUS : '++';
520
+
521
+ MINUS : '-' ;
522
+
523
+ MINUSEQUAL : '-=';
524
+
525
+ MINUSMINUS : '--';
526
+
527
+ STAR : '*' ;
528
+
529
+ TIMESEQUAL : '*=';
530
+
531
+ MOD : '%' ;
532
+
533
+ MODEQUAL : '%=';
534
+
535
+ SHIFTRIGHT : '>>';
536
+
537
+ SHIFTRIGHTEQUAL : '>>=';
538
+
539
+ SHIFTLEFT : '<<';
540
+
541
+ SHIFTLEFTEQUAL : '<<=';
542
+
543
+ AND : '&&';
544
+
545
+ NOT : '!' ;
546
+
547
+ OR : '||';
548
+
549
+ AMPERSAND : '&' ;
550
+
551
+ BITWISEANDEQUAL : '&=';
552
+
553
+ TILDE : '~' ;
554
+
555
+ BITWISEOR : '|' ;
556
+
557
+ BITWISEOREQUAL : '|=';
558
+
559
+ BITWISEXOR : '^' ;
560
+
561
+ BITWISEXOREQUAL : '^=';
562
+
563
+ DOT : '.' ;
564
+
565
+ POINTERTOMBR : '->*' ;
566
+
567
+ DOTMBR : '.*' ;
568
+
569
+ SCOPE : '::' ;
570
+
571
+ ELLIPSIS : '...' ;
572
+
573
+ fragment
574
+ INCLUDE_FILE
575
+ : '<' ( ' ' | '!' | '#' ..';' | '=' | '?' .. '[' | ']' .. '\u00FF')+ '>'
576
+ | '"' ( ' ' | '!' | '#' ..';' | '=' | '?' .. '[' | ']' .. '\u00FF')+ '"'
577
+ ;
578
+
579
+ CHARACTER_LITERAL
580
+ : ('L')? '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
581
+ ;
582
+
583
+ //STRING_LITERAL: '"' (options {greedy=false;} : EscapeSequence | ~('"'))* '"'
584
+ STRING_LITERAL
585
+ : '"' (EscapeSequence | ~('"'))* '"'
586
+ ;
587
+
588
+ HEX_LITERAL
589
+ : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix?
590
+ ;
591
+
592
+ DECIMAL_LITERAL
593
+ : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix?
594
+ ;
595
+
596
+ OCTAL_LITERAL
597
+ : '0' ('0'..'7')+ IntegerTypeSuffix?
598
+ ;
599
+
600
+ fragment HexDigit
601
+ : ('0'..'9'|'a'..'f'|'A'..'F')
602
+ ;
603
+
604
+ fragment IntegerTypeSuffix
605
+ : 'u' | 'ul' | 'U' | 'UL' | 'l' | 'L'
606
+ ;
607
+
608
+ FLOATING_POINT_LITERAL
609
+ : ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
610
+ | '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
611
+ | ('0'..'9')+ Exponent FloatTypeSuffix?
612
+ | ('0'..'9')+ FloatTypeSuffix
613
+ ;
614
+
615
+ fragment Exponent
616
+ : ('e'|'E') ('+'|'-')? ('0'..'9')+
617
+ ;
618
+
619
+ fragment FloatTypeSuffix
620
+ : ('f'|'F'|'d'|'D')
621
+ ;
622
+
623
+ fragment EscapeSequence
624
+ : '\\' ('b'|'t'|'n'|'f'|'r'|'v'|'\"'|'\''|'\\')
625
+ | '\\' 'x' HexDigit+
626
+ | OctalEscape
627
+ ;
628
+
629
+ fragment OctalEscape
630
+ : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
631
+ | '\\' ('0'..'7') ('0'..'7')
632
+ | '\\' ('0'..'7')
633
+ ;
634
+
635
+ fragment UnicodeEscape
636
+ : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
637
+ ;
638
+
639
+ COMMENT
640
+ : '/*' .* '*/' { $channel = HIDDEN }
641
+ | '/' '\\' '\n' '*' .* '*/' { $channel = HIDDEN }
642
+ ;
643
+
644
+ LINE_COMMENT
645
+ : '//' ~('\n'|'\r')* '\r'? '\n'
646
+ {
647
+ if @in_directive
648
+ $type = End
649
+ else
650
+ case (@input.peek rescue nil)
651
+ when ?#, -1
652
+ $type = TEXT_END
653
+ else
654
+ $type = End
655
+ end
656
+ end
657
+ @in_define_mode = @in_directive = false
658
+ @discard_space = true
659
+ }
660
+ ;
661
+
662
+ WS : {
663
+ c = @input.peek(-1) rescue End
664
+ c ||= End
665
+ @ltoken = c == ?\n ? End : c
666
+ }
667
+ (' '|'\r'|'\t'|'\f')+
668
+ {
669
+ if @in_directive
670
+ if @discard_space
671
+ $channel = HIDDEN
672
+ else
673
+ $type = WS
674
+ end
675
+ else
676
+ unless @in_define_mode
677
+ begin
678
+ c = @input.peek or $channel = HIDDEN
679
+ if c != EOF && c != ?# && @ltoken == End
680
+ $type = End
681
+ end
682
+ rescue
683
+ $channel = HIDDEN
684
+ end
685
+ end
686
+ end
687
+ }
688
+ ;
689
+
690
+ macro_text
691
+ : source_text+ -> ^(MACRO_DEFINE source_text+)
692
+ ;
693
+
694
+ End : WS? '\n'
695
+ {
696
+ if @in_directive
697
+ $type = End
698
+ else
699
+ if @in_define_mode
700
+ $type = End
701
+ else
702
+ begin
703
+ if c = @input.peek(1) and c != ?# and c != -1
704
+ $type = TEXT_END
705
+ elsif c.nil?
706
+ $type = End
707
+ end
708
+ rescue
709
+ $type = End
710
+ end
711
+ end
712
+ end
713
+
714
+ @ltoken = End
715
+ @in_directive = @in_define_mode = false
716
+ @discard_space = true
717
+ }
718
+ ;
719
+
720
+ ESCAPED_NEWLINE
721
+ : ('\\\n' | '\\\r\n') { $channel = HIDDEN }
722
+ ;
723
+
724
+ COPERATOR
725
+ : {!@in_directive}?
726
+ ( COLON | QUESTIONMARK
727
+ | POINTERTO | LCURLY | RCURLY
728
+ | LSQUARE | RSQUARE | STAR
729
+ | EQUAL | NOTEQUAL | LESSTHANOREQUALTO | LESSTHAN
730
+ | GREATERTHANOREQUALTO | GREATERTHAN | DIVIDE | PLUSPLUS
731
+ | MINUSMINUS | MOD | SHIFTRIGHT | SHIFTLEFT
732
+ | AND | OR | BITWISEOR | BITWISEXOR
733
+ | DOT | POINTERTOMBR | DOTMBR | SCOPE
734
+ | AMPERSAND | PLUS | MINUS
735
+ | TILDE | ASSIGNEQUAL | TIMESEQUAL | DIVIDEEQUAL
736
+ | MODEQUAL | PLUSEQUAL | MINUSEQUAL | SHIFTLEFTEQUAL
737
+ | SHIFTRIGHTEQUAL | BITWISEANDEQUAL | BITWISEXOREQUAL | BITWISEOREQUAL
738
+ | NOT | ELLIPSIS
739
+ )
740
+ ;
741
+
742
+ CKEYWORD: {!@in_directive}?
743
+ 'typedef' | '__va_list__' | 'extern' | 'static' | 'auto' | 'register'
744
+ | 'void' | 'char' | 'short' | 'int' | 'long' | 'float'
745
+ | 'double' | 'signed' | 'unsigned' | '__fpreg' | '__float80' | 'struct'
746
+ | 'union' | 'enum' | 'const' | 'volatile' | 'case' | 'default'
747
+ | 'switch' | 'while' | 'do' | 'for' | 'goto' | 'continue'
748
+ | 'break' | 'return' | 'if' | 'else'
749
+ ;