cleanup and remove redundant operation for trailing single line comment on expanded type name

This commit is contained in:
Lulu13022002 2024-04-12 18:59:30 +02:00
parent 969c2636fb
commit 1ff22c7119
No known key found for this signature in database
GPG Key ID: 491C8F0B8ACDEB01
14 changed files with 72 additions and 85 deletions

View File

@ -11,7 +11,7 @@ public interface ImportCollector {
}
@Override
public void addStaticImport(final String typeName) {
public void addStaticImport(final String fullName) {
}
@ -29,7 +29,7 @@ public interface ImportCollector {
void addImport(String typeName);
void addStaticImport(String typeName);
void addStaticImport(String fullName);
String getStaticMemberShortName(String fullName);

View File

@ -37,11 +37,11 @@ public class ImportTypeCollector implements ImportCollector {
}
@Override
public void addStaticImport(String typeName) {
if (typeName.endsWith("*")) {
this.globalStaticImports.add(typeName.substring(0, typeName.lastIndexOf('.')));
public void addStaticImport(String fullName) {
if (fullName.endsWith("*")) {
this.globalStaticImports.add(fullName.substring(0, fullName.lastIndexOf('.')));
} else {
this.staticImports.put(typeName, typeName.substring(typeName.lastIndexOf('.') + 1));
this.staticImports.put(fullName, fullName.substring(fullName.lastIndexOf('.') + 1));
}
}
@ -85,11 +85,11 @@ public class ImportTypeCollector implements ImportCollector {
int skipNode = Formatting.countOccurrences(originalNestedName, '.') - advancedNode;
StringReader reader = new StringReader(originalNestedName);
while (skipNode > 0) {
reader.skipString('.');
reader.skipStringUntil('.');
reader.skip(); // skip dot
skipNode--;
}
typeName = reader.readRemainingString();
typeName = reader.getRemaining();
} else {
typeName = type.simpleName();
}

View File

@ -3,6 +3,7 @@ package io.papermc.generator.rewriter.parser;
import io.papermc.generator.rewriter.context.ImportCollector;
import io.papermc.generator.rewriter.parser.step.IterativeStep;
import io.papermc.generator.rewriter.parser.step.StepHolder;
import io.papermc.generator.rewriter.parser.step.StepManager;
import io.papermc.generator.rewriter.parser.step.factory.AnnotationSteps;
import io.papermc.generator.rewriter.parser.step.factory.ImportSteps;
import org.jetbrains.annotations.ApiStatus;
@ -15,7 +16,7 @@ import java.util.Set;
public class LineParser {
private final Set<ClosureType> closures = EnumSet.noneOf(ClosureType.class);
private final Deque<IterativeStep> steps = new ArrayDeque<>(10);
private final StepManager stepManager = new StepManager();
public boolean advanceEnclosure(ClosureType type, StringReader line) {
boolean inside = this.closures.contains(type);
@ -60,24 +61,6 @@ public class LineParser {
return skipped;
}
public boolean skipCommentOrWhitespaceInName(StringReader line, NameCursorState state) {
if (state == NameCursorState.AFTER_DOT) {
return this.skipCommentOrWhitespace(line);
} else if (state == NameCursorState.INVALID_CHAR) { // this is tricky todo redo this part later
boolean skipped = this.trySkipCommentOrWhitespaceUntil(line, '.');
int previousCursor = line.getCursor();
if (!skipped && this.skipCommentOrWhitespace(line) && line.canRead() && this.nextSingleLineComment(line)) {
// ignore single line comment at the end of the name
line.setCursor(line.getTotalLength());
skipped = true;
} else {
line.setCursor(previousCursor);
}
return skipped;
}
return false;
}
public boolean nextSingleLineComment(StringReader line) {
return line.peek() == '/' && line.canRead(2) && line.peek(1) == '/';
}
@ -86,7 +69,7 @@ public class LineParser {
outerLoop:
while (line.canRead()) {
IterativeStep step;
while ((step = this.steps.poll()) != null) {
while ((step = this.stepManager.getSteps().poll()) != null) {
step.run(line, this);
if (!line.canRead()) {
break outerLoop;
@ -105,12 +88,12 @@ public class LineParser {
// not commented
char c = line.peek();
if (AnnotationSteps.canStart(c)) { // handle annotation with param to avoid open curly bracket that occur in array argument
this.enqueue(new AnnotationSteps());
this.stepManager.enqueue(new AnnotationSteps());
continue;
} else if (c == '{') {
return true;
} else if (ImportSteps.canStart(line)) {
this.enqueue(new ImportSteps(collector));
this.stepManager.enqueue(new ImportSteps(collector));
continue;
}
@ -119,21 +102,7 @@ public class LineParser {
return false;
}
private void enqueue(StepHolder holder) {
for (IterativeStep step : holder.initialSteps()) {
if (!this.steps.offerLast(step)) {
throw new IllegalStateException("Cannot add a step into the queue!");
}
}
}
public void addPriorityStep(IterativeStep step) {
if (!this.steps.offerFirst(step)) {
throw new IllegalStateException("Cannot add a priority step into the queue!");
}
}
public void clearRemainingSteps() {
this.steps.clear();
public StepManager getSteps() {
return this.stepManager;
}
}

View File

@ -1,6 +0,0 @@
package io.papermc.generator.rewriter.parser;
public enum NameCursorState {
AFTER_DOT,
INVALID_CHAR
}

View File

@ -5,8 +5,8 @@ public class ProtoTypeName {
private final String initialName;
private StringBuilder currentName;
private ProtoTypeName(String name) {
this.initialName = name;
private ProtoTypeName(String initialName) {
this.initialName = initialName;
}
public void append(String part) {
@ -16,7 +16,7 @@ public class ProtoTypeName {
this.currentName.append(part);
}
public String getName() {
public String getFinalName() {
return this.currentName != null ? this.currentName.toString() : this.initialName;
}
@ -27,8 +27,8 @@ public class ProtoTypeName {
return this.initialName.isEmpty() || this.initialName.lastIndexOf('.') == this.initialName.length() - 1;
}
public static ProtoTypeName create(String name) {
return new ProtoTypeName(name);
public static ProtoTypeName create(String initialName) {
return new ProtoTypeName(initialName);
}
}

View File

@ -4,6 +4,7 @@ import com.mojang.brigadier.ImmutableStringReader;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;
// based on brigadier string reader with some extra/removed features for rewriter
public class StringReader implements ImmutableStringReader {
@ -106,7 +107,7 @@ public class StringReader implements ImmutableStringReader {
return i;
}
public void skipString(final char terminator) {
public void skipStringUntil(final char terminator) {
while (this.canRead() && this.peek() != terminator) {
this.skip();
}
@ -158,22 +159,16 @@ public class StringReader implements ImmutableStringReader {
return false;
}
public String readRemainingString() {
final int start = this.cursor;
this.cursor = this.getTotalLength();
return this.string.substring(start);
}
public String readStringUntil(final char terminator) {
final int start = this.cursor;
this.skipString(terminator);
this.skipStringUntil(terminator);
return this.string.substring(start, this.cursor);
}
@Nullable
public String getStringUntil(final char terminator) {
final int start = this.cursor;
this.skipString(terminator);
this.skipStringUntil(terminator);
if (!this.canRead()) {
return null;
}
@ -181,9 +176,9 @@ public class StringReader implements ImmutableStringReader {
}
// cleaner is used to skip stuff like : net/* hi */./**/kyori.adventure.translation/**/.Translatable within the type name
public String getPartNameUntil(final char terminator, final BiPredicate<StringReader, NameCursorState> cleaner, final boolean forImport, final BooleanSupplier checkStartGetter) { // this break the concept of this a class a bit but it's not worth making a code point equivalent for only this method
public String getPartNameUntil(final char terminator, final Predicate<StringReader> cleaner, final boolean forImport, final @Nullable ProtoTypeName currentName) { // this break the concept of this a class a bit but it's not worth making a code point equivalent for only this method
boolean hasCleaner = cleaner != null;
boolean checkStart = checkStartGetter.getAsBoolean();
boolean checkStart = currentName == null || currentName.shouldCheckStartIdentifier();
StringBuilder name = new StringBuilder();
while (this.canRead()) {
int c = this.peekPoint();
@ -192,14 +187,14 @@ public class StringReader implements ImmutableStringReader {
}
if (checkStart) { // had a dot before
if (hasCleaner && cleaner.test(this, NameCursorState.AFTER_DOT)) {
if (hasCleaner && cleaner.test(this)) {
continue;
}
}
boolean isJavaIdChar = checkStart ? Character.isJavaIdentifierStart(c) : Character.isJavaIdentifierPart(c);
if (!isJavaIdChar && (checkStart || c != '.') && !(c == '*' && forImport)) { // star should be allowed only at the end for import todo
if (hasCleaner && cleaner.test(this, NameCursorState.INVALID_CHAR)) {
if (hasCleaner && cleaner.test(this)) {
continue;
} else {
break;

View File

@ -15,7 +15,7 @@ public class RepeatIterativeStep implements IterativeStep {
@Override
public void run(StringReader line, LineParser parser) {
if (this.runner.test(line, parser)) {
parser.addPriorityStep(this);
parser.getSteps().addPriority(this);
}
}
}

View File

@ -0,0 +1,31 @@
package io.papermc.generator.rewriter.parser.step;
import java.util.ArrayDeque;
import java.util.Deque;
public class StepManager {
private final Deque<IterativeStep> steps = new ArrayDeque<>(10);
public Deque<IterativeStep> getSteps() {
return this.steps;
}
public void enqueue(StepHolder holder) {
for (IterativeStep step : holder.initialSteps()) {
if (!this.steps.offerLast(step)) {
throw new IllegalStateException("Cannot add a step into the queue!");
}
}
}
public void addPriority(IterativeStep step) {
if (!this.steps.offerFirst(step)) {
throw new IllegalStateException("Cannot add a priority step into the queue!");
}
}
public void clearRemaining() {
this.steps.clear();
}
}

View File

@ -39,8 +39,7 @@ public final class AnnotationSteps implements StepHolder {
return true;
}
String name = line.getPartNameUntil('(', parser::skipCommentOrWhitespaceInName, false,
() -> checkStartId);
String name = line.getPartNameUntil('(', parser::skipCommentOrWhitespace, false, this.name);
if (line.canRead() && parser.nextSingleLineComment(line)) {
// ignore single line comment at the end and allow the name to continue
@ -67,9 +66,9 @@ public final class AnnotationSteps implements StepHolder {
// filter out @interface
public void checkAnnotationName(StringReader line, LineParser parser) {
String name = this.name.getName();
String name = this.name.getFinalName();
if (name.isEmpty() || NamingManager.hasIllegalKeyword(name)) { // keyword are checked after to simplify things
parser.clearRemainingSteps();
parser.getSteps().clearRemaining();
}
}
@ -80,7 +79,7 @@ public final class AnnotationSteps implements StepHolder {
}
if (parser.advanceEnclosure(ClosureType.PARENTHESIS, line)) { // open parenthesis?
parser.addPriorityStep(this.skipParenthesesStep);
parser.getSteps().addPriority(this.skipParenthesesStep);
}
return false;
}

View File

@ -36,7 +36,7 @@ public final class ImportSteps implements StepHolder {
if (!parser.skipComment(line)) {
if (line.skipWhitespace() == 0) { // expect at least one space between import, static and type name unless a multi comment is here to fill the gap
parser.clearRemainingSteps();
parser.getSteps().clearRemaining();
}
}
}
@ -48,14 +48,14 @@ public final class ImportSteps implements StepHolder {
}
if (line.trySkipString("static")) {
parser.addPriorityStep(this.enforceSpaceStep);
parser.getSteps().addPriority(this.enforceSpaceStep);
this.isStatic = true;
}
return false;
}
public void collectImport(StringReader line, LineParser parser) {
String name = this.name.getName();
String name = this.name.getFinalName();
if (name.isEmpty() || NamingManager.hasIllegalKeyword(name)) { // keyword are checked after to simplify things
return;
}
@ -73,8 +73,7 @@ public final class ImportSteps implements StepHolder {
return true;
}
String name = line.getPartNameUntil(';', parser::skipCommentOrWhitespaceInName, true,
() -> this.name == null || this.name.shouldCheckStartIdentifier());
String name = line.getPartNameUntil(';', parser::skipCommentOrWhitespace, true, this.name);
if (line.canRead() && parser.nextSingleLineComment(line)) {
// ignore single line comment at the end of the name

View File

@ -243,7 +243,7 @@ public class SearchReplaceRewriter implements SourceRewriter {
return EMPTY_MARKER;
}
String pattern = lineIterator.readRemainingString();
String pattern = lineIterator.getRemaining();
if (patterns == null || patterns.contains(pattern)) { // patterns will be null only for tests
return new CommentMarker(pattern, foundStart, indentSize);
}

View File

@ -85,7 +85,7 @@ public class OldGeneratedCodeTest {
String generatedComment = "// %s ".formatted(Annotations.annotationStyle(GeneratedFrom.class));
if (nextLineIterator.trySkipString(generatedComment) && nextLineIterator.canRead()) {
String generatedVersion = nextLineIterator.readRemainingString();
String generatedVersion = nextLineIterator.getRemaining();
assertEquals(CURRENT_VERSION, generatedVersion,
"Code at line %s in %s is marked as being generated in version %s when the current version is %s".formatted(
lineCount, srt.getRewrittenClass().canonicalName(),

View File

@ -2,7 +2,7 @@ package io.papermc.generator.rewriter.data.sample.parser.area;
import org.jetbrains.annotations.ApiStatus;
@SuppressWarnings//discord /**/
@SuppressWarnings//discard /**/
/* a*/ ( /* a b*/
{ /*a*/
"DeprecatedIsStillUsed" /*a*/

View File

@ -3,7 +3,7 @@ package io.papermc.generator.rewriter.data.sample.parser.imports;
import// discard?
org /* hi */
/* hi */ .
bukkit /* hi */ // /*/
bukkit /* hi */ /*/
/* hi */ .
NamespacedKey// discard?