Once there is a parse tree, a visitor or listener walk of the parse tree to do the actual translation will encounter errors that the parse cannot know about (like references to external entities which do not exist). When the user is presented with the errors from their source code, they need to see both the errors generated by the parser, and these "semantic" errors.
Right now, I have a custom listener which captures the parse errors, and then I have a completely different mechanism for collecting second pass errors, and when I present errors to the user, I merge these two error sources into one error stream. I was in a hurry and it got the job done, now it is time to do better.
To fix this I have two choices.
- Change my custom listener to reformat the parse errors so they can be added to the merged stream as they are generated. Then the merged stream is the authoritative source of all errors.
- Somehow be able to add semantic errors to the error stream that my customer listener receives, something like being able to call
notifyErrorListener
from inside a visit function. Then my custom error collector is now the authoritative source of all errors.
I can easily do #1, but it feels like #2 is the right choice and I just don't quite grasp how I do that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…