Exemplo n.º 1
0
        protected override StepResult ExecuteInternal(TContext ctx)
        {
            BranchedStagingCache cache   = ctx.BeginStagingBranched();
            StagingCache         longest = null;
            bool addRecursion            = false;

            foreach (ProducerStep <TContext> choice in GetValidChoices(ctx))
            {
                StagingCache branch = cache.BeginSingleBranch();
                StepResult   result = choice.Execute(ctx);

                if (result == StepResult.AddRecursion)
                {
                    addRecursion = true;
                }
                else if (result == StepResult.Positive)
                {
                    if (longest == null)
                    {
                        goto keepBranch;
                    }

                    if (longest.Consumed < branch.Consumed)
                    {
                        cache.EndBranch(longest);
                        goto keepBranch;
                    }
                }

                cache.EndBranch(branch);
                goto @continue;

keepBranch:
                longest = branch;

@continue:
                if (longest != null)
                {
                    cache.CurrentBranch = longest;
                }
            }

            if (longest == null)
            {
                ctx.EndStaging(cache, false);

                return(addRecursion ? StepResult.AddRecursion : StepResult.Negative);
            }

            ctx.EndStaging(cache, true, false);
            ctx.AddSymbol(new Production(Name, cache.Symbols));

            return(StepResult.Positive);
        }
Exemplo n.º 2
0
        protected override StepResult ExecuteInternal(TContext ctx)
        {
            BranchedStagingCache cache = ctx.BeginStagingBranched();

            StagingCache posBranch = cache.BeginSingleBranch();

            StepResult posResult = a.Execute(ctx);

            if (posResult == StepResult.Positive)
            {
                StagingCache negBranch = cache.BeginSingleBranch();

                StepResult negResult = b.Execute(ctx);

                switch (negResult)
                {
                case StepResult.Negative:
                    cache.EndBranch(negBranch, posBranch);

                    ctx.EndStaging(cache, true, false);

                    ctx.AddSymbol(new Production(Name, cache.Symbols));

                    return(StepResult.Positive);

                case StepResult.Positive:
                    if (negBranch.Consumed < posBranch.Consumed)
                    {
                        goto case StepResult.Negative;
                    }

                    posResult = StepResult.Negative;
                    break;

                case StepResult.AddRecursion:
                    posResult = StepResult.AddRecursion;
                    break;
                }
            }

            ctx.EndStaging(cache, false);

            return(posResult);
        }