Good progress with parser

This commit is contained in:
Eric Vergnaud 2016-12-12 02:00:09 +08:00
parent 19d70cd777
commit 0170c9a064
7 changed files with 51 additions and 43 deletions

View File

@ -64,9 +64,9 @@ PlusText(a) ::= <%"<a>" + this.Text%>
InputText() ::= "this.TokenStream.GetText()"
LTEquals(i, v) ::= <%this.TokenStream.Lt(<i>).Text.Equals(<v>)%>
LTEquals(i, v) ::= <%this.TokenStream.LT(<i>).Text.Equals(<v>)%>
LANotEquals(i, v) ::= <%this.InputStream.La(<i>)!=<v>%>
LANotEquals(i, v) ::= <%this.InputStream.LA(<i>)!=<v>%>
TokenStartColumnEquals(i) ::= <%this.TokenStartColumn==<i>%>

View File

@ -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" +
"}"
);

View File

@ -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);
}
}

View File

@ -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
}
/// <seealso cref="ParserATNSimulator.GetPredsForAmbigAlts(Antlr4.Runtime.Sharpen.BitSet, ATNConfigSet, int)"/>
public static SemanticContext OrOp(SemanticContext a, SemanticContext b)
public static SemanticContext Or(SemanticContext a, SemanticContext b)
{
if (a == null)
{

View File

@ -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();
}

View File

@ -82,35 +82,35 @@ namespace Antlr4.Runtime.Dfa
if (dfa.states != null)
{
List<DFAState> states = new List<DFAState>(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<DFAState>
{
public _IComparer_103()
{
}
public int Compare(DFAState o1, DFAState o2)
{
return o1.stateNumber - o2.stateNumber;
}
}
protected internal virtual string GetContextLabel(int i)
{

View File

@ -269,6 +269,8 @@ Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= <<
[System.CodeDom.Compiler.GeneratedCode("ANTLR", "<file.ANTLRVersion>")]
[System.CLSCompliant(false)]
public partial class <csIdentifier.(parser.name)> : <superClass; null="Parser"> {
protected static DFA[] decisionToDFA;
protected static PredictionContextCache sharedContextCache = new PredictionContextCache();
<if(parser.tokens)>
public const int
<parser.tokens:{k | <k>=<parser.tokens.(k)>}; separator=", ", wrap, anchor>;
@ -287,6 +289,13 @@ public partial class <csIdentifier.(parser.name)> : <superClass; null="Parser">
public override string SerializedAtn { get { return _serializedATN; } }
static <csIdentifier.(parser.name)>() {
decisionToDFA = new DFA[_ATN.NumberOfDecisions];
for (int i = 0; i \< _ATN.NumberOfDecisions; i++) {
decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
}
}
<namedActions.members>
<parser:(ctor)()>
<funcs; separator="\n">
@ -351,7 +360,7 @@ parser_ctor(parser) ::= <<
public <csIdentifier.(parser.name)>(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 = <choice.stateNumber>;
ErrorHandler.Sync(this);
<if(choice.label)><labelref(choice.label)> = TokenStream.Lt(1);<endif>
<if(choice.label)><labelref(choice.label)> = TokenStream.LT(1);<endif>
<preamble; separator="\n">
switch (TokenStream.La(1)) {
switch (TokenStream.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<alt>
break;}; separator="\n">
@ -552,7 +561,7 @@ default:
LL1OptionalBlock(choice, alts, error) ::= <<
State = <choice.stateNumber>;
ErrorHandler.Sync(this);
switch (TokenStream.La(1)) {
switch (TokenStream.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<alt>
break;}; separator="\n">
@ -600,7 +609,7 @@ do {
AltBlock(choice, preamble, alts, error) ::= <<
State = <choice.stateNumber>;
ErrorHandler.Sync(this);
<if(choice.label)><labelref(choice.label)> = TokenStream.Lt(1);<endif>
<if(choice.label)><labelref(choice.label)> = TokenStream.LT(1);<endif>
<preamble; separator="\n">
switch ( Interpreter.AdaptivePredict(TokenStream,<choice.decision>,Context) ) {
<alts:{alt |
@ -625,7 +634,7 @@ StarBlock(choice, alts, sync, iteration) ::= <<
State = <choice.stateNumber>;
ErrorHandler.Sync(this);
_alt = Interpreter.AdaptivePredict(TokenStream,<choice.decision>,Context);
while ( _alt!=<choice.exitAlt> && _alt!=global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber ) {
while ( _alt!=<choice.exitAlt> && _alt!=global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER ) {
if ( _alt==1<if(!choice.ast.greedy)>+1<endif> ) {
<iteration>
<alts> <! should only be one !>
@ -652,7 +661,7 @@ case <i><if(!choice.ast.greedy)>+1<endif>:
State = <choice.loopBackStateNumber>;<! loopback/exit decision !>
ErrorHandler.Sync(this);
_alt = Interpreter.AdaptivePredict(TokenStream,<choice.decision>,Context);
} while ( _alt!=<choice.exitAlt> && _alt!=global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber );
} while ( _alt!=<choice.exitAlt> && _alt!=global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER );
>>
Sync(s) ::= "Sync(<s.expecting.name>);"
@ -705,7 +714,7 @@ MatchNotSet(m, expr, capture) ::= "<CommonSetStuff(m, expr, capture, true)>"
CommonSetStuff(m, expr, capture, invert) ::= <<
State = <m.stateNumber>;
<if(m.labels)><m.labels:{l | <labelref(l)> = }>TokenStream.Lt(1);<endif>
<if(m.labels)><m.labels:{l | <labelref(l)> = }>TokenStream.LT(1);<endif>
<capture>
if ( <if(invert)><m.varName> \<= 0 || <else>!<endif>(<expr>) ) {
<if(m.labels)><m.labels:{l | <labelref(l)> = }><endif>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) ::= "_<ruleName>"
ImplicitSetLabel(id) ::= "_tset<id>"
ListLabelName(label) ::= "_<label>"
CaptureNextToken(d) ::= "<d.varName> = TokenStream.Lt(1);"
CaptureNextTokenType(d) ::= "<d.varName> = TokenStream.La(1);"
CaptureNextToken(d) ::= "<d.varName> = TokenStream.LT(1);"
CaptureNextTokenType(d) ::= "<d.varName> = TokenStream.LA(1);"
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,
superClass={ParserRuleContext}) ::= <<
@ -902,7 +911,7 @@ typedContext(ctx) ::= "<if(ctx.provideCopyFrom)>((<ctx.name>)_localctx)<else>_lo
// used for left-recursive rules
recRuleAltPredicate(ruleName,opPrec) ::= "Precpred(Context, <opPrec>)"
recRuleSetReturnAction(src,name) ::= "$<name>=$<src>.<name>;"
recRuleSetStopToken() ::= "Context.Stop = TokenStream.Lt(-1);"
recRuleSetStopToken() ::= "Context.Stop = TokenStream.LT(-1);"
recRuleAltStartAction(ruleName, ctxName, label, isListLabel) ::= <<
_localctx = new <ctxName>Context(_parentctx, _parentState);