示例#1
0
        public IParserResult <ImmutableList <R> > Parse(ParserInput input)
        {
            var list      = ImmutableList <R> .Empty;
            var curResult = this.ElementParser.Parse(input);

            if (!curResult.IsSuccessful)
            {
                return(new ParserFailed <ImmutableList <R> >());
            }
            IParserResult <R> lastElementResult;

            do
            {
                list = list.Add(curResult.Result);
                lastElementResult = curResult;

                var separatorResult = this.SeparatorConsumer.Consume(curResult.ToInput());
                if (!separatorResult.IsSuccessful)
                {
                    break;
                }
                curResult = this.ElementParser.Parse(separatorResult.ToInput());
            } while (curResult.IsSuccessful);

            return(ParserSucceeded.Create(list, lastElementResult.Environment, lastElementResult.Source));
        }
示例#2
0
 public IParserResult Consume(ParserInput input)
 {
     if ((input.Source.First() as TokenOperator)?.Val == this.OperatorVal)
     {
         return(ParserSucceeded.Create(input.Environment, input.Source.Skip(1)));
     }
     return(new ParserFailed());
 }
示例#3
0
 public IParserResult <R> Parse(ParserInput input)
 {
     if ((input.Source.First() as TokenKeyword)?.Val == this.KeywordVal)
     {
         return(ParserSucceeded.Create(this.Result, input.Environment, input.Source.Skip(1)));
     }
     return(new ParserFailed <R>());
 }
示例#4
0
            public IParserResult <Expr> Parse(ParserInput input)
            {
                var token = input.Source.First() as TokenString;

                if (token == null)
                {
                    return(new ParserFailed <Expr>());
                }
                return(ParserSucceeded.Create(new StringLiteral(token.Raw), input.Environment, input.Source.Skip(1)));
            }
示例#5
0
            public IParserResult <Expr> Parse(ParserInput input)
            {
                var token = input.Source.First() as TokenFloat;

                if (token == null)
                {
                    return(new ParserFailed <Expr>());
                }
                return(ParserSucceeded.Create(new FloatLiteral(token.Value, token.Suffix), input.Environment, input.Source.Skip(1)));
            }
示例#6
0
        public IParserResult <R> Transform(R seed, ParserInput input)
        {
            var result = this.Transformer.Transform(seed, input);

            if (result.IsSuccessful)
            {
                return(result);
            }
            return(ParserSucceeded.Create(seed, input.Environment, input.Source));
        }
示例#7
0
            public IParserResult <Expr> Parse(ParserInput input)
            {
                var token = input.Source.First() as TokenCharConst;

                if (token == null)
                {
                    return(new ParserFailed <Expr>());
                }
                return(ParserSucceeded.Create(new IntLiteral(token.Value, TokenInt.IntSuffix.NONE), input.Environment, input.Source.Skip(1)));
            }
示例#8
0
        public IParserResult <R> Parse(ParserInput input)
        {
            var result = this.Parser.Parse(input);

            if (result.IsSuccessful)
            {
                return(result);
            }
            return(ParserSucceeded.Create(this.DefaultValue, input.Environment, input.Source));
        }
示例#9
0
        public IParserResult <Boolean> Parse(ParserInput input)
        {
            var result = this.Consumer.Consume(input);

            if (result.IsSuccessful)
            {
                return(ParserSucceeded.Create(true, result.Environment, result.Source));
            }
            return(ParserSucceeded.Create(false, input.Environment, input.Source));
        }
示例#10
0
        public IParserResult <String> Parse(ParserInput input)
        {
            var token = input.Source.First() as TokenIdentifier;

            if (token == null)
            {
                return(new ParserFailed <String>());
            }
            return(ParserSucceeded.Create(token.Val, input.Environment, input.Source.Skip(1)));
        }
示例#11
0
        public IParserResult <Option <R> > Parse(ParserInput input)
        {
            var result = this.Parser.Parse(input);

            if (result.IsSuccessful)
            {
                return(ParserSucceeded.Create(new Some <R>(result.Result), result.Environment, result.Source));
            }
            return(ParserSucceeded.Create(new None <R>(), input.Environment, input.Source));
        }
示例#12
0
        public IParserResult <ImmutableList <R> > Parse(ParserInput input)
        {
            var list = ImmutableList <R> .Empty;
            IParserResult <R> curResult;

            while ((curResult = this.Parser.Parse(input)).IsSuccessful)
            {
                list  = list.Add(curResult.Result);
                input = curResult.ToInput();
            }
            return(ParserSucceeded.Create(list, input.Environment, input.Source));
        }
示例#13
0
        public IParserResult <R> Transform(R seed, ParserInput input)
        {
            IParserResult <R> curResult = ParserSucceeded.Create(seed, input.Environment, input.Source);

            IParserResult <R> lastSuccessfulResult;

            do
            {
                lastSuccessfulResult = curResult;
                curResult            = this.Transformer.Transform(lastSuccessfulResult.Result, lastSuccessfulResult.ToInput());
            } while (curResult.IsSuccessful);

            return(lastSuccessfulResult);
        }
示例#14
0
        public IParserResult <Tuple <R2, R1> > Transform(S seed, ParserInput input)
        {
            var result1 = this.Transformer.Transform(seed, input);

            if (!result1.IsSuccessful)
            {
                return(new ParserFailed <Tuple <R2, R1> >());
            }
            var result2 = this.Parser.Parse(result1.ToInput());

            if (!result2.IsSuccessful)
            {
                return(new ParserFailed <Tuple <R2, R1> >());
            }
            return(ParserSucceeded.Create(Tuple.Create(result2.Result, result1.Result), result2.Environment, result2.Source));
        }
示例#15
0
        public IParserResult <R> Parse(ParserInput input)
        {
            var firstResult = this.Parser.Parse(input);

            if (!firstResult.IsSuccessful)
            {
                return(new ParserFailed <R>());
            }
            var secondResult = this.Consumer.Consume(firstResult.ToInput());

            if (!secondResult.IsSuccessful)
            {
                return(new ParserFailed <R>());
            }
            return(ParserSucceeded.Create(firstResult.Result, secondResult.Environment, secondResult.Source));
        }
示例#16
0
        public IParserResult <R> Transform(S seed, ParserInput input)
        {
            var result1 = this.Transformer.Transform(seed, input);

            if (!result1.IsSuccessful)
            {
                return(result1);
            }
            var result2 = this.Consumer.Consume(result1.ToInput());

            if (!result2.IsSuccessful)
            {
                return(new ParserFailed <R>());
            }
            return(ParserSucceeded.Create(result1.Result, result2.Environment, result2.Source));
        }
示例#17
0
        public IParserResult <Tuple <R2, R1> > Parse(ParserInput input)
        {
            var firstResult = this.FirstParser.Parse(input);

            if (!firstResult.IsSuccessful)
            {
                return(new ParserFailed <Tuple <R2, R1> >());
            }
            var secondResult = this.SecondParser.Parse(firstResult.ToInput());

            if (!secondResult.IsSuccessful)
            {
                return(new ParserFailed <Tuple <R2, R1> >());
            }
            return(ParserSucceeded.Create(Tuple.Create(secondResult.Result, firstResult.Result), secondResult.Environment, secondResult.Source));
        }
示例#18
0
        public IParserResult <ImmutableList <R> > Parse(ParserInput input)
        {
            var list      = ImmutableList <R> .Empty;
            var curResult = this.Parser.Parse(input);

            if (!curResult.IsSuccessful)
            {
                return(new ParserFailed <ImmutableList <R> >());
            }

            IParserResult <R> lastSuccessfulResult;

            do
            {
                list = list.Add(curResult.Result);
                lastSuccessfulResult = curResult;
                curResult            = this.Parser.Parse(lastSuccessfulResult.ToInput());
            } while (curResult.IsSuccessful);

            return(ParserSucceeded.Create(list, lastSuccessfulResult.Environment, lastSuccessfulResult.Source));
        }
示例#19
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)
                    )
                );
        }
示例#20
0
 public IParserResult Consume(ParserInput input)
 {
     return(ParserSucceeded.Create(this.Transformer(input.Environment), input.Source));
 }
示例#21
0
 public IParserResult <R> Parse(ParserInput input) =>
 ParserSucceeded.Create(this.Result, input.Environment, input.Source);
示例#22
0
 public IParserResult <R> Transform(R seed, ParserInput input) =>
 this.TransformFunc(ParserSucceeded.Create(seed, input.Environment, input.Source));