Skip to content

Commit

Permalink
rule name 'state' conflict with Python runtime.
Browse files Browse the repository at this point in the history
Signed-off-by: cyqw <cyqw@163.com>
  • Loading branch information
cyqw authored and parrt committed Jul 28, 2024
1 parent 88a0c7a commit e8bf978
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[type]
Parser

[grammar]
grammar T;

program : state*{<writeln("$text")>} EOF ;
state: 'break;' | 'continue;' | 'return;' ;

[start]
program

[input]
break;continue;return;

[output]
"""break;continue;return;
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[type]
Parser

[grammar]
grammar T;

program : sempred*{<writeln("$text")>} EOF ;
sempred: 'break;' | 'continue;' | 'return;' ;

[start]
program

[input]
break;continue;return;

[output]
"""break;continue;return;
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[type]
Parser

[grammar]
grammar T;

program : action*{<writeln("$text")>} EOF ;
action: 'break;' | 'continue;' | 'return;' ;

[start]
program

[input]
break;continue;return;

[output]
"""break;continue;return;
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[type]
Parser

[grammar]
grammar T;

program : ruleIndexMap*{<writeln("$text")>} EOF ;
ruleIndexMap: 'break;' | 'continue;' | 'return;' ;

[start]
program

[input]
break;continue;return;

[output]
"""break;continue;return;
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[type]
Parser

[grammar]
grammar T;

program : addErrorListener*{<writeln("$text")>} EOF ;
addErrorListener: 'break;' | 'continue;' | 'return;' ;

[start]
program

[input]
break;continue;return;

[output]
"""break;continue;return;
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[type]
Parser

[grammar]
grammar T;

program : reset*{<writeln("$text")>} EOF ;
reset: 'break;' | 'continue;' | 'return;' ;

[start]
program

[input]
break;continue;return;

[output]
"""break;continue;return;
"""

Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina
/**
* @throws RecognitionException
*/
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><endif>public function <currentRule.name>(<args; separator=",">): Context\\<currentRule.ctxType>
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><endif>public function <currentRule.escapedName>(<args; separator=",">): Context\\<currentRule.ctxType>
{
$localContext = new Context\\<currentRule.ctxType>($this->ctx, $this->getState()<currentRule.args:{a | , $<a.name>}>);

Expand Down Expand Up @@ -701,7 +701,7 @@ cases(tokens) ::= <<

InvokeRule(r, argExprsChunks) ::= <<
$this->setState(<r.stateNumber>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>$this-><if(r.ast.options.p)>recursive<r.name; format="cap"><else><r.name><endif>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif>$this-><if(r.ast.options.p)>recursive<r.name; format="cap"><else><r.escapedName><endif>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
>>

MatchToken(m) ::= <<
Expand Down
2 changes: 1 addition & 1 deletion tool/src/org/antlr/v4/codegen/target/CppTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class CppTarget extends Target {
"using", "virtual", "void", "volatile", "wchar_t", "while",
"xor", "xor_eq",

"rule", "parserRule"
"rule", "parserRule", "reset"
));

public CppTarget(CodeGenerator gen) {
Expand Down
10 changes: 9 additions & 1 deletion tool/src/org/antlr/v4/codegen/target/DartTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ public class DartTarget extends Target {
"deferred", "hide", "return", "with",
"do", "if", "set", "yield",

"rule", "parserRule"
"rule", "parserRule",
"interpreter", "state", "ruleNames", "vocabulary", "ruleIndexMap", "getTokenType", "grammarFileName", "getATN",
"parseInfo", "getErrorHeader", "addErrorListener", "removeErrorListener", "removeErrorListeners","errorListeners",
"errorListenerDispatch", "sempred", "precpred", "action", "inputStream", "tokenFactory",

"errorHandler", "context", "buildParseTree", "matchedEOF", "reset", "match", "matchWildcard", "trimParseTree",
"addParseListener", "removeParseListener", "removeParseListeners", "triggerEnterRuleEvent", "triggerExitRuleEvent",
"numberOfSyntaxErrors", "currentToken", "notifyErrorListeners", "consume", "createTerminalNode", "createErrorNode",
"addContextToParseTree", "enterRule", "exitRule", "enterOuterAlt", "precedence", "enterRecursionRule"
));

public DartTarget(CodeGenerator gen) {
Expand Down
9 changes: 5 additions & 4 deletions tool/src/org/antlr/v4/codegen/target/GoTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.tool.Grammar;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class GoTarget extends Target {
protected static final HashSet<String> reservedWords = new HashSet<>(Arrays.asList(
Expand Down Expand Up @@ -50,7 +50,8 @@ public class GoTarget extends Target {
// then clashes with the GetStart() or GetStop() method that is generated by the code gen for the rule. So, we need to
// convert it. This is not ideal as it will still probably confuse authors of parse listeners etc. but the code will
// compile. This is a proof of Hyrum's law.
"start", "stop", "exception"
"start", "stop", "exception",
"sempred", "precpred", "addErrorListener"
));

private static final boolean DO_GOFMT = !Boolean.parseBoolean(System.getenv("ANTLR_GO_DISABLE_GOFMT"))
Expand Down
3 changes: 2 additions & 1 deletion tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class JavaScriptTarget extends Target {
"null", "true", "false",

// misc
"rule", "parserRule"
"rule", "parserRule",
"state", "reset"
));

public JavaScriptTarget(CodeGenerator gen) {
Expand Down
7 changes: 4 additions & 3 deletions tool/src/org/antlr/v4/codegen/target/JavaTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class JavaTarget extends Target {
/**
Expand All @@ -30,7 +31,7 @@ public class JavaTarget extends Target {
"void", "volatile", "while",

// misc
"rule", "parserRule"
"rule", "parserRule", "reset"
));

public JavaTarget(CodeGenerator gen) {
Expand Down
3 changes: 2 additions & 1 deletion tool/src/org/antlr/v4/codegen/target/PHPTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class PHPTarget extends Target {
"__LINE__", "__METHOD__", "__NAMESPACE__", "__TRAIT__",

// misc
"rule", "parserRule"
"rule", "parserRule",
"state", "reset", "action", "sempred", "addErrorListener"
));

protected static final Map<Character, String> targetCharValueEscape;
Expand Down
3 changes: 2 additions & 1 deletion tool/src/org/antlr/v4/codegen/target/Python3Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class Python3Target extends Target {
"True", "False", "None",

// misc
"rule", "parserRule"
"rule", "parserRule",
"state", "reset"
));

protected static final Map<Character, String> targetCharValueEscape;
Expand Down
6 changes: 3 additions & 3 deletions tool/src/org/antlr/v4/codegen/target/TypeScriptTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.Target;
import org.antlr.v4.misc.CharSupport;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -75,7 +74,8 @@ public class TypeScriptTarget extends Target {
"while",
"with",
"yield",
"of"
"of",
"state", "reset"
));

public TypeScriptTarget(CodeGenerator gen) {
Expand All @@ -86,7 +86,7 @@ public TypeScriptTarget(CodeGenerator gen) {
protected Set<String> getReservedWords() {
return reservedWords;
}

@Override
public int getInlineTestSetWordSize() {
return 32;
Expand Down

0 comments on commit e8bf978

Please sign in to comment.