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; }
public void Define() { /* case <expression>: * <expression> */ var caseBlock = new NonTerminal("case_block"); caseBlock.Rule = grammar.Keywords.Case + grammar.expression + grammar.ToTerm(":") + grammar.Eos + grammar.MethodDeclarations.OptionalMethodBody; // List of case block expressions. var caseBlockList = new NonTerminal("case_block_list"); caseBlockList.Rule = grammar.MakeStarRule(caseBlockList, caseBlock); // The list of case block expressions is optional. var caseBlockListOpt = new NonTerminal("case_block_list_opt"); caseBlockListOpt.Rule = (grammar.Indent + caseBlockList + grammar.Dedent) | grammar.Empty; /* switch <expression>: * case <expression>: * <expression> */ SwitchBlock.Rule = grammar.Keywords.Switch + grammar.expression + grammar.ToTerm(":") + grammar.Eos + caseBlockListOpt; }
public void Define() { // throw <expression> ThrowStatement.Rule = grammar.Keywords.Throw + grammar.expression + (grammar.Eos | grammar.Empty); // try: var tryBlock = new NonTerminal("try_block"); tryBlock.Rule = grammar.Keywords.Try + grammar.ToTerm(":") + grammar.Eos + grammar.MethodDeclarations.OptionalMethodBody; // catch Exception e: var catchBlock = new NonTerminal("catch_block"); catchBlock.Rule = grammar.Keywords.Catch + grammar.Identifier + grammar.Identifier + grammar.ToTerm(":") + grammar.Eos + grammar.MethodDeclarations.OptionalMethodBody; // The catch blocks are optional var catchBlockOpt = new NonTerminal("catch_block_opt"); catchBlockOpt.Rule = grammar.MakeStarRule(catchBlockOpt, catchBlock); // finally: var finallyBlock = new NonTerminal("finally_block"); finallyBlock.Rule = grammar.Keywords.Finally + grammar.ToTerm(":") + grammar.Eos + grammar.MethodDeclarations.OptionalMethodBody; // The finally block is optional var finallyBlockOpt = new NonTerminal("finally_block_opt"); finallyBlockOpt.Rule = finallyBlock | grammar.Empty; /* * try: * catch Exception ex: * finally: */ ExceptionHandlingBlock.Rule = tryBlock + catchBlockOpt + finallyBlockOpt; }
public void Define() { // A method interface contracted by the interface var methodInterface = new NonTerminal("method_interface"); methodInterface.Rule = grammar.TwoIdentifiers + grammar.Classes.GenericTypeListOpt + grammar.OpenParenthese + grammar.MethodDeclarations.ParameterList + grammar.CloseParenthese + grammar.Eos; // A property interface contracted by the interface var propertyInterface = new NonTerminal("property_interface"); propertyInterface.Rule = grammar.TwoIdentifiers + grammar.Eos; // Valid inteface members are the method and property interfaces var validInterfaceMembers = new NonTerminal("valid_interface_members"); validInterfaceMembers.Rule = methodInterface | propertyInterface; // The member list can be zero or more of the previous var interfaceMembersList = new NonTerminal("interface_members"); interfaceMembersList.Rule = grammar.MakeStarRule(interfaceMembersList, validInterfaceMembers); // The member body is an indent, the member list, and a dedent. var interfaceBody = new NonTerminal("interface_body"); interfaceBody.Rule = grammar.Indent + interfaceMembersList + grammar.Dedent; // The body is optional. var interfaceBodyOpt = new NonTerminal("interface_body_opt"); interfaceBodyOpt.Rule = interfaceBody | grammar.Empty; InterfaceDeclaration.Rule = grammar.Classes.Modifier + grammar.Keywords.Interface + grammar.Identifier + grammar.Classes.GenericTypeListOpt + grammar.Classes.BaseTypeListOpt + grammar.ToTerm(":") + grammar.Eos + interfaceBodyOpt; }
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; }
// 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); }
public void Define() { StarIdentifierList.Rule = grammar.MakeStarRule(StarIdentifierList, grammar.ToTerm(","), grammar.Identifier); PlusIdentifierList.Rule = grammar.MakePlusRule(PlusIdentifierList, grammar.ToTerm(","), grammar.Identifier); grammar.MarkTransient(StarIdentifierList); }