ANTLR 4:正在实现这种形式的输出形式



在扫描的上下文中,当我的文本被扫描时,我需要覆盖,扩展,倾听,访问,访问,能够打印出这种信息的输出形式?

-- Example output only ---------
DEBUG ... current mode: DEFAULT_MODE
DEBUG ... matching text '#' on rule SHARP ; pushing and switching to DIRECTIVE_MODE
DEBUG ... matching text 'IF" on rule IF ; pushing and switching to IF_MODE
DEBUG ... matching text '          ' on rule WS; skipping
DEBUG ... no match for text %
DEBUG ... no match for text &
DEBUG ... mathcing text 'rn' on rule EOL; popping mode; current mode: DIRECTIVE_MODE
...

谢谢

解决方案比我想象的要简单得多。您只需要将生成的Lexer和覆盖方法(例如popmode(),puspMode()获得您想要的打印输出即可。如果执行此操作,您还应该覆盖EMIT()方法以获取正确的顺序和上下文信息。

这是C#中的一个示例:

class ExtendedLexer : MyGeneratedLexer
{
public ExtendedLexer(ICharStream input)
    : base(input)  { }
public override int PopMode()
{
    Console.WriteLine($"Mode is being popped: Line: {Line} Column:{Column} ModeName: {ModeNames[ModeStack.Peek()]}");
    return base.PopMode();
}
public override void PushMode(int m)
{
    Console.WriteLine($"Mode is being pushed: Line: {Line} Column:{Column} ModeName: {ModeNames[m]}");
    base.PushMode(m);
}
public override void Emit(IToken t)
{
    Console.WriteLine($"[@{t.TokenIndex},{t.StartIndex}:{t.StopIndex}, <{Vocabulary.GetSymbolicName(t.Type)}> = '{t.Text}']");
    base.Emit(t);
}

}

,输出将是:

Mode is being pushed: Line: 4 Column:3 ModeName: IF_MODE
[@-1,163:165, <IF> = '#IF']
Mode is being pushed: Line: 4 Column:4 ModeName: CONDITION_MODE
[@-1,166:166, <LPAREN> = '(']
[@-1,167:189, <EXP> = '@setStartDateAndEndDate']
Mode is being popped: Line: 4 Column:28 ModeName: IF_MODE
[@-1,190:190, <RPAREN> = ')']

相关内容

  • 没有找到相关文章

最新更新