public static StructOrUnionSpec Create(StructOrUnion structOrUnion, Option <String> name, ImmutableList <StructDecln> memberDeclns) =>
 new StructOrUnionSpec(structOrUnion, name, Option.Some(memberDeclns));
 public static StructOrUnionSpec Create(StructOrUnion structOrUnion, String name) =>
 new StructOrUnionSpec(structOrUnion, Option.Some(name), Option <ImmutableList <StructDecln> > .None);
 private StructOrUnionSpec(StructOrUnion structOrUnion, Option <String> name, Option <ImmutableList <StructDecln> > memberDeclns)
 {
     this.StructOrUnion = structOrUnion;
     this.Name          = name;
     this.MemberDeclns  = memberDeclns;
 }
 public static StructOrUnionSpec Create(StructOrUnion structOrUnion, String name) =>
     new StructOrUnionSpec(structOrUnion, Option.Some(name), Option<ImmutableList<StructDecln>>.None);
 public static StructOrUnionSpec Create(StructOrUnion structOrUnion, Option<String> name, ImmutableList<StructDecln> memberDeclns) =>
     new StructOrUnionSpec(structOrUnion, name, Option.Some(memberDeclns));
 private StructOrUnionSpec(StructOrUnion structOrUnion, Option<String> name, Option<ImmutableList<StructDecln>> memberDeclns) {
     this.StructOrUnion = structOrUnion;
     this.Name = name;
     this.MemberDeclns = memberDeclns;
 }
