Exemplo n.º 1
0
                    public override ParseTreeResult <IList <IParseTree <TToken, TRule> > > TryParse(
                        IList <Token <TToken> > tokens,
                        int matchFrom,
                        IDictionary <TRule, RuleDefinition> rulesByType)
                    {
                        if (matchFrom >= tokens.Count)
                        {
                            return(this.AllowZero
                                ? RuleElementResult.Empty(matchFrom, matchFrom)
                                : RuleElementResult.Error(matchFrom, matchFrom));
                        }

                        var currentParseTree = rulesByType[this.RuleType]
                                               .TryParse(tokens, matchFrom, rulesByType);

                        if (currentParseTree.IsError)
                        {
                            return(this.AllowZero
                                ? RuleElementResult.Empty(matchFrom, currentParseTree.MaxMatchedNextTokenIndex)
                                : RuleElementResult.Error(currentParseTree.NextTokenIndex, currentParseTree.MaxMatchedNextTokenIndex));
                        }

                        var matches = new List <IParseTree <TToken, TRule> >();

                        if (currentParseTree.HasTreeItems)
                        {
                            matches.Add(currentParseTree.TreeItems);
                        }

                        matchFrom = currentParseTree.NextTokenIndex;
                        var maxMatchedNextTokenIndex = currentParseTree.MaxMatchedNextTokenIndex;

                        if (this.AllowMany)
                        {
                            while (matchFrom < tokens.Count)
                            {
                                currentParseTree = rulesByType[this.RuleType]
                                                   .TryParse(tokens, matchFrom, rulesByType);

                                if (currentParseTree.IsError)
                                {
                                    break;
                                }

                                if (currentParseTree.HasTreeItems)
                                {
                                    matches.Add(currentParseTree.TreeItems);
                                }

                                matchFrom = currentParseTree.NextTokenIndex;
                                maxMatchedNextTokenIndex = currentParseTree.MaxMatchedNextTokenIndex;
                            }
                        }

                        return(RuleElementResult.NonEmpty(
                                   this.hoist ? matches.Elements().ToList() : matches,
                                   matchFrom,
                                   maxMatchedNextTokenIndex));
                    }
Exemplo n.º 2
0
                    public override ParseTreeResult <IList <IParseTree <TToken, TRule> > > TryParse(
                        IList <Token <TToken> > tokens,
                        int matchFrom,
                        IDictionary <TRule, RuleDefinition> rulesByType)
                    {
                        if (matchFrom >= tokens.Count)
                        {
                            return(this.AllowZero
                                ? RuleElementResult.Empty(matchFrom, matchFrom)
                                : RuleElementResult.Error(matchFrom, matchFrom));
                        }

                        if (!tokens[matchFrom].Type.Equals(this.tokenType))
                        {
                            return(this.AllowZero
                                ? RuleElementResult.Empty(matchFrom, matchFrom)
                                : RuleElementResult.Error(matchFrom, matchFrom));
                        }

                        var matches = this.exclude
                            ? null
                            : new List <IParseTree <TToken, TRule> > {
                            new ParseTreeToken(tokens[matchFrom])
                        };

                        ++matchFrom;
                        if (this.AllowMany)
                        {
                            while (matchFrom < tokens.Count && tokens[matchFrom].Type.Equals(this.tokenType))
                            {
                                matches?.Add(new ParseTreeToken(tokens[matchFrom]));
                                ++matchFrom;
                            }
                        }

                        return(RuleElementResult.NonEmpty(matches, matchFrom, matchFrom));
                    }