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/09/24 21:32:00 UTC
(groovy) branch GROOVY-12425 updated (6ff0fe40a3 -> 363cb9dc42)
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a change to branch GROOVY-12425
in repository https://gitbox.apache.org/repos/asf/groovy.git
omit 6ff0fe40a3 GROOVY-12425: Refine error message for unclosed type argument and unclosed slashy string
add 083406352c styling tweaks
new 363cb9dc42 GROOVY-12425: Refine error message for unclosed type argument and unclosed slashy string
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (6ff0fe40a3)
\
N -- N -- N refs/heads/GROOVY-12425 (363cb9dc42)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../gradle/GenerateSwingBuilderWidgetDocs.groovy | 9 +-
src/antlr/GroovyLexer.g4 | 20 +--
.../apache/groovy/parser/antlr4/AbstractLexer.java | 13 +-
src/spec/doc/assets/css/style.css | 7 +-
.../groovy/parser/antlr4/AbstractLexerTest.java | 21 +++
.../src/spec/doc/_swing-builder-widgets.adoc | 189 +++++++--------------
6 files changed, 108 insertions(+), 151 deletions(-)
(groovy) 01/01: GROOVY-12425: Refine error message for unclosed type argument and unclosed slashy string
Posted by su...@apache.org.
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 363cb9dc42a49e7a39b89567d0fef284998087bd
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 | 28 +++-
.../apache/groovy/parser/antlr4/AbstractLexer.java | 16 ++-
.../apache/groovy/parser/antlr4/AstBuilder.java | 6 +-
.../parser/antlr4/ParserNegativeSyntaxTest.groovy | 148 +++++++++++++++++++--
.../groovy/parser/antlr4/AbstractLexerTest.java | 73 +++++++++-
.../groovy/parser/antlr4/AstBuilderHelperTest.java | 2 +
6 files changed, 258 insertions(+), 15 deletions(-)
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 12f228a7d0..f75547f174 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -48,6 +48,8 @@ options {
private boolean errorIgnored;
private long tokenIndex;
private int lastTokenType;
+ /** Last default-channel token whose type is not {@code NL}. */
+ private int lastRealTokenType;
/**
* When {@code false}, the {@code val} keyword is treated as a regular
@@ -68,6 +70,9 @@ options {
int tokenType = token.getType();
if (Token.DEFAULT_CHANNEL == token.getChannel()) {
+ if (NL != tokenType) {
+ this.lastRealTokenType = tokenType;
+ }
this.lastTokenType = tokenType;
}
@@ -109,6 +114,16 @@ options {
return !REGEX_CHECK_SET.get(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.
+ */
+ private boolean isUnclosedSlashy() {
+ int decision = this.lastTokenType == NL ? this.lastRealTokenType : this.lastTokenType;
+ return !REGEX_CHECK_SET.get(decision);
+ }
+
/**
* just a hook, which will be overrided by GroovyLangLexer
*/
@@ -289,13 +304,24 @@ options {
// ยง3.10.5 String Literals
+// A quote that never closes is UNEXPECTED_CHAR: nothing else matches it.
+// `/` is also DIV, so an unclosed `/.../` 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..0dc3bfb16e 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,13 @@ 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`.
+ // 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.
if ("<".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..9f1ab5062a 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,78 @@ 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 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("""