private static SubProduction RelopRules()
        {
            return(new SubProduction
                   (
                       new List <ExpressionDefinition>
            {
                new NonTerminalExpressionDefinition {
                    Identifier = ParserConstants.NumericExpression
                },
                new TerminalExpressionDefinition {
                    TokenType = TokenType.RelOp
                },
                new NonTerminalExpressionDefinition {
                    Key = "Factor2", Identifier = ParserConstants.NumericExpression
                },
                new SemanticActionDefinition((ParsingNode node) =>
                {
                    string relOpToken = node.GetAttributeForKey <WordToken>("RelOp", ParserConstants.Token).Lexeme;
                    RelOp relOp;

                    if (relOpToken == "==")
                    {
                        relOp = RelOp.Equals;
                    }
                    else if (relOpToken == "!=")
                    {
                        relOp = RelOp.NotEquals;
                    }
                    else if (relOpToken == "<")
                    {
                        relOp = RelOp.LessThan;
                    }
                    else if (relOpToken == "<=")
                    {
                        relOp = RelOp.LessOrEqualThan;
                    }
                    else if (relOpToken == ">")
                    {
                        relOp = RelOp.GreaterThan;
                    }
                    else if (relOpToken == ">=")
                    {
                        relOp = RelOp.GreaterOrEqualThan;
                    }
                    else
                    {
                        throw new Exception();
                    }

                    FactorASTNode left = node.GetAttributeForKey <FactorASTNode>(ParserConstants.NumericExpression, ParserConstants.SyntaxTreeNode);
                    FactorASTNode right = node.GetAttributeForKey <FactorASTNode>("Factor2", ParserConstants.SyntaxTreeNode);
                    RelOpASTNode syntaxTreeNode = new RelOpASTNode(left, relOp, right);

                    node.Attributes.Add(ParserConstants.SyntaxTreeNode, syntaxTreeNode);
                })
            }
                   ));
        }
Ejemplo n.º 2
0
        private object GetValueFromConditionASTNode(FactorASTNode node)
        {
            if (node is StringASTNode stringASTNode)
            {
                return(stringASTNode.Value);
            }
            else if (node is NumberASTNode intASTNode)
            {
                return(intASTNode.Value);
            }
            else if (node is SelectASTNode selectASTNode)
            {
                return(GetElementForTreeNode(selectASTNode));
            }

            return(null);
        }
Ejemplo n.º 3
0
        private object GetValueFromFactor(FactorASTNode x)
        {
            if (x is NumberASTNode numberASTNode)
            {
                return(numberASTNode.Value);
            }
            else if (x is StringASTNode stringASTNode)
            {
                return(stringASTNode.Value);
            }
            else if (x is BooleanASTNode booleanASTNode)
            {
                return(booleanASTNode.Value);
            }

            return(null);
        }
 private static SubProduction PlusRule()
 {
     return(new SubProduction
            (
                new List <ExpressionDefinition> {
         new NonTerminalExpressionDefinition {
             Key = "NumericExpression1", Identifier = ParserConstants.NumericExpression
         },
         new TerminalExpressionDefinition {
             TokenType = TokenType.Plus
         },
         new NonTerminalExpressionDefinition {
             Key = "NumericExpression2", Identifier = ParserConstants.NumericExpression
         },
         new SemanticActionDefinition((ParsingNode node) => {
             FactorASTNode left = node.GetAttributeForKey <FactorASTNode>("NumericExpression1", ParserConstants.SyntaxTreeNode);
             FactorASTNode right = node.GetAttributeForKey <FactorASTNode>("NumericExpression2", ParserConstants.SyntaxTreeNode);
             AdditionASTNode syntaxTreeNode = new AdditionASTNode(left, right);
             node.Attributes.Add(ParserConstants.SyntaxTreeNode, syntaxTreeNode);
         })
     }
            ));
 }
