diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/LexerExec/ReservedWordsEscaping.txt b/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/LexerExec/ReservedWordsEscaping.txt new file mode 100644 index 00000000000..785f1644e32 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/LexerExec/ReservedWordsEscaping.txt @@ -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='',<-1>,1:2] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/ParserExec/ReservedWordsEscaping.txt b/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/ParserExec/ReservedWordsEscaping.txt new file mode 100644 index 00000000000..0e6adc03fd9 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/ParserExec/ReservedWordsEscaping.txt @@ -0,0 +1,35 @@ +[notes] +https://github.com/antlr/antlr4/issues/1070 + +[type] +Parser + +[grammar] +grammar G; + +root + : {0==0}? continue+ {} + ; + +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_ +""" \ No newline at end of file diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java index 945bfab87b2..b4efaf5e500 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java @@ -167,7 +167,7 @@ public boolean checkIgnored() { @Override protected void succeeded(Description description) { // remove tmpdir if no error. - delegate.eraseTempDir(); + //delegate.eraseTempDir(); } }; diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java index d906ff0cd0f..7778abd285a 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java @@ -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() { diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java index 126e2e0552c..e5ccfd6b63e 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java @@ -193,30 +193,11 @@ public String execModule(String fileName) { } private String locateTool(List 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 getPythonExecutables(); diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg index 7eb26d8db2f..28ca46c2de9 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg @@ -65,7 +65,7 @@ using IToken = Antlr4.Runtime.IToken; /// \ /// This interface defines a complete listener for a parse tree produced by -/// \. +/// \. /// \ [System.CodeDom.Compiler.GeneratedCode("ANTLR", "")] [System.CLSCompliant(false)] @@ -80,7 +80,7 @@ public interface IListener : IParseTreeListener { /// \ /// \The parse tree.\ -void Enter([NotNull] .Context context); +void Enter([NotNull] .Context context); /// \ /// Exit a parse tree produced by the \\ @@ -90,7 +90,7 @@ void Enter([NotNull] . /// \ /// \The parse tree.\ -void Exit([NotNull] .Context context);}; separator="\n"> +void Exit([NotNull] .Context context);}; separator="\n"> } } // namespace @@ -130,7 +130,7 @@ public partial class BaseListener : IListene /// \The default implementation does nothing.\ /// \ /// \The parse tree.\ -public virtual void Enter([NotNull] .Context context) { \} +public virtual void Enter([NotNull] .Context context) { \} /// \ /// Exit a parse tree produced by the \\ @@ -141,7 +141,7 @@ public virtual void Enter([NotNull] The default implementation does nothing.\ /// \ /// \The parse tree.\ -public virtual void Exit([NotNull] .Context context) { \}}; separator="\n"> +public virtual void Exit([NotNull] .Context context) { \}}; separator="\n"> /// \ /// \The default implementation does nothing.\ @@ -173,7 +173,7 @@ using IToken = Antlr4.Runtime.IToken; /// \ /// This interface defines a complete generic visitor for a parse tree produced -/// by \. +/// by \. /// \ /// \The return type of the visit operation.\ [System.CodeDom.Compiler.GeneratedCode("ANTLR", "")] @@ -190,7 +190,7 @@ public interface IVisitor\ : IParseTreeVisitor\ /// \The parse tree.\ /// \The visitor result.\ -Result Visit([NotNull] .Context context);}; separator="\n"> +Result Visit([NotNull] .Context context);}; separator="\n"> } } // namespace @@ -233,7 +233,7 @@ public partial class BaseVisitor\ : AbstractParseTreeV /// \ /// \The parse tree.\ /// \The visitor result.\ -public virtual Result Visit([NotNull] .Context context) { return VisitChildren(context); \}}; separator="\n"> +public virtual Result Visit([NotNull] .Context context) { return VisitChildren(context); \}}; separator="\n"> } } // namespace @@ -271,7 +271,7 @@ Parser(parser, funcs, atn, sempredFuncs, superClass) ::= << Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= << [System.CodeDom.Compiler.GeneratedCode("ANTLR", "")] [System.CLSCompliant(false)] -public partial class : { +public partial class : { protected static DFA[] decisionToDFA; protected static PredictionContextCache sharedContextCache = new PredictionContextCache(); @@ -294,7 +294,7 @@ public partial class : public override string SerializedAtn { get { return new string(_serializedATN); } } - static () { + static () { decisionToDFA = new DFA[_ATN.NumberOfDecisions]; for (int i = 0; i \< _ATN.NumberOfDecisions; i++) { decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i); @@ -309,7 +309,7 @@ public partial class : public override bool Sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { : return _sempred(()_localctx, predIndex);}; separator="\n"> +case : return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } @@ -353,7 +353,7 @@ case : _action(()_loca public override bool Sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { : return _sempred(()_localctx, predIndex);}; separator="\n"> +case : return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } @@ -362,9 +362,9 @@ case : return _sempred(()> parser_ctor(parser) ::= << - public (ITokenStream input) : this(input, Console.Out, Console.Error) { } + public (ITokenStream input) : this(input, Console.Out, Console.Error) { } - public (ITokenStream input, TextWriter output, TextWriter errorOutput) + public (ITokenStream input, TextWriter output, TextWriter errorOutput) : base(input, output, errorOutput) { Interpreter = new ParserATNSimulator(this, _ATN, decisionToDFA, sharedContextCache); @@ -387,7 +387,7 @@ case : break;}; separator="\n"> * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -private bool _sempred( _localctx, int predIndex) { +private bool _sempred( _localctx, int predIndex) { switch (predIndex) { : return ;}; separator="\n"> @@ -404,8 +404,8 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina }; separator="\n"> [RuleVersion()] - }>public () { - _localctx = new (Context, State}>); + }>public () { + _localctx = new (Context, State}>); EnterRule(_localctx, , RULE_); @@ -437,8 +437,8 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina LeftFactoredRuleFunction(currentRule,args,code,locals,namedActions,finallyAction,postamble) ::= << - }>private () { - _localctx = new (Context, State}>); + }>private () { + _localctx = new (Context, State}>); EnterLeftFactoredRule(_localctx, , RULE_); @@ -469,8 +469,8 @@ LeftFactoredRuleFunction(currentRule,args,code,locals,namedActions,finallyAction LeftUnfactoredRuleFunction(currentRule,args,code,locals,namedActions,finallyAction,postamble) ::= << - }>private () { - _localctx = new (Context, State}>); + }>private () { + _localctx = new (Context, State}>); EnterRule(_localctx, , RULE_); @@ -503,14 +503,14 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, }; separator="\n"> [RuleVersion()] - }>public () { - return (0}>); + }>public () { + return (0}>); } -private (int _p}>) { +private (int _p}>) { ParserRuleContext _parentctx = Context; int _parentState = State; - _localctx = new (Context, _parentState}>); + _localctx = new (Context, _parentState}>); _prevctx = _localctx; int _startState = ; EnterRecursionRule(_localctx, , RULE_, _p); @@ -709,7 +709,7 @@ cases(ttypes) ::= << InvokeRule(r, argExprsChunks) ::= << State = ; - = }>(,); + = }>(,); >> MatchToken(m) ::= << @@ -769,15 +769,15 @@ LexerPushModeCommand(arg, grammar) ::= "PushMode();" ActionText(t) ::= "" ActionTemplate(t) ::= "" -ArgRef(a) ::= "_localctx." -LocalRef(a) ::= "_localctx." -RetValueRef(a) ::= "_localctx." -QRetValueRef(a) ::= ".." +ArgRef(a) ::= "_localctx." +LocalRef(a) ::= "_localctx." +RetValueRef(a) ::= "_localctx." +QRetValueRef(a) ::= ".." /** How to translate $tokenLabel */ -TokenRef(t) ::= "." -LabelRef(t) ::= "." -ListLabelRef(t) ::= "." -SetAttr(s,rhsChunks) ::= ". = ;" +TokenRef(t) ::= "." +LabelRef(t) ::= "." +ListLabelRef(t) ::= "." +SetAttr(s,rhsChunks) ::= ". = ;" TokenLabelType() ::= "" InputSymbolType() ::= "" @@ -802,44 +802,44 @@ ThisRulePropertyRef_text(r) ::= "TokenStream.GetText(_localctx.Start, TokenStre ThisRulePropertyRef_ctx(r) ::= "_localctx" ThisRulePropertyRef_parser(r) ::= "this" -NonLocalAttrRef(s) ::= <%((Context)GetInvokingContext()).%> +NonLocalAttrRef(s) ::= <%((Context)GetInvokingContext()).%> SetNonLocalAttr(s, rhsChunks) ::= - <%((Context)GetInvokingContext()). = ;%> + <%((Context)GetInvokingContext()). = ;%> AddToLabelList(a) ::= "..Add();" -TokenDecl(t) ::= " " -TokenTypeDecl(t) ::= "int ;" -TokenListDecl(t) ::= "IList\ = new List\()" -RuleContextDecl(r) ::= " " -RuleContextListDecl(rdecl) ::= "IList\<> = new List\<>()" +TokenDecl(t) ::= " " +TokenTypeDecl(t) ::= "int ;" +TokenListDecl(t) ::= "IList\ = new List\()" +RuleContextDecl(r) ::= " " +RuleContextListDecl(rdecl) ::= "IList\<> = new List\<>()" contextGetterCollection(elementType) ::= <% [] %> ContextTokenGetterDecl(t) ::= - "[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode () { return GetToken(., 0); }" + "[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode () { return GetToken(., 0); }" ContextTokenListGetterDecl(t) ::= << -[System.Diagnostics.DebuggerNonUserCode] public () { return GetTokens(.); } +[System.Diagnostics.DebuggerNonUserCode] public () { return GetTokens(.); } >> ContextTokenListIndexedGetterDecl(t) ::= << -[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode (int i) { - return GetToken(., i); +[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode (int i) { + return GetToken(., i); } >> ContextRuleGetterDecl(r) ::= << -[System.Diagnostics.DebuggerNonUserCode] public () { +[System.Diagnostics.DebuggerNonUserCode] public () { return GetRuleContext\<\>(0); } >> ContextRuleListGetterDecl(r) ::= << -[System.Diagnostics.DebuggerNonUserCode] public })> () { +[System.Diagnostics.DebuggerNonUserCode] public })> () { return GetRuleContexts\<\>(); } >> ContextRuleListIndexedGetterDecl(r) ::= << -[System.Diagnostics.DebuggerNonUserCode] public (int i) { +[System.Diagnostics.DebuggerNonUserCode] public (int i) { return GetRuleContext\<\>(i); } >> @@ -868,14 +868,14 @@ public partial class : < public (ParserRuleContext parent, int invokingState}>) : base(parent, invokingState) { - = ;}; separator="\n"> + = ;}; separator="\n"> } public override int RuleIndex { get { return RULE_; } } public () { } public virtual void CopyFrom( context) { base.CopyFrom(context); - = context.;}; separator="\n"> + = context.;}; separator="\n"> } @@ -909,10 +909,10 @@ public override TResult Accept\(IParseTreeVisitor\ visitor) { } >> -AttributeDecl(d) ::= " = " +AttributeDecl(d) ::= " = " /** If we don't know location of label def x, use this template */ -labelref(x) ::= "." +labelref(x) ::= "." /** For any action chunk, what is correctly-typed context struct ptr? */ ctx(actionChunk) ::= "" @@ -985,7 +985,7 @@ using DFA = Antlr4.Runtime.Dfa.DFA; Lexer(lexer, atn, actionFuncs, sempredFuncs, superClass) ::= << [System.CodeDom.Compiler.GeneratedCode("ANTLR", "")] [System.CLSCompliant(false)] -public partial class : { +public partial class : { protected static DFA[] decisionToDFA; protected static PredictionContextCache sharedContextCache = new PredictionContextCache(); @@ -994,14 +994,14 @@ public partial class : { public const int - =}; separator=", ", wrap, anchor>; + =}; separator=", ", wrap, anchor>; - + public const int - =}; separator=", ", wrap, anchor>; + =}; separator=", ", wrap, anchor>; public static string[] channelNames = { - "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> + "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> }; public static string[] modeNames = { @@ -1014,10 +1014,10 @@ public partial class : { - public (ICharStream input) + public (ICharStream input) : this(input, Console.Out, Console.Error) { } - public (ICharStream input, TextWriter output, TextWriter errorOutput) + public (ICharStream input, TextWriter output, TextWriter errorOutput) : base(input, output, errorOutput) { Interpreter = new LexerATNSimulator(this, _ATN, decisionToDFA, sharedContextCache); @@ -1035,7 +1035,7 @@ public partial class : { public override string SerializedAtn { get { return new string(_serializedATN); } } - static () { + static () { decisionToDFA = new DFA[_ATN.NumberOfDecisions]; for (int i = 0; i \< _ATN.NumberOfDecisions; i++) { decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i); @@ -1079,85 +1079,3 @@ tokenType ::= [ "EOF" : "Eof", default : key ] - -csIdentifier ::= [ - "abstract" : "@abstract", - "as" : "@as", - "base" : "@base", - "bool" : "@bool", - "break" : "@break", - "byte" : "@byte", - "case" : "@case", - "catch" : "@catch", - "char" : "@char", - "checked" : "@checked", - "class" : "@class", - "const" : "@const", - "continue" : "@continue", - "decimal" : "@decimal", - "default" : "@default", - "delegate" : "@delegate", - "do" : "@do", - "double" : "@double", - "else" : "@else", - "enum" : "@enum", - "event" : "@event", - "explicit" : "@explicit", - "extern" : "@extern", - "false" : "@false", - "finally" : "@finally", - "fixed" : "@fixed", - "float" : "@float", - "for" : "@for", - "foreach" : "@foreach", - "goto" : "@goto", - "if" : "@if", - "implicit" : "@implicit", - "in" : "@in", - "int" : "@int", - "interface" : "@interface", - "internal" : "@internal", - "is" : "@is", - "lock" : "@lock", - "long" : "@long", - "namespace" : "@namespace", - "new" : "@new", - "null" : "@null", - "object" : "@object", - "operator" : "@operator", - "out" : "@out", - "override" : "@override", - "params" : "@params", - "private" : "@private", - "protected" : "@protected", - "public" : "@public", - "readonly" : "@readonly", - "ref" : "@ref", - "return" : "@return", - "sbyte" : "@sbyte", - "sealed" : "@sealed", - "short" : "@short", - "sizeof" : "@sizeof", - "stackalloc" : "@stackalloc", - "static" : "@static", - "string" : "@string", - "struct" : "@struct", - "switch" : "@switch", - "this" : "@this", - "throw" : "@throw", - "true" : "@true", - "try" : "@try", - "typeof" : "@typeof", - "uint" : "@uint", - "ulong" : "@ulong", - "unchecked" : "@unchecked", - "unsafe" : "@unsafe", - "ushort" : "@ushort", - "using" : "@using", - "virtual" : "@virtual", - "values" : "@values", - "void" : "@void", - "volatile" : "@volatile", - "while" : "@while", - default : key -] diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg index c98705f3282..1f5f549fda4 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg @@ -57,9 +57,9 @@ public: }; - + enum { - = }; separator=", ", wrap, anchor> + = }; separator=", ", wrap, anchor> }; @@ -164,7 +164,7 @@ void ::action(RuleContext *context, size_t ruleIndex, size_t actionI bool ::sempred(RuleContext *context, size_t ruleIndex, size_t predicateIndex) { switch (ruleIndex) { - : return Sempred(antlrcpp::downCast\< *>(context), predicateIndex);}; separator="\n"> + : return Sempred(antlrcpp::downCast\< *>(context), predicateIndex);}; separator="\n"> default: break; @@ -190,7 +190,7 @@ std::vector\ ::_ruleNames = { }; std::vector\ ::_channelNames = { - "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator = ", ", wrap, anchor> + "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator = ", ", wrap, anchor> }; std::vector\ ::_modeNames = { @@ -231,13 +231,13 @@ void ::Action( *context, size_t actio >> RuleSempredFunctionHeader(r, actions) ::= << -bool Sempred( *_localctx, size_t predicateIndex); +bool Sempred( *_localctx, size_t predicateIndex); >> RuleSempredFunction(r, actions) ::= << -bool ::Sempred( *_localctx, size_t predicateIndex) { +bool ::Sempred( *_localctx, size_t predicateIndex) { switch (predicateIndex) { : return }; separator=";\n">; @@ -337,7 +337,7 @@ dfa::Vocabulary& ::getVocabulary() const { bool ::sempred(RuleContext *context, size_t ruleIndex, size_t predicateIndex) { switch (ruleIndex) { : return Sempred(antlrcpp::downCast\< *>(context), predicateIndex);}; separator="\n"> + case : return Sempred(antlrcpp::downCast\< *>(context), predicateIndex);}; separator="\n"> default: break; @@ -404,14 +404,14 @@ for (size_t i = 0; i \< count; i++) { }; separator="\n"> -* (); +* (); >> RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedActions, finallyAction, postamble, exceptions) ::= << }; separator = "\n"> -::* ::() { +::* ::() { *_localctx = _tracker.createInstance\<\>(_ctx, getState()}>); enterRule(_localctx, , ::Rule); @@ -450,19 +450,19 @@ RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedAction LeftRecursiveRuleFunctionHeader(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedActions, finallyAction, postamble) ::= << }; separator="\n"> -* (); -* (int precedence}>); +* (); +* (int precedence}>); >> LeftRecursiveRuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedActions, finallyAction, postamble) ::= << }; separator="\n"> -::* ::() { +::* ::() { return (0}>); } -::* ::(int precedence}>) { +::* ::(int precedence}>) { ParserRuleContext *parentContext = _ctx; size_t parentState = getState(); :: *_localctx = _tracker.createInstance\<\>(_ctx, parentState}>); diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg index 5b047ac1427..e989ae6bee0 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg @@ -234,7 +234,7 @@ class extends { switch (ruleIndex) { : - return __sempred(_localctx as ?, predIndex);}; separator="\n"> + return __sempred(_localctx as ?, predIndex);}; separator="\n"> } return true; } @@ -280,7 +280,7 @@ bool sempred(RuleContext? _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { : - return __sempred(_localctx, predIndex);}; separator="\n"> + return __sempred(_localctx, predIndex);}; separator="\n"> } return true; } @@ -307,7 +307,7 @@ void __action(? _localctx, int actionIndex) { /// This generates a private method since the predIndex is generated, making an /// overriding implementation impossible to maintain. RuleSempredFunction(r, actions) ::= << -bool __sempred(dynamic _localctx, int predIndex) { +bool __sempred(dynamic _localctx, int predIndex) { switch (predIndex) { : return ;}; separator="\n"> } @@ -317,7 +317,7 @@ bool __sempred(dynamic _localctx, int predIndex) { RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << - }> () { + }> () { dynamic _localctx = (context, state}>); enterRule(_localctx, , RULE_); @@ -345,7 +345,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, namedActions,finallyAction,postamble) ::= << - ([int _p = 0]}>) { + ([int _p = 0]}>) { final _parentctx = context; final _parentState = state; dynamic _localctx = (context, _parentState}>); @@ -810,14 +810,14 @@ class extends { static const int = }; separator=", ", wrap, anchor>; - + static const int - = }; separator=", ", wrap, anchor>; + = }; separator=", ", wrap, anchor>; @override final List\ channelNames = [ - 'DEFAULT_TOKEN_CHANNEL', 'HIDDEN', '}; separator=", ", wrap, anchor> + 'DEFAULT_TOKEN_CHANNEL', 'HIDDEN', '}; separator=", ", wrap, anchor> ]; @override diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg index c3c4d308c75..75564e5b24e 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg @@ -244,9 +244,9 @@ func (p *) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex switch ruleIndex { : - var t *Context = nil - if localctx != nil { t = localctx.(*Context) \} - return p._Sempred(t, predIndex)}; separator="\n\n"> + var t *Context = nil + if localctx != nil { t = localctx.(*Context) \} + return p._Sempred(t, predIndex)}; separator="\n\n"> @@ -269,11 +269,11 @@ func (l *) Action(localctx antlr.RuleContext, ruleIndex, actionIndex : - l._Action(localctx, actionIndex) + l._Action(localctx, actionIndex) - var t *Context = nil + var t *Context = nil if localctx != nil { t = localctx.(*) \} - l._Action(t, actionIndex) + l._Action(t, actionIndex) }; separator="\n\n"> @@ -297,11 +297,11 @@ func (l *) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex : - return l._Sempred(localctx, predIndex) + return l._Sempred(localctx, predIndex) - var t *Context = nil + var t *Context = nil if localctx != nil { t = localctx.(*) \} - return l._Sempred(t, predIndex) + return l._Sempred(t, predIndex) }; separator="\n\n"> @@ -343,7 +343,7 @@ func (l *) _Action(localctx ) _Sempred(localctx antlr.RuleContext, predIndex int) bool { +func (p *) _Sempred(localctx antlr.RuleContext, predIndex int) bool { this := p _ = this @@ -443,10 +443,10 @@ LeftRecursiveRuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs func (p *) ( }; separator=", ">) (localctx I) { - return p.(0}>) + return p.(0}>) } -func (p *) (_p int }>) (localctx I) { +func (p *) (_p int }>) (localctx I) { this := p _ = this @@ -754,7 +754,7 @@ InvokeRule(r, argExprsChunks) ::= << var _x = p.(, ) - var _x = p.() + var _x = p.() @@ -763,7 +763,7 @@ InvokeRule(r, argExprsChunks) ::= << p.(, ) - p.() + p.() } @@ -864,7 +864,7 @@ ActionTemplate(t) ::= "" ArgRef(a) ::= "." LocalRef(a) ::= "." RetValueRef(a) ::= "." -QRetValueRef(a) ::= ".Get().Get()" +QRetValueRef(a) ::= ".Get().Get()" /** How to translate $tokenLabel */ TokenRef(t) ::= ".Get()" @@ -929,7 +929,7 @@ ContextTokenListIndexedGetterDecl(t) ::= << >> ContextRuleGetterDecl(r) ::= << -() I { +() I { var t antlr.RuleContext; for _, ctx := range s.GetChildren() { if _, ok := ctx.(I); ok { @@ -947,7 +947,7 @@ ContextRuleGetterDecl(r) ::= << >> ContextRuleListGetterDecl(r) ::= << -All() []I { +All() []I { children := s.GetChildren() len := 0 for _, ctx := range children { @@ -970,7 +970,7 @@ All() []I { >> ContextRuleListIndexedGetterDecl(r) ::= << -(i int) I { +(i int) I { var t antlr.RuleContext; j := 0 for _, ctx := range s.GetChildren() { @@ -1015,74 +1015,74 @@ type I interface { GetParser() antlr.Parser - returns the token. -Get() }; separator="\n\n"> + returns the token. +Get() }; separator="\n\n"> - sets the token. -Set() }; separator="\n\n"> + sets the token. +Set() }; separator="\n\n"> - returns the token type. -Get() int }; separator="\n\n"> + returns the token type. +Get() int }; separator="\n\n"> - sets the token type. -Set(int) }; separator="\n\n"> + sets the token type. +Set(int) }; separator="\n\n"> - returns the token list. -Get() []}; separator="\n\n"> + returns the token list. +Get() []}; separator="\n\n"> - sets the token list. -Set([])}; separator="\n\n"> + sets the token list. +Set([])}; separator="\n\n"> - returns the rule contexts. -Get() I}; separator="\n\n"> + returns the rule contexts. +Get() I}; separator="\n\n"> - sets the rule contexts. -Set(I)}; separator="\n\n"> + sets the rule contexts. +Set(I)}; separator="\n\n"> - returns the rule context list. -Get() []I}; separator="\n\n"> + returns the rule context list. +Get() []I}; separator="\n\n"> - sets the rule context list. -Set([]I) }; separator="\n\n"> + sets the rule context list. +Set([]I) }; separator="\n\n"> - returns the attribute. -Get() }; separator="\n\n"> + returns the attribute. +Get() }; separator="\n\n"> - sets the attribute. -Set()}; separator="\n\n"> + sets the attribute. +Set()}; separator="\n\n"> @@ -1125,62 +1125,62 @@ func New(parser antlr.Parser, parent antlr.ParserRuleContext, invok func (s *) GetParser() antlr.Parser { return s.parser } -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> -) Get() int { return s. \}}; separator="\n\n"> +) Get() int { return s. \}}; separator="\n\n"> -) Set(v int) { s. = v \}}; separator="\n\n"> +) Set(v int) { s. = v \}}; separator="\n\n"> -) Get() [] { return s. \}}; separator="\n\n"> +) Get() [] { return s. \}}; separator="\n\n"> -) Set(v []) { s. = v \}}; separator="\n\n"> +) Set(v []) { s. = v \}}; separator="\n\n"> -) Get() I { return s. \}}; separator="\n\n"> +) Get() I { return s. \}}; separator="\n\n"> -) Set(v I) { s. = v \}}; separator="\n\n"> +) Set(v I) { s. = v \}}; separator="\n\n"> -) Get() []I { return s. \}}; separator="\n\n"> +) Get() []I { return s. \}}; separator="\n\n"> -) Set(v []I) { s. = v \}}; separator="\n\n"> +) Set(v []I) { s. = v \}}; separator="\n\n"> -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> @@ -1235,62 +1235,62 @@ func New(parser antlr.Parser, ctx antlr.ParserRuleContext) * -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> -) Get() int { return s. \}}; separator="\n\n"> +) Get() int { return s. \}}; separator="\n\n"> -) Set(v int) { s. = v \}}; separator="\n\n"> +) Set(v int) { s. = v \}}; separator="\n\n"> -) Get() [] { return s. \}}; separator="\n\n"> +) Get() [] { return s. \}}; separator="\n\n"> -) Set(v []) { s. = v \}}; separator="\n\n"> +) Set(v []) { s. = v \}}; separator="\n\n"> -) Get() I { return s. \}}; separator="\n\n"> +) Get() I { return s. \}}; separator="\n\n"> -) Set(v I) { s. = v \}}; separator="\n\n"> +) Set(v I) { s. = v \}}; separator="\n\n"> -) Get() []I { return s. \}}; separator="\n\n"> +) Get() []I { return s. \}}; separator="\n\n"> -) Set(v []I) { s. = v \}}; separator="\n\n"> +) Set(v []I) { s. = v \}}; separator="\n\n"> -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> func (s *) GetRuleContext() antlr.RuleContext { @@ -1411,11 +1411,11 @@ var serializedLexerAtn []uint16 var lexerChannelNames = []string{ - "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap>, + "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap>, } var lexerModeNames = []string{ - "}; separator=", ", wrap>, + "}; separator=", ", wrap>, } @@ -1502,17 +1502,17 @@ const ( const = - + // modes. const ( - = iota + 1}> - }; separator="\n"> + = iota + 1}> + }; separator="\n"> ) - + -// is the mode. -const = 1 +// is the mode. +const = 1 diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 2e8f9a41fbb..d1f3062934b 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -268,7 +268,7 @@ public class extends { switch (ruleIndex) { : - return _sempred(()_localctx, predIndex);}; separator="\n"> + return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } @@ -345,7 +345,7 @@ public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { : - return _sempred(()_localctx, predIndex);}; separator="\n"> + return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } @@ -378,7 +378,7 @@ case : * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -private boolean _sempred( _localctx, int predIndex) { +private boolean _sempred( _localctx, int predIndex) { switch (predIndex) { : @@ -393,7 +393,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina }; separator="\n"> - }>public final () throws RecognitionException { + }>public final () throws RecognitionException { _localctx = new (_ctx, getState()}>); enterRule(_localctx, , RULE_); @@ -430,11 +430,11 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, }; separator="\n"> - }>public final () throws RecognitionException { - return (0}>); + }>public final () throws RecognitionException { + return (0}>); } -private (int _p}>) throws RecognitionException { +private (int _p}>) throws RecognitionException { ParserRuleContext _parentctx = _ctx; int _parentState = getState(); _localctx = new (_ctx, _parentState}>); @@ -913,12 +913,12 @@ public class extends { public static final int =}; separator=", ", wrap, anchor>; - + public static final int - =}; separator=", ", wrap, anchor>; + =}; separator=", ", wrap, anchor>; public static String[] channelNames = { - "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> + "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> }; public static String[] modeNames = { diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg index 890c6036ec7..d0b6f034d8a 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg @@ -144,7 +144,7 @@ export default class extends { sempred(localctx, ruleIndex, predIndex) { switch(ruleIndex) { : - return this._sempred(localctx, predIndex);}; separator="\n"> + return this._sempred(localctx, predIndex);}; separator="\n"> default: throw "No predicate with index:" + ruleIndex; } @@ -198,7 +198,7 @@ case : .prototype.sempred = function(localctx, ruleIndex, predIndex) { switch (ruleIndex) { : - return this._sempred(localctx, predIndex);}; separator="\n"> + return this._sempred(localctx, predIndex);}; separator="\n"> default: throw "No registered predicate for:" + ruleIndex; } @@ -230,7 +230,7 @@ case : * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -.prototype._sempred = function_sempred(localctx, predIndex) { +.prototype._sempred = function_sempred(localctx, predIndex) { switch(predIndex) { : return ;}; separator="\n"> @@ -244,7 +244,7 @@ RuleSempredFunction(r, actions) ::= << RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << -(}; separator=", ">) { +(}; separator=", ">) { let localctx = new (this, this._ctx, this.state}>); this.enterRule(localctx, , .RULE_); @@ -276,7 +276,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, namedActions,finallyAction,postamble) ::= << -(_p, }>) { +(_p, }>) { if(_p===undefined) { _p = 0; } @@ -800,7 +800,7 @@ const decisionsToDFA = atn.decisionToState.map( (ds, index) => new antlr4.dfa.DF export default class extends antlr4.Lexer { static grammarFileName = ""; - static channelNames = [ "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> ]; + static channelNames = [ "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> ]; static modeNames = [ "}; separator=", ", wrap, anchor> ]; static literalNames = [ }; null="null", separator=", ", wrap, anchor> ]; static symbolicNames = [ }; null="null", separator=", ", wrap, anchor> ]; @@ -826,8 +826,8 @@ export default class extends antl . = ;}; separator="\n"> - -. = ;}; separator="\n"> + +. = ;}; separator="\n"> diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/PHP/PHP.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/PHP/PHP.stg index 19d7010f36f..3855b59551d 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/PHP/PHP.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/PHP/PHP.stg @@ -324,7 +324,7 @@ namespace { switch ($ruleIndex) { : - return $this->sempred($localContext, $predicateIndex);}; separator="\n\n"> + return $this->sempred($localContext, $predicateIndex);}; separator="\n\n"> default: return true; @@ -391,7 +391,7 @@ public function sempred(?RuleContext $localContext, int $ruleIndex, int $predica switch ($ruleIndex) { : - return $this->sempred($localContext, $predicateIndex);}; separator="\n\n"> + return $this->sempred($localContext, $predicateIndex);}; separator="\n\n"> } return true; @@ -433,7 +433,7 @@ private function action(? $localContext, int $a * an overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -private function sempred(?Context\\ $localContext, int $predicateIndex) : bool +private function sempred(?Context\\ $localContext, int $predicateIndex) : bool { switch ($predicateIndex) { }>public function () : Context\\ + }>public function () : Context\\ { $localContext = new Context\\($this->ctx, $this->getState()}>); @@ -478,7 +478,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,name /** * @throws RecognitionException */ - }>public function () : Context\\ + }>public function () : Context\\ { return $this->recursive(0}>); } @@ -703,7 +703,7 @@ cases(ttypes) ::= << InvokeRule(r, argExprsChunks) ::= << $this->setState(); - = }>$this->recursive(,); + = }>$this->recursive(,); >> MatchToken(m) ::= << @@ -1091,15 +1091,15 @@ namespace { public const = }; separator=", ", wrap, anchor>; - - public const =}; separator=", ", wrap, anchor>; + + public const =}; separator=", ", wrap, anchor>; /** * @var array\ */ public const CHANNEL_NAMES = [ - 'DEFAULT_TOKEN_CHANNEL', 'HIDDEN', '}; separator=", ", wrap, anchor> + 'DEFAULT_TOKEN_CHANNEL', 'HIDDEN', '}; separator=", ", wrap, anchor> ]; /** diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg index 4c82d5b5a1d..cba248c74b5 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Python2/Python2.stg @@ -155,7 +155,7 @@ class ( Parser ): if self._predicates == None: self._predicates = dict() ] = self._sempred}; separator="\n "> + self._predicates[] = self._sempred}; separator="\n "> pred = self._predicates.get(ruleIndex, None) if pred is None: raise Exception("No predicate with index:" + str(ruleIndex)) @@ -191,7 +191,7 @@ def sempred(self, localctx, ruleIndex, predIndex): if self._predicates is None: preds = dict() ] = self._sempred}; separator="\n"> + preds[] = self._sempred}; separator="\n"> self._predicates = preds pred = self._predicates.get(ruleIndex, None) if pred is not None: @@ -232,7 +232,7 @@ def _action(self, localctx , actionIndex): * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -def _sempred(self, localctx, predIndex): +def _sempred(self, localctx, predIndex): if predIndex == : @@ -250,7 +250,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina }; separator="\n"> -def (self}>): +def (self}>): localctx = .(self, self._ctx, self.state}>) self.enterRule(localctx, , self.RULE_) @@ -282,7 +282,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, }; separator="\n"> -def (self, _p=0, }>): +def (self, _p=0, }>): _parentctx = self._ctx _parentState = self.state localctx = .(self, self._ctx, _parentState}>) @@ -773,15 +773,15 @@ class (Lexer): = }; separator="\n"> - - = }; separator="\n"> + + = }; separator="\n"> = }; separator="\n", wrap, anchor> - channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN", "}; separator=", ", wrap, anchor> ] + channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN", "}; separator=", ", wrap, anchor> ] modeNames = [ "}; separator=", ", wrap, anchor> ] diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg index 8b445dd792d..a4e7affc45a 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg @@ -168,7 +168,7 @@ class ( Parser ): if self._predicates == None: self._predicates = dict() ] = self._sempred}; separator="\n "> + self._predicates[] = self._sempred}; separator="\n "> pred = self._predicates.get(ruleIndex, None) if pred is None: raise Exception("No predicate with index:" + str(ruleIndex)) @@ -204,7 +204,7 @@ def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int): if self._predicates is None: preds = dict() ] = self._sempred}; separator="\n"> + preds[] = self._sempred}; separator="\n"> self._predicates = preds pred = self._predicates.get(ruleIndex, None) if pred is not None: @@ -245,7 +245,7 @@ def _action(self, localctx: , actionIndex:int): * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -def _sempred(self, localctx:, predIndex:int): +def _sempred(self, localctx:, predIndex:int): if predIndex == : @@ -263,7 +263,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina }; separator="\n"> -def (self:}>): +def (self:}>): localctx = .(self, self._ctx, self.state}>) self.enterRule(localctx, , self.RULE_) @@ -295,7 +295,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, }; separator="\n"> -def (self, _p:int=0, }>): +def (self, _p:int=0, }>): _parentctx = self._ctx _parentState = self.state localctx = .(self, self._ctx, _parentState}>) @@ -787,15 +787,15 @@ class (Lexer): = }; separator="\n"> - - = }; separator="\n"> + + = }; separator="\n"> = }; separator="\n", wrap, anchor> - channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN", "}; separator=", ", wrap, anchor> ] + channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN", "}; separator=", ", wrap, anchor> ] modeNames = [ "}; separator=", ", wrap, anchor> ] diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg index 957a161a63c..6fc2b9247ba 100755 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg @@ -289,7 +289,7 @@ Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= << switch (ruleIndex) { : - return try _sempred(_localctx?.castdown(.self), predIndex)}; separator="\n"> + return try _sempred(_localctx?.castdown(.self), predIndex)}; separator="\n"> default: return true } @@ -338,7 +338,7 @@ func sempred(_ _localctx: RuleContext?, _ ruleIndex: Int,_ predIndex: Int) th switch (ruleIndex) { : - return try _sempred(_localctx?.castdown(.self), predIndex)}; separator="\n"> + return try _sempred(_localctx?.castdown(.self), predIndex)}; separator="\n"> default: return true } @@ -382,7 +382,7 @@ case : * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -private func _sempred(_ _localctx: !, _ predIndex: Int) throws -> Bool { +private func _sempred(_ _localctx: !, _ predIndex: Int) throws -> Bool { switch (predIndex) { :return }; separator="\n"> @@ -396,7 +396,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina }; separator="\n"> @discardableResult - }> func (_ ) throws -> { + }> func (_ ) throws -> { var _localctx: _localctx = (_ctx, getState()}>) try enterRule(_localctx, , .RULE_) @@ -435,11 +435,11 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, }; separator="\n"> - }> final func ( _ ) throws -> { - return try (0}>) + }> final func ( _ ) throws -> { + return try (0}>) } @discardableResult -private func (_ _p}>: Int) throws -> { +private func (_ _p}>: Int) throws -> { let _parentctx: ParserRuleContext? = _ctx let _parentState: Int = getState() var _localctx: @@ -989,13 +989,13 @@ Lexer(lexer, atn, actionFuncs, sempredFuncs, superClass) ::= << static let =}; separator=", ", wrap, anchor> - + - static let =}; separator=", ", wrap, anchor> + static let =}; separator=", ", wrap, anchor> static let channelNames: [String] = [ - "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> + "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> ] diff --git a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java index 50acbd58f4e..884f8ecff0f 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java @@ -5,16 +5,12 @@ */ package org.antlr.v4.codegen; -import org.antlr.v4.parse.ANTLRParser; -import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.tool.ErrorType; import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.ast.GrammarAST; +import org.antlr.v4.tool.Rule; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.gui.STViz; -import java.util.List; - public class CodeGenPipeline { final Grammar g; final CodeGenerator gen; @@ -25,24 +21,13 @@ public CodeGenPipeline(Grammar g, CodeGenerator gen) { } public void process() { - IntervalSet idTypes = new IntervalSet(); - idTypes.add(ANTLRParser.ID); - idTypes.add(ANTLRParser.RULE_REF); - idTypes.add(ANTLRParser.TOKEN_REF); - List idNodes = g.ast.getNodesWithType(idTypes); - for (GrammarAST idNode : idNodes) { - if ( gen.getTarget().grammarSymbolCausesIssueInGeneratedCode(idNode) ) { - g.tool.errMgr.grammarError(ErrorType.USE_OF_BAD_WORD, - g.fileName, idNode.getToken(), - idNode.getText()); - } - } - // all templates are generated in memory to report the most complete // error information possible, but actually writing output files stops // after the first error is reported int errorCount = g.tool.errMgr.getNumErrors(); + assignRuntimeRuleNames(); + if ( g.isLexer() ) { if (gen.getTarget().needsHeader()) { ST lexer = gen.generateLexer(true); // Header file if needed. @@ -121,6 +106,13 @@ public void process() { gen.writeVocabFile(); } + private void assignRuntimeRuleNames() { + Target target = gen.getTarget(); + for (Rule rule : g.rules.values()) { + rule.runtimeName = target.escapeIfNeeded(rule.name); + } + } + protected void writeRecognizer(ST template, CodeGenerator gen, boolean header) { if ( g.tool.launch_ST_inspector ) { STViz viz = template.inspect(); diff --git a/tool/src/org/antlr/v4/codegen/ParserFactory.java b/tool/src/org/antlr/v4/codegen/ParserFactory.java index 36742fc9c2e..ade54e5eee4 100644 --- a/tool/src/org/antlr/v4/codegen/ParserFactory.java +++ b/tool/src/org/antlr/v4/codegen/ParserFactory.java @@ -335,8 +335,10 @@ else if ( ast.getType()==ANTLRParser.RULE_REF ) { // a rule reference? public AddToLabelList getAddToListOpIfListLabelPresent(LabeledOp op, GrammarAST label) { AddToLabelList labelOp = null; if ( label!=null && label.parent.getType()==ANTLRParser.PLUS_ASSIGN ) { - String listLabel = gen.getTarget().getListLabel(label.getText()); - labelOp = new AddToLabelList(this, listLabel, op.getLabels().get(0)); + Target target = gen.getTarget(); + String listLabel = target.getListLabel(label.getText()); + String listRuntimeName = target.escapeIfNeeded(listLabel); + labelOp = new AddToLabelList(this, listRuntimeName, op.getLabels().get(0)); } return labelOp; } diff --git a/tool/src/org/antlr/v4/codegen/Target.java b/tool/src/org/antlr/v4/codegen/Target.java index 4d84ac96361..20168625ab9 100644 --- a/tool/src/org/antlr/v4/codegen/Target.java +++ b/tool/src/org/antlr/v4/codegen/Target.java @@ -26,6 +26,8 @@ import org.stringtemplate.v4.StringRenderer; import org.stringtemplate.v4.misc.STMessage; +import java.util.Set; + /** */ public abstract class Target { /** For pure strings of Java 16-bit Unicode char, how can we display @@ -88,6 +90,18 @@ public STGroup getTemplates() { return templates; } + protected abstract Set getReservedWords(); + + public String escapeIfNeeded(String identifier) { + return getReservedWords().contains(identifier) ? escapeWord(identifier) : identifier; + } + + protected String escapeWord(String word) { + return word + "_"; + } + + protected boolean supportsWordEscaping() { return false; } + protected void genFile(Grammar g, ST outputFileST, String fileName) { getCodeGenerator().write(outputFileST, fileName); @@ -518,10 +532,13 @@ public boolean grammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { break; } - return visibleGrammarSymbolCausesIssueInGeneratedCode(idNode); + return getReservedWords().contains(idNode.getText()); } - protected abstract boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode); + @Deprecated + protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { + return getReservedWords().contains(idNode.getText()); + } public boolean templatesExist() { return loadTemplatesHelper(false) != null; diff --git a/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java b/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java index 27a169a3dbc..fc43685c0a9 100644 --- a/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java +++ b/tool/src/org/antlr/v4/codegen/model/AddToLabelList.java @@ -11,8 +11,8 @@ /** */ public class AddToLabelList extends SrcOp { - public Decl label; - public String listName; + public final Decl label; + public final String listName; public AddToLabelList(OutputModelFactory factory, String listName, Decl label) { super(factory); diff --git a/tool/src/org/antlr/v4/codegen/model/InvokeRule.java b/tool/src/org/antlr/v4/codegen/model/InvokeRule.java index fb580cc2b26..978bad806fb 100644 --- a/tool/src/org/antlr/v4/codegen/model/InvokeRule.java +++ b/tool/src/org/antlr/v4/codegen/model/InvokeRule.java @@ -9,12 +9,12 @@ import org.antlr.v4.codegen.ActionTranslator; import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.ParserFactory; +import org.antlr.v4.codegen.Target; import org.antlr.v4.codegen.model.chunk.ActionChunk; import org.antlr.v4.codegen.model.decl.Decl; import org.antlr.v4.codegen.model.decl.RuleContextDecl; import org.antlr.v4.codegen.model.decl.RuleContextListDecl; import org.antlr.v4.parse.ANTLRParser; -import org.antlr.v4.runtime.atn.RuleTransition; import org.antlr.v4.runtime.misc.OrderedHashSet; import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.ActionAST; @@ -24,40 +24,43 @@ /** */ public class InvokeRule extends RuleElement implements LabeledOp { - public String name; - public OrderedHashSet labels = new OrderedHashSet(); // TODO: should need just 1 - public String ctxName; + public final String grammarName; + public final String name; + public final OrderedHashSet labels = new OrderedHashSet(); // TODO: should need just 1 + public final String ctxName; @ModelElement public List argExprsChunks; public InvokeRule(ParserFactory factory, GrammarAST ast, GrammarAST labelAST) { super(factory, ast); if ( ast.atnState!=null ) { - RuleTransition ruleTrans = (RuleTransition)ast.atnState.transition(0); stateNumber = ast.atnState.stateNumber; } - this.name = ast.getText(); CodeGenerator gen = factory.getGenerator(); - Rule r = factory.getGrammar().getRule(name); - ctxName = gen.getTarget().getRuleFunctionContextStructName(r); + Target target = gen.getTarget(); + String identifier = ast.getText(); + Rule r = factory.getGrammar().getRule(identifier); + this.grammarName = r.name; + this.name = r.runtimeName; + ctxName = target.getRuleFunctionContextStructName(r); // TODO: move to factory RuleFunction rf = factory.getCurrentRuleFunction(); if ( labelAST!=null ) { + RuleContextDecl decl; // for x=r, define x and list_x String label = labelAST.getText(); if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) { factory.defineImplicitLabel(ast, this); String listLabel = gen.getTarget().getListLabel(label); - RuleContextListDecl l = new RuleContextListDecl(factory, listLabel, ctxName); - rf.addContextDecl(ast.getAltLabel(), l); + decl = new RuleContextListDecl(factory, listLabel, ctxName); } else { - RuleContextDecl d = new RuleContextDecl(factory,label,ctxName); - labels.add(d); - rf.addContextDecl(ast.getAltLabel(), d); + decl = new RuleContextDecl(factory,label,ctxName); + labels.add(decl); } + rf.addContextDecl(ast.getAltLabel(), decl); } ActionAST arg = (ActionAST)ast.getFirstChildWithType(ANTLRParser.ARG_ACTION); @@ -66,8 +69,8 @@ public InvokeRule(ParserFactory factory, GrammarAST ast, GrammarAST labelAST) { } // If action refs rule as rulename not label, we need to define implicit label - if ( factory.getCurrentOuterMostAlt().ruleRefsInActions.containsKey(ast.getText()) ) { - String label = gen.getTarget().getImplicitRuleLabel(ast.getText()); + if ( factory.getCurrentOuterMostAlt().ruleRefsInActions.containsKey(identifier) ) { + String label = gen.getTarget().getImplicitRuleLabel(identifier); RuleContextDecl d = new RuleContextDecl(factory,label,ctxName); labels.add(d); rf.addContextDecl(ast.getAltLabel(), d); diff --git a/tool/src/org/antlr/v4/codegen/model/Lexer.java b/tool/src/org/antlr/v4/codegen/model/Lexer.java index 35170e28ca7..65155054903 100644 --- a/tool/src/org/antlr/v4/codegen/model/Lexer.java +++ b/tool/src/org/antlr/v4/codegen/model/Lexer.java @@ -7,18 +7,19 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; +import org.antlr.v4.codegen.Target; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LexerGrammar; import org.antlr.v4.tool.Rule; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.*; public class Lexer extends Recognizer { - public Map channels; - public LexerFile file; - public Collection modes; + public final Map channels; + public final Collection channelNames; + public final LexerFile file; + public final Collection modes; + public final Collection runtimeModeNames; @ModelElement public LinkedHashMap actionFuncs = new LinkedHashMap(); @@ -28,7 +29,20 @@ public Lexer(OutputModelFactory factory, LexerFile file) { this.file = file; // who contains us? Grammar g = factory.getGrammar(); - channels = new LinkedHashMap(g.channelNameToValueMap); + Target target = factory.getGenerator().getTarget(); + + channels = new LinkedHashMap<>(); + channelNames = new ArrayList<>(); + for (String key : g.channelNameToValueMap.keySet()) { + Integer value = g.channelNameToValueMap.get(key); + channels.put(target.escapeIfNeeded(key), value); + channelNames.add(key); + } + modes = ((LexerGrammar)g).modes.keySet(); + runtimeModeNames = new ArrayList<>(modes.size()); + for (String mode : modes) { + runtimeModeNames.add(target.escapeIfNeeded(mode)); + } } } diff --git a/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java index fffd5d266c7..220364eb1cd 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleActionFunction.java @@ -12,9 +12,10 @@ import java.util.LinkedHashMap; public class RuleActionFunction extends OutputModelObject { - public String name; - public String ctxType; - public int ruleIndex; + public final String grammarName; + public final String name; + public final String ctxType; + public final int ruleIndex; /** Map actionIndex to Action */ @ModelElement public LinkedHashMap actions = @@ -22,7 +23,8 @@ public class RuleActionFunction extends OutputModelObject { public RuleActionFunction(OutputModelFactory factory, Rule r, String ctxType) { super(factory); - name = r.name; + grammarName = r.name; + name = r.runtimeName; ruleIndex = r.index; this.ctxType = ctxType; } diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index d6b745f0ad7..d1744c04f57 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -51,6 +51,7 @@ /** */ public class RuleFunction extends OutputModelObject { public String name; + public String runtimeName; public List modifiers; public String ctxType; public Collection ruleLabels; @@ -74,6 +75,7 @@ public class RuleFunction extends OutputModelObject { public RuleFunction(OutputModelFactory factory, Rule r) { super(factory); this.name = r.name; + this.runtimeName = r.runtimeName; this.rule = r; if ( r.modifiers!=null && !r.modifiers.isEmpty() ) { this.modifiers = new ArrayList(); diff --git a/tool/src/org/antlr/v4/codegen/model/decl/Decl.java b/tool/src/org/antlr/v4/codegen/model/decl/Decl.java index b2b97425b6c..e183ddc0630 100644 --- a/tool/src/org/antlr/v4/codegen/model/decl/Decl.java +++ b/tool/src/org/antlr/v4/codegen/model/decl/Decl.java @@ -12,18 +12,20 @@ /** */ public class Decl extends SrcOp { public String name; - public String decl; // whole thing if copied from action + public final String grammarName; + public final String decl; // whole thing if copied from action public boolean isLocal; // if local var (not in RuleContext struct) public StructDecl ctx; // which context contains us? set by addDecl - public Decl(OutputModelFactory factory, String name, String decl) { - this(factory, name); - this.decl = decl; + public Decl(OutputModelFactory factory, String name) { + this(factory, name, null); } - public Decl(OutputModelFactory factory, String name) { + public Decl(OutputModelFactory factory, String name, String decl) { super(factory); - this.name = name; + this.grammarName = name; + this.name = factory.getGenerator().getTarget().escapeIfNeeded(name); + this.decl = decl; } @Override diff --git a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java index 227949c6d25..62d02141d24 100644 --- a/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/CSharpTarget.java @@ -8,7 +8,6 @@ import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; import org.antlr.v4.tool.ErrorType; -import org.antlr.v4.tool.ast.GrammarAST; import org.stringtemplate.v4.NumberRenderer; import org.stringtemplate.v4.STErrorListener; import org.stringtemplate.v4.STGroup; @@ -16,7 +15,92 @@ import org.stringtemplate.v4.StringRenderer; import org.stringtemplate.v4.misc.STMessage; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + public class CSharpTarget extends Target { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( + "abstract", + "as", + "base", + "bool", + "break", + "byte", + "case", + "catch", + "char", + "checked", + "class", + "const", + "continue", + "decimal", + "default", + "delegate", + "do", + "double", + "else", + "enum", + "event", + "explicit", + "extern", + "false", + "finally", + "fixed", + "float", + "for", + "foreach", + "goto", + "if", + "implicit", + "in", + "int", + "interface", + "internal", + "is", + "lock", + "long", + "namespace", + "new", + "null", + "object", + "operator", + "out", + "override", + "params", + "private", + "protected", + "public", + "readonly", + "ref", + "return", + "sbyte", + "sealed", + "short", + "sizeof", + "stackalloc", + "static", + "string", + "struct", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "uint", + "ulong", + "unchecked", + "unsafe", + "ushort", + "using", + "virtual", + "values", + "void", + "volatile", + "while" + )); + public CSharpTarget(CodeGenerator gen) { super(gen); targetCharValueEscape[0] = "\\0"; @@ -24,6 +108,19 @@ public CSharpTarget(CodeGenerator gen) { targetCharValueEscape[0x000B] = "\\v"; } + @Override + protected Set getReservedWords() { + return reservedWords; + } + + @Override + protected String escapeWord(String word) { + return "@" + word; + } + + @Override + protected boolean supportsWordEscaping() { return true; } + @Override public String encodeIntAsCharEscape(int v) { if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) { @@ -44,11 +141,6 @@ else if (v >= 0x20 && v < 127 && (v < '0' || v > '9') && (v < 'a' || v > 'f') && return "'" + formatted + "'"; } - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return false; - } - @Override protected STGroup loadTemplates() { // override the superclass behavior to put all C# templates in the same folder diff --git a/tool/src/org/antlr/v4/codegen/target/CppTarget.java b/tool/src/org/antlr/v4/codegen/target/CppTarget.java index 1cdfac157fe..58cbc4e29c3 100644 --- a/tool/src/org/antlr/v4/codegen/target/CppTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/CppTarget.java @@ -9,7 +9,6 @@ import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; import org.antlr.v4.tool.ErrorType; -import org.antlr.v4.tool.ast.GrammarAST; import org.stringtemplate.v4.NumberRenderer; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STErrorListener; @@ -22,7 +21,7 @@ import java.util.Set; public class CppTarget extends Target { - protected static final String[] cppKeywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", "compl", "concept", "const", "constexpr", @@ -37,34 +36,24 @@ public class CppTarget extends Target { "switch", "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", - "xor", "xor_eq" - }; + "xor", "xor_eq", - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - protected final Set badWords = new HashSet(); + "rule", "parserRule" + )); public CppTarget(CodeGenerator gen) { super(gen); targetCharValueEscape['?'] = "\\?"; } - public boolean needsHeader() { return true; } - - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; + @Override + protected Set getReservedWords() { + return reservedWords; } - protected void addBadWords() { - badWords.addAll(Arrays.asList(cppKeywords)); - badWords.add("rule"); - badWords.add("parserRule"); - } + public boolean needsHeader() { return true; } - @Override + @Override protected boolean shouldUseUnicodeEscapeForCodePointInDoubleQuotedString(int codePoint) { if (codePoint == '?') { // in addition to the default escaped code points, also escape ? to prevent trigraphs @@ -120,11 +109,6 @@ public String getBaseVisitorFileName(boolean header) { return listenerName+extST.render(); } - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); - } - @Override protected STGroup loadTemplates() { STGroup result = super.loadTemplates(); diff --git a/tool/src/org/antlr/v4/codegen/target/DartTarget.java b/tool/src/org/antlr/v4/codegen/target/DartTarget.java index ce42fd64e1c..3b43c36636a 100644 --- a/tool/src/org/antlr/v4/codegen/target/DartTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/DartTarget.java @@ -17,12 +17,7 @@ import java.util.Set; public class DartTarget extends Target { - /** - * The Java target can cache the code generation templates. - */ - private static final ThreadLocal targetTemplates = new ThreadLocal(); - - protected static final String[] javaKeywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "abstract", "dynamic", "implements", "show", "as", "else", "import", "static", "assert", "enum", "in", "super", @@ -38,10 +33,9 @@ public class DartTarget extends Target { "default", "get", "rethrow", "while", "deferred", "hide", "return", "with", "do", "if", "set", "yield", - }; - /// Avoid grammar symbols in this set to prevent conflicts in gen'd code. - protected final Set badWords = new HashSet(); + "rule", "parserRule" + )); public DartTarget(CodeGenerator gen) { super(gen); @@ -55,23 +49,8 @@ public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generat return super.getTargetStringLiteralFromANTLRStringLiteral(generator, literal, addQuotes, escapeSpecial).replace("$", "\\$"); } - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(javaKeywords)); - badWords.add("rule"); - badWords.add("parserRule"); - } - - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); + public Set getReservedWords() { + return reservedWords; } @Override diff --git a/tool/src/org/antlr/v4/codegen/target/GoTarget.java b/tool/src/org/antlr/v4/codegen/target/GoTarget.java index 86e64895da3..279ff44ae5e 100644 --- a/tool/src/org/antlr/v4/codegen/target/GoTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/GoTarget.java @@ -23,42 +23,32 @@ import java.util.Locale; import java.util.Set; -/** - * - * @author Peter Boyer - * - * */ public class GoTarget extends Target { - private static final String[] goKeywords = { - "break", "default", "func", "interface", "select", - "case", "defer", "go", "map", "struct", - "chan", "else", "goto", "package", "switch", - "const", "fallthrough", "if", "range", "type", - "continue", "for", "import", "return", "var" - }; - - // predeclared identifiers https://golang.org/ref/spec#Predeclared_identifiers - private static final String[] goPredeclaredIdentifiers = { - "bool", "byte", "complex64", "complex128", "error", "float32", "float64", - "int", "int8", "int16", "int32", "int64", "rune", "string", - "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", - "true", "false", "iota", "nil", - "append", "cap", "close", "complex", "copy", "delete", "imag", "len", - "make", "new", "panic", "print", "println", "real", "recover" - }; - - // interface definition of RuleContext from runtime/Go/antlr/rule_context.go - private static final String[] goRuleContextInterfaceMethods = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( + // keywords + "break", "default", "func", "interface", "select", + "case", "defer", "go", "map", "struct", + "chan", "else", "goto", "package", "switch", + "const", "fallthrough", "if", "range", "type", + "continue", "for", "import", "return", "var", + + // predeclared identifiers https://golang.org/ref/spec#Predeclared_identifiers + "bool", "byte", "complex64", "complex128", "error", "float32", "float64", + "int", "int8", "int16", "int32", "int64", "rune", "string", + "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", + "true", "false", "iota", "nil", + "append", "cap", "close", "complex", "copy", "delete", "imag", "len", + "make", "new", "panic", "print", "println", "real", "recover", + + // interface definition of RuleContext from runtime/Go/antlr/rule_context.go "Accept", "GetAltNumber", "GetBaseRuleContext", "GetChild", "GetChildCount", "GetChildren", "GetInvokingState", "GetParent", "GetPayload", "GetRuleContext", "GetRuleIndex", "GetSourceInterval", "GetText", "IsEmpty", "SetAltNumber", - "SetInvokingState", "SetParent", "String" - }; + "SetInvokingState", "SetParent", "String", - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - private final Set badWords = new HashSet( - goKeywords.length + goPredeclaredIdentifiers.length + goRuleContextInterfaceMethods.length + 3 - ); + // misc + "rule", "parserRule", "action" + )); private static final boolean DO_GOFMT = !Boolean.parseBoolean(System.getenv("ANTLR_GO_DISABLE_GOFMT")) && !Boolean.parseBoolean(System.getProperty("antlr.go.disable-gofmt")); @@ -67,21 +57,9 @@ public GoTarget(CodeGenerator gen) { super(gen); } - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(goKeywords)); - badWords.addAll(Arrays.asList(goPredeclaredIdentifiers)); - badWords.addAll(Arrays.asList(goRuleContextInterfaceMethods)); - badWords.add("rule"); - badWords.add("parserRule"); - badWords.add("action"); + @Override + protected Set getReservedWords() { + return reservedWords; } @Override @@ -130,11 +108,6 @@ public int getInlineTestSetWordSize() { return 32; } - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); - } - @Override protected STGroup loadTemplates() { STGroup result = super.loadTemplates(); diff --git a/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java b/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java index e821d882f5f..78a60b1ec62 100644 --- a/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/JavaScriptTarget.java @@ -17,48 +17,38 @@ import java.util.Locale; import java.util.Set; -/** - * - * @author Eric Vergnaud - */ public class JavaScriptTarget extends Target { /** Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar */ - protected static final String[] javaScriptKeywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "break", "case", "class", "catch", "const", "continue", "debugger", "default", "delete", "do", "else", "export", "extends", "finally", "for", "function", "if", "import", "in", "instanceof", "let", "new", "return", "super", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with", "yield", + //future reserved "enum", "await", "implements", "package", "protected", "static", "interface", "private", "public", + //future reserved in older standards "abstract", "boolean", "byte", "char", "double", "final", "float", "goto", "int", "long", "native", "short", "synchronized", "transient", "volatile", + //literals - "null", "true", "false" - }; + "null", "true", "false", - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - protected final Set badWords = new HashSet(); + // misc + "rule", "parserRule" + )); public JavaScriptTarget(CodeGenerator gen) { super(gen); } - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(javaScriptKeywords)); - badWords.add("rule"); - badWords.add("parserRule"); + @Override + protected Set getReservedWords() { + return reservedWords; } @Override @@ -84,11 +74,6 @@ public int getInlineTestSetWordSize() { return 32; } - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); - } - @Override protected STGroup loadTemplates() { STGroup result = super.loadTemplates(); diff --git a/tool/src/org/antlr/v4/codegen/target/JavaTarget.java b/tool/src/org/antlr/v4/codegen/target/JavaTarget.java index 2ef48fe568a..305840e89c8 100644 --- a/tool/src/org/antlr/v4/codegen/target/JavaTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/JavaTarget.java @@ -6,10 +6,8 @@ package org.antlr.v4.codegen.target; -import org.antlr.v4.Tool; import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; -import org.antlr.v4.tool.ast.GrammarAST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.StringRenderer; @@ -24,7 +22,7 @@ public class JavaTarget extends Target { */ private static final ThreadLocal targetTemplates = new ThreadLocal(); - protected static final String[] javaKeywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", @@ -32,33 +30,19 @@ public class JavaTarget extends Target { "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", - "void", "volatile", "while" - }; + "void", "volatile", "while", - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - protected final Set badWords = new HashSet(); + // misc + "rule", "parserRule" + )); public JavaTarget(CodeGenerator gen) { super(gen); } - @Override - public String getVersion() { - return Tool.VERSION; // Java and tool versions move in lock step - } - - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(javaKeywords)); - badWords.add("rule"); - badWords.add("parserRule"); + @Override + public Set getReservedWords() { + return reservedWords; } @Override @@ -68,11 +52,6 @@ public int getSerializedATNSegmentLimit() { return 65535 / 3; } - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); - } - @Override protected STGroup loadTemplates() { STGroup result = targetTemplates.get(); diff --git a/tool/src/org/antlr/v4/codegen/target/PHPTarget.java b/tool/src/org/antlr/v4/codegen/target/PHPTarget.java index 16b09c99799..08558eb1903 100644 --- a/tool/src/org/antlr/v4/codegen/target/PHPTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/PHPTarget.java @@ -8,7 +8,6 @@ import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; -import org.antlr.v4.tool.ast.GrammarAST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.StringRenderer; @@ -17,7 +16,7 @@ import java.util.Set; public class PHPTarget extends Target { - private static final String[] phpKeywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "abstract", "and", "array", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", @@ -40,16 +39,22 @@ public class PHPTarget extends Target { "xor", "yield", "__halt_compiler", "__CLASS__", "__DIR__", "__FILE__", "__FUNCTION__", - "__LINE__", "__METHOD__", "__NAMESPACE__", "__TRAIT__" - }; + "__LINE__", "__METHOD__", "__NAMESPACE__", "__TRAIT__", - private final Set badWords = new HashSet(); + // misc + "rule", "parserRule" + )); public PHPTarget(CodeGenerator gen) { super(gen); targetCharValueEscape['$'] = "\\$"; } + @Override + protected Set getReservedWords() { + return reservedWords; + } + @Override public String encodeIntAsCharEscape(int v) { if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) { @@ -59,25 +64,6 @@ public String encodeIntAsCharEscape(int v) { return String.format("\\u{%X}", v & 0xFFFF); } - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(phpKeywords)); - badWords.add("rule"); - badWords.add("parserRule"); - } - - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); - } - @Override protected STGroup loadTemplates() { STGroup result = super.loadTemplates(); diff --git a/tool/src/org/antlr/v4/codegen/target/Python2Target.java b/tool/src/org/antlr/v4/codegen/target/Python2Target.java index 1a2caff9f49..9061fb14054 100644 --- a/tool/src/org/antlr/v4/codegen/target/Python2Target.java +++ b/tool/src/org/antlr/v4/codegen/target/Python2Target.java @@ -17,12 +17,8 @@ import java.util.Locale; import java.util.Set; -/** - * - * @author Eric Vergnaud - */ public class Python2Target extends Target { - protected static final String[] python2Keywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "abs", "all", "and", "any", "apply", "as", "assert", "bin", "bool", "break", "buffer", "bytearray", "callable", "chr", "class", "classmethod", "coerce", "compile", "complex", "continue", @@ -47,19 +43,19 @@ public class Python2Target extends Target { "yield", "zip", "__import__", - "True", "False", "None" - }; + "True", "False", "None", - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - protected final Set badWords = new HashSet(); + // misc + "rule", "parserRule" + )); public Python2Target(CodeGenerator gen) { super(gen); } @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); + protected Set getReservedWords() { + return reservedWords; } @Override @@ -91,18 +87,4 @@ public boolean wantsBaseVisitor() { public boolean supportsOverloadedMethods() { return false; } - - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(python2Keywords)); - badWords.add("rule"); - badWords.add("parserRule"); - } } diff --git a/tool/src/org/antlr/v4/codegen/target/Python3Target.java b/tool/src/org/antlr/v4/codegen/target/Python3Target.java index d7560432b18..acd0c395f22 100644 --- a/tool/src/org/antlr/v4/codegen/target/Python3Target.java +++ b/tool/src/org/antlr/v4/codegen/target/Python3Target.java @@ -8,7 +8,6 @@ import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; -import org.antlr.v4.tool.ast.GrammarAST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.StringRenderer; @@ -17,12 +16,8 @@ import java.util.Locale; import java.util.Set; -/** - * - * @author Eric Vergnaud - */ public class Python3Target extends Target { - protected static final String[] python3Keywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "abs", "all", "and", "any", "apply", "as", "assert", "bin", "bool", "break", "buffer", "bytearray", "callable", "chr", "class", "classmethod", "coerce", "compile", "complex", "continue", @@ -46,16 +41,19 @@ public class Python3Target extends Target { "yield", "zip", "__import__", - "True", "False", "None" - }; + "True", "False", "None", + + // misc + "rule", "parserRule" + )); public Python3Target(CodeGenerator gen) { super(gen); } @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); + protected Set getReservedWords() { + return reservedWords; } @Override @@ -87,21 +85,4 @@ public boolean wantsBaseVisitor() { public boolean supportsOverloadedMethods() { return false; } - - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - protected final Set badWords = new HashSet(); - - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } - - return badWords; - } - - protected void addBadWords() { - badWords.addAll(Arrays.asList(python3Keywords)); - badWords.add("rule"); - badWords.add("parserRule"); - } } diff --git a/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java b/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java index 060a7b48c34..78b3da45d5c 100644 --- a/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/SwiftTarget.java @@ -62,7 +62,7 @@ public class SwiftTarget extends Target { */ private static final ThreadLocal targetTemplates = new ThreadLocal(); - protected static final String[] swiftKeywords = { + protected static final HashSet reservedWords = new HashSet<>(Arrays.asList( "associatedtype", "class", "deinit", "enum", "extension", "func", "import", "init", "inout", "internal", "let", "operator", "private", "protocol", "public", "static", "struct", "subscript", "typealias", "var", "break", "case", "continue", "default", "defer", "do", "else", "fallthrough", "for", "guard", "if", @@ -71,11 +71,10 @@ public class SwiftTarget extends Target { "true", "try", "__COLUMN__", "__FILE__", "__FUNCTION__","__LINE__", "#column", "#file", "#function", "#line", "_" , "#available", "#else", "#elseif", "#endif", "#if", "#selector", "associativity", "convenience", "dynamic", "didSet", "final", "get", "infix", "indirect", "lazy", "left", "mutating", "none", "nonmutating", "optional", "override", "postfix", "precedence", - "prefix", "Protocol", "required", "right", "set", "Type", "unowned", "weak", "willSet" - }; + "prefix", "Protocol", "required", "right", "set", "Type", "unowned", "weak", "willSet", - /** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */ - protected final Set badWords = new HashSet(); + "rule", "parserRule" + )); public String lexerAtnJSON = null; public String parserAtnJSON = null; @@ -84,24 +83,19 @@ public SwiftTarget(CodeGenerator gen) { super(gen); } - public Set getBadWords() { - if (badWords.isEmpty()) { - addBadWords(); - } + @Override + protected Set getReservedWords() { + return reservedWords; + } - return badWords; - } + @Override + protected String escapeWord(String word) { + return "`" + word + "`"; + } - protected void addBadWords() { - badWords.addAll(Arrays.asList(swiftKeywords)); - badWords.add("rule"); - badWords.add("parserRule"); - } + @Override + protected boolean supportsWordEscaping() { return true; } - @Override - protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { - return getBadWords().contains(idNode.getText()); - } @Override protected void genFile(Grammar g, ST outputFileST, diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 069747e0077..9fd36943f9f 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -515,20 +515,13 @@ public enum ErrorType { *

* symbol symbol conflicts with generated code in target language * or runtime

- * - *

- * Note: This error has the same number as the unrelated error - * {@link #UNSUPPORTED_REFERENCE_IN_LEXER_SET}.

*/ + @Deprecated USE_OF_BAD_WORD(134, "symbol conflicts with generated code in target language or runtime", ErrorSeverity.ERROR), /** * Compiler Error 183. * *

rule reference rule is not currently supported in a set

- * - *

- * Note: This error has the same number as the unrelated error - * {@link #USE_OF_BAD_WORD}.

*/ UNSUPPORTED_REFERENCE_IN_LEXER_SET(183, "rule reference is not currently supported in a set", ErrorSeverity.ERROR), /** diff --git a/tool/src/org/antlr/v4/tool/Rule.java b/tool/src/org/antlr/v4/tool/Rule.java index 5389f2b182e..0b1f52b5686 100644 --- a/tool/src/org/antlr/v4/tool/Rule.java +++ b/tool/src/org/antlr/v4/tool/Rule.java @@ -52,7 +52,8 @@ public class Rule implements AttributeResolver { validLexerCommands.add("more"); } - public String name; + public final String name; + public String runtimeName; public List modifiers; public RuleAST ast; @@ -61,7 +62,7 @@ public class Rule implements AttributeResolver { public AttributeDict locals; /** In which grammar does this rule live? */ - public Grammar g; + public final Grammar g; /** If we're in a lexer grammar, we might be in a mode */ public final String mode; @@ -93,7 +94,7 @@ public class Rule implements AttributeResolver { public ActionAST finallyAction; - public int numberOfAlts; + public final int numberOfAlts; public boolean isStartRule = true; // nobody calls us