Exemple #1
0
        public void Define()
        {
            // the valid modifiers for an enum: public or internal
            var validModifiers = new NonTerminal("enum_valid_modifiers");

            validModifiers.Rule = grammar.Keywords.Public
                                  | grammar.Keywords.Internal;

            grammar.MarkTransient(validModifiers);

            // Enums can have no or multiple modifiers
            var modifier = new NonTerminal("enum_modifiers");

            modifier.Rule = grammar.MakeStarRule(modifier, validModifiers);

            // A constant that simply a name, and that will have a value automatically assigned.
            var identifierConstant = new NonTerminal("identifier_constant");

            identifierConstant.Rule = grammar.Identifier + grammar.Eos;

            // A constant that is assigned a value by the programmer
            var assignmentConstant = new NonTerminal("assignment_constant");

            assignmentConstant.Rule = grammar.Identifier + grammar.ToTerm("=") + grammar.Literals.NumberLiteral + grammar.Eos;

            // List of valid members of an enum: the previous two constant rules.
            var enumValidMembers = new NonTerminal("enum_valid_members");

            enumValidMembers.Rule = identifierConstant | assignmentConstant;

            // The list of members in the enum.
            var enumMemberList = new NonTerminal("enum_member_list");

            enumMemberList.Rule = grammar.MakeStarRule(enumMemberList, enumValidMembers);

            // The enum body is an indent, the list of members, and a dedent.
            var enumBody = new NonTerminal("enum_body");

            enumBody.Rule = grammar.Indent + enumMemberList + grammar.Dedent;
            grammar.MarkTransient(enumBody);

            // The enum body is optional
            var enumListOpt = new NonTerminal("enum_list_opt");

            enumListOpt.Rule = grammar.Empty | enumBody;

            /* enum foo:
             *       bar
             *      bar2 = 24
             */
            EnumDeclaration.Rule = modifier
                                   + grammar.Keywords.Enum
                                   + grammar.Identifier
                                   + grammar.ToTerm(":")
                                   + grammar.Eos
                                   + enumListOpt;
        }
        public void Define()
        {
            var validParameters      = new NonTerminal("valid_parameters");
            var simpleParameter      = new NonTerminal("simple_parameter");
            var outParameter         = new NonTerminal("out_parameter");
            var arrayParameter       = new NonTerminal("array_parameter");
            var genericParameter     = new NonTerminal("generic_parameter");
            var methodStatementsList = new NonTerminal("method_statements_list");
            var methodBody           = new NonTerminal("method_body");
            var genericTypeListEos   = new NonTerminal("generic_type_list_eos");

            // Comma separated list of zero or more valid method parameters
            ParameterList.Rule = grammar.MakeStarRule(ParameterList, grammar.ToTerm(","), validParameters);
            // Valid parameters are simple ones: "int i", arrays "int [] i", directioned "ref int i" or generic "List{int} i"
            validParameters.Rule  = simpleParameter | outParameter | arrayParameter | genericParameter;
            simpleParameter.Rule  = grammar.Identifier + grammar.Identifier;
            outParameter.Rule     = (grammar.Keywords.Out | grammar.Keywords.Ref) + grammar.Identifier + grammar.Identifier;
            arrayParameter.Rule   = grammar.Identifier + grammar.ToTerm("[") + grammar.ToTerm("]") + grammar.Identifier;
            genericParameter.Rule = grammar.GenericIdentifier + grammar.Identifier;

            var validModifiers = new NonTerminal("valid_method_modifiers");

            validModifiers.Rule = grammar.Keywords.Public
                                  | grammar.Keywords.Internal
                                  | grammar.Keywords.Abstract
                                  | grammar.Keywords.Shared
                                  | grammar.Keywords.Protected
                                  | grammar.Keywords.Private
                                  | grammar.Keywords.Virtual
                                  | grammar.Keywords.Final
                                  | grammar.Keywords.Override;
            grammar.MarkTransient(validModifiers);

            // Modifiers are zero or more from the list of valid ones.
            Modifier.Rule = grammar.MakeStarRule(Modifier, validModifiers);

            // "member header" is a Modifier followed by two identifiers.
            MemberHeader.Rule = Modifier + grammar.TwoIdentifiers;

            // Method body is a list of statements between an indent and a dedent.
            methodBody.Rule         = grammar.Indent + methodStatementsList + grammar.Dedent;
            OptionalMethodBody.Rule = grammar.Empty | methodBody;

            // The list of statements in a method is a list of zero or more expressions.
            methodStatementsList.Rule = grammar.MakeStarRule(methodStatementsList, grammar.expression);

            MethodDeclaration.Rule =
                MemberHeader
                + grammar.Classes.GenericTypeListOpt
                + grammar.OpenParenthese
                + ParameterList
                + grammar.CloseParenthese
                + grammar.ToTerm(":")
                + grammar.Eos
                + OptionalMethodBody;
        }
