Пример #1
0
        Ast.TableConstructor ParseTableConstruct(ParseTreeNode node)
        {
            if (node.Term.Name == "TableConstruct")
            {
                if (node.ChildNodes.Count == 0)
                {
                    return(new Ast.TableConstructor()
                    {
                        Values = new Dictionary <Ast.IExpression, Ast.IExpression>()
                    });
                }
                else
                {
                    var child = node.ChildNodes[0];
                    Ast.TableConstructor t = new Ast.TableConstructor();
                    t.Values = new Dictionary <Ast.IExpression, Ast.IExpression>();

                    int i = 1;
                    while (true)
                    {
                        if (child.ChildNodes.Count == 0)
                        {
                            break;
                        }

                        var value = child.ChildNodes[0];

                        if (value.ChildNodes.Count == 1)
                        {
                            t.Values.Add(new Ast.NumberLiteral()
                            {
                                Value = i
                            }, ParseExpression(value.ChildNodes[0]));
                            i++;
                        }
                        else
                        {
                            var prefix = value.ChildNodes[0];

                            Ast.IExpression key;
                            if (prefix.ChildNodes[0].Term.Name == "identifier")
                            {
                                key = new Ast.StringLiteral()
                                {
                                    Value = prefix.ChildNodes[0].Token.ValueString
                                }
                            }
                            ;
                            else
                            {
                                key = ParseExpression(prefix.ChildNodes[0]);
                            }

                            var             expr = value.ChildNodes[1];
                            Ast.IExpression val  = ParseExpression(expr);

                            t.Values.Add(key, val);
                        }

                        //child = child.ChildNodes[1].ChildNodes[0];
                        child = child.ChildNodes[1];
                        if (child.ChildNodes.Count == 0)
                        {
                            break;
                        }
                        child = child.ChildNodes[0];
                    }
                    return(t);
                }
            }
            throw new Exception("Invalid TableConstruct node");
        }
Пример #2
0
        Ast.TableConstructor ParseTableConstruct(ParseTreeNode node)
        {
            if (node.Term.Name == "TableConstruct")
            {
                if (node.ChildNodes.Count == 0)
                {
                    return new Ast.TableConstructor() { Values = new Dictionary<Ast.IExpression, Ast.IExpression>() };
                }
                else
                {
                    var child = node.ChildNodes[0];
                    Ast.TableConstructor t = new Ast.TableConstructor();
                    t.Values = new Dictionary<Ast.IExpression, Ast.IExpression>();
                    
                    int i = 1;
                    while (true)
                    {
                        if (child.ChildNodes.Count == 0)
                            break;

                        var value = child.ChildNodes[0];

                        if (value.ChildNodes.Count == 1)
                        {
                            t.Values.Add(new Ast.NumberLiteral() { Value = i }, ParseExpression(value.ChildNodes[0]));
                            i++;
                        }
                        else
                        {
                            var prefix = value.ChildNodes[0];

                            Ast.IExpression key;
                            if (prefix.ChildNodes[0].Term.Name == "identifier")
                                key = new Ast.StringLiteral() { Value = prefix.ChildNodes[0].Token.ValueString };
                            else
                                key = ParseExpression(prefix.ChildNodes[0]);

                            var expr = value.ChildNodes[1];
                            Ast.IExpression val = ParseExpression(expr);

                            t.Values.Add(key, val);
                        }

                        //child = child.ChildNodes[1].ChildNodes[0];
                        child = child.ChildNodes[1];
                        if (child.ChildNodes.Count == 0)
                            break;
                        child = child.ChildNodes[0];
                    }
                    return t;
                }
            }
            throw new Exception("Invalid TableConstruct node");
        }