Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,36 @@ private Space statementDelim(@Nullable Tree t) {
case ASSERT:
case ASSIGNMENT:
case DO_WHILE_LOOP:
case IMPORT:
case IMPORT:{
System.err.println("IMPORT: cursor before=" + cursor + ", char='" +
(cursor < source.length() ? source.charAt(cursor) : "EOF") + "'");

// Only consume the required semicolon
Space result = sourceBefore(";");

// Check for extra semicolons - consume them and handle specially
if (cursor < source.length() && source.charAt(cursor) == ';') {
// Count and consume extra semicolons
int extraSemicolons = 0;
while (cursor < source.length() && source.charAt(cursor) == ';') {
extraSemicolons++;
cursor++;
}

// Store extra semicolons in the whitespace with a special format
// Prepend the semicolons to the existing whitespace
String currentWhitespace = result.getWhitespace();
String extraSemiStr = ";".repeat(extraSemicolons);
result = result.withWhitespace(extraSemiStr + currentWhitespace);

System.err.println("IMPORT: consumed " + extraSemicolons + " extra semicolon(s), cursor now at " + cursor);
}


System.err.println("IMPORT: cursor after=" + cursor + ", char='" +
(cursor < source.length() ? source.charAt(cursor) : "EOF") + "'");
return result;
}
case METHOD_INVOCATION:
case NEW_CLASS:
case THROW:
Expand Down
80 changes: 80 additions & 0 deletions rewrite-java/src/test/java/org/openrewrite/java/MyParserDebug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.openrewrite.java;

import org.junit.jupiter.api.Test;
import org.openrewrite.SourceFile;
import org.openrewrite.java.tree.J;
import org.openrewrite.tree.ParseError;
import java.util.List;
import java.util.stream.Collectors;
import org.openrewrite.java.TreeVisitingPrinter;


class MyParserDebug {

@Test
void debugDirectly() {
JavaParser parser = JavaParser.fromJavaVersion().build();

String sourceCode = """
import java.util.*;;
import java.io.*;

public class Main {
public static void main(String[] args) {}
}
""";

List<SourceFile> sourceFiles = parser.parse(sourceCode)
.collect(Collectors.toList());

SourceFile result = sourceFiles.get(0);

// --- CHECK FOR PARSE ERROR ---
if (result instanceof ParseError) {
ParseError error = (ParseError) result;
System.err.println("\n\n========================================");
System.err.println("CRITICAL: PARSER FAILED");
System.err.println("The parser could not understand the code.");
System.err.println("Error Message: " + error.getText()); // This usually holds the source
System.err.println("To String: " + error.toString());
System.err.println("========================================\n\n");

// Test should fail if parsing fails
throw new RuntimeException("Parser failed: " + error.toString());
}
else if (result instanceof J.CompilationUnit) {
// This part runs if parsing SUCCEEDS
J.CompilationUnit cu = (J.CompilationUnit) result;
var imports = cu.getImports();

System.err.println("========================================");
System.err.println("SUCCESS: Parser succeeded!");
System.err.println("Number of imports: " + imports.size());

if (imports.size() > 1) {
String prefix = imports.get(1).getPrefix().getWhitespace();
System.err.println("Second import prefix hex: " + stringToHex(prefix));
System.err.println("Second import prefix text: '" + prefix.replace("\n", "\\n").replace("\r", "\\r") + "'");
}

String printedSource = cu.print();
System.err.println("Original source length: " + sourceCode.length());
System.err.println("Printed source length: " + printedSource.length());
System.err.println("Sources match: " + sourceCode.equals(printedSource));

System.err.println("LST Structure:");
System.err.println(TreeVisitingPrinter.printTree((cu)));

System.err.println("========================================");
}
}

private String stringToHex(String input) {
if (input == null) return "null";
StringBuilder sb = new StringBuilder();
for (char c : input.toCharArray()) {
sb.append(String.format("\\u%04x ", (int) c));
}
return sb.toString();
}
}
Loading