Exemple #3
0
        public void Define()
        {
            // import System, System.IO
            Import.Rule = grammar.Keywords.Import + grammar.Lists.StarIdentifierList + grammar.Eos;

            // The namespace body is optional.
            var namespaceBodyOpt = new NonTerminal("namespace_body_opt");

            namespaceBodyOpt.Rule = (NamespaceBody | grammar.Empty);
            grammar.MarkTransient(NamespaceBody);

            // namespace foo:
            NamespaceDeclaration.Rule = grammar.Keywords.NamespaceKeyword
                                        + grammar.Identifier
                                        + grammar.ToTerm(":")
                                        + grammar.Eos
                                        + namespaceBodyOpt;

            // Valid namespace members are: namespaces, imports, enums, delegates, interfaces, classes
            NamespaceValidMembers.Rule = NamespaceDeclaration
                                         | grammar.Classes.ClassDeclaration
                                         | Import
                                         | grammar.Enums.EnumDeclaration
                                         | grammar.Delegates.DelegateDeclaration
                                         | grammar.Interfaces.InterfaceDeclaration;
            grammar.MarkTransient(NamespaceValidMembers);

            // The list of members in the namespace.
            NamespaceMembersList.Rule = grammar.MakeStarRule(NamespaceMembersList, NamespaceValidMembers);
            grammar.MarkTransient(NamespaceMembersList);

            // The body of the namespace is an indent, a member list, and a dedent.
            NamespaceBody.Rule =
                grammar.Indent
                + NamespaceMembersList
                + grammar.Dedent;
        }
Exemple #4
0
        // Define the rules (declaration and definition must be separate).
        public void Define()
        {
            // List of base types
            var baseTypeList = new NonTerminal("base_type_list");
            // List of members in the class body
            var membersList = new NonTerminal("class_members_list");
            // The members that may go into a member list.
            var validMembers = new NonTerminal("class_valid_members");
            // The optional body of the class
            var memberBodyOpt = new NonTerminal("member_body_opt");
            // The modifiers that may make up a class.
            var validClassModifiers = new NonTerminal("valid_class_modifiers");


            // The rule describing which modifiers can be applied to a class variable.
            var classVariableValidModifers = new NonTerminal("classVariableValidModifiers");

            classVariableValidModifers.Rule = grammar.Keywords.Public
                                              | grammar.Keywords.Private
                                              | grammar.Keywords.Protected
                                              | grammar.Keywords.Internal
                                              | grammar.Keywords.Final
                                              | grammar.Keywords.Shared;

            // A list rule is needed as there can be more than one modifier.
            ClassVariableModifierList.Rule = grammar.MakeStarRule(ClassVariableModifierList, classVariableValidModifers);

            // The rule to declare a class varible: public int foo
            var classVariable = new NonTerminal("class_variable");

            classVariable.Rule = grammar.MethodDeclarations.MemberHeader + grammar.Eos;

            // The rule describing which modifiers can be used for a class declaration
            validClassModifiers.Rule = grammar.Keywords.Public
                                       | grammar.Keywords.Internal
                                       | grammar.Keywords.Final
                                       | grammar.Keywords.Partial
                                       | grammar.Keywords.Abstract;
            grammar.MarkTransient(validClassModifiers);

            // A list rule since there can be more than on modifier.
            Modifier.Rule = grammar.MakeStarRule(Modifier, validClassModifiers);

            // Could be a class, struct, or module
            var classStructOrModule = new NonTerminal("class_or_module");

            classStructOrModule.Rule = grammar.Keywords.Class | grammar.Keywords.Module | grammar.Keywords.Struct;
            grammar.MarkTransient(classStructOrModule);

            // (basetype1, basetype2)
            baseTypeList.Rule = grammar.OpenParenthese + grammar.Lists.PlusIdentifierList + grammar.CloseParenthese;

            // Base type list is optional: it may be empty
            BaseTypeListOpt.Rule = grammar.Empty | baseTypeList;

            // {T,L}
            GenericTypeList.Rule = grammar.ToTerm("{") + grammar.Lists.PlusIdentifierList + grammar.ToTerm("}");

            // Generic type list is optional.
            GenericTypeListOpt.Rule = grammar.Empty | GenericTypeList;

            // Class body is a list of members between an indent and a dedent
            var memberBody = new NonTerminal("member_body");

            memberBody.Rule = (grammar.Indent + membersList + grammar.Dedent);
            grammar.MarkTransient(memberBody);

            // member list is optional: it may be empty.
            var optionalMembersList = new NonTerminal("optional_members_list");

            optionalMembersList.Rule = grammar.Empty | membersList;

            // Body of the class is optional
            memberBodyOpt.Rule = grammar.Empty | memberBody;

            /* class foo{T,K}(basetype1,basetype2}:
             *      memberlist
             */
            ClassDeclaration.Rule = Modifier
                                    + classStructOrModule
                                    + grammar.Identifier
                                    + GenericTypeListOpt
                                    + BaseTypeListOpt
                                    + grammar.ToTerm(":")
                                    + grammar.Eos
                                    + memberBodyOpt;

            // Valid class members are methods, constructors, variables, properties, and events.
            validMembers.Rule = grammar.MethodDeclarations.MethodDeclaration
                                | classVariable
                                | grammar.Constructors.ConstructorDeclaration
                                | grammar.Properties.PropertyDeclaration
                                | grammar.Delegates.EventMember;
            membersList.Rule = grammar.MakeStarRule(membersList, validMembers);
        }
Exemple #5
0
 public void Define()
 {
     StarIdentifierList.Rule = grammar.MakeStarRule(StarIdentifierList, grammar.ToTerm(","), grammar.Identifier);
     PlusIdentifierList.Rule = grammar.MakePlusRule(PlusIdentifierList, grammar.ToTerm(","), grammar.Identifier);
     grammar.MarkTransient(StarIdentifierList);
 }
Exemple #6
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");
        }