Example #1
0
        private static void EvaluateSingleTagDirective(
            [NotNull] TextWriter writer,
            [NotNull] IExpressionEvaluationContext context,
            [NotNull] Directive directive,
            [NotNull] IReadOnlyList <object> components)
        {
            /* Pre-evaluate the tag's components, as these  */
            var tagEvaluatedComponents = EvaluateTag(context, components);

            /* Evaluate tag. */
            object state        = null;
            var    flowDecision = Directive.FlowDecision.Evaluate;

            while (flowDecision != Directive.FlowDecision.Terminate)
            {
                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                flowDecision = directive.Execute(0, tagEvaluatedComponents, ref state, context, out string directiveText);

                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                if (directiveText != null)
                {
                    writer.Write(directiveText);
                }
            }
        }
Example #2
0
        private static void EvaluateTagDirective(
            [NotNull] TextWriter writer,
            [NotNull] EvaluationContext context,
            [NotNull] Directive directive,
            [NotNull] IReadOnlyList <object[]> components,
            [NotNull] IReadOnlyList <CompiledEvaluationDelegate <EvaluationContext> > evaluationProcs)
        {
            /* Get tag components. */
            var evaluatedComponents = new object[components.Count][];

            /* Evaluate tag. */
            object state           = null;
            var    currentTagIndex = 0;

            while (currentTagIndex >= 0)
            {
                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                if (evaluatedComponents[currentTagIndex] == null)
                {
                    evaluatedComponents[currentTagIndex] = EvaluateTag(context, components[currentTagIndex]);
                }

                var flowDecision = directive.Execute(currentTagIndex, evaluatedComponents[currentTagIndex], ref state, context, out string directiveText);

                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                if (directiveText != null)
                {
                    writer.Write(directiveText);
                }

                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (flowDecision)
                {
                case Directive.FlowDecision.Terminate:
                    currentTagIndex = -1;
                    break;

                case Directive.FlowDecision.Restart:
                    currentTagIndex = 0;
                    break;

                default:
                    currentTagIndex++;
                    if (currentTagIndex == components.Count)
                    {
                        currentTagIndex = 0;
                    }
                    else if (flowDecision == Directive.FlowDecision.Evaluate)
                    {
                        evaluationProcs[currentTagIndex - 1]?.Invoke(writer, context);
                    }
                    break;
                }
            }
        }
Example #3
0
        private static void EvaluateDoubleTagDirective(
            [NotNull] TextWriter writer,
            [NotNull] EvaluationContext context,
            [NotNull] Directive directive,
            [NotNull] IReadOnlyList <object> beginComponents,
            [NotNull] IReadOnlyList <object> closeComponents,
            [CanBeNull] CompiledEvaluationDelegate <EvaluationContext> beginToCloseEvaluateProc)
        {
            /* Get tag components. */
            var beginTagEvaluatedComponents = EvaluateTag(context, beginComponents);

            object[] closeTagEvaluatedComponents = null;

            /* Evaluate tag. */
            object state = null;

            while (true)
            {
                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                var flowDecision = directive.Execute(0, beginTagEvaluatedComponents, ref state, context, out string directiveText);

                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                if (directiveText != null)
                {
                    writer.Write(directiveText);
                }

                if (flowDecision == Directive.FlowDecision.Terminate)
                {
                    break;
                }

                switch (flowDecision)
                {
                case Directive.FlowDecision.Restart:
                    continue;

                case Directive.FlowDecision.Evaluate:
                    beginToCloseEvaluateProc?.Invoke(writer, context);
                    break;

                case Directive.FlowDecision.Terminate:
                    break;

                case Directive.FlowDecision.Skip:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(flowDecision));
                }

                if (closeTagEvaluatedComponents == null)
                {
                    closeTagEvaluatedComponents = EvaluateTag(context, closeComponents);
                }

                /* Check for cancellation. */
                context.CancellationToken.ThrowIfCancellationRequested();

                flowDecision = directive.Execute(1, closeTagEvaluatedComponents, ref state, context, out directiveText);

                if (directiveText != null)
                {
                    writer.Write(directiveText);
                }

                if (flowDecision == Directive.FlowDecision.Terminate)
                {
                    break;
                }
            }
        }