예제 #1
0
    public IResult <string> Parse(IParseState <char> state)
    {
        Assert.ArgumentNotNull(state, nameof(state));
        var startCp = state.Input.Checkpoint();
        var result  = Engine.GetMatch(state.Input, Regex);

        if (!result.Success)
        {
            startCp.Rewind();
        }
        return(state.Result(this, result));
    }
예제 #2
0
 public Either <IParseState> Match(IParseState State)
 {
     return(m_Rules
            .Select(R => Try.Call(() => R.Match(State)))
            .Where(E => !E.HasError())
            .Select(E => E.Value)
            .Where(E => !E.HasError())
            .OrderBy(S => S.Value.Tokens.Count)
            .FirstOrDefault(E => !E.HasError())
            ??
            new Exception("No match was found"));
 }
예제 #3
0
        private IParser <TInput, TOutput> GetParserFromCacheOrCallback(IParseState <TInput> state)
        {
            var existing = state.Cache.Get <IParser <TInput, TOutput> >(this, default);

            if (existing.Success)
            {
                return(existing.Value);
            }

            var parser = GetParser() ?? throw new InvalidOperationException("Deferred parser value must not be null");

            state.Cache.Add(this, default, parser);
예제 #4
0
            internal override InternalResult <IEnumerable <T> > Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    // state.Error set by _parser
                    return(InternalResult.Failure <IEnumerable <T> >(result.ConsumedInput));
                }
                return(Rest(_remainderParser, state, new List <T> {
                    result.Value
                }, result.ConsumedInput));
            }
예제 #5
0
파일: TokenList.cs 프로젝트: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////////////////////////////

        public void Insert(
            int index,
            IToken item,
            IParseState parseState
            )
        {
            if ((item != null) && (parseState != null))
            {
                item.EndLine = parseState.CurrentLine;
            }

            this.Insert(index, item);
        }
예제 #6
0
        private Either <IParseState> _CreateExpression(IParseState OldState, IParseState NewState)
        {
            var t_Attempt = m_CreateExpression
                            .Select(F => _AttemptCreate(F, OldState, NewState))
                            .FirstOrDefault(E => !E.HasError());

            return(t_Attempt == null ?
                   new Exception("Unable to match any expression creation function") :
                   t_Attempt.HasError() ?
                   t_Attempt.Error :
                   new Either <IParseState>(
                       NewState.ToCopy(NewState.Tokens, OldState.Expressions.Append(t_Attempt.Value))
                       ));
        }
예제 #7
0
    public ParseContext(IParseState <TInput> state, Engine <TInput, TOutput> engine, int rbp, bool canRecurse, string name, ParseControl parseControl)
    {
        Assert.ArgumentNotNull(state, nameof(state));
        Assert.ArgumentNotNull(engine, nameof(engine));
        Assert.ArgumentNotNull(parseControl, nameof(parseControl));

        _state         = state;
        _engine        = engine;
        _rbp           = rbp;
        _startConsumed = state.Input.Consumed;
        Name           = name;
        _parseControl  = parseControl;
        _canRecurse    = canRecurse;
    }
예제 #8
0
            internal override InternalResult <T> Parse(IParseState <TToken> state)
            {
                state.PushBookmark();

                var result = _parser.Parse(state);

                if (result.Success)
                {
                    state.Rewind();
                    return(InternalResult.Success <T>(result.Value, false));
                }
                state.PopBookmark();
                return(result);
            }
예제 #9
0
    public PartialResult <TOutput> TryParse(IParseState <TInput> state, int rbp, ParseControl parseControl)
    {
        Assert.ArgumentNotNull(state, nameof(state));
        var levelCp = state.Input.Checkpoint();

        try
        {
            return(Parse(state, rbp, parseControl));
        }
        catch (ParseException pe) when(pe.Severity == ParseExceptionSeverity.Level)
        {
            levelCp.Rewind();
            return(new PartialResult <TOutput>(pe.Message, pe.Location ?? state.Input.CurrentLocation));
        }
    }
예제 #10
0
    public IMultiResult <TOutput> Parse(IParseState <TInput> state)
    {
        var startCheckpoint = state.Input.Checkpoint();
        var results         = new List <IResultAlternative <TOutput> >();

        foreach (var parser in _parsers)
        {
            var result = ParseOne(parser, state, startCheckpoint);
            results.Add(result);
            startCheckpoint.Rewind();
            continue;
        }

        return(new MultiResult <TOutput>(this, startCheckpoint.Location, startCheckpoint, results));
    }