Ejemplo n.º 5
0
        public static void Initialize(ref Grammar grammar)
        {
            grammar.Add(new Production(ParserConstants.Statement,
                                       new List <SubProduction>
            {
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.OpenStatement
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        node.Attributes[ParserConstants.SyntaxTreeNode] = node.GetAttributeForKey <SyntaxTreeNode>(ParserConstants.OpenStatement, ParserConstants.SyntaxTreeNode);
                    })
                }
                ),
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.ClosedStatement
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        node.Attributes[ParserConstants.SyntaxTreeNode] = node.GetAttributeForKey <SyntaxTreeNode>(ParserConstants.ClosedStatement, ParserConstants.SyntaxTreeNode);
                    })
                }
                )
            }
                                       ));

            grammar.Add(new Production(ParserConstants.OpenStatement,
                                       new List <SubProduction>
            {
                If(),
                IfElse()
            }
                                       ));

            grammar.Add(new Production(ParserConstants.ClosedStatement,
                                       new List <SubProduction>
            {
                IfElseClosed(),
                Declaration(),
                Codeblock(),
                Assignment(),
                FunctionCall(),
                Return(),
                While()
            }
                                       ));

            grammar.Add(new Production(ParserConstants.Factors,
                                       new List <SubProduction>()
            {
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.Factors
                    },
                    new TerminalExpressionDefinition {
                        TokenType = TokenType.Comma
                    },
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.Factor
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        List <FactorASTNode> result = new List <FactorASTNode>();

                        List <FactorASTNode> factors = node.GetAttributeForKey <List <FactorASTNode> >(ParserConstants.Factors, Factors);
                        FactorASTNode factor         = node.GetAttributeForKey <FactorASTNode>(ParserConstants.Factor, ParserConstants.SyntaxTreeNode);

                        result.AddRange(factors);
                        result.Add(factor);

                        node.Attributes.Add(Factors, result);
                    })
                }
                ),
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.Factor
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        List <FactorASTNode> factors = new List <FactorASTNode>();
                        FactorASTNode factor         = node.GetAttributeForKey <FactorASTNode>(ParserConstants.Factor, ParserConstants.SyntaxTreeNode);
                        factors.Add(factor);
                        node.Attributes.Add(Factors, factors);
                    })
                }
                ),
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new TerminalExpressionDefinition {
                        TokenType = TokenType.EmptyString
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        node.Attributes.Add(Factors, new List <FactorASTNode>());
                    })
                }
                )
            }
                                       ));
        }
Ejemplo n.º 6
0
        public static void Initialize(ref Grammar grammar)
        {
            grammar.Add(new Production(ParserConstants.Factor,
                                       new List <SubProduction>
            {
                ParenthesisRule(),
                FactorTermRule()
            }
                                       ));

            grammar.Add(new Production(ParserConstants.FactorTerm,
                                       new List <SubProduction>
            {
                IdentifierRule(),
                //BooleanRule(),
                NumExpressionRule(),
                StringRule(),
                SubQueryRule()
            }
                                       ));

            grammar.Add(new Production(ParserConstants.Factors,
                                       new List <SubProduction>()
            {
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.Factors
                    },
                    new TerminalExpressionDefinition {
                        TokenType = TokenType.Comma
                    },
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.Factor
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        List <FactorASTNode> result = new List <FactorASTNode>();

                        List <FactorASTNode> factors = node.GetAttributeForKey <List <FactorASTNode> >(ParserConstants.Factors, ParserConstants.Factors);
                        FactorASTNode factor         = node.GetAttributeForKey <FactorASTNode>(ParserConstants.Factor, ParserConstants.SyntaxTreeNode);

                        result.AddRange(factors);
                        result.Add(factor);

                        node.Attributes.Add(ParserConstants.Factors, result);
                    })
                }
                ),
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new NonTerminalExpressionDefinition {
                        Identifier = ParserConstants.Factor
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        List <FactorASTNode> factors = new List <FactorASTNode>();
                        FactorASTNode factor         = node.GetAttributeForKey <FactorASTNode>(ParserConstants.Factor, ParserConstants.SyntaxTreeNode);
                        factors.Add(factor);
                        node.Attributes.Add(ParserConstants.Factors, factors);
                    })
                }
                ),
                new SubProduction
                (
                    new List <ExpressionDefinition>
                {
                    new TerminalExpressionDefinition {
                        TokenType = TokenType.EmptyString
                    },
                    new SemanticActionDefinition((ParsingNode node) =>
                    {
                        node.Attributes.Add(ParserConstants.Factors, new List <FactorASTNode>());
                    })
                }
                )
            }
                                       ));
        }