1lexer grammar t020fuzzyLexer; 2options { 3 language=Cpp; 4 filter=true; 5} 6 7@lexer::includes 8{ 9#include "UserTestTraits.hpp" 10} 11@lexer::namespace 12{ Antlr3Test } 13 14@header { 15from io import StringIO 16} 17 18@init { 19self.output = StringIO() 20} 21 22IMPORT 23 : 'import' WS name=QIDStar WS? ';' 24 ; 25 26/** Avoids having "return foo;" match as a field */ 27RETURN 28 : 'return' (options {greedy=false;}:.)* ';' 29 ; 30 31CLASS 32 : 'class' WS name=ID WS? ('extends' WS QID WS?)? 33 ('implements' WS QID WS? (',' WS? QID WS?)*)? '{' 34 {self.output.write("found class "+$name.text+"\n")} 35 ; 36 37METHOD 38 : TYPE WS name=ID WS? '(' ( ARG WS? (',' WS? ARG WS?)* )? ')' WS? 39 ('throws' WS QID WS? (',' WS? QID WS?)*)? '{' 40 {self.output.write("found method "+$name.text+"\n");} 41 ; 42 43FIELD 44 : TYPE WS name=ID '[]'? WS? (';'|'=') 45 {self.output.write("found var "+$name.text+"\n");} 46 ; 47 48STAT: ('if'|'while'|'switch'|'for') WS? '(' ; 49 50CALL 51 : name=QID WS? '(' 52 {self.output.write("found call "+$name.text+"\n");} 53 ; 54 55COMMENT 56 : '/*' (options {greedy=false;} : . )* '*/' 57 {self.output.write("found comment "+self.getText()+"\n");} 58 ; 59 60SL_COMMENT 61 : '//' (options {greedy=false;} : . )* '\n' 62 {self.output.write("found // comment "+self.getText()+"\n");} 63 ; 64 65STRING 66 : '"' (options {greedy=false;}: ESC | .)* '"' 67 ; 68 69CHAR 70 : '\'' (options {greedy=false;}: ESC | .)* '\'' 71 ; 72 73WS : (' '|'\t'|'\n')+ 74 ; 75 76fragment 77QID : ID ('.' ID)* 78 ; 79 80/** QID cannot see beyond end of token so using QID '.*'? somewhere won't 81 * ever match since k=1 lookahead in the QID loop of '.' will make it loop. 82 * I made this rule to compensate. 83 */ 84fragment 85QIDStar 86 : ID ('.' ID)* '.*'? 87 ; 88 89fragment 90TYPE: QID '[]'? 91 ; 92 93fragment 94ARG : TYPE WS ID 95 ; 96 97fragment 98ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* 99 ; 100 101fragment 102ESC : '\\' ('"'|'\''|'\\') 103 ; 104