public virtual void DefineLexerRuleFoundInParser( IToken ruleToken,
GrammarAST ruleAST )
{
//[email protected]("rule tree is:\n"+ruleAST.toStringTree());
/*
String ruleText = tokenBuffer.toOriginalString(ruleAST.ruleStartTokenIndex,
ruleAST.ruleStopTokenIndex);
*/
// first, create the text of the rule
StringBuilder buf = new StringBuilder();
buf.Append( "// $ANTLR src \"" );
buf.Append( FileName );
buf.Append( "\" " );
buf.Append( ruleAST.Line );
buf.AppendLine();
for ( int i = ruleAST.TokenStartIndex;
i <= ruleAST.TokenStopIndex && i < tokenBuffer.Count;
i++ )
{
CommonToken t = (CommonToken)tokenBuffer.Get( i );
// undo the text deletions done by the lexer (ugh)
if ( t.Type == ANTLRParser.BLOCK )
{
buf.Append( "(" );
}
else if ( t.Type == ANTLRParser.ACTION )
{
buf.Append( "{" );
buf.Append( t.Text );
buf.Append( "}" );
}
else if ( t.Type == ANTLRParser.SEMPRED ||
t.Type == ANTLRParser.SYN_SEMPRED ||
t.Type == ANTLRParser.GATED_SEMPRED ||
t.Type == ANTLRParser.BACKTRACK_SEMPRED )
{
buf.Append( "{" );
buf.Append( t.Text );
buf.Append( "}?" );
}
else if ( t.Type == ANTLRParser.ARG_ACTION )
{
buf.Append( "[" );
buf.Append( t.Text );
buf.Append( "]" );
}
else
{
buf.Append( t.Text );
}
}
string ruleText = buf.ToString();
//[email protected]("[["+ruleText+"]]");
// now put the rule into the lexer grammar template
if ( IsRoot )
{ // don't build lexers for delegates
LexerGrammarTemplate.SetAttribute( "rules", ruleText );
}
// track this lexer rule's name
composite.LexerRules.Add( ruleToken.Text );
}