C# をターゲットとして Antlr 4 を使用しており、構文エラーが発生したときにイベントを発生させたいと考えています。
私の文法
/*
* Parser Rules
*/
text : term+ EOF
;
term : a1 a2 a3
;
a1: ....
...
...
エラーが発生した場合、次の単語が見つかるまでパーサーの動作で入力テキストを無視するようにします。発生したイベントは、無視されたテキスト全体、エラー位置の行と列、およびエラー メッセージを取得する必要があります。
基本クラス BaseErrorListener を拡張しました。
public class DictionaryErrorListener : Antlr4.Runtime.BaseErrorListener
{
public event EventHandler<ParsingErrorEventArgs> ParsingErrorOccured;
protected void OnParsingErrorOccured(int line, int column, string errorMessage, string text)
{
EventHandler<ParsingErrorEventArgs> handler = ParsingErrorOccured;
if (handler != null)
{
handler(this, new ParsingErrorEventArgs(line, column, errorMessage, text));
}
}
public override void SyntaxError(Antlr4.Runtime.IRecognizer recognizer, Antlr4.Runtime.IToken offendingSymbol, int line, int charPositionInLine, string msg, Antlr4.Runtime.RecognitionException e)
{
OnParsingErrorOccured(line, charPositionInLine, msg, ignoredText);
}
}
私の質問は次のとおりです。
- 私は正しい道を進んでいますか?
- 無視されたテキストを取得するには?