Skip to content

Commit

Permalink
Throw EOF_CLOSURE error, fixes #3359
Browse files Browse the repository at this point in the history
Remove EOFInClosure test since now it's not actual on runtime
  • Loading branch information
KvanTTT committed Nov 20, 2021
1 parent 957d1b2 commit dedcb3a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,28 +262,6 @@ public static class Wildcard extends BaseParserTestDescriptor {
public String grammar;
}

/**
* This test ensures that {@link org.antlr.v4.runtime.atn.ParserATNSimulator} does not produce a
* {@link StackOverflowError} when it encounters an {@code EOF} transition
* inside a closure.
*/
public static class EOFInClosure extends BaseParserTestDescriptor {
public String input = "x";
public String output = null;
public String errors = null;
public String startRule = "prog";
public String grammarName = "T";

/**
grammar T;
prog : stat EOF;
stat : 'x' ('y' | EOF)*?;
*/
@CommentHasStringValue
public String grammar;

}

public static class IfIfElseGreedyBinding1 extends BaseParserTestDescriptor {
public String input = "if y if y x else x";
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ public void AllErrorCodesDistinct() {
super.testErrors(pair, true);
}

// Test for https://github.com/antlr/antlr4/issues/3359
@Test public void testEofClosure() {
String grammar =
"lexer grammar EofClosure;\n" +
"EofClosure: 'x' EOF*;";

String expected =
"error(" + ErrorType.EOF_CLOSURE.code + "): EofClosure.g4:2:0: rule EofClosure contains a closure with at least one alternative that can match EOF\n";

String[] pair = new String[] {
grammar,
expected
};

super.testErrors(pair, true);
}

// Test for https://github.com/antlr/antlr4/issues/1203
@Test public void testEpsilonOptionalAndClosureAnalysis() {
String grammar =
Expand Down
3 changes: 3 additions & 0 deletions tool/src/org/antlr/v4/automata/ParserATNFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ protected void checkEpsilonClosure() {
ErrorType errorType = pair.a instanceof LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
g.tool.errMgr.grammarError(errorType, g.fileName, ((GrammarAST)pair.a.ast.getChild(0)).getToken(), pair.a.name);
}
if ( lookahead.contains(org.antlr.v4.runtime.Token.EOF)) {
g.tool.errMgr.grammarError(ErrorType.EOF_CLOSURE, g.fileName, ((GrammarAST)pair.a.ast.getChild(0)).getToken(), pair.a.name);
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions tool/src/org/antlr/v4/tool/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,30 @@ public enum ErrorType {
"One of the token <arg> values unreachable. <arg2> is always overlapped by token <arg3>",
ErrorSeverity.WARNING),

/**
* Compiler Error 185.
*
* <p>
* rule <em>rule</em> contains a closure with at least one alternative
* that can match EOF</p>
*
* <p>A rule contains a closure ({@code (...)*}) or positive closure
* ({@code (...)+}) around EOF.</p>
*
* <p>The following rule produces this error.</p>
*
* <pre>
* x : EOF*; // error
* y : EOF+; // error
* z : EOF; // ok
* </pre>
*/
EOF_CLOSURE(
185,
"rule <arg> contains a closure with at least one alternative that can match EOF",
ErrorSeverity.ERROR
),

/*
* Backward incompatibility errors
*/
Expand Down

0 comments on commit dedcb3a

Please sign in to comment.