//throws Exception
protected void checkDecision( Grammar g,
int decision,
string expecting,
int[] expectingUnreachableAlts )
{
Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
// mimic actions of org.antlr.Tool first time for grammar g
if ( g.CodeGenerator == null )
{
CodeGenerator generator = new CodeGenerator( tool, g, "Java" );
g.CodeGenerator = generator;
g.BuildNFA();
g.CreateLookaheadDFAs( false );
}
DFA dfa = g.GetLookaheadDFA( decision );
Assert.IsNotNull(dfa, "unknown decision #" + decision);
FASerializer serializer = new FASerializer( g );
string result = serializer.Serialize( dfa.StartState );
//System.out.print(result);
var nonDetAlts = dfa.UnreachableAlts;
//System.out.println("alts w/o predict state="+nonDetAlts);
// first make sure nondeterministic alts are as expected
if ( expectingUnreachableAlts == null )
{
if ( nonDetAlts != null && nonDetAlts.Count != 0 )
{
Console.Error.WriteLine( "nondeterministic alts (should be empty): " + ( (IList)nonDetAlts ).ToElementString() );
}
Assert.AreEqual(0, nonDetAlts != null ? nonDetAlts.Count : 0, "unreachable alts mismatch");
}
else
{
for ( int i = 0; i < expectingUnreachableAlts.Length; i++ )
{
Assert.IsTrue(nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false, "unreachable alts mismatch");
}
}
Assert.AreEqual( expecting, result );
}