예제 #11
0
            internal sealed override Result <TToken, IEnumerable <T> > Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    return(Result.Failure <TToken, IEnumerable <T> >(
                               result.Error,
                               result.ConsumedInput
                               ));
                }
                return(ManyImpl(_parser, state, _keepResults ? new List <T> {
                    result.Value
                } : null, result.ConsumedInput));
            }
예제 #12
0
            internal override Result <TToken, IEnumerable <T> > Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    return(Result.Failure <TToken, IEnumerable <T> >(
                               result.Error,
                               result.ConsumedInput
                               ));
                }
                return(ManyImpl(_remainderParser, state, new List <T> {
                    result.Value
                }, result.ConsumedInput));
            }
예제 #13
0
        public IResult Parse(IParseState <TInput> state)
        {
            var startCheckpoint = state.Input.Checkpoint();
            var result = _func(state);

            if (!result.Success)
            {
                startCheckpoint.Rewind();
                return(result);
            }

            var totalConsumed = state.Input.Consumed - startCheckpoint.Consumed;

            return(result.AdjustConsumed(totalConsumed));
        }
예제 #14
0
        static LiftedList <Instruction> GetSwitchTargets(IParseState <byte, InstructionParseState> input, int n)
        {
            var remainingInput   = input.RemainingInput;
            var methodDefinition = input.Result.MethodDefinition;
            var context          = input.Result.InstructionContext;
            var operand          = new LiftedList <Instruction>(
                n,
                index => {
                var target = remainingInput.SubList(checked ((index + 1) * 4)).ReadLittleEndianInt();
                return(methodDefinition.FindInstruction(context.Offset + context.Size + target));
            }
                );

            return(operand);
        }
예제 #15
0
            internal sealed override Result <TToken, R> Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    return(Result.Failure <TToken, R>(result.Error, result.ConsumedInput));
                }
                var result2 = _func(result.Value).Parse(state);

                if (!result2.Success)
                {
                    return(Result.Failure <TToken, R>(result2.Error, result.ConsumedInput || result2.ConsumedInput));
                }
                return(Result.Success <TToken, R>(_result(result.Value, result2.Value), result.ConsumedInput || result2.ConsumedInput));
            }
예제 #16
0
파일: Parser.Try.cs 프로젝트: n9/Pidgin
            internal sealed override InternalResult <T> Parse(IParseState <TToken> state)
            {
                // start buffering the input
                state.PushBookmark();
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    // return to the start of the buffer and discard the bookmark
                    state.Rewind();
                    return(InternalResult.Failure <T>(false));
                }

                // discard the buffer
                state.PopBookmark();
                return(result);
            }
예제 #17
0
        public virtual Either <IParseState> Match(IParseState State)
        {
            var t_CurrentState = State;

            foreach (var t_Rule in m_Rules)
            {
                var t_MatchData = _MatchRule(t_Rule, t_CurrentState);
                if (t_MatchData.HasError())
                {
                    return(new Exception("Rule Match Error", t_MatchData.Error));
                }

                t_CurrentState = t_MatchData.Value;
            }

            return(new Either <IParseState>(t_CurrentState));
        }
예제 #18
0
            internal sealed override InternalResult <Unit> Parse(IParseState <TToken> state)
            {
                var result = state.Peek();

                if (result.HasValue)
                {
                    state.Error = new ParseError <TToken>(
                        result,
                        false,
                        Expected,
                        state.SourcePos,
                        null
                        );
                    return(InternalResult.Failure <Unit>(false));
                }
                return(InternalResult.Success(Unit.Value, false));
            }
예제 #19
0
            internal sealed override Result <TToken, T> Parse(IParseState <TToken> state)
            {
                var failureResult = Result.Failure <TToken, T>(
                    new ParseError <TToken>(Maybe.Nothing <TToken>(), false, Expected, state.SourcePos, "OneOf had no arguments"),
                    false
                    );

                foreach (var p in _parsers)
                {
                    var thisResult = p.Parse(state);
                    if (thisResult.Success || thisResult.ConsumedInput)
                    {
                        return(thisResult);
                    }
                    failureResult = LongestMatch(failureResult, thisResult);
                }
                return(failureResult.WithExpected(Expected));
            }
