Ejemplo n.º 1
0
        public override object Parse(IEnumerable <Token <string> > tokens, string input)
        {
            var _input = tokens.Concat(new [] { baseToken });

            stateStack.Clear();
            stateStack.Push(baseToken);
            stateStack.Push((uint)0);
            Queue <Token <string> > _i = new Queue <Token <string> >(_input);
            var  a      = _i.Dequeue();
            uint s      = 0;
            var  result = table[a.TokenType][(int)s];

            while (true)
            {
                //object __input = stateStack.Peek();
                //Console.WriteLine("typeof input is {0}", __input.GetType());
                s      = (uint)stateStack.Peek();
                result = table[a.TokenType][(int)s];
                IEnumerable <uint> encoding = enc.Decode(result);
                TableCellAction    action   = (TableCellAction)encoding.ElementAt(0);
                uint targetRule             = encoding.ElementAt(1);
                uint targetProduction       = encoding.ElementAt(2);
                switch (action)
                {
                case TableCellAction.Shift:
                    stateStack.Push(a);
                    stateStack.Push(targetProduction);
                    a = _i.Dequeue();
                    break;

                case TableCellAction.Reduce:
                    var      rule      = TargetGrammar.LookupRule(Encode(targetRule, targetProduction));
                    var      firstRule = rule[0];
                    int      count     = firstRule.Count; //not the rule's length but the rule's first production's length
                    object[] elements  = new object[count];
                    for (int i = 0; i < count; i++)
                    {
                        stateStack.Pop(); //targetState
                        elements[i] = stateStack.Pop();
                    }
                    var element = (uint)stateStack.Peek();
                    //we just do the goto anyway
                    var next     = enc.Decode(table[rule.Name][(int)element]);
                    var quantity = firstRule.Rule(elements.Reverse().ToArray());
                    stateStack.Push(quantity);
                    stateStack.Push(next.ElementAt(2));
                    break;

                case TableCellAction.Accept:
                    if (!SupressMessages)
                    {
                        Console.WriteLine("State Stack Contents before accept");
                        foreach (var v in stateStack)
                        {
                            Console.WriteLine(v);
                        }
                    }
                    stateStack.Pop();
                    return(onAccept(new object[] { stateStack.Pop() }));

                case TableCellAction.Error:
                    Console.WriteLine("Error Occurred at position {0}, {1}", a.Start, a.Value);
                    Console.WriteLine("{0}", input.Substring(0, a.Start + 1));
                    return(false);
                }
            }
        }
Ejemplo n.º 2
0
        protected object CoreParse(IEnumerable <Token <string> > tokens, string input,
                                   bool printErrorMessages, bool invokeRules)
        {
            var _input = tokens.Concat(new [] { baseToken });

            stateStack.Clear();
            stateStack.Push(baseToken);
            stateStack.Push(0);
            Queue <Token <string> > _i = new Queue <Token <string> >(_input);
            var a      = _i.Dequeue();
            int s      = 0;
            var result = actionTable[s, a.TokenType];

            while (true)
            {
                s      = (int)stateStack.Peek();
                result = actionTable[s, a.TokenType];
                switch (result.Action)
                {
                case TableCellAction.Shift:
                    stateStack.Push(a);
                    stateStack.Push(result.TargetState);
                    a = _i.Dequeue();
                    break;

                case TableCellAction.Reduce:
                    var      rule      = TargetGrammar.LookupRule(result.TargetState);
                    var      firstRule = rule[0];
                    int      count     = firstRule.Count; //not the rule's length but the rule's first production's length
                    object[] elements  = new object[count];
                    for (int i = 0; i < count; i++)
                    {
                        stateStack.Pop(); //targetState
                        elements[i] = stateStack.Pop();
                    }
                    var element = (int)stateStack.Peek();
                    var next    = gotoTable[element, rule.Name];
                    if (invokeRules)
                    {
                        var quantity = firstRule.Rule(elements.Reverse().ToArray());
                        stateStack.Push(quantity);
                    }
                    else
                    {
                        stateStack.Push(new object());
                    }
                    stateStack.Push(next);
                    break;

                case TableCellAction.Accept:
                    if (!SupressMessages)
                    {
                        Console.WriteLine("State Stack Contents before accept");
                        foreach (var v in stateStack)
                        {
                            Console.WriteLine(v);
                        }
                    }
                    stateStack.Pop();
                    if (invokeRules)
                    {
                        return(onAccept(new object[] { stateStack.Pop() }));
                    }
                    else
                    {
                        return(true);
                    }

                case TableCellAction.Error:
                    if (printErrorMessages)
                    {
                        Console.WriteLine("Error Occurred at position {0}, {1}", a.Start, a.Value);
                        Console.WriteLine("{0}", input.Substring(0, a.Start + 1));
                    }
                    return(false);
                }
            }
        }