Example #7
0
        public static void SetDeclarationRules()
        {
            // declaration
            //   : declaration-specifiers [Init-declarator-list]? ';'
            Declaration.Is(
                (DeclarationSpecifiers)
                .Then(InitDeclaratorList.Optional(ImmutableList <InitDeclr> .Empty))
                .Then(Semicolon)
                .Then(Decln.Create)
                .TransformResult(
                    _ => {
                var result = _.Result;
                var env    = _.Environment;
                env        = result.InitDeclrs.Aggregate(env, (currentEnv, initDeclr) =>
                                                         currentEnv.AddSymbol(
                                                             initDeclr.Declr.Name,
                                                             result.DeclnSpecs.StorageClsSpecs.DefaultIfEmpty(StorageClsSpec.AUTO).First()
                                                             )
                                                         );
                return(ParserSucceeded.Create(result, env, _.Source));
            }
                    )
                );

            // declaration-specifiers
            //   : [ storage-class-specifier | Type-specifier | Type-qualifier ]+
            DeclarationSpecifiers.Is(
                Parser.Seed(DeclnSpecs.Empty)
                .Then(
                    Either(
                        Given <DeclnSpecs>()
                        .Then(StorageClassSpecifier)
                        .Then(DeclnSpecs.Add)
                        ).Or(
                        Given <DeclnSpecs>()
                        .Then(TypeSpecifier)
                        .Then(DeclnSpecs.Add)
                        ).Or(
                        Given <DeclnSpecs>()
                        .Then(TypeQualifier)
                        .Then(DeclnSpecs.Add)
                        ).OneOrMore()
                    )
                );

            // Init-declarator-list
            //   : Init-declarator [ ',' Init-declarator ]*
            InitDeclaratorList.Is(
                InitDeclarator.OneOrMore(Comma)
                );

            // Init-declarator
            //   : declarator [ '=' initializer ]?
            InitDeclarator.Is(
                (Declarator)
                .Then(
                    (Assign).Then(Initializer).Optional()
                    ).Then(InitDeclr.Create)
                );

            // storage-class-specifier
            //   : auto | register | static | extern | typedef
            StorageClassSpecifier.Is(
                Either(Auto)
                .Or(Register)
                .Or(Static)
                .Or(Extern)
                .Or(Typedef)
                );

            // Type-specifier
            //   : void
            //   | char
            //   | short
            //   | int
            //   | long
            //   | float
            //   | double
            //   | signed
            //   | unsigned
            //   | struct-or-union-specifier
            //   | enum-specifier
            //   | typedef-name
            TypeSpecifier.Is(
                Either(
                    Either(Void)
                    .Or(Char)
                    .Or(Short)
                    .Or(Int)
                    .Or(Long)
                    .Or(Float)
                    .Or(Double)
                    .Or(Signed)
                    .Or(Unsigned)
                    .Then(kind => new BasicTypeSpec(kind) as TypeSpec)
                    )
                .Or(StructOrUnionSpecifier)
                .Or(EnumSpecifier)
                .Or(TypeDefName.Then(TypedefName.Create))
                );

            // type_qualifier
            //   : const
            //   | volatile
            TypeQualifier.Is(
                Either(Const).Or(Volatile)
                );

            // declarator
            //   : [pointer]? direct-declarator
            Declarator.Is(
                (Pointer.Optional())
                .Then(DirectDeclarator)
                .Then(Declr.Create)
                );

            // pointer
            //   : [ '*' [Type-qualifier-list]? ]+
            Pointer.Is(
                (
                    Mult.
                    Then(TypeQualifierList.Optional(ImmutableList <TypeQual> .Empty))
                    .Then(PointerModifier.Create)
                ).OneOrMore()
                .Then(pointerModifiers => pointerModifiers.Reverse())
                );

            // parameter-Type-list
            //   : parameter-list [ ',' '...' ]?
            ParameterTypeList.Is(
                ParameterList
                .Then(
                    (Comma)
                    .Then(Period).Then(Period).Then(Period)
                    .Optional()
                    ).Then(ParamTypeList.Create)
                );

            // parameter-list
            //   : parameter-declaration [ ',' parameter-declaration ]*
            ParameterList.Is(
                ParameterDeclaration.OneOrMore(Comma)
                );

            // Type-qualifier-list
            //   : [Type-qualifier]+
            TypeQualifierList.Is(
                TypeQualifier.OneOrMore()
                );

            // direct-declarator
            //   : [
            //         identifier | '(' declarator ')'
            //     ] [
            //         '[' [constant-expression]? ']'
            //       | '(' [parameter-Type-list]? ')'
            //     ]*
            DirectDeclarator.Is(
                (
                    Either(
                        (Identifier).Then(Declr.Create)
                        ).Or(
                        (LeftParen).Then(Declarator).Then(RightParen)
                        )
                ).Then(
                    Either(
                        Given <Declr>()
                        .Then(LeftBracket)
                        .Then(
                            ConstantExpression.Optional().Then(ArrayModifier.Create)
                            ).Then(RightBracket)
                        .Then(Declr.Add)
                        ).Or(
                        Given <Declr>()
                        .Then(LeftParen)
                        .Then(
                            ParameterTypeList
                            .Optional()
                            .Then(FunctionModifier.Create)
                            ).Then(RightParen)
                        .Then(Declr.Add)
                        )
                    .ZeroOrMore()
                    )
                );

            // enum-specifier
            //   : enum [identifier]? '{' enumerator-list '}'
            //   | enum identifier
            EnumSpecifier.Is(
                (Enum)
                .Then(
                    Either(
                        Identifier.Optional()
                        .Then(LeftCurlyBrace)
                        .Then(EnumeratorList)
                        .Then(RightCurlyBrace)
                        .Then(EnumSpec.Create)
                        ).Or(
                        (Identifier)
                        .Then(EnumSpec.Create)
                        )
                    )
                );

            // enumerator-list
            //   : enumerator [ ',' enumerator ]*
            EnumeratorList.Is(
                Enumerator.OneOrMore(Comma)
                );

            // enumerator
            //   : enumeration-constant [ '=' constant-expression ]?
            Enumerator.Is(
                EnumerationConstant
                .Then(
                    (Assign)
                    .Then(ConstantExpression)
                    .Optional()
                    ).Then(Enumr.Create)
                );

            // enumeration-constant
            //   : identifier
            EnumerationConstant.Is(
                Identifier
                );

            // struct-or-union-specifier
            //   : struct-or-union [identifier]? { struct-declaration-list }
            //   | struct-or-union identifier
            StructOrUnionSpecifier.Is(
                (StructOrUnion)
                .Then(
                    Either(
                        Given <StructOrUnion>()
                        .Then(Identifier.Optional())
                        .Then(LeftCurlyBrace)
                        .Then(StructDeclarationList)
                        .Then(RightCurlyBrace)
                        .Then(StructOrUnionSpec.Create)
                        ).Or(
                        Given <StructOrUnion>()
                        .Then(Identifier)
                        .Then(StructOrUnionSpec.Create)
                        )
                    )
                );

            // struct-or-union
            //   : struct | union
            StructOrUnion.Is(
                Either(Struct).Or(Union)
                );

            // struct-declaration-list
            //   : [struct-declaration]+
            StructDeclarationList.Is(
                StructDeclaration.OneOrMore()
                );

            // struct-declaration
            //   : specifier-qualifier-list struct-declarator-list ';'
            StructDeclaration.Is(
                (SpecifierQualifierList)
                .Then(StructDeclaratorList)
                .Then(Semicolon)
                .Then(StructDecln.Create)
                );

            // specifier-qualifier-list
            //   : [ Type-specifier | Type-qualifier ]+
            SpecifierQualifierList.Is(
                Parser.Seed(SpecQualList.Empty)
                .Then(
                    Either(
                        Given <SpecQualList>()
                        .Then(TypeSpecifier)
                        .Then(SpecQualList.Add)
                        ).Or(
                        Given <SpecQualList>()
                        .Then(TypeQualifier)
                        .Then(SpecQualList.Add)
                        )
                    .OneOrMore()
                    )
                );

            // struct-declarator-list
            //   : struct-declarator [ ',' struct-declarator ]*
            StructDeclaratorList.Is(
                StructDeclarator.OneOrMore(Comma)
                );

            // struct-declarator
            //   : [declarator]? ':' constant-expression
            //   | declarator
            StructDeclarator.Is(
                Either(
                    (Declarator.Optional())
                    .Then(Colon)
                    .Then(ConstantExpression)
                    .Then(StructDeclr.Create)
                    ).Or(
                    (Declarator)
                    .Then(StructDeclr.Create)
                    )
                );

            // parameter-declaration
            //   : declaration-specifiers [ declarator | abstract-declarator ]?
            ParameterDeclaration.Is(
                (DeclarationSpecifiers)
                .Then(
                    Either(
                        (Declarator).Then(ParamDeclr.Create)
                        ).Or(
                        (AbstractDeclarator).Then(ParamDeclr.Create)
                        ).Optional()
                    ).Then(ParamDecln.Create)
                );

            // abstract-declarator
            //   : [pointer]? direct-abstract-declarator
            //   | pointer
            AbstractDeclarator.Is(
                Either(
                    (Pointer.Optional(ImmutableList <PointerModifier> .Empty))
                    .Then(DirectAbstractDeclarator)
                    .Then(AbstractDeclr.Add)
                    ).Or(
                    (Pointer)
                    .Then(AbstractDeclr.Create)
                    )
                );

            // direct-abstract-declarator
            //   : [
            //         '(' abstract-declarator ')'
            //       | '[' [constant-expression]? ']'  // array modifier
            //       | '(' [parameter-type_list]? ')'  // function modifier
            //     ] [
            //         '[' [constant-expression]? ']'  // array modifier
            //       | '(' [parameter-Type-list]? ')'  // function modifier
            //     ]*
            DirectAbstractDeclarator.Is(
                (
                    Either(
                        (LeftParen)
                        .Then(AbstractDeclarator)
                        .Then(RightParen)
                        ).Or(
                        (LeftBracket)
                        .Then(ConstantExpression.Optional())
                        .Then(RightBracket)
                        .Then(ArrayModifier.Create)
                        .Then <ArrayModifier, ImmutableList <ArrayModifier> >(ImmutableList.Create)
                        .Then(AbstractDeclr.Create)
                        ).Or(
                        (LeftParen)
                        .Then(ParameterTypeList.Optional())
                        .Then(RightParen)
                        .Then(FunctionModifier.Create)
                        .Then <FunctionModifier, ImmutableList <FunctionModifier> >(ImmutableList.Create)
                        .Then(AbstractDeclr.Create)
                        )
                ).Then(
                    Either(
                        Given <AbstractDeclr>()
                        .Then(
                            LeftBracket
                            .Then(ConstantExpression.Optional())
                            .Then(RightBracket)
                            .Then(ArrayModifier.Create)
                            ).Then(
                            AbstractDeclr.Add
                            )
                        ).Or(
                        Given <AbstractDeclr>()
                        .Then(
                            (LeftParen)
                            .Then(ParameterTypeList.Optional())
                            .Then(RightParen)
                            .Then(FunctionModifier.Create)
                            ).Then(
                            AbstractDeclr.Add
                            )
                        )
                    .ZeroOrMore()
                    )
                );

            // initializer
            //   : assignment-expression
            //   | '{' initializer-list '}'
            //   | '{' initializer-list ',' '}'
            Initializer.Is(
                Either <Initr>(
                    (AssignmentExpression)
                    .Then(InitExpr.Create)
                    ).Or(
                    (LeftCurlyBrace)
                    .Then(InitializerList)
                    .Then(RightCurlyBrace)
                    ).Or(
                    (LeftCurlyBrace)
                    .Then(InitializerList)
                    .Then(Comma)
                    .Then(RightCurlyBrace)
                    )
                );

            // initializer-list
            //   : initializer [ ',' initializer ]*
            InitializerList.Is(
                Initializer.OneOrMore(Comma)
                .Then(InitList.Create)
                );

            // Type-name
            //   : specifier-qualifier-list [abstract-declarator]?
            TypeName.Is(
                (SpecifierQualifierList)
                .Then(AbstractDeclarator.Optional())
                .Then(AST.TypeName.Create)
                );

            // typedef-name
            //   : identifier
            TypeDefName.Is(
                (Identifier)
                .Check(
                    result => result.Environment.IsTypedefName(result.Result)
                    )
                );
        }