예제 #20
0
            internal sealed override InternalResult <R> Parse(IParseState <TToken> state)
            {
                var consumedInput = false;


                var result1 = _p1.Parse(state);

                consumedInput = consumedInput || result1.ConsumedInput;
                if (!result1.Success)
                {
                    state.Error = state.Error.WithExpected(Expected);
                    return(InternalResult.Failure <R>(
                               consumedInput
                               ));
                }

                var result2 = _p2.Parse(state);

                consumedInput = consumedInput || result2.ConsumedInput;
                if (!result2.Success)
                {
                    state.Error = state.Error.WithExpected(Expected);
                    return(InternalResult.Failure <R>(
                               consumedInput
                               ));
                }

                var result3 = _p3.Parse(state);

                consumedInput = consumedInput || result3.ConsumedInput;
                if (!result3.Success)
                {
                    state.Error = state.Error.WithExpected(Expected);
                    return(InternalResult.Failure <R>(
                               consumedInput
                               ));
                }

                return(InternalResult.Success <R>(_func(
                                                      result1.Value,
                                                      result2.Value,
                                                      result3.Value
                                                      ), consumedInput));
            }
예제 #21
0
            internal sealed override Result <TToken, R> Parse(IParseState <TToken> state)
            {
                var consumedInput = false;


                var result1 = _p1.Parse(state);

                consumedInput = consumedInput || result1.ConsumedInput;
                if (!result1.Success)
                {
                    return(Result.Failure <TToken, R>(
                               result1.Error.WithExpected(Expected),
                               consumedInput
                               ));
                }

                var result2 = _p2.Parse(state);

                consumedInput = consumedInput || result2.ConsumedInput;
                if (!result2.Success)
                {
                    return(Result.Failure <TToken, R>(
                               result2.Error.WithExpected(Expected),
                               consumedInput
                               ));
                }

                var result3 = _p3.Parse(state);

                consumedInput = consumedInput || result3.ConsumedInput;
                if (!result3.Success)
                {
                    return(Result.Failure <TToken, R>(
                               result3.Error.WithExpected(Expected),
                               consumedInput
                               ));
                }

                return(Result.Success <TToken, R>(_func(
                                                      result1.GetValueOrDefault(),
                                                      result2.GetValueOrDefault(),
                                                      result3.GetValueOrDefault()
                                                      ), consumedInput));
            }
예제 #22
0
        static Instruction CreateInstruction(IParseState <Byte, InstructionParseState> state)
        {
            var results = state.Result;

            results.InstructionContext.Size = state.ConsumedInput.Count;
            return(new Instruction(
                       results.Opcode,
                       results.InstructionContext.Offset,
                       results.InstructionContext.Size,
                       results.Operand,
                       state.ConsumedInput,
                       () => {
                var b = new StringBuilder();
                results.PrettyPrint(b);
                return b.ToString();
            },
                       results.NestedInstruction
                       ));
        }
예제 #23
0
            internal sealed override Result <TToken, Unit> Parse(IParseState <TToken> state)
            {
                var result = state.Peek();

                if (result.HasValue)
                {
                    return(Result.Failure <TToken, Unit>(
                               new ParseError <TToken>(
                                   result,
                                   false,
                                   Expected,
                                   state.SourcePos,
                                   null
                                   ),
                               false
                               ));
                }
                return(Result.Success <TToken, Unit>(Unit.Value, false));
            }
예제 #24
0
파일: TokenList.cs 프로젝트: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////////////////////////////

        public void InsertRange(
            int index,
            IEnumerable <IToken> collection,
            IParseState parseState
            )
        {
            if (parseState != null)
            {
                foreach (IToken item in collection)
                {
                    if (item != null)
                    {
                        item.EndLine = parseState.CurrentLine;
                    }
                }
            }

            this.InsertRange(index, collection);
        }
예제 #25
0
            internal sealed override InternalResult <R> Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    // state.Error set by _parser
                    return(InternalResult.Failure <R>(result.ConsumedInput));
                }
                var nextParser = _func(result.Value);
                var result2    = nextParser.Parse(state);

                if (!result2.Success)
                {
                    // state.Error set by nextParser
                    return(InternalResult.Failure <R>(result.ConsumedInput || result2.ConsumedInput));
                }
                return(InternalResult.Success <R>(_result(result.Value, result2.Value), result.ConsumedInput || result2.ConsumedInput));
            }
