antlr4
antlr4 copied to clipboard
Provide Javascript port of TokenStreamRewriter
What
Add a JavaScript port of the TokenStreamRewriter that includes most, but not all, of the methods from the Java implementation.
Why
So that JavaScript developers can benefit from having it to implement all sorts of neat things (refactorings, translation etc.)
Scope
Included
-
insertAfter
-
insertBefore
-
replace
-
delete
-
getText
-
getProgram
-
getTokenStream
Omitted
The following methods were omitted because a) I couldn't find any tests for them and b) their utility is not immediately obvious to me.
-
rollback
-
deleteProgram
-
getLastRewriteTokenIndex
If someone needs them then we can obviously add support for them in a subsequent PR.
How
By
- Porting most of the
TokenStreamRewriter
class - Porting tests and supporting fixtures (there are a couple of toy grammars used by the tests)
- Adding more tests to get close to 100% coverage
Credit to @sharwell for having ported the TokenStreamRewriter and the corresponding unit tests to TypeScript (see https://github.com/tunnelvisionlabs/antlr4ts). That has made creating a JavaScript port relatively straight-forward.
Drive-bys
- For added convenience, I added a Makefile within the spec folder to automate the code generation from the toy grammars.
- I added a dev dependency for c8 and a run target in package.json that generates a code coverage report. This I used to find uncovered sections in the
TokenStreamRewriter
. - I added an explicit dev dependency on eslint (seems it was transitive before) and upgraded it to the latest version. I then tweaked the parser options in order for it to recognize class static fields. See this SO thread for more context.
- I made a few additions to
.gitignore
to exclude autogenerated files that don't need to be placed under source control. - I added an
npm run lint
target
Closes #1767