Refactor and Modernize Java CUP with AST Auto-Generation Support - #5
Open
EmptyDreams wants to merge 137 commits into
Open
Refactor and Modernize Java CUP with AST Auto-Generation Support#5EmptyDreams wants to merge 137 commits into
EmptyDreams wants to merge 137 commits into
Conversation
+ Replace Stack with ArrayStack, and Hashtable with HashMap + Optimize some code logic to improve execution efficiency + Modify code formatting to enhance readability
… occurring in certain cases.
- No longer require a production with only a single element to exist necessarily.
…etter calls and definition signatures.
…d checker for IAstNode
…ted .java files to fail compilation
…te `Location` design: require each subclass to declare a `public static final <SelfType> NO_LOCATION` to prevent type-casting issues.
… base and List nodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thank you sincerely for your years of dedicated work on the Java CUP project.
This pull request represents a comprehensive modernization of Java CUP—refactoring its architecture, replacing outdated patterns with modern Java practices, and adding significant new capabilities, most notably automatic AST generation directly from
.cupgrammar files.I fully understand that such a large, compatibility-breaking change may not align with the current maintenance goals or release strategy of the upstream project. If you decide not to merge it, I will maintain this version independently as a separate fork.
Key changes include:
Standardized class naming:
The original
symclass has been renamed toGrammarSymConstants; similarly, classes likeparser—which were prone to naming conflicts with user code—have been updated to follow Java’s PascalCase convention.Refactored source location handling:
-locationscommand-line option (which toggled whether position information was attached to symbols) has been removed;Locationinterface is now used as the standard abstraction for source positions;-locationCLI option allows users to specify a customLocationimplementation (defaults toComplexLocation), offering greater flexibility and type safety.Removed outdated or redundant components:
xmlactionmodule has been entirely removed;Simple-series symbols and their correspondingSymbolFactoryimplementations are deleted;minijava2test file has been removed—it shares identical grammar withminijava, differing only in that it includes a simulation runtime, which provides limited value in the current testing context.Overhauled symbol system:
symbolclass is now namedGrammarSymbol, while the user-facing class remainsSymbolto clarify responsibilities;Symbolis simplified into an immutable class with cleaner logic;ComplexSymbolvariants for all primitive types (e.g.,IntComplexSymbol) eliminate boxing/unboxing overhead;SymbolandLocationdesign.Modernized collections:
Legacy collection types such as
StackandHashtablehave been replaced with modern JDK equivalents (e.g.,ArrayList,HashMap). Additionally, a newIntArrayStackis introduced to support integer stack operations without boxing.Build system migration:
src/java→src/main/java, test grammars moved tosrc/test/resources;New AST auto-generation feature (core addition):
.cupgrammar files;-astCLI option enables AST generation and controls node class naming format;%namerdirective is introduced:By default, a distinct AST class is generated for each non-empty production (productions with identical RHS are merged), but the class name is auto-generated and not human-friendly. Users can append
%namer MyNodeNameto a production to explicitly assign a readable class name to its AST node;symbol?— optional;symbol*— nullable list;symbol+— non-empty list;symbol[sep1 sep2 …]+orsymbol[sep1 sep2 …]*:Represents a list separated by one or more delimiters (e.g.,
comma,semicolon, etc., space-separated). Trailing delimiters are controlled by!(forbidden) or?(optional). For example:symbol[comma !]+→ non-empty list, no trailing comma;symbol[comma ?]*→ nullable list, trailing comma optional;...symbol— inline (flatten) child node into the current AST node;toTreeString(boolean)method for human-readable tree printing, with console color highlighting whentrue.Miscellaneous improvements:
And more...
Thank you again for your time and consideration. Regardless of the outcome, I deeply appreciate the foundation Java CUP has provided—and hope this contribution offers a compelling path forward for those seeking a more modern, expressive, and maintainable parser generator.