예제 #26
0
            internal override Result <TToken, IEnumerable <T> > Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    return(Result.Failure <TToken, IEnumerable <T> >(result.Error, result.ConsumedInput));
                }
                var ts = new List <T> {
                    result.Value
                };
                var consumedInput = result.ConsumedInput;

                while (true)
                {
                    var sepResult = _separator.Parse(state);
                    consumedInput = consumedInput || sepResult.ConsumedInput;
                    if (sepResult.ConsumedInput && !sepResult.Success)
                    {
                        return(Result.Failure <TToken, IEnumerable <T> >(sepResult.Error, consumedInput));
                    }
                    if (!sepResult.ConsumedInput)
                    {
                        return(Result.Success <TToken, IEnumerable <T> >(ts, consumedInput));
                    }

                    var itemResult = _parser.Parse(state);
                    consumedInput = consumedInput || itemResult.ConsumedInput;
                    if (itemResult.ConsumedInput && !itemResult.Success)
                    {
                        return(Result.Failure <TToken, IEnumerable <T> >(sepResult.Error, consumedInput));
                    }
                    if (itemResult.Success)
                    {
                        ts.Add(itemResult.GetValueOrDefault());
                    }
                    if (!itemResult.ConsumedInput)
                    {
                        return(Result.Success <TToken, IEnumerable <T> >(ts, consumedInput));
                    }
                }
            }
예제 #27
0
            internal override InternalResult <IEnumerable <T> > Parse(IParseState <TToken> state)
            {
                var result = _parser.Parse(state);

                if (!result.Success)
                {
                    // state.Error set by _parser
                    return(InternalResult.Failure <IEnumerable <T> >(result.ConsumedInput));
                }
                var ts = new List <T> {
                    result.Value
                };
                var consumedInput = result.ConsumedInput;

                while (true)
                {
                    var sepResult = _separator.Parse(state);
                    consumedInput = consumedInput || sepResult.ConsumedInput;
                    if (!sepResult.Success)
                    {
                        if (sepResult.ConsumedInput)
                        {
                            // state.Error set by _separator
                            return(InternalResult.Failure <IEnumerable <T> >(consumedInput));
                        }
                        return(InternalResult.Success <IEnumerable <T> >(ts, consumedInput));
                    }

                    var itemResult = _parser.Parse(state);
                    consumedInput = consumedInput || itemResult.ConsumedInput;
                    if (!itemResult.Success)
                    {
                        if (itemResult.ConsumedInput)
                        {
                            // state.Error set by _parser
                            return(InternalResult.Failure <IEnumerable <T> >(consumedInput));
                        }
                        return(InternalResult.Success <IEnumerable <T> >(ts, consumedInput));
                    }
                    ts.Add(itemResult.Value);
                }
            }
예제 #28
0
            internal sealed override Result <TToken, IEnumerable <T> > Parse(IParseState <TToken> state)
            {
                var consumedInput = false;
                var ts            = new List <T>();

                foreach (var p in _parsers)
                {
                    var result = p.Parse(state);
                    consumedInput = consumedInput || result.ConsumedInput;
                    if (!result.Success)
                    {
                        return(Result.Failure <TToken, IEnumerable <T> >(
                                   result.Error.WithExpected(Expected),
                                   consumedInput
                                   ));
                    }
                    ts.Add(result.GetValueOrDefault());
                }
                return(Result.Success <TToken, IEnumerable <T> >(ts, consumedInput));
            }
예제 #29
0
        public IResult <TOutput> Parse(IParseState <TInput> state)
        {
            Assert.ArgumentNotNull(state, nameof(state));

            var startCp = state.Input.Checkpoint();

            var leftResult = _item.Parse(state);

            if (!leftResult.Success)
            {
                return(leftResult);
            }

            return(_quantifier switch
            {
                Quantifier.ExactlyOne => ParseExactlyOne(state, leftResult, startCp),
                Quantifier.ZeroOrOne => ParseZeroOrOne(state, leftResult),
                Quantifier.ZeroOrMore => ParseZeroOrMore(state, leftResult),
                _ => throw new InvalidOperationException("Unsupported quantifier"),
            });
예제 #30
0
        public override Either <IParseState> Match(IParseState State)
        {
            var t_CurrentState = State;

            while (true)
            {
                var t_NewState = _MatchRules(t_CurrentState);
                if (t_NewState.HasError())
                {
                    break;
                }

                t_CurrentState = t_NewState.Value;
            }

            return
                (State.Expressions.Count() == t_CurrentState.Expressions.Count() ?
                 new Exception("No Rules Matched") :
                 new Either <IParseState>(t_CurrentState));
        }