问题
I am interested in modifying java syntax and some implicit paradigms. Since I develop with eclipse which provides it's own compiler, which can also be used standalone, I was wondering if it wasn't possible to extend ecj to respect additional grammar rules (and correctly handle them). My syntactical changes are all resolvable by removing elements from the AST and creating some new ones, so I assume that what I want to do is possible without diving into bytecode.
Essentially, what I want to do could be done by 'virtually' modifying the source code before the actual compilation. However I suspect that doing so would mess up the source mapping, which would make debugging a hell.
On a sidenote: I am aware of project Lombok, which extends and alters class compilation, however lombok uses annotations only, and does not modify syntax, strictly speaking. So what I want to do is more invasive to the language specs.
回答1:
As Object Teams has been mentioned in comments:
(1) Object Teams itself extends JDT for its own language OT/J which is an extension of Java. This is done in a dual strategy:
- We maintain a fork of org.eclipse.jdt.core. While this is quite heavy lifting it successfully demonstrates that the JDT architecture is suitable for modification.
- We use our own concepts of role objects to non-invasively adapt the behavior of other parts of the IDE (notably org.eclipse.jdt.ui) to reflect the semantics of OT/J
(2) I have a few (oldish) blog posts that demonstrate how OT/J can be used for creating non-invasive variants of JDT including support for extended syntax:
- IDE for your own language embedded in Java? (part 1)
- IDE for your own language embedded in Java? (part 2)
- Get for free what Coin doesn’t buy you
Disclaimer: I am author of OT/J and lead of its implementation, and later became a committer on Eclipse JDT.
For further questions, there's a forum.
来源:https://stackoverflow.com/questions/60170365/is-eclipses-ecj-compiler-extensible