public int HandleMatch(IProcessorState processor, int bufferLength, ref int currentBufferPosition, int token, Stream target)
            {
                bool flag;

                if (processor.Config.Flags.TryGetValue(Conditional.OperationName, out flag) && !flag)
                {
                    target.Write(Tokens[token].Value, Tokens[token].Start, Tokens[token].Length);
                    return(Tokens[token].Length);
                }

                List <byte> conditionBytes = new List <byte>();

                ScanToCloseCondition(processor, conditionBytes, ref bufferLength, ref currentBufferPosition);
                byte[]          condition      = conditionBytes.ToArray();
                EngineConfig    adjustedConfig = new EngineConfig(processor.Config.EnvironmentSettings, processor.Config.Whitespaces, processor.Config.LineEndings, processor.Config.Variables, _definition.VariableFormat);
                IProcessorState localState     = new ProcessorState(new MemoryStream(condition), new MemoryStream(), conditionBytes.Count, int.MaxValue, adjustedConfig, new IOperationProvider[0]);
                int             pos            = 0;
                int             len            = conditionBytes.Count;

                bool faulted;
                bool value = _definition.Evaluator(localState, ref len, ref pos, out faulted);

                if (faulted)
                {
                    target.Write(Tokens[0].Value, Tokens[0].Start, Tokens[0].Length);
                    MemoryStream fragment = new MemoryStream();
                    fragment.Write(condition, 0, condition.Length);
                    fragment.Write(_closeConditionTrie.Tokens[0].Value, _closeConditionTrie.Tokens[0].Start, _closeConditionTrie.Tokens[0].Length);
                    fragment.Write(processor.CurrentBuffer, currentBufferPosition, bufferLength - currentBufferPosition);
                    fragment.Position = 0;
                    processor.Inject(fragment);
                    currentBufferPosition = processor.CurrentBufferPosition;
                    int written = Tokens[0].Length;
                    return(written);
                }

                if (value)
                {
                    processor.WhitespaceHandler(ref bufferLength, ref currentBufferPosition, trimBackward: true);
                    return(0);
                }

                processor.SeekBackUntil(_scanBackTrie, true);
                FindEnd(processor, ref bufferLength, ref currentBufferPosition);
                processor.WhitespaceHandler(ref bufferLength, ref currentBufferPosition, _definition.WholeLine, _definition.TrimWhitespace);
                return(0);
            }
Exemple #2
0
            public int HandleMatch(IProcessorState processor, int bufferLength, ref int currentBufferPosition, int token, Stream target)
            {
                bool flag;

                if (processor.Config.Flags.TryGetValue(Conditional.OperationName, out flag) && !flag)
                {
                    byte[] tokenValue = Tokens[token];
                    target.Write(tokenValue, 0, tokenValue.Length);
                    return(tokenValue.Length);
                }

                List <byte> conditionBytes = new List <byte>();

                ScanToCloseCondition(processor, conditionBytes, ref bufferLength, ref currentBufferPosition);
                byte[]          condition  = conditionBytes.ToArray();
                IProcessorState localState = new ProcessorState(new MemoryStream(condition), new MemoryStream(), conditionBytes.Count, int.MaxValue, processor.Config, new IOperationProvider[0]);
                int             pos        = 0;
                int             len        = conditionBytes.Count;

                bool faulted;
                bool value = _definition.Evaluator(localState, ref len, ref pos, out faulted);

                if (faulted)
                {
                    target.Write(Tokens[0], 0, Tokens[0].Length);
                    target.Write(condition, 0, condition.Length);
                    target.Write(_closeConditionTrie.Tokens[0], 0, _closeConditionTrie.Tokens[0].Length);
                    int written = Tokens[0].Length + condition.Length + _closeConditionTrie.Tokens[0].Length;
                    return(written);
                }

                if (value)
                {
                    processor.WhitespaceHandler(ref bufferLength, ref currentBufferPosition, trimBackward: true);
                    return(0);
                }

                processor.SeekBackUntil(_scanBackTrie, true);
                FindEnd(processor, ref bufferLength, ref currentBufferPosition);
                processor.WhitespaceHandler(ref bufferLength, ref currentBufferPosition, _definition.WholeLine, _definition.TrimWhitespace);
                return(0);
            }