Beispiel #1
0
        public Value ApplyWhile()
        {
            var argument = Arguments.ApplyValue;
            var variable = Arguments.ApplyVariable;

            State.PushPatternManager();
            State.Multi = true;
            var    isVariable = variable != null;
            var    isReadOnly = isVariable && Regions.IsReadOnly(variable.Name);
            var    array      = new Array();
            string input;

            switch (argument)
            {
            case PatternResult patternResult when patternResult.Success:
                input      = patternResult.Text;
                variable   = patternResult.Variable;
                isVariable = variable != null;
                break;

            case PatternResult _:
                return(PatternResult.Falure());

            default:
                input = argument.Text;
                break;
            }

            var oneSuccess = false;

            for (var i = 0; i < MAX_LOOP && withinLimit(i); i++)
            {
                if (Scan(input))
                {
                    State.Alternates.Clear();
                    if (withinNth(i))
                    {
                        oneSuccess = true;
                    }
                }
                else
                {
                    break;
                }

                input = State.Input;
                if (isReadOnly)
                {
                    array.Add(input.Skip(startIndex).Take(stopIndex - startIndex));
                }
            }

            if (isReadOnly)
            {
                State.PopPatternManager();
                return(oneSuccess ? array : new Array());
            }

            var result = oneSuccess ? new PatternResult
            {
                Input      = input,
                Text       = State.Input.Copy(),
                Value      = argument,
                Success    = true,
                StartIndex = startIndex,
                StopIndex  = stopIndex,
                Variable   = variable
            } : PatternResult.Falure();

            if (isVariable && result.IsTrue)
            {
                variable.Value = State.Input.Copy();
            }
            if (State.Result != null)
            {
                var managerResult = State.Result;
                State.Result = null;
                State.PopPatternManager();
                return(managerResult);
            }

            State.PopPatternManager();
            return(result);
        }
Beispiel #2
0
        public Value Apply()
        {
            var argument = Arguments.ApplyValue;
            var variable = Arguments.ApplyVariable;

            State.PushPatternManager();
            var    isVariable = variable != null;
            var    isReadOnly = isVariable && Regions.IsReadOnly(variable.Name) || !isVariable;
            string input;

            if (argument is PatternResult patternResult)
            {
                if (patternResult.Success)
                {
                    input      = patternResult.Text;
                    variable   = patternResult.Variable;
                    isVariable = variable != null;
                }
                else
                {
                    return(PatternResult.Falure());
                }
            }
            else
            {
                input = argument.Text;
            }

            var matched = Scan(input);
            var text    = State.Input.Copy();

            if (isReadOnly)
            {
                State.PopPatternManager();
                return(matched ? (Value) new Some(text.Skip(startIndex).Take(stopIndex - startIndex)) : new None());
            }

            var result = matched ? new PatternResult
            {
                Input      = input,
                Text       = text,
                Value      = argument,
                Success    = true,
                StartIndex = startIndex,
                StopIndex  = stopIndex,
                Variable   = variable
            } : PatternResult.Falure();

            if (isVariable && result.IsTrue)
            {
                variable.Value = text;
            }
            if (State.Result != null)
            {
                var managerResult = State.Result;
                State.Result = null;
                State.PopPatternManager();
                return(managerResult);
            }

            State.PopPatternManager();
            return(result);
        }