Skip to content

Commit ed16b7c

Browse files
committed
prepare for case insensitive scanning
1 parent 0fac126 commit ed16b7c

File tree

8 files changed

+153
-4
lines changed

8 files changed

+153
-4
lines changed

tool/src/org/antlr/v4/Tool.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public Option(String fieldName, String name, OptionArgType argType, String descr
137137
public Map<String, String> grammarOptions = null;
138138
public boolean warnings_are_errors = false;
139139
public boolean longMessages = false;
140+
public boolean ignoreCase = false;
140141

141142
public static Option[] optionDefs = {
142143
new Option("outputDirectory", "-o", OptionArgType.STRING, "specify output directory where all output is generated"),
@@ -157,6 +158,8 @@ public Option(String fieldName, String name, OptionArgType argType, String descr
157158
new Option("ST_inspector_wait_for_close", "-XdbgSTWait", "wait for STViz to close before continuing"),
158159
new Option("force_atn", "-Xforce-atn", "use the ATN simulator for all predictions"),
159160
new Option("log", "-Xlog", "dump lots of logging info to antlr-timestamp.log"),
161+
new Option("ignoreCase", "-ignorecase", "the generated lexer will be case insensitive"),
162+
new Option("", "-help", "show tool help"),
160163
};
161164

162165
// helper vars for option management
@@ -226,6 +229,10 @@ protected void handleArgs() {
226229
handleOptionSetArg(arg);
227230
continue;
228231
}
232+
if ( arg.startsWith("-help") ) {
233+
help();
234+
continue;
235+
}
229236
if ( arg.charAt(0)!='-' ) { // file name
230237
if ( !grammarFiles.contains(arg) ) grammarFiles.add(arg);
231238
continue;
@@ -404,6 +411,12 @@ public void processNonCombinedGrammar(Grammar g, boolean gencode) {
404411
ATNFactory factory;
405412
if ( g.isLexer() ) factory = new LexerATNFactory((LexerGrammar)g);
406413
else factory = new ParserATNFactory(g);
414+
415+
if (ignoreCase)
416+
if (g.isLexer()) {//TODO: factory.setLexerIgnoreCase(true);
417+
}
418+
else errMgr.toolError(ErrorType.LEXER_OPTION_IGNORECASE);
419+
407420
g.atn = factory.createATN();
408421

409422
if ( generate_ATN_dot ) generateATNs(g);
@@ -786,7 +799,7 @@ protected void writeDOTFile(Grammar g, String name, String dot) throws IOExcepti
786799
}
787800

788801
public void help() {
789-
info("ANTLR Parser Generator (HS Styled) Version " + Tool.VERSION);
802+
version();
790803
for (Option o : optionDefs) {
791804
String name = o.name + (o.argType!=OptionArgType.NONE? " ___" : "");
792805
String s = String.format(" %-19s %s", name, o.description);

tool/src/org/antlr/v4/automata/ATNFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public String toString() {
6464
void setCurrentRuleName(String name);
6565

6666
void setCurrentOuterAlt(int alt);
67+
68+
//void setLexerIgnoreCase(boolean ignoreCase);
6769

6870
Handle rule(GrammarAST ruleAST, String name, Handle blk);
6971

tool/src/org/antlr/v4/automata/LexerATNFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161

6262
public class LexerATNFactory extends ParserATNFactory {
6363
public STGroup codegenTemplates;
64+
/*
65+
public boolean lexerIgnoreCase=false;
66+
67+
@Override
68+
public void setLexerIgnoreCase(boolean ignoreCase) {
69+
lexerIgnoreCase=ignoreCase;
70+
}
71+
*/
6472
public LexerATNFactory(LexerGrammar g) {
6573
super(g);
6674
// use codegen to get correct language templates for lexer commands
@@ -243,6 +251,12 @@ else if ( t.getType()==ANTLRParser.TOKEN_REF ) {
243251
@Override
244252
public Handle stringLiteral(TerminalAST stringLiteralAST) {
245253
String chars = stringLiteralAST.getText();
254+
// TODO:ignorecase
255+
// To implement case insensitive scanning, capture the 'STRING'
256+
// and convert to [Ss][Tt][Rr][Ii][Nn][Gg]
257+
// and use the charSetLiteral to construct the Handle
258+
// System.out.println("LexerATNFactor.java stringLiteral>"+chars+"<");
259+
246260
chars = CharSupport.getStringFromGrammarStringLiteral(chars);
247261
int n = chars.length();
248262
ATNState left = newState(stringLiteralAST);
@@ -260,6 +274,8 @@ public Handle stringLiteral(TerminalAST stringLiteralAST) {
260274
/** [Aa\t \u1234a-z\]\-] char sets */
261275
@Override
262276
public Handle charSetLiteral(GrammarAST charSetAST) {
277+
// TODO:ignorecase
278+
// System.out.println("LexerATNFactor.java charSetLiteral>"+charSetAST.getText()+"<");
263279
ATNState left = newState(charSetAST);
264280
ATNState right = newState(charSetAST);
265281
IntervalSet set = getSetFromCharSetLiteral(charSetAST);

tool/src/org/antlr/v4/automata/ParserATNFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ public void setCurrentOuterAlt(int alt) {
193193
currentOuterAlt = alt;
194194
}
195195

196+
/*
197+
@Override
198+
public void setLexerIgnoreCase(boolean ignoreCase) {
199+
}
200+
*/
201+
196202
/* start->ruleblock->end */
197203
@Override
198204
public Handle rule(GrammarAST ruleAST, String name, Handle blk) {

tool/src/org/antlr/v4/tool/ErrorType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public enum ErrorType {
246246
* </pre>
247247
*/
248248
EPSILON_OPTIONAL(154, "rule '<arg>' contains an optional block with at least one alternative that can match an empty string", ErrorSeverity.WARNING),
249+
LEXER_OPTION_IGNORECASE(155, "option -ignorecase is only valid for Lexer grammars", ErrorSeverity.ERROR),
249250

250251
// Backward incompatibility errors
251252
V3_TREE_GRAMMAR(200, "tree grammars are not supported in ANTLR 4", ErrorSeverity.ERROR),

tool/test/org/antlr/v4/test/AllTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
TestPerformance.class, TestScopeParsing.class,
2121
TestSemPredEvalLexer.class, TestSemPredEvalParser.class,
2222
TestSets.class, TestSymbolIssues.class, TestTokenStreamRewriter.class,
23-
TestTokenTypeAssignment.class, TestToolSyntaxErrors.class,
24-
TestTopologicalSort.class, TestUnbufferedCharStream.class,
25-
TestUnbufferedTokenStream.class })
23+
TestTokenTypeAssignment.class, TestToolOptions.class,
24+
TestToolSyntaxErrors.class, TestTopologicalSort.class,
25+
TestUnbufferedCharStream.class, TestUnbufferedTokenStream.class })
2626
public class AllTests {
2727

2828
}

tool/test/org/antlr/v4/test/BaseTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,29 @@ public void testErrors(String[] pairs, boolean printTree) {
697697
}
698698
}
699699

700+
701+
public void testErrors(String[] pairs, String... extraOptions ) {
702+
for (int i = 0; i < pairs.length; i+=2) {
703+
String input = pairs[i];
704+
String expect = pairs[i+1];
705+
706+
String[] lines = input.split("\n");
707+
String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
708+
ErrorQueue equeue = antlr(fileName, fileName, input, false,extraOptions);
709+
710+
String actual = equeue.toString(true);
711+
actual = actual.replace(tmpdir + File.separator, "");
712+
System.err.println(actual);
713+
String msg = input;
714+
msg = msg.replace("\n","\\n");
715+
msg = msg.replace("\r","\\r");
716+
msg = msg.replace("\t","\\t");
717+
718+
assertEquals("error in: "+msg,expect,actual);
719+
}
720+
}
721+
722+
700723
public String getFilenameFromFirstLineOfGrammar(String line) {
701724
String fileName = "A" + Tool.GRAMMAR_EXTENSION;
702725
int grIndex = line.lastIndexOf("grammar");
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* [The "BSD license"]
3+
* Copyright (c) 2012 Terence Parr
4+
* Copyright (c) 2012 Sam Harwell
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
* 3. The name of the author may not be used to endorse or promote products
17+
* derived from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package org.antlr.v4.test;
32+
33+
import org.antlr.v4.tool.ErrorType;
34+
import org.junit.Test;
35+
36+
import static org.junit.Assert.*;
37+
38+
public class TestToolOptions extends BaseTest {
39+
@Test public void testOptionIgnoreCaseParser() {
40+
String[] pair = new String[] {
41+
"grammar A;\n" +
42+
"a : 'A' ;\n",
43+
"error(" + ErrorType.LEXER_OPTION_IGNORECASE.code + "): option -ignorecase is only valid for Lexer grammars\n",
44+
};
45+
super.testErrors(pair, "-ignorecase");
46+
}
47+
@Test public void testOptionIgnoreCaseLexer() {
48+
String[] pair = new String[] {
49+
"lexer grammar A;\n" +
50+
"A : 'A' ;\n",
51+
"", /* no errors */
52+
};
53+
super.testErrors(pair, "-ignorecase");
54+
}
55+
56+
/*
57+
* testOptionHelp disabled for now. The testError expect output to be writtent o System.err
58+
*/
59+
public void testOptionHelp() {
60+
String[] pair = new String[] {
61+
"lexer grammar A;\n" +
62+
"A : 'A' ;\n",
63+
"ANTLR Parser Generator Version 4.x\n"+
64+
" -o ___ specify output directory where all output is generated\n"+
65+
" -lib ___ specify location of grammars, tokens files\n"+
66+
" -atn generate rule augmented transition network diagrams\n"+
67+
" -encoding ___ specify grammar file encoding; e.g., euc-jp\n"+
68+
" -message-format ___ specify output style for messages in antlr, gnu, vs2005\n"+
69+
" -long-messages show exception details when available for errors and warnings\n"+
70+
" -listener generate parse tree listener (default)\n"+
71+
" -no-listener don't generate parse tree listener\n"+
72+
" -visitor generate parse tree visitor\n"+
73+
" -no-visitor don't generate parse tree visitor (default)\n"+
74+
" -package ___ specify a package/namespace for the generated code\n"+
75+
" -depend generate file dependencies\n"+
76+
" -D<option>=value set/override a grammar-level option\n"+
77+
" -Werror treat warnings as errors\n"+
78+
" -XdbgST launch StringTemplate visualizer on generated code\n"+
79+
" -XdbgSTWait wait for STViz to close before continuing\n"+
80+
" -Xforce-atn use the ATN simulator for all predictions\n"+
81+
" -Xlog dump lots of logging info to antlr-timestamp.log\n"+
82+
" -ignorecase the generated lexer will be case insensitive\n"+
83+
" -help show tool help\n",
84+
};
85+
super.testErrors(pair, "-help");
86+
}
87+
88+
}

0 commit comments

Comments
 (0)