Exemple #1
0
 public StateTransition(TokenType tokenType, int targetState, List <ProductionRule> productions, LookAheadHint lookAheadHint)
 {
     Productions   = productions;
     LookAheadHint = lookAheadHint;
     TokenType     = tokenType;
     TargetState   = targetState;
 }
Exemple #2
0
        private static List <Branch> GetBranchesInSubRules(CallStackItem caller, out bool ruleCanFinish, List <ProductionRule> productions, LookAheadHint lookAheadHint = null)
        {
            var result    = new List <Branch>();
            var lookAhead = caller.Rule.LookAheadHint ?? lookAheadHint;

            if (caller.Position == AFTER_RULE_POSITION)
            {
                ruleCanFinish = true;
                return(result);
            }

            if (caller.Rule is TokenRule)
            {
                ruleCanFinish = false;
                productions.Add(new ProductionRule(ProductionRuleType.Process, caller.Rule));
                result.Add(CreateBranch(caller, productions, lookAhead));
                return(result);
            }

            var ruleElements = ((DerivedRule)caller.Rule).RuleElements;

            if (caller.Rule is SequenceRule)
            {
                int subRuleIndex = caller.Position;
                for (; subRuleIndex < ruleElements.Length; subRuleIndex++)
                {
                    var subRuleElement = ruleElements[subRuleIndex];

                    if (RuleCountInCallStack(subRuleElement.ResolvedRule, caller) > 3)
                    {
                        ruleCanFinish = false;
                        return(result);
                    }

                    var  subRuleCaller        = new CallStackItem(caller.Parent, caller.Rule, subRuleIndex);
                    var  subRuleCallStackItem = new CallStackItem(subRuleCaller, subRuleElement.ResolvedRule);
                    bool subRuleCanFinish;

                    var subRuleProductions = new List <ProductionRule>(productions)
                    {
                        new ProductionRule(ProductionRuleType.Start, subRuleElement.ResolvedRule)
                    };
                    var subRuleBranches = GetBranchesInSubRules(subRuleCallStackItem, out subRuleCanFinish, subRuleProductions, lookAhead);

                    result.AddRange(subRuleBranches);

                    bool continueWithNext = subRuleCanFinish ||
                                            subRuleElement.Multilicator == Multilicator.Any ||
                                            subRuleElement.Multilicator == Multilicator.OneOrZero;

                    if (!continueWithNext)
                    {
                        break;
                    }
                }
                ruleCanFinish = subRuleIndex == ruleElements.Length;
                return(result);
            }

            Debug.Assert(caller.Rule is AlternateRule);
            if (caller.Rule is AlternateRule)
            {
                Debug.Assert(caller.Position == 0);

                ruleCanFinish = false;
                for (int subRuleIndex = 0; subRuleIndex < ruleElements.Length; subRuleIndex++)
                {
                    var  subRuleElement       = ruleElements[subRuleIndex];
                    var  subRuleCaller        = new CallStackItem(caller.Parent, caller.Rule, subRuleIndex);
                    var  subRuleCallStackItem = new CallStackItem(subRuleCaller, subRuleElement.ResolvedRule);
                    bool subRuleCanFinish;

                    var subRuleProductions = new List <ProductionRule>(productions)
                    {
                        new ProductionRule(ProductionRuleType.Start, subRuleElement.ResolvedRule)
                    };
                    var subRuleBranches = GetBranchesInSubRules(subRuleCallStackItem, out subRuleCanFinish, subRuleProductions, lookAhead);

                    result.AddRange(subRuleBranches);
                    ruleCanFinish |= subRuleCanFinish;
                }
                return(result);
            }

            throw new NotSupportedException();
        }
Exemple #3
0
        private static Branch CreateBranch(CallStackItem caller, List <ProductionRule> productions, LookAheadHint lookAheadHint)
        {
            var branch = new Branch(((TokenRule)caller.Rule).TokenType, caller, productions)
            {
                LookAheadHint = lookAheadHint
            };

            OptimizeProductions(branch);
            return(branch);
        }
Exemple #4
0
 private void Resolve(LookAheadHint lookAheadHint)
 {
     lookAheadHint.Id = lookAheadHints.Count;
     lookAheadHints.Add(lookAheadHint);
 }