C# System.InvalidCastException when calling RecognitionException.GetExpectedTokens()

43 views
Skip to first unread message

Roberto Mencia

unread,
Apr 16, 2015, 12:34:29 AM4/16/15
to antlr-di...@googlegroups.com
I've got this exception when trying to get the expected tokens from a recognition exception.
I also traced the exception to this point of the code.

System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Unable to cast object of type 'Antlr4.Runtime.Atn.EpsilonTransition' to type 'Antlr4.Runtime.Atn.RuleTransition'.
  Source=Antlr4.Runtime
  StackTrace:
       at Antlr4.Runtime.Atn.ATN.GetExpectedTokens(Int32 stateNumber, RuleContext context)
       at Antlr4.Runtime.RecognitionException.GetExpectedTokens()



public class ATN
{
...
public virtual IntervalSet GetExpectedTokens(int stateNumber, RuleContext context)
        {
            if (stateNumber < 0 || stateNumber >= states.Count)
            {
                throw new ArgumentException("Invalid state number.");
            }
            RuleContext ctx = context;
            ATNState s = states[stateNumber];
            IntervalSet following = NextTokens(s);
            if (!following.Contains(TokenConstants.Epsilon))
            {
                return following;
            }
            IntervalSet expected = new IntervalSet();
            expected.AddAll(following);
            expected.Remove(TokenConstants.Epsilon);
            while (ctx != null && ctx.invokingState >= 0 && following.Contains(TokenConstants.Epsilon))
            {
                ATNState invokingState = states[ctx.invokingState];
                RuleTransition rt = (RuleTransition)invokingState.Transition(0);
                following = NextTokens(rt.followState);
                expected.AddAll(following);
                expected.Remove(TokenConstants.Epsilon);
                ctx = ctx.parent;
            }
            if (following.Contains(TokenConstants.Epsilon))
            {
                expected.Add(TokenConstants.Eof);
            }
            return expected;
        }

Eric Vergnaud

unread,
Apr 16, 2015, 12:43:56 PM4/16/15
to antlr-di...@googlegroups.com
Hi,

file a bug with versioning and a reproducible example at:
 - antlr4-csharp, if you're using the runtime from the antlr organization
 - antlr4cs, if you're using the Nuget version with Visual Studio

Eric
Reply all
Reply to author
Forward
0 new messages