Exemple #1
0
        private void WriteCondition(IVpcConditional value)
        {
            if (value.Negated)
            {
                _writer.Write('!');
            }

            switch (value)
            {
            case VpcConditionalCollection collection:
            {
                _writer.Write('(');
                foreach (var item in collection)
                {
                    WriteCondition(item);
                }
                _writer.Write(')');
                break;
            }

            case VpcConditional conditional:
                _writer.Write($"${conditional.Value}");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            switch (value.Operator)
            {
            case VpcOperator.None:
                break;

            case VpcOperator.And:
                _writer.Write(" && ");
                break;

            case VpcOperator.Or:
                _writer.Write(" || ");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private void EndVariable()
        {
            if (_lastConditional == null)
            {
                return;
            }

            // negation
            _lastConditional.Negated = _nextNegated;

            // set its name
            if (_lastConditional is VpcConditional conditional)
            {
                conditional.Value = _builder.ToString();
            }
            _builder.Clear();

            _conditionalStack.Peek().Add(_lastConditional); // append to the next one up the stack

            // reset
            _nextNegated     = false;
            _lastConditional = null;
        }
        private void ReadCondition(char @char)
        {
            // ignore whitespace
            if (char.IsWhiteSpace(@char))
            {
                return;
            }

            // operator
            else if (@char == '&' || @char == '|')
            {
                // If we see any operator, set the operator of the last statement
                if (NextChar() != @char)
                {
                    throw new Exception("Invalid conditional operator!");
                }
                _lastConditional.Operator = (VpcOperator)@char;

                // we can probably do it here
                EndVariable();
            }

            // negation
            else if (@char == '!')
            {
                _nextNegated = true;
            }

            // end
            else if (@char == ']')
            {
                // Still characters left in the builder? Add one.
                if (_builder.Length > 0)
                {
                    EndVariable();
                }

                PopCurrentObject();

                // pop the conditional
                _conditionalStack.Pop();

                // pop the state again to get out of the array
                _stateStack.Pop();
            }
            // start of conditional sub-array
            else if (@char == '(')
            {
                _conditionalStack.Push(new VpcConditionalCollection());
            }
            else if (@char == ')')
            {
                // end of conditional sub-array
                EndVariable();
                _lastConditional = _conditionalStack.Pop();
            }
            else if (@char == '$')
            {
                // start of variable also terminates the last variable
                EndVariable();
                _lastConditional = new VpcConditional(null, VpcOperator.None);
            }
            else
            {
                _builder.Append(@char);
            }
        }