Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #163 - tokens with backslashes #164

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions tool/src/main/java/org/antlr/codegen/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ protected ST genTokenVocabOutput() {
// now dump the strings
for (String literal : grammar.getStringLiterals()) {
int tokenType = grammar.getTokenType(literal);
literal = literal.replace("\\", "\\\\");
if ( tokenType>=Label.MIN_TOKEN_TYPE ) {
vocabFileST.addAggr("tokens.{name,type}", literal, Utils.integer(tokenType));
}
Expand Down
83 changes: 83 additions & 0 deletions tool/src/test/java/org/antlr/test/TestJavaCodeGeneration.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,87 @@ public void testSemanticPredicateAnalysisStackOverflow() throws Exception {
boolean success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
assertTrue(success);
}

/**
* Regression test for antlr/antlr3#163 - NullPointerException when literal
* of a token is not escaped
*/
@Test
public void testTokenVocabWithUnescapedBackslashThrowsNullPointerException(){
mkdir(tmpdir);
writeFile(tmpdir, "T2.tokens", "Backslash=4\n'\\\\'=4\n");
String grammar =
"grammar T;\n"
+ "\n"
+ "options{\n"
+ " tokenVocab=T2;\n"
+ "}\n"
+ "tokens{\n"
+ " Backslash = '\\\\';\n"
+ "}\n"
+ "main : '\\\\' EOF;";
boolean success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
assertFalse(success);
}

/**
* Regression test for antlr/antlr3#163 - NullPointerException when literal
* of a token is not escaped
*/
@Test
public void testTokenVocabWithEscapedBackslash(){
mkdir(tmpdir);
writeFile(tmpdir, "T2.tokens", "Backslash=4\n'\\\\\\\\'=4\n");
String grammar =
"grammar T;\n"
+ "\n"
+ "options{\n"
+ " tokenVocab=T2;\n"
+ "}\n"
+ "tokens{\n"
+ " Backslash = '\\\\';\n"
+ "}\n"
+ "main : '\\\\' 'another token, reason see below' EOF;";
/* needed to insert another token since pull request #157 is not yet merged
* No lexer would be generated without the additional token since a tokenVocab
* was defined which covers all tokens used in the grammar
* See https://github.com/antlr/antlr3/pull/157 for more information
*/
boolean success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
assertTrue(success);
}

/**
* Regression test for antlr/antlr3#163 - NullPointerException when literal
* of a token is not escaped
*/
@Test
public void testTokenVocabWithBackslashReusedInOtherGrammar(){
String grammar =
"grammar T2;\n"
+ "tokens{\n"
+ " Backslash = '\\\\';\n"
+ "}\n"
+ "main : '\\\\' EOF;";

boolean success = rawGenerateAndBuildRecognizer("T2.g", grammar, "T2Parser", "T2Lexer", false);
assertTrue(success);
grammar =
"grammar T;\n"
+ "\n"
+ "options{\n"
+ " tokenVocab=T2;\n"
+ "}\n"
+ "tokens{\n"
+ " Backslash = '\\\\';\n"
+ "}\n"
+ "main : '\\\\' 'another token, reason see below' EOF;";
/* needed to insert another token since pull request #157 is not yet merged
* No lexer would be generated without the additional token since a tokenVocab
* was defined which covers all tokens used in the grammar
* See https://github.com/antlr/antlr3/pull/157 for more information
*/
success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
assertTrue(success);
}
}