Пример #1
0
        public override List <Token> ApplyFilter(List <Token> tokens)
        {
            var from       = 0;
            var patternRes = PatternToRemove.GetMatch(tokens, from);

            while (patternRes.IsFullMatch)
            {
                tokens.RemoveRange(patternRes.Start, patternRes.Length);
                from       = patternRes.Start;
                patternRes = PatternToRemove.GetMatch(tokens, from);
            }

            return(tokens);
        }
        protected List <Token> FilterTryBlock(TokenPattern tryPattern, List <Token> tokens)
        {
            var tryMatch = tryPattern.GetMatch(tokens);

            while (tryMatch.IsPartMatch)
            {
                var tryOpMatch     = tryMatch.GetMatch(0);
                var codeBlockMatch = tryMatch.GetMatch(1);
                if (tryMatch.IsFullMatch)
                {
                    var tryBlockTokens = tokens.GetRange(codeBlockMatch.Start + 1, codeBlockMatch.Length - 2);
                    tokens.RemoveRange(tryMatch.Start, tryMatch.Length);
                    tokens.InsertRange(tryMatch.Start, tryBlockTokens);
                    tryMatch = tryPattern.GetMatch(tokens, tryMatch.Start + codeBlockMatch.Length);
                }
                else if (tryOpMatch.IsFullMatch && !codeBlockMatch.IsFullMatch)
                {
                    throw new ParseRuleException(tryPattern, tokens, tryMatch.Start);
                }
            }

            return(tokens);
        }
        protected List <Token> ClassFilter(TokenPattern classDefPattern, List <Token> tokens)
        {
            var classDef = classDefPattern.GetMatch(tokens);

            while (classDef.IsPartMatch)
            {
                var classOperatorMatch = classDef.GetMatch(0);
                var classArgsMatch     = classDef.GetMatch(1);
                var codeBlockMatch     = classDef.GetMatch(2);
                if (codeBlockMatch.IsFullMatch)
                {
                    var classBlockLex = TokenUtils.GetMatchResultBlockTokens(tokens, codeBlockMatch);
                    tokens.RemoveRange(classDef.Start, classDef.Length);
                    tokens.InsertRange(classDef.Start, classBlockLex);
                    classDef = classDefPattern.GetMatch(tokens, classDef.End - 4);
                }
                else if (classOperatorMatch.IsFullMatch && (!classArgsMatch.IsFullMatch | classArgsMatch.IsFullMatch))
                {
                    throw new ParseRuleException(classDefPattern, tokens, classDef.Start);
                }
            }

            return(tokens);
        }
Пример #4
0
        public List <Method> ParseCode(List <Token> tokensToParse)
        {
            var methods = new List <Method>();
            var pos     = 0;

            while (pos < tokensToParse.Count)
            {
                var          methodNodes     = new List <Node>();
                var          methodSignature = "main()";
                var          nexMethodMatch  = MethodDefPattern.GetMatch(tokensToParse, pos);
                List <Token> methodBlockTokens;
                if (nexMethodMatch.IsFullMatch)
                {
                    var globalNodes = new List <Node>();
                    if (nexMethodMatch.Start > pos)
                    {
                        var globalTokens = tokensToParse.GetRange(pos, nexMethodMatch.Start - pos);
                        globalNodes = ParseBlock(globalTokens);
                        pos         = nexMethodMatch.Start;
                    }

                    pos += nexMethodMatch.Length;

                    var methodSignatureTokens = GetMethodSignatureTokens(tokensToParse, nexMethodMatch);
                    methodSignature = TokenUtils.TokensToString(methodSignatureTokens);
                    if (globalNodes.Count > 0)
                    {
                        methodNodes.AddRange(globalNodes);
                    }

                    methodBlockTokens = GetMethodBlockTokens(tokensToParse, nexMethodMatch);
                }
                else
                {
                    methodBlockTokens = tokensToParse;
                }

                methodNodes = ParseBlock(methodBlockTokens);
                var newMethod = new Method(methodSignature, methodNodes);
                methods.Add(newMethod);
            }

            return(methods);
        }