コード例 #1
0
        public CppTokenFilterChain()
        {
            var argsBlock = new TokensBlockWithExcludePatternBuilder()
                            .Reset()
                            .StartWith(TokenType.ARGS_OPEN)
                            .ExceptRange(
                Enum.GetValues(typeof(TokenType))
                .Cast <TokenType>()
                .Where(t => t.IsOperatorToken())
                .ToList())
                            .Except(TokenType.ACCESS_MODIFICATOR)
                            .EndWith(TokenType.ARGS_CLOSE)
                            .Build();

            var gotoLabelPattern = TokenSequencePatternBuilder
                                   .Reset()
                                   .Then(TokenType.IDENTIFIER)
                                   .Then(TokenType.DOUBLE_DOT)
                                   .Build();
            var gotoConstructionPattern = new StartEndTokensPattern(
                TokenType.RESERVE_5,
                TokenType.LINE_END);
            var accessModificatorPattern = TokenSequencePatternBuilder
                                           .Reset()
                                           .Then(TokenType.ACCESS_MODIFICATOR)
                                           .Then(TokenType.DOUBLE_DOT)
                                           .Build();
            var classQualifierPattern = TokenSequencePatternBuilder
                                        .Reset()
                                        .Then(TokenType.IDENTIFIER)
                                        .Then(TokenType.DOUBLE_DOT)
                                        .Then(TokenType.DOUBLE_DOT)
                                        .Build();
            var throwLinePattern = new StartEndTokensPattern(
                TokenType.THROW_OPERATOR,
                TokenType.LINE_END);
            var emptyObjVarsPattern = TokenSequencePatternBuilder
                                      .Reset()
                                      .Then(TokenType.IDENTIFIER)
                                      .Then(TokenType.IDENTIFIER)
                                      .Then(TokenType.LINE_END)
                                      .Build();
            var varDefPattern = TokenSequencePatternBuilder
                                .Reset()
                                .Then(TokenType.IDENTIFIER)
                                .Then(TokenType.LINE_END)
                                .Build();
            var castPattern = TokenSequencePatternBuilder
                              .Reset()
                              .Then(TokenType.ARGS_OPEN)
                              .Then(TokenType.IDENTIFIER)
                              .Then(TokenType.ARGS_CLOSE)
                              .Build();

            var catchBlockPattern = StrictPatternBuilder
                                    .Reset()
                                    .NextPattern(new SingleTokenPattern(TokenType.CATCH_OPERATOR))
                                    .NextPattern(argsBlock)
                                    .NextPattern(TokensBlockPattern.BracketBlock)
                                    .Build();

            var structDefPattern = StrictPatternBuilder
                                   .Reset()
                                   .NextPattern(TokenSequencePatternBuilder
                                                .Reset()
                                                .Then(TokenType.RESERVE_1)
                                                .Then(TokenType.IDENTIFIER)
                                                .Build())
                                   .NextPattern(TokensBlockPattern.BracketBlock)
                                   .Build();

            var structVarInitComboPattern = StrictPatternBuilder
                                            .Reset()
                                            .NextPattern(TokenSequencePatternBuilder
                                                         .Reset()
                                                         .Then(TokenType.IDENTIFIER)
                                                         .Then(TokenType.IDENTIFIER)
                                                         .Then(TokenType.EQUAL_ACTION)
                                                         .Build())
                                            .NextPattern(TokensBlockPattern.BracketBlock)
                                            .Build();

            Filters.AddRange(new List <TokenFilter>()
            {
                new RemoveTokensFilter(gotoLabelPattern, TokenFilter.HighestPriority),
                new RemoveTokensFilter(gotoConstructionPattern, TokenFilter.HighestPriority),
                new RemoveTokensFilter(catchBlockPattern, TokenFilter.HighestPriority),
                new RemoveTokensFilter(structDefPattern, TokenFilter.HighestPriority),
                new RemoveTokensFilter(structVarInitComboPattern, TokenFilter.HighestPriority),
                new RemoveTokensFilter(accessModificatorPattern, TokenFilter.HighestPriority),
                new RemoveTokensFilter(classQualifierPattern, TokenFilter.HighestPriority),

                new EditTokensFilter(FilterTryBlock, TokenFilter.PreHighestPriority),
                new EditTokensFilter(ClassFilter, TokenFilter.PreHighestPriority),

                new RemoveTokensFilter(throwLinePattern),
                new RemoveTokensFilter(castPattern),

                new RemoveTokensFilter(emptyObjVarsPattern, TokenFilter.LowestPriority),
                new RemoveTokensFilter(varDefPattern, TokenFilter.LowestPriority)
            });
        }
コード例 #2
0
        public CppCodeParser()
        {
            var ifPattern     = BuildFullOperatorPattern(TokenType.IF_OPERATOR);
            var elseIfPattern = BuildFullOperatorPattern(TokenType.ELSE_IF_OPERATOR);
            var forPattern    = BuildFullOperatorPattern(TokenType.FOR_OPERATOR);
            var whilePattern  = BuildFullOperatorPattern(TokenType.WHILE_OPERATOR);
            var elsePattern   = StrictPatternBuilder
                                .Reset()
                                .NextPattern(new SingleTokenPattern(TokenType.ELSE_OPERATOR))
                                .NextPattern(TokensBlockPattern.BracketBlock)
                                .Build();
            var doWhilePattern = StrictPatternBuilder
                                 .Reset()
                                 .NextPattern(new SingleTokenPattern(TokenType.DO_WHILE_OPERATOR))
                                 .NextPattern(TokensBlockPattern.BracketBlock)
                                 .NextPattern(new SingleTokenPattern(TokenType.WHILE_OPERATOR))
                                 .NextPattern(ArgsPattern)
                                 .NextPattern(new SingleTokenPattern(TokenType.LINE_END))
                                 .Build();
            var inputPattern = TokenSequencePatternBuilder.Reset()
                               .Then(TokenType.INPUT_OPERATOR)
                               .Then(TokenType.TEXT)
                               .Then(TokenType.LINE_END)
                               .Build();
            var outputPattern = TokenSequencePatternBuilder.Reset()
                                .Then(TokenType.OUTPUT_OPERATOR)
                                .Then(TokenType.TEXT)
                                .Then(TokenType.LINE_END)
                                .Build();
            var processPattern = new StartEndTokensPattern(TokenType.IDENTIFIER, TokenType.LINE_END);
            var programPattern = new StartEndTokensPattern(
                StrictPatternBuilder
                .Reset()
                .NextPattern(TokenSequencePatternBuilder
                             .Reset()
                             .Then(TokenType.IDENTIFIER)
                             .Then(TokenType.DOT)
                             .Then(TokenType.IDENTIFIER)
                             .Build())
                .NextPattern(ArgsPattern)
                .Build(),
                new SingleTokenPattern(TokenType.LINE_END));

            var programPattern1 = new StartEndTokensPattern(
                StrictPatternBuilder
                .Reset()
                .NextPattern(new SingleTokenPattern(TokenType.IDENTIFIER))
                .NextPattern(ArgsPattern)
                .Build(),
                new SingleTokenPattern(TokenType.LINE_END));

            var ifSyntaxRule       = GetCommonOperatorParseRule(NodeType.IF, ifPattern);
            var elseIfSyntaxRule   = GetCommonOperatorParseRule(NodeType.ELSE_IF, elseIfPattern);
            var forSyntaxRule      = GetCommonOperatorParseRule(NodeType.FOR, forPattern);
            var whileSyntaxRule    = GetCommonOperatorParseRule(NodeType.WHILE, whilePattern);
            var elseSyntaxRule     = GetElseParseRule(elsePattern);
            var doWhileSyntaxRule  = GetDoWhileParseRule(doWhilePattern);
            var inputSyntaxRule    = GetSimpleOperatorParseRule(NodeType.INPUT, inputPattern);
            var outputSyntaxRule   = GetSimpleOperatorParseRule(NodeType.OUTPUT, outputPattern);
            var processSyntaxRule  = GetSimpleOperatorParseRule(NodeType.PROCESS, processPattern);
            var programSyntaxRule  = GetSimpleOperatorParseRule(NodeType.PROGRAM, programPattern);
            var programSyntaxRule1 = GetSimpleOperatorParseRule(NodeType.PROGRAM, programPattern1);

            ParseRules = new List <ParseRule>()
            {
                ifSyntaxRule,
                elseIfSyntaxRule,
                elseSyntaxRule,
                forSyntaxRule,
                whileSyntaxRule,
                doWhileSyntaxRule,
                inputSyntaxRule,
                outputSyntaxRule,
                processSyntaxRule,
                programSyntaxRule,
                programSyntaxRule1,
            };
        }