예제 #1
0
        public void Define()
        {
            // The rule describing all valid binary operators.
            ValidBinaryOperators.Rule = Add
                                        | Modulo
                                        | Divide
                                        | LessOrEqual
                                        | Less
                                        | Greater
                                        | GreaterOrEqual
                                        | Equal
                                        | NotEqual
                                        | grammar.Keywords.And
                                        | grammar.Keywords.Or
                                        | BitwiseShiftRight
                                        | BitwiseShiftLeft
                                        | BitwiseOr
                                        | BitwiseAnd
                                        | BitwiseXor
                                        | Subtract
                                        | Multiply
                                        | As;
            grammar.MarkTransient(ValidBinaryOperators);

            // Set operators precedence and associativity, in increasing order of precedence.
            int precedence = 1;

            grammar.RegisterOperators(precedence += 1, Associativity.Right, "=", "+=", "-=", "/=", "*=", "|=", "&=", "^=");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "||");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "&&");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "|");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "^");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "&");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "==", "!=");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "<", ">", ">=", "<=");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "<<", ">>");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "+", "-");
            grammar.RegisterOperators(precedence += 1, Associativity.Left, "*", "/", "%");
            grammar.RegisterOperators(precedence += 1, Associativity.Right, "!", "-");
            grammar.RegisterOperators(precedence += 1, As); // Casting

            // Set comma and closing brace as operators so that lists can continue on the next line.
            grammar.RegisterOperators(0, grammar.ToTerm("}"));
            grammar.RegisterOperators(0, grammar.ToTerm(","));

            // An operator expression: two expressions with the operator between
            BinaryOperator.Rule = grammar.expression + ValidBinaryOperators + grammar.expression;

            // The list of valid assignment operators: +=, -=, *=, /=, |*, &=, ^=
            var validAssigments = new NonTerminal("valid_assigments");

            validAssigments.Rule = EqualsAssignment
                                   | AddAssignment
                                   | SubtractAssignment
                                   | MultiplyAssignment
                                   | DivideAssignment
                                   | OrAssignment
                                   | AndAssignment
                                   | XorAssignment;
            grammar.MarkTransient(validAssigments);

            // Assignment expression is an expression that can be assigned to, the operator, and the assigned expression.
            Assignment.Rule = grammar.Assignable + validAssigments + grammar.expression + (grammar.Empty | grammar.Eos);

            // Valid unary operators.
            ValidUnaryOperators.Rule = Not | Negate;

            // Unary operator + expression
            UnaryOperator.Rule = ValidUnaryOperators + grammar.expression + (grammar.Empty | grammar.Eos);

            grammar.MarkReservedWords("As");
        }