ちょっと中身を整理してみた。
さっきのを、ちょこちょこと弄って整理してみた。
grammar TwoWaySQL; options { language = Java; output = AST; ASTLabelType = CommonTree; } tokens { BEGINNODE; IFNODE; EXPRESSIONNODE; ELSENODE; } @header { package twowaysql.grammar; } @lexer::header { package twowaysql.grammar; } @lexer::members { boolean inComment = false; boolean inLineComment = false; } twowaySQL : txt EOF; txt : (comment | charactors)+ ; charactors : (IDENT| SYMBOLS | QUOTED)+ ; // $<comment comment : begincomment | ifcomment | blockcomment | linecomment ; blockcomment : C_ST (charactors) C_ED ; linecomment : C_LN_ST (charactors) C_LN_ED ; ifcomment : C_ST IF expression C_ED txt (elsecomment txt)* endcomment -> ^(IFNODE ^(EXPRESSIONNODE expression) txt ^(ELSENODE elsecomment txt)*) ; elsecomment : (C_ST ELSE expression? C_ED -> ^(EXPRESSIONNODE expression?) |C_LN_ST ELSE expression? C_LN_ED -> ^(EXPRESSIONNODE expression?) ) ; expression : (charactors) ; begincomment : ((C_ST BEGIN C_ED | C_LN_ST BEGIN C_LN_ED) txt endcomment) -> ^(BEGINNODE txt); endcomment : C_ST END C_ED | C_LN_ST END C_LN_ED; // $> QUOTED : SYM_Q ~(SYM_Q)+ SYM_Q; SYMBOLS : '*' | '/' | '-'; fragment SYM_Q : '\u0027' | '"'; C_ST : {!inComment}? '/*' { inComment = true; }; C_ED : {inComment}? '*/' { inComment = false; }; C_LN_ST : {!inComment}? '--' { inLineComment = true; inComment = true; }; C_LN_ED : {inLineComment}? ( LN_R LN_N | LN_R | LN_N | EOF ) { inLineComment = false; inComment = false; }; // $<Keywords BEGIN : ('b'|'B')('e'|'E')('g'|'G')('i'|'I')('n'|'N'); IF : ('i'|'I')('f'|'F'); ELSE : ('e'|'E')('l'|'L')('s'|'S')('e'|'E'); END : ('e'|'E')('n'|'N')('d'|'D'); // $> IDENT : CHAR+; fragment WS : '\t' | '\v' | '\f' | ' ' | '\u00A0'; fragment LN_R : '\r'; fragment LN_N : '\n'; CHAR : ~(SYMBOLS | SYM_Q | LN_R | LN_N | WS); LT : {!inLineComment}? (LN_R | LN_N)+ { $channel = HIDDEN; }; WHITE_SPACES : (WS)+ { $channel = HIDDEN; };
こんな文字列を食わせとります。
ada.cec のしのし / 3 d/*aa hoge piro*/ ぎゃぼー -- BeGiN /* If aa - bb dd*/moge piro /*eLsE ccc + 44 '*/' */cccd -- elSE fuga < '0' dfdze -- ElSe zzz "-- fugafuga" -- eNd -- moge うぼぁ zz /* EnD*/