Ejemplo n.º 1
0
        public TokenParseResult ParseRule(Grouping group, Token startToken, ParserSettings settings)
        {
            List <SyntaxNode> nodes       = new List <SyntaxNode>();
            TokenParseResult  groupResult = TokenParseResult.Failed(startToken);
            Token             token;
            TokenParseResult  ruleResult;

            token = startToken;

            foreach (Rule rule in group)
            {
                ruleResult = rule.Parse(token, settings);
                if (ruleResult)
                {
                    if (!groupResult || settings.AlternateCritera == ParserSettings.SelectionCriteria.First ||
                        (groupResult.Node.GetTokenCount() < ruleResult.Node.GetTokenCount()))
                    {
                        groupResult           = ruleResult;
                        groupResult.Node.Rule = group;
                        if (settings.AlternateCritera == ParserSettings.SelectionCriteria.First)
                        {
                            break;
                        }
                    }
                }
            }

            return(groupResult);
        }
Ejemplo n.º 2
0
        public TokenParseResult ParseRule(Token token, ParserSettings settings)
        {
            TokenParseResult result;

            if (TokenType.Name == token.TokenType.Name)
            {
                result = TokenParseResult.Success(new SyntaxNode(Source, token), token.Next);
            }
            else
            {
                result = TokenParseResult.Failed(token);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public override TokenParseResult Parse(Token token, ParserSettings settings)
        {
            TokenParseResult result;

            if (Text == token.Text)
            {
                result = TokenParseResult.Success(new SyntaxNode(this, token), token.Next);
            }
            else
            {
                result = TokenParseResult.Failed(token);
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses a Group who's children are part of a chain and do not alternate
        /// </summary>
        /// <param name="group"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public TokenParseResult ParseRule(Grouping group, Token startToken, ParserSettings settings)
        {
            List <SyntaxNode> nodes       = new List <SyntaxNode>();
            TokenParseResult  groupResult = TokenParseResult.Failed(startToken);

            TokenParseResult ruleResult;
            int   ruleIdx;
            Rule  rule;
            Token token;
            // got the minimum results and wasn't an exception
            bool lastRuleOK;
            List <SyntaxNode> ruleNodes = new List <SyntaxNode>();

            ruleIdx    = 0;
            token      = startToken;
            lastRuleOK = true;

            while (lastRuleOK && ruleIdx < group.Count && token != null)
            {
                // every rule has its own scope
                rule = group[ruleIdx];
                ruleNodes.Clear();
                lastRuleOK = false;
                while (token != null)
                {
                    if (ruleResult = rule.Parse(token, settings))
                    {
                        ruleNodes.Add(ruleResult);
                        token = ruleResult.NextToken;
                        if (ruleNodes.Count >= rule.Quantifier.MaxValue)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                lastRuleOK = (rule.Quantifier.MinValue <= ruleNodes.Count) ^ rule.IsException;


                // if we achieved min qty then add to nodes.
                if (lastRuleOK)
                {
                    nodes.AddRange(ruleNodes);
                    ruleIdx++;
                }
            }

            if (lastRuleOK && ruleIdx >= group.LastRequiredRuleIndex && nodes.Count > 0)
            {
                SyntaxNode parent = new SyntaxNode(group, startToken);
                parent.AddRange(nodes);
                // token should be set to the last succesful nextToken
                groupResult = TokenParseResult.Success(parent, token);
            }

            return(groupResult);
        }