3 ms·
This looks extremely neat --- I'm about to need to dive into Javascript parsing; I was planning on PEG.js, but I'm thinking about using Chevrotain instead. The
by david-given 11y ago
This looks extremely neat --- I'm about to need to dive into Javascript parsing; I was planning on PEG.js, but I'm thinking about using Chevrotain instead. The error recovery and handling look exactly what I'm looking for.
The demo is really slick, too. I'm thoroughly impressed by the automatic highlighting of syntactic errors in the source via CodeMirror; that is precisely what I'm going to end up doing. How much of that is built in to the parser as opposed to part of the demo? That is, do I need to care about handling and reporting errors when writing my parser?
Is there anything novel about the parsing algorithm itself?
- bd82 11y agoThanks for the feedback. The Lexer and Parser both expose a collection of Errors. http://sap.github.io/chevrotain/documentation/0_5_19/classes/parser.html#errors http://sap.github.io/chevrotain/documentation/0_5_19/classes... http://sap.github.io/chevrotain/documentation/0_5_19/interfaces/ilexingresult.html#errors http://sap.github.io/chevrotain/documentation/0_5_19/interfa... Both contain information regarding the position of the error. For a lexer Error it is an offset. For a Parsing Error the erroneous Token is referenced, It contains line/column/offset and more... If you want to build an Editor using codeMirror you will have to extract that error position information and highlight it using codeMirror's APIs. example: https://github.com/SAP/chevrotain/blob/gh-pages/playground/errorMarkers.js#L1 https://github.com/SAP/chevrotain/blob/gh-pages/playground/e... Chevrotain is not aware of codeMirror/ACE or any other Editor frameworks. It is just a ±3,000 LOC library meant for building parsers. If you are indeed interested in building an Online Editor for some language, you may want to have a look here: https://github.com/SAP/demos_json_editor_services_chevrotain https://github.com/SAP/demos_json_editor_services_chevrotain It is a work in progress on using Chevrotain to build Editor services (outline/formatting/Ast Building/...).
- david-given 11y agoThat's ideal. It's so nice to have a parser which actually does proper error handling. So many really nice parsers completely fail to do this --- e.g. Lua's PEG works really, really well, except when you give it invalid input, at which point it usually just gives up and goes home...
- bd82 11y agoThere is nothing special about the parsing algorithm. It is currently a naive LL(1) (single token lookahead) Parser. I may try to upgrade it to LL(*) in the future. However that does not mean Chevrotain it limited to LL(1) grammars. You can explicitly implement the lookahead function in any point the grammar. So if you are trying to parse a well designed grammar like Python who explicitly declare that being LL(1) is a blessing not a curse. https://www.python.org/dev/peps/pep-3099/#core-language https://www.python.org/dev/peps/pep-3099/#core-language You won't have any issues. If you are trying to parse a language that is nearly always LL(1) You also won't have many issues as you can define your own custom lookahead. Example: https://github.com/SAP/chevrotain/blob/master/examples/custom_lookahead/large_lookahead.js https://github.com/SAP/chevrotain/blob/master/examples/custo... If you are trying to parse a language that is nearly always NOT LL(1) You may want to use a different tool to parse it(at this point in time).