diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg index 473398ad2..aac1b8186 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/CSharp.test.stg @@ -64,9 +64,9 @@ PlusText(a) ::= <%"" + this.Text%> InputText() ::= "this.TokenStream.GetText()" -LTEquals(i, v) ::= <%this.TokenStream.Lt().Text.Equals()%> +LTEquals(i, v) ::= <%this.TokenStream.LT().Text.Equals()%> -LANotEquals(i, v) ::= <%this.InputStream.La()!=%> +LANotEquals(i, v) ::= <%this.InputStream.LA()!=%> TokenStartColumnEquals(i) ::= <%this.TokenStartColumn==%> diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java index 734223522..ded2c9904 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java @@ -691,7 +691,7 @@ public class BaseCSharpTest implements RuntimeTestSupport, SpecialRuntimeTestAss " tokens.Fill();\n" + " foreach (object t in tokens.GetTokens())\n" + " Console.WriteLine(t);\n" + - (showDFA?"Console.Write(lex.Interpreter.GetDFA(Lexer.DefaultMode).ToLexerString());\n":"")+ + (showDFA?" Console.Write(lex.Interpreter.GetDFA(Lexer.DEFAULT_MODE).ToLexerString());\n":"")+ " }\n" + "}" ); diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs index ab4522ca7..83cf7ffce 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs @@ -1275,7 +1275,7 @@ namespace Antlr4.Runtime.Atn { if (ambigAlts[c.alt]) { - altToPred[c.alt] = new SemanticContext.OR(altToPred[c.alt], c.semanticContext); + altToPred[c.alt] = SemanticContext.Or(altToPred[c.alt], c.semanticContext); } } @@ -1960,8 +1960,7 @@ namespace Antlr4.Runtime.Atn } } else { - SemanticContext newSemCtx = - new SemanticContext.AND(config.semanticContext, pt.Predicate); + SemanticContext newSemCtx = SemanticContext.And(config.semanticContext, pt.Predicate); c = new ATNConfig(config, pt.target, newSemCtx); } } @@ -2012,8 +2011,7 @@ namespace Antlr4.Runtime.Atn } } else { - SemanticContext newSemCtx = - new SemanticContext.AND(config.semanticContext, pt.Predicate); + SemanticContext newSemCtx = SemanticContext.And(config.semanticContext, pt.Predicate); c = new ATNConfig(config, pt.target, newSemCtx); } } diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/SemanticContext.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/SemanticContext.cs index 95a715f26..2c03a6737 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/SemanticContext.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Atn/SemanticContext.cs @@ -399,7 +399,7 @@ namespace Antlr4.Runtime.Atn SemanticContext result = operands[0]; for (int i = 1; i < operands.Count; i++) { - result = SemanticContext.AndOp(result, operands[i]); + result = SemanticContext.And(result, operands[i]); } return result; } @@ -531,7 +531,7 @@ namespace Antlr4.Runtime.Atn SemanticContext result = operands[0]; for (int i = 1; i < operands.Count; i++) { - result = SemanticContext.OrOp(result, operands[i]); + result = SemanticContext.Or(result, operands[i]); } return result; } @@ -542,7 +542,7 @@ namespace Antlr4.Runtime.Atn } } - public static SemanticContext AndOp(SemanticContext a, SemanticContext b) + public static SemanticContext And(SemanticContext a, SemanticContext b) { if (a == null || a == NONE) { @@ -561,7 +561,7 @@ namespace Antlr4.Runtime.Atn } /// - public static SemanticContext OrOp(SemanticContext a, SemanticContext b) + public static SemanticContext Or(SemanticContext a, SemanticContext b) { if (a == null) { diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFA.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFA.cs index c691c434c..b29f15732 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFA.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFA.cs @@ -189,9 +189,10 @@ namespace Antlr4.Runtime.Dfa return serializer.ToString(); } - public String toLexerString() + public String ToLexerString() { - if (s0 == null) return ""; + if (s0 == null) + return ""; DFASerializer serializer = new LexerDFASerializer(this); return serializer.ToString(); } diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFASerializer.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFASerializer.cs index bf6480ec2..5633d531b 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFASerializer.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Dfa/DFASerializer.cs @@ -82,35 +82,35 @@ namespace Antlr4.Runtime.Dfa if (dfa.states != null) { List states = new List(dfa.states.Values); - states.Sort(new _IComparer_103()); + states.Sort((x,y)=>x.stateNumber - y.stateNumber); foreach (DFAState s in states) { - foreach (DFAState edge in s.edges) - { - buf.Append(GetStateString(s)); - } - } + int n = s.edges != null ? s.edges.Length : 0; + for (int i = 0; i < n; i++) + { + DFAState t = s.edges[i]; + if (t != null && t.stateNumber != int.MaxValue) + { + buf.Append(GetStateString(s)); + String label = GetEdgeLabel(i); + buf.Append("-"); + buf.Append(label); + buf.Append("->"); + buf.Append(GetStateString(t)); + buf.Append('\n'); + } + } + } } string output = buf.ToString(); if (output.Length == 0) { return null; } - //return Utils.sortLinesInString(output); return output; } - private sealed class _IComparer_103 : IComparer - { - public _IComparer_103() - { - } - public int Compare(DFAState o1, DFAState o2) - { - return o1.stateNumber - o2.stateNumber; - } - } protected internal virtual string GetContextLabel(int i) { 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 4aae5ab67..3fe028749 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 @@ -269,6 +269,8 @@ Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= << [System.CodeDom.Compiler.GeneratedCode("ANTLR", "")] [System.CLSCompliant(false)] public partial class : { + protected static DFA[] decisionToDFA; + protected static PredictionContextCache sharedContextCache = new PredictionContextCache(); public const int =}; separator=", ", wrap, anchor>; @@ -287,6 +289,13 @@ public partial class : public override string SerializedAtn { get { return _serializedATN; } } + static () { + decisionToDFA = new DFA[_ATN.NumberOfDecisions]; + for (int i = 0; i \< _ATN.NumberOfDecisions; i++) { + decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i); + } + } + @@ -351,7 +360,7 @@ parser_ctor(parser) ::= << public (ITokenStream input) : base(input) { - Interpreter = new ParserATNSimulator(this,_ATN); + Interpreter = new ParserATNSimulator(this, _ATN, decisionToDFA, sharedContextCache); } >> @@ -538,9 +547,9 @@ CodeBlockForAlt(currentAltCodeBlock, locals, preamble, ops) ::= << LL1AltBlock(choice, preamble, alts, error) ::= << State = ; ErrorHandler.Sync(this); - = TokenStream.Lt(1); + = TokenStream.LT(1); -switch (TokenStream.La(1)) { +switch (TokenStream.LA(1)) { break;}; separator="\n"> @@ -552,7 +561,7 @@ default: LL1OptionalBlock(choice, alts, error) ::= << State = ; ErrorHandler.Sync(this); -switch (TokenStream.La(1)) { +switch (TokenStream.LA(1)) { break;}; separator="\n"> @@ -600,7 +609,7 @@ do { AltBlock(choice, preamble, alts, error) ::= << State = ; ErrorHandler.Sync(this); - = TokenStream.Lt(1); + = TokenStream.LT(1); switch ( Interpreter.AdaptivePredict(TokenStream,,Context) ) { ; ErrorHandler.Sync(this); _alt = Interpreter.AdaptivePredict(TokenStream,,Context); -while ( _alt!= && _alt!=global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber ) { +while ( _alt!= && _alt!=global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER ) { if ( _alt==1+1 ) { @@ -652,7 +661,7 @@ case +1: State = ; ErrorHandler.Sync(this); _alt = Interpreter.AdaptivePredict(TokenStream,,Context); -} while ( _alt!= && _alt!=global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber ); +} while ( _alt!= && _alt!=global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER ); >> Sync(s) ::= "Sync();" @@ -705,7 +714,7 @@ MatchNotSet(m, expr, capture) ::= "" CommonSetStuff(m, expr, capture, invert) ::= << State = ; - = }>TokenStream.Lt(1); + = }>TokenStream.LT(1); if ( \<= 0 || !() ) { = }>ErrorHandler.RecoverInline(this); @@ -779,7 +788,7 @@ RulePropertyRef_parser(r) ::= "this" ThisRulePropertyRef_start(r) ::= "_localctx.Start" ThisRulePropertyRef_stop(r) ::= "_localctx.Stop" -ThisRulePropertyRef_text(r) ::= "TokenStream.GetText(_localctx.Start, TokenStream.Lt(-1))" +ThisRulePropertyRef_text(r) ::= "TokenStream.GetText(_localctx.Start, TokenStream.LT(-1))" ThisRulePropertyRef_ctx(r) ::= "_localctx" ThisRulePropertyRef_parser(r) ::= "this" @@ -837,8 +846,8 @@ ImplicitRuleLabel(ruleName) ::= "_" ImplicitSetLabel(id) ::= "_tset" ListLabelName(label) ::= "_