-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Javascript: migrate prototypical Tree objects to es6 classes #2749
Conversation
@@ -30,7 +30,6 @@ var INVALID_INTERVAL = require('./tree/Tree').INVALID_INTERVAL; | |||
var INVALID_ALT_NUMBER = require('./atn/ATN').INVALID_ALT_NUMBER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
INVALID_ALT_NUMBER is a member of ATN, so I think this should be: ...).ATN.INVALID_ALT_NUMBER;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine as it is because I changed the export of ATN.js to be module.exports = ATN;
which means that the object itself is exported. This is precisely the equivalent of export default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so it's fine now? I thought you said it was undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for the misunderstanding. The situation is as follows:
- the ATN module is exported directly so its members are directly available as shown above.
- the cyclic dependency prevents the ATN module from being correctly imported, therefore node just returns an empty object
{}
which results inINVALID_ALT_NUMBER
beingundefined
. However, that has nothing to do with the way the object is exposed but rather with the cyclic dependency itself.
runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/browser/BaseBrowserTest.java
Outdated
Show resolved
Hide resolved
runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/BaseNodeTest.java
Outdated
Show resolved
Hide resolved
hey @ericvergnaud I think it is more appropiate to continue the discussion over here from: #2749 (comment) So I have good news and bad news: good: I think I have a fix for the failing tests. I basically did a rollback of the bad: I tried to run the tests on different browsers but in all cases they fail with empty output 😞 . Eventually I started doubting the tests themselves and rolled back to commit 2d7f727 and tried the tests; and they also fail 😨 . Therefore right now I am stuck. There are no documentation on the browser test suite (so I dont know if I need a special setup) and even the tests before the refactoring started are failing for me. Any hints/suggestion are welcomed. |
@carocad I will try and look into this by end of next week |
Ok, I've been looking into this, and from what I can see:
|
Thanks for looking into this :)
I just double checked this and for full disclosure here is the webpack processed antlr4 runtime. It seems that it is still using classes and other "newer" stuff. Therefore I think I would need to modify the webpack configuration (separate PR?). Btw, how do we proceed with this PR then ? Is it ok as it is ? |
do we still need a fix for the INVALID_ALT_NUMBER? |
definitely, but as a mention before that would require quite some refactoring on the way that several modules are imported and exported. Therefore I wanted to tackle that separatedly as it is not a problem that arised due to this PR but rather it has been there from the very beginning. |
removed unused Utils import
use module.exports with object literal for exports
use object destructuring on import
removed unused RuleNode import
…t on an anonymous object
382581c
to
c8ae1a8
Compare
how about the following: |
For transparency here is the cyclic dependency cycle. I got it using madge $ madge --circular src/antlr4/index.js
Processed 47 files (1.1s)
✖ Found 3 circular dependencies!
1) LL1Analyzer.js > PredictionContext.js > RuleContext.js > atn/ATN.js
2) RuleContext.js > tree/Trees.js > ParserRuleContext.js
3) RuleContext.js > tree/Trees.js and the complete dependency graph starting at |
My usual workaround in ES2015 is as follows: in imported script
in importing script
not sure how this can be achieved in ES6 though... |
@parrt blessed |
This PR is a follow up on #2746.
The (original) scope was to refactor the Tree object declarations to:
However, during the refactoring a couple of problems arised which increased the scope to also include:
antlr4.tree.ParseTreeListener.call(this)
. This is a consecuence of this PR refactor. Classes unlike functions cannot be "called" only instantiated. A possible substitution for this would benew antlr....
. However a closer inspection showed that this is an interface with no inner members therefore it makes no sense to instantiate an objec and throw it away inmediately.module.export = MyClass
. This is a follow up on this comment from @ericvergnaud to make the JS target closer to the Java one. As I was already touching these files it made sense to do the change inmediately.Note to @ericvergnaud :
const ATN = require ....
to return an empty object which means thatATN.INVALID_ALT_NUMBER
returnsundefined
as opposed to0
. Since fixing that bug would require quite some refactoring I left it there. This bug has been there even before I started the refactoring. See Fix errors in JavaScript run-time. #2496 (comment)