Class JavaCodeScanner
java.lang.Object
net.mcreator.java.JavaCodeScanner
A lightweight scanner for Java-like source code that classifies each character as either actual code
or part of a string literal, text block, character literal, or comment. Unlike a full parser, it is
tolerant to incomplete or invalid code: unterminated literals and comments simply extend to the end
of the input. This makes it suitable for processing code fragments and code being edited.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumClassification of a scanned character.static interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringmaskStringsAndComments(String code) Returns a copy of the given code in which every character of string literals, text blocks, character literals, and comments (including their delimiters) is replaced with a space.static booleanscan(String code, JavaCodeScanner.Visitor visitor) Scans the given code and reports every character along with itsJavaCodeScanner.Regionto the visitor.
-
Constructor Details
-
JavaCodeScanner
public JavaCodeScanner()
-
-
Method Details
-
scan
Scans the given code and reports every character along with itsJavaCodeScanner.Regionto the visitor. Text blocks (""" ... """) are reported asJavaCodeScanner.Region.STRING. The line terminator ending a line comment is not part of the comment and is reported asJavaCodeScanner.Region.CODE.- Parameters:
code- The code to scanvisitor- The visitor to report each character to- Returns:
- true if the entire code was scanned, false if the visitor aborted the scan
-
maskStringsAndComments
Returns a copy of the given code in which every character of string literals, text blocks, character literals, and comments (including their delimiters) is replaced with a space. The returned string has the same length as the input, so every index maps 1:1 to the original code, which allows performing index-based operations on the original code based on findings in the masked copy.- Parameters:
code- The code to mask- Returns:
- The code of the same length with strings, char literals, and comments masked out
-