You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2026/10/08 16:02:26 UTC

(groovy) 01/01: GROOVY-12425: Refine error message for unclosed type argument and unclosed slashy string

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch GROOVY-12425
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 338a97830616ad43441492ee5d0db4256c095c32
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Sep 25 05:21:54 2026 +0900

    GROOVY-12425: Refine error message for unclosed type argument and unclosed slashy string
---
 src/antlr/GroovyLexer.g4                           |  57 +++++++-
 .../apache/groovy/parser/antlr4/AbstractLexer.java |  16 ++-
 .../apache/groovy/parser/antlr4/AstBuilder.java    |  11 +-
 .../parser/antlr4/ParserNegativeSyntaxTest.groovy  | 148 +++++++++++++++++++--
 .../groovy/parser/antlr4/AbstractLexerTest.java    | 103 +++++++++++++-
 .../groovy/parser/antlr4/AstBuilderHelperTest.java |   2 +
 6 files changed, 318 insertions(+), 19 deletions(-)

diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 12f228a7d0..ad0cc80f61 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -47,7 +47,14 @@ options {
 @members {
     private boolean errorIgnored;
     private long tokenIndex;
-    private int  lastTokenType;
+    /** Previous default-channel token. {@link Token#INVALID_TYPE} at the start of an input. */
+    private int  lastTokenType = Token.INVALID_TYPE;
+    /**
+     * Last default-channel token whose type is not {@code NL}.
+     * Stays {@link Token#INVALID_TYPE} until one is seen. Not {@link Token#EOF}:
+     * a negative index is rejected by {@link java.util.BitSet#get(int)}.
+     */
+    private int  lastRealTokenType = Token.INVALID_TYPE;
 
     /**
      * When {@code false}, the {@code val} keyword is treated as a regular
@@ -67,7 +74,12 @@ options {
         this.tokenIndex++;
 
         int tokenType = token.getType();
-        if (Token.DEFAULT_CHANNEL == token.getChannel()) {
+        // EOF is negative. Recording it makes the next slashy decision call
+        // BitSet.get(-1). setInputStream rewinds only through reset().
+        if (tokenType >= 0 && Token.DEFAULT_CHANNEL == token.getChannel()) {
+            if (NL != tokenType) {
+                this.lastRealTokenType = tokenType;
+            }
             this.lastTokenType = tokenType;
         }
 
@@ -78,6 +90,13 @@ options {
         super.emit(token);
     }
 
+    @Override
+    public void reset() {
+        super.reset();
+        this.lastTokenType = Token.INVALID_TYPE;
+        this.lastRealTokenType = Token.INVALID_TYPE;
+    }
+
     /**
      * Token types after which {@code /.../} must not be treated as a slashy string
      * (e.g. {@code a++ / b}, {@code list[i] / n}). {@link java.util.BitSet} gives
@@ -106,7 +125,24 @@ options {
     }
 
     private boolean isRegexAllowed() {
-        return !REGEX_CHECK_SET.get(this.lastTokenType);
+        return !isDivisionOperand(this.lastTokenType);
+    }
+
+    /**
+     * A {@code /} that reaches EOF is an unclosed slashy string only when the
+     * previous real token (ignoring {@code NL}) could not be the left operand
+     * of division. {@code 9 \n / \n 3} stays division; {@code s = /hello} does not.
+     * {@link Token#INVALID_TYPE} (no real token yet, including a leading newline)
+     * is not an operand.
+     */
+    private boolean isUnclosedSlashy() {
+        int decision = this.lastTokenType == NL ? this.lastRealTokenType : this.lastTokenType;
+        return !isDivisionOperand(decision);
+    }
+
+    /** {@code REGEX_CHECK_SET} rejects a negative type, and {@link Token#EOF} is not an operand. */
+    private static boolean isDivisionOperand(final int tokenType) {
+        return tokenType >= 0 && REGEX_CHECK_SET.get(tokenType);
     }
 
     /**
@@ -289,13 +325,26 @@ options {
 
 
 // ยง3.10.5 String Literals
+// A quote that never closes matches nothing here, so it is UNEXPECTED_CHAR.
+// requireUnexpectedCharacter maps that quote to "Unclosed string literal"
+// unless the remainder is an illegal escape. `/` is also DIV, so an unclosed
+// `/.../` cannot use that fall-through and is an EOF alternative of this rule.
+// isUnclosedSlashy() is the first edge of that alternative, before any body
+// character is read. `*` is safe because the follower is EOF, not `/`:
+// `//` is not an empty slashy. EOF stays here so a second rule cannot
+// swallow source after a real closer.
 StringLiteral
     :   GStringQuotationMark  DqStringCharacter*  GStringQuotationMark
     |   SqStringQuotationMark  SqStringCharacter*  SqStringQuotationMark
-    |   Slash { this.isRegexAllowed() && _input.LA(1) != '*' }?  SlashyStringCharacter+  Slash
+    |   Slash { this.isRegexAllowed() && _input.LA(1) != '*' }?
+            (   SlashyStringCharacter+ Slash
+            |   { this.isUnclosedSlashy() }? SlashyStringCharacter* EOF { requireUnclosedString(errorIgnored); }
+            )
 
     |   TdqStringQuotationMark  TdqStringCharacter*  TdqStringQuotationMark
     |   TsqStringQuotationMark  TsqStringCharacter*  TsqStringQuotationMark
+    // `$/` is also identifier `$` plus division. An EOF alternative here would
+    // reject `($/2)` and `$/=`. Unclosed `$/...` stays a later error.
     |   DollarSlashyGStringQuotationMarkBegin  DollarSlashyStringCharacter+  DollarSlashyGStringQuotationMarkEnd
     ;
 
diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AbstractLexer.java b/src/main/java/org/apache/groovy/parser/antlr4/AbstractLexer.java
index 4057eff850..6347aa9e4e 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AbstractLexer.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AbstractLexer.java
@@ -213,6 +213,16 @@ public abstract class AbstractLexer extends Lexer implements SyntaxErrorReportab
         requireAtTokenStart(errorIgnored, "Unclosed comment");
     }
 
+    /**
+     * Report an unclosed {@code /.../} at the opener (javac:
+     * {@code unclosed string literal}), not at EOF after the scan.
+     *
+     * @param errorIgnored when {@code true}, keep tokenising (IDE highlighting)
+     */
+    void requireUnclosedString(final boolean errorIgnored) {
+        requireAtTokenStart(errorIgnored, "Unclosed string literal");
+    }
+
     /**
      * Invalid octal ({@code 08}, {@code 09}, {@code 08L}) reports at the
      * token start. Using the start index rather than a running digit count keeps
@@ -226,9 +236,9 @@ public abstract class AbstractLexer extends Lexer implements SyntaxErrorReportab
     }
 
     /**
-     * {@link SyntaxErrorReportable#require} positions relative to the current
-     * lexer cursor. After scanning to EOF that cursor is past the comment, so
-     * the offset is token-start minus current (line and column).
+     * {@link SyntaxErrorReportable#require} is relative to the current lexer
+     * cursor. The offset is token-start minus that cursor, so the caret stays
+     * on the opener after a scan to EOF and on an invalid octal's first digit.
      */
     private void requireAtTokenStart(final boolean errorIgnored, final String msg) {
         require(errorIgnored, msg,
diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index dde9972427..86f99b6878 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -2594,11 +2594,16 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
                 && baseExpr instanceof BinaryExpression bin
                 && !"[".equals(bin.getOperation().getText())) {
             // `List<Integer name` is parsed as the comparison `List < Integer` plus a
-            // command argument. javac: `'>' expected`.
-            if ("<".equals(bin.getOperation().getText())
+            // following argument list. javac: `'>' expected`. Unmatched `>` in the
+            // error strategy keeps ANTLR's offender; point at that list, not at `List`.
+            // An identifier after `T < U` is argumentList, not commandArgument.
+            // The list is the caret. Without one, keep the Unexpected input path
+            // below instead of passing a null anchor.
+            if (hasArgumentList
+                    && "<".equals(bin.getOperation().getText())
                     && looksLikeTypeName(bin.getLeftExpression())
                     && looksLikeTypeName(bin.getRightExpression())) {
-                throw createParsingFailedException("Missing '>'", bin);
+                throw createParsingFailedException("Missing '>'", ctx.enhancedArgumentListInPar());
             }
             throw createParsingFailedException("Unexpected input: '" + getOriginalText(ctx.expression()) + "'", ctx.expression());
         }
diff --git a/src/test/groovy/org/apache/groovy/parser/antlr4/ParserNegativeSyntaxTest.groovy b/src/test/groovy/org/apache/groovy/parser/antlr4/ParserNegativeSyntaxTest.groovy
index c1f90e2c8a..2a41c976d6 100644
--- a/src/test/groovy/org/apache/groovy/parser/antlr4/ParserNegativeSyntaxTest.groovy
+++ b/src/test/groovy/org/apache/groovy/parser/antlr4/ParserNegativeSyntaxTest.groovy
@@ -265,9 +265,36 @@ final class ParserNegativeSyntaxTest {
         expectParseError '''\
             |List<Integer list2 = new ArrayList<Integer>()
             |'''.stripMargin(), '''\
-            |Missing '>' @ line 1, column 1.
+            |Missing '>' @ line 1, column 14.
             |   List<Integer list2 = new ArrayList<Integer>()
-            |   ^
+            |                ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed nested type argument'() {
+        expectParseError '''\
+            |List<List<Integer> list2 = new ArrayList<>()
+            |'''.stripMargin(), '''\
+            |Missing '>' @ line 1, column 20.
+            |   List<List<Integer> list2 = new ArrayList<>()
+            |                      ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed type argument with extra space before the identifier'() {
+        // Two spaces so `name` is not the column after Integer.
+        expectParseError '''\
+            |List<Integer  name
+            |'''.stripMargin(), '''\
+            |Missing '>' @ line 1, column 15.
+            |   List<Integer  name
+            |                 ^
             |
             |1 error
             |'''.stripMargin()
@@ -3259,6 +3286,77 @@ final class ParserNegativeSyntaxTest {
             |'''.stripMargin()
     }
 
+    @Test
+    void 'unclosed quote at the start of the file'() {
+        // No surrounding tokens: the caret is the quote itself, not omitted.
+        expectParseError "'abc", '''\
+            |Unclosed string literal @ line 1, column 1.
+            |   'abc
+            |   ^
+            |
+            |1 error
+            |'''.stripMargin()
+        expectParseError '"abc', '''\
+            |Unclosed string literal @ line 1, column 1.
+            |   "abc
+            |   ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed slashy string'() {
+        expectParseError 's = /hello', '''\
+            |Unclosed string literal @ line 1, column 5.
+            |   s = /hello
+            |       ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed slashy string on a later line'() {
+        expectParseError '''\
+            |def n = 1
+            |s = /hello
+            |world
+            |'''.stripMargin(), '''\
+            |Unclosed string literal @ line 2, column 5.
+            |   s = /hello
+            |       ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed slashy string after a line break'() {
+        // The newline is not a division operand. The caret is the '/' on line 2.
+        expectParseError '''\
+            |s =
+            |/hello
+            |'''.stripMargin(), '''\
+            |Unclosed string literal @ line 2, column 1.
+            |   /hello
+            |   ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed slashy at end of file after equals'() {
+        expectParseError 's = /', '''\
+            |Unclosed string literal @ line 1, column 5.
+            |   s = /
+            |       ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
     @Test
     void 'unclosed double-quoted string'() {
         expectParseError '''\
@@ -3423,7 +3521,15 @@ final class ParserNegativeSyntaxTest {
 
     @Test
     void 'generic type missing closing angle'() {
-        expectContains 'def list1 = new ArrayList<Integer()', "Missing '>'"
+        expectParseError '''\
+            |def list1 = new ArrayList<Integer()
+            |'''.stripMargin(), '''\
+            |Missing '>' @ line 1, column 34.
+            |   def list1 = new ArrayList<Integer()
+            |                                    ^
+            |
+            |1 error
+            |'''.stripMargin()
     }
 
     @Test
@@ -3707,14 +3813,14 @@ final class ParserNegativeSyntaxTest {
 
     @Test
     void 'unclosed type argument as a comparison plus command argument'() {
-        // Spaces make `<` a comparison; `name` is then a command argument.
+        // Spaces make `<` a comparison; `name` is then the argument list.
         // AstBuilder rewrites that shape to javac's `'>' expected`.
         expectParseError '''\
             |List < Integer name
             |'''.stripMargin(), '''\
-            |Missing '>' @ line 1, column 1.
+            |Missing '>' @ line 1, column 16.
             |   List < Integer name
-            |   ^
+            |                  ^
             |
             |1 error
             |'''.stripMargin()
@@ -3725,9 +3831,35 @@ final class ParserNegativeSyntaxTest {
         expectParseError '''\
             |List<String> < Integer name
             |'''.stripMargin(), '''\
-            |Missing '>' @ line 1, column 1.
+            |Missing '>' @ line 1, column 24.
             |   List<String> < Integer name
-            |   ^
+            |                          ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed type argument with a comma-separated argument list'() {
+        expectParseError '''\
+            |List < Integer 1, 2
+            |'''.stripMargin(), '''\
+            |Missing '>' @ line 1, column 16.
+            |   List < Integer 1, 2
+            |                  ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
+    @Test
+    void 'unclosed type argument with an argument list then a command argument'() {
+        expectParseError '''\
+            |List < Integer name foo
+            |'''.stripMargin(), '''\
+            |Missing '>' @ line 1, column 16.
+            |   List < Integer name foo
+            |                  ^
             |
             |1 error
             |'''.stripMargin()
diff --git a/src/test/java/org/apache/groovy/parser/antlr4/AbstractLexerTest.java b/src/test/java/org/apache/groovy/parser/antlr4/AbstractLexerTest.java
index 587fb2f727..e3e620b8d5 100644
--- a/src/test/java/org/apache/groovy/parser/antlr4/AbstractLexerTest.java
+++ b/src/test/java/org/apache/groovy/parser/antlr4/AbstractLexerTest.java
@@ -31,12 +31,15 @@ import java.util.List;
 import java.util.stream.Stream;
 
 import static org.apache.groovy.parser.antlr4.GroovyLexer.CapitalizedIdentifier;
+import static org.apache.groovy.parser.antlr4.GroovyLexer.DIV;
 import static org.apache.groovy.parser.antlr4.GroovyLexer.FloatingPointLiteral;
+import static org.apache.groovy.parser.antlr4.GroovyLexer.StringLiteral;
 import static org.apache.groovy.parser.antlr4.GroovyLexer.GStringBegin;
 import static org.apache.groovy.parser.antlr4.GroovyLexer.GStringEnd;
 import static org.apache.groovy.parser.antlr4.GroovyLexer.GStringPathPart;
 import static org.apache.groovy.parser.antlr4.GroovyLexer.Identifier;
 import static org.apache.groovy.parser.antlr4.GroovyLexer.IntegerLiteral;
+import static org.apache.groovy.parser.antlr4.GroovyLexer.NL;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -148,10 +151,108 @@ final class AbstractLexerTest {
                 Arguments.of("\"C:\\Users\\me\"", "Illegal escape character: '\\U'", 1, 4),
                 Arguments.of("\"C:\\Users", "Illegal escape character: '\\U'", 1, 4),
                 Arguments.of("/* comment", "Unclosed comment", 1, 1),
-                Arguments.of("    /* comment", "Unclosed comment", 1, 5)
+                Arguments.of("    /* comment", "Unclosed comment", 1, 5),
+                Arguments.of("/hello", "Unclosed string literal", 1, 1),
+                Arguments.of("    /hello", "Unclosed string literal", 1, 5),
+                Arguments.of("/hello\nworld", "Unclosed string literal", 1, 1),
+                Arguments.of("/", "Unclosed string literal", 1, 1)
         );
     }
 
+    @Test
+    void closedSlashyAndDivisionStayDistinct() {
+        List<Token> slashy = defaultChannel("/ab/");
+        assertEquals(StringLiteral, slashy.get(0).getType());
+        assertEquals("/ab/", slashy.get(0).getText());
+
+        List<Token> division = defaultChannel("a / b");
+        assertEquals(Identifier, division.get(0).getType());
+        assertEquals(DIV, division.get(1).getType());
+        assertEquals(Identifier, division.get(2).getType());
+
+        List<Token> dollar = defaultChannel("$/ab/$");
+        assertEquals(StringLiteral, dollar.get(0).getType());
+        assertEquals("$/ab/$", dollar.get(0).getText());
+    }
+
+    @Test
+    void errorIgnoredUnclosedSlashyTokenizesWithoutThrowing() {
+        GroovyLangLexer lexer = new GroovyLangLexer(CharStreams.fromString("s = /hello"));
+        lexer.setErrorIgnored(true);
+        List<Token> tokens = assertDoesNotThrow(() -> collect(lexer));
+        assertEquals(Token.EOF, tokens.get(tokens.size() - 1).getType());
+        assertTrue(tokens.stream().anyMatch(t -> t.getType() == StringLiteral && "/hello".equals(t.getText())),
+                tokens.toString());
+    }
+
+    @Test
+    void closedSlashyAfterNewlineIsStillAString() {
+        List<Token> tokens = defaultChannel("9\n/3/");
+        assertEquals(IntegerLiteral, tokens.get(0).getType());
+        assertEquals(StringLiteral, tokens.get(2).getType());
+        assertEquals("/3/", tokens.get(2).getText());
+    }
+
+    @Test
+    void lineCommentIsNotAnEmptySlashy() {
+        // Bare // is length 2, the same length as an empty slashy. StringLiteral
+        // is the earlier rule, so it would win that tie.
+        List<Token> tokens = defaultChannel("s = //\n1");
+        assertTrue(tokens.stream().anyMatch(t -> t.getType() == NL && "//".equals(t.getText())),
+                tokens.toString());
+        assertTrue(tokens.stream().noneMatch(t -> t.getType() == StringLiteral || t.getType() == DIV),
+                tokens.toString());
+        assertEquals(IntegerLiteral, tokens.get(tokens.size() - 1).getType());
+    }
+
+    @Test
+    void divisionAcrossLinesIsNotAnUnclosedSlashy() {
+        List<Token> tokens = defaultChannel("9\n/\n3");
+        assertEquals(IntegerLiteral, tokens.get(0).getType());
+        assertEquals(DIV, tokens.get(2).getType());
+        assertEquals(IntegerLiteral, tokens.get(4).getType());
+    }
+
+    @Test
+    void leadingNewlineWithoutAnOperandIsAnUnclosedSlashy() {
+        // No real token yet. A leading newline is not a division operand.
+        GroovySyntaxError err = assertThrows(GroovySyntaxError.class, () -> drain(lexer("\n/")));
+        assertEquals("Unclosed string literal", err.getMessage());
+        assertEquals(2, err.getLine());
+        assertEquals(1, err.getColumn());
+    }
+
+    @Test
+    void reusedLexerForgetsThePreviousDivisionOperand() {
+        // setInputStream rewinds through Lexer.reset(). Draining emits EOF
+        // (-1); a partial lex leaves the operand. Neither may decide the next input.
+        GroovyLangLexer drained = lexer("9");
+        drain(drained);
+        drained.setInputStream(CharStreams.fromString("\n/"));
+        GroovySyntaxError afterNewline = assertThrows(GroovySyntaxError.class, () -> drain(drained));
+        assertEquals("Unclosed string literal", afterNewline.getMessage());
+        assertEquals(2, afterNewline.getLine());
+        assertEquals(1, afterNewline.getColumn());
+
+        GroovyLangLexer partial = lexer("9");
+        assertEquals(IntegerLiteral, partial.nextToken().getType());
+        partial.setInputStream(CharStreams.fromString("/hello"));
+        GroovySyntaxError freshSlash = assertThrows(GroovySyntaxError.class, () -> drain(partial));
+        assertEquals("Unclosed string literal", freshSlash.getMessage());
+        assertEquals(1, freshSlash.getLine());
+        assertEquals(1, freshSlash.getColumn());
+    }
+
+    @Test
+    void dollarThenDivisionIsNotAString() {
+        // `$` is an identifier. `$/2` is division, not an unclosed dollar-slashy string.
+        List<Token> tokens = defaultChannel("($/2)");
+        assertEquals(Identifier, tokens.get(1).getType());
+        assertEquals("$", tokens.get(1).getText());
+        assertEquals(DIV, tokens.get(2).getType());
+        assertEquals(IntegerLiteral, tokens.get(3).getType());
+    }
+
     @Test
     void twoDigitOctalIsLegal() {
         assertEquals(0, AbstractLexer.illegalEscapeLaIndex(CharStreams.fromString("\\77x\""), '"'));
diff --git a/src/test/java/org/apache/groovy/parser/antlr4/AstBuilderHelperTest.java b/src/test/java/org/apache/groovy/parser/antlr4/AstBuilderHelperTest.java
index 82f1cf9c8c..1e135b6168 100644
--- a/src/test/java/org/apache/groovy/parser/antlr4/AstBuilderHelperTest.java
+++ b/src/test/java/org/apache/groovy/parser/antlr4/AstBuilderHelperTest.java
@@ -186,6 +186,8 @@ final class AstBuilderHelperTest {
                         }
                         """, "Annotation type elements cannot have a void return type"),
                 Arguments.of("List < Integer name", "Missing '>'"),
+                Arguments.of("List < Integer 1, 2", "Missing '>'"),
+                Arguments.of("List < Integer name foo", "Missing '>'"),
                 Arguments.of("1 = 2", "The left-hand side of an assignment must be a variable or a field"),
                 Arguments.of("foo(String a)", "Invalid method declaration; a return type or 'def' is required"),
                 Arguments.of("""