yzyzsun / CP-next

The Next Generation of Compositional Programming
https://plground.org
Other
21 stars 5 forks source link

Displays Syntax Errors #6

Closed yoodee closed 2 years ago

yoodee commented 2 years ago

Currently, extended Lexer and ErrorListener classes are in app.js itself. Should they have a separate file?

yzyzsun commented 2 years ago

Currently, extended Lexer and ErrorListener classes are in app.js itself. Should they have a separate file?

Yes, it would be better to separate them into some file like antlr/index.js.

Btw, why do we need to extend CPLexer and define a popMode in it?

yoodee commented 2 years ago

We have different modes for program, document, and command. Antlr maintains a stack to switch between modes. PushMode and popMode are triggered by parentheses, brackets, backticks etc. as defined in CPLexer.g4. There can occur some syntax errors where there is an extra closing parentheses for example. Because closing parentheses trigger popMode, this can cause the Lexer to attempt a Pop from an empty stack, which results in an "Empty Stack" error. CPLexer extends antlr4.Lexer where popMode is defined. I have overridden popMode so as to keep the mode unchanged when popMode is attempted in an empty stack. This causes a Syntax Error to occur in Parser, instead of an Empty Stack error in Lexer.

yzyzsun commented 2 years ago

LGTM; thanks!