ASTは、タダじゃ作ってくれない。

ANTLRWorksで、ParseTreeなんてのが、出てるから、これでいいんじゃね?
とか思っていたのだけど、世の中そんなに甘く無い事を理解。


とりあえず、全く意味を理解していないのだけど、ANTLRWorksのASTと言うビューで見ると、
期待通りにASTが出来ている所までを晒してみる。

grammar TwoWaySQL;

options {
	language = Java;
	output = AST;
	ASTLabelType = CommonTree;
}

tokens {
	BEGINNODE;
	IFNODE;
	EXPRESSIONNODE;
	ELSENODE;
}


@header {
package twowaysql.grammar;
}

@lexer::header {
package twowaysql.grammar;
}

twowaySQL : txt EOF;

txt 	:
	(charactors
	| comment
	| WS | LT)+
	;

charactors : (CHAR)+;

// $<comment

comment :
	begincomment
	| ifcomment
	| blockcomment
	| linecomment
	;

blockcomment :
	C_ST (charactors | WS | LT)+ C_ED
	;

linecomment :
	C_LN (charactors | WS)+ (LT|EOF)
	;

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
	| C_LN ELSE expression? LT
	) -> ^(EXPRESSIONNODE expression?)
	;

expression :
	(charactors | WS | LT)+
	;

begincomment :	
	C_ST BEGIN C_ED txt endcomment -> ^(BEGINNODE txt)
	;

endcomment :
	C_ST END C_ED
	;

// $>
	

// $<Charactors

CHAR	: ('0'..'9'|'a'..'z'|'A'..'Z') ;

LT	: '\n'		// Line feed.
	| '\r'		// Carriage return.
	| '\u2028'	// Line separator.
	| '\u2029'	// Paragraph separator.
	;
WS	: '\t' | '\v' | '\f' | ' ' | '\u00A0';

// $>

C_ST	:	'/*';
C_ED	:	'*/';
C_LN	:	'--';
BEGIN	: WS* 'BEGIN' WS*;
IF	: WS* 'IF' WS*;
ELSE 	: WS* 'ELSE' WS*;
END	: WS* 'END' WS*;

こんな感じのテキストを食わせたりしている。

ada  /*aa hoge
 piro*/  fuga
/*BEGIN*/
 /*IF aa bb
 dd*/moge piro /*ELSE ccc*/ccc
	/* ELSE */ zzz
 /*END*/
	-- moge
/* END*/

でも、これ、実行すると、charactorsルールに適合する文字が、
一文字毎にCommonTreeのインスタンスを作っちゃうんだぜ?
僕としては、charactorsルールに適合する文字列を一塊りに、TXTNODEみたいな感じにしたいんだけどなぁ…。