Пример #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);
        }
Пример #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);
        }
Пример #3
0
        public StagingCache BeginStaging()
        {
            StagingCache cache;

            if (caches.Count == 0)
            {
                cache = new StagingCache(this, 0);
            }
            else
            {
                cache = new StagingCache(this, caches.Peek().End);
            }

            caches.Push(cache);

            Print($"&b;Pushed staging cache onto the stack; stack now contains &d;{caches.Count}&b;.");

            return(cache);
        }
Пример #4
0
        protected override StepResult ExecuteInternal(TContext ctx)
        {
            StagingCache cache = ctx.BeginStaging();

            StepResult result = child.Execute(ctx);

            if (result != StepResult.Positive)
            {
                ctx.EndStaging(cache, false);
                return(result);
            }

            while (child.Execute(ctx) == StepResult.Positive)
            {
                ;
            }

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

            return(StepResult.Positive);
        }
Пример #5
0
        protected override StepResult ExecuteInternal(TContext ctx)
        {
            StagingCache cache = ctx.BeginStaging();
            StepResult   result;
            bool         addRecursion = false;

            foreach (SequenceStepItem <TContext> item in items)
            {
                result = item.Step.Execute(ctx);
                switch (result)
                {
                case StepResult.Positive: continue;

                case StepResult.Negative:
                    if (!item.IsOptional)
                    {
                        goto negative;
                    }
                    break;

                case StepResult.AddRecursion:
                    addRecursion = true;
                    goto case StepResult.Negative;
                }

negative:
                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);
        }