Skip to content

Commit

Permalink
Escape reserved words during grammar generation, fixes #1070 (for -> …
Browse files Browse the repository at this point in the history
…for_ but RULE_for)

Deprecate USE_OF_BAD_WORD
  • Loading branch information
KvanTTT committed Jan 2, 2022
1 parent b342b67 commit 25dcf9e
Show file tree
Hide file tree
Showing 36 changed files with 560 additions and 667 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[notes]
https://github.com/antlr/antlr4/issues/1070

[type]
Lexer

[grammar]
lexer grammar L;

channels { break }

A: 'a' -> mode(for);

mode for;
B: 'b' -> channel(break);

[input]
ab

[output]
[@0,0:0='a',<1>,1:0]
[@1,1:1='b',<2>,channel=2,1:1]
[@2,2:1='<EOF>',<-1>,1:2]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[notes]
https://github.com/antlr/antlr4/issues/1070

[type]
Parser

[grammar]
grammar G;

root
: {0==0}? continue+ {<writeln("$text")>}
;

continue
: for for? {1==1}? #else
| break=BREAK BREAK+ (for | IF) #class
| if+=IF if+=IF* #int
| continue CONTINUE #static
;

for: FOR;
FOR: 'for_';
BREAK: 'break_';
IF: 'if_';
CONTINUE: 'continue_';

[start]
root

[input]
for_for_break_break_for_if_if_for_continue_

[output]
"""for_for_break_break_for_if_if_for_continue_
"""
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public boolean checkIgnored() {
@Override
protected void succeeded(Description description) {
// remove tmpdir if no error.
delegate.eraseTempDir();
//delegate.eraseTempDir();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,38 +238,11 @@ public String execModule(String fileName) {
}

private String locateTool(String tool) {
final String phpPath = System.getProperty("PHP_PATH");

if (phpPath != null && new File(phpPath).exists()) {
return phpPath;
}

String[] roots = {"/usr/local/bin/", "/opt/local/bin", "/usr/bin/"};

for (String root: roots) {
if (new File(root + tool).exists()) {
return root + tool;
}
}

throw new RuntimeException("Could not locate " + tool);
return "php";
}

protected String locatePhp() {
String propName = getPropertyPrefix() + "-php";
String prop = System.getProperty(propName);

if (prop == null || prop.length() == 0) {
prop = locateTool("php");
}

File file = new File(prop);

if (!file.exists()) {
throw new RuntimeException("Missing system property:" + propName);
}

return file.getAbsolutePath();
return "php";
}

protected String locateRuntime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,30 +193,11 @@ public String execModule(String fileName) {
}

private String locateTool(List<String> tools) {
String[] roots = {
"/opt/local/bin", "/usr/bin/", "/usr/local/bin/",
"/Users/"+System.getProperty("user.name")+"/anaconda3/bin/",
"/Users/"+System.getProperty("user.name")+"/opt/anaconda3/bin/"
};
for(String root : roots) {
for (String tool : tools) {
if ( new File(root+tool).exists() ) {
return root+tool;
}
}
}
throw new RuntimeException("Could not locate " + tools);
return "python";
}

protected String locatePython() {
String propName = getPropertyPrefix() + "-python";
String prop = System.getProperty(propName);
if(prop==null || prop.length()==0)
prop = locateTool(getPythonExecutables());
File file = new File(prop);
if(!file.exists())
throw new RuntimeException("Missing system property:" + propName);
return file.getAbsolutePath();
return "python";
}

protected abstract List<String> getPythonExecutables();
Expand Down
Loading

0 comments on commit 25dcf9e

Please sign in to comment.