Esempio n. 1
0
        public IEither <T, ParseError> Parse(IInputReader input)
        {
            Position position = input.GetPosition();

            var result = this.parserA.Parse(input);

            if (result.IsSuccess || input.GetPosition() != position)
            {
                return(result);
            }

            return(this.parserB.Parse(input));
        }
Esempio n. 2
0
        public void Try_Error_ReturnsErrorAndResetsPosition()
        {
            var parser = Parse.Try(from x in Chars.Any()
                                   from y in Chars.Any()
                                   from f in Parse.Fail <char>("test")
                                   select x);

            IInputReader input            = InputReader.Create("abc");
            Position     expectedPosition = input.GetPosition();

            var result = parser.Parse(input);

            ParseAssert.IsError(result);
            Assert.AreEqual(expectedPosition, input.GetPosition());
        }
        public void Seek_UpdatesPosition()
        {
            IInputReader stream   = CreateInputStream("abc");
            Position     position = stream.GetPosition();

            stream.Read();
            stream.Read();
            stream.Seek(position);

            Position newPosition = stream.GetPosition();

            Assert.AreEqual(position, newPosition);

            char result = (char)stream.Read();

            Assert.AreEqual('a', result);
        }
        public void GetPosition_NothingRead()
        {
            IInputReader stream = CreateInputStream("");

            Position position = stream.GetPosition();

            Assert.AreEqual(1, position.Line, "Line");
            Assert.AreEqual(1, position.Column, "Column");
        }
        public void GetPosition_Read_DoesNotUpdateReturnedPosition()
        {
            IInputReader stream   = CreateInputStream("abc");
            Position     position = stream.GetPosition();

            stream.Read();

            Assert.AreEqual(1, position.Line, "Line");
            Assert.AreEqual(1, position.Column, "Column");
        }
        public void GetPosition_ReadChar()
        {
            IInputReader stream = CreateInputStream("abc\nxyz");

            stream.Read();

            Position position = stream.GetPosition();

            Assert.AreEqual(1, position.Line, "Line");
            Assert.AreEqual(2, position.Column, "Column");
        }
Esempio n. 7
0
        public IParseResult <Unit> TryParse(IInputReader inp)
        {
            var pos    = inp.GetPosition();
            var result = _parser.TryParse(inp);

            if (result.IsSuccess())
            {
                inp.Seek(pos);
                return(Parse.Error <Unit>($"Expect {_parser.Name}, but got '{result.Result}'", inp));
            }
            return(Parse.Success(Unit.Instance));
        }
Esempio n. 8
0
        public IParseResult <T> TryParse(IInputReader inp)
        {
            var pos    = inp.GetPosition();
            var parsed = _parser.TryParse(inp);

            if (!parsed.IsSuccess())
            {
                return(Parse.Error <T>(parsed.Error));
            }
            inp.Seek(pos);
            return(Parse.Success <T>());
        }
Esempio n. 9
0
        public IEither <TResult, ParseError> Parse(IInputReader input)
        {
            TAccum acc = this.seed();

            IEither <T, ParseError> result = null;
            Position position = input.GetPosition();

            while ((result = this.parser.Parse(input)).IsSuccess)
            {
                acc      = this.func(acc, result.FromSuccess());
                position = input.GetPosition();
            }

            if (input.GetPosition() == position)
            {
                return(ParseResult.Success(this.resultSelector(acc)));
            }
            else
            {
                return(ParseResult.Error <TResult>(result.FromError()));
            }
        }
Esempio n. 10
0
        public IEither <T, ParseError> Parse(IInputReader input)
        {
            Position position = input.GetPosition();

            var result = this.parser.Parse(input);

            if (result.IsError)
            {
                input.Seek(position);
            }

            return(result);
        }
Esempio n. 11
0
        public IParseResult <TResult> TryParse(IInputReader inp)
        {
            TAccum acc = _seed();

            do
            {
                var pos    = inp.GetPosition();
                var parsed = _parser.TryParse(inp);
                if (!parsed.IsSuccess())
                {
                    inp.Seek(pos);
                    break;
                }
                acc = _accFunc(acc, parsed.Result);
            } while (!inp.Eof());

            return(Parse.Success(_resultSelector(acc)));
        }
Esempio n. 12
0
        public IParseResult <T1> TryParse(IInputReader inp)
        {
            var parsed1 = _p1.TryParse(inp);

            if (!parsed1.IsSuccess())
            {
                return(parsed1);
            }
            var pos = inp.GetPosition();

            var parsed2 = _p2.TryParse(inp);

            if (!parsed2.IsSuccess())
            {
                return(Parse.Error <T1>(parsed1.Error));
            }
            inp.Seek(pos);
            return(parsed1);
        }
Esempio n. 13
0
        public IParseResult <IEnumerable <T> > TryParse(IInputReader inp)
        {
            var acc = new T[_parsers.Length];
            var idx = 0;
            var pos = inp.GetPosition();

            foreach (var parser in _parsers)
            {
                var parsed = parser.TryParse(inp);
                if (!parsed.IsSuccess())
                {
                    inp.Seek(pos);
                    return(Parse.Error <IEnumerable <T> >(parsed.Error));
                }
                acc[idx] = parsed.Result;
                idx++;
            }

            return(Parse.Success <IEnumerable <T> >(acc));
        }
Esempio n. 14
0
        public IParseResult <T> TryParse(IInputReader inp)
        {
            var pos1    = inp.GetPosition();
            var parsed1 = _parserA.TryParse(inp);

            if (parsed1.IsSuccess())
            {
                return(parsed1);
            }

            inp.Seek(pos1);
            var parsed2 = _parserB.TryParse(inp);

            if (parsed2.IsSuccess())
            {
                return(parsed2);
            }

            var ch = inp.Substr(20);

            return(Parse.Error <T>($"Expect {Name}, but got '{ch}'", inp));
        }
Esempio n. 15
0
 public static IEither <T, ParseError> UnexpectedValue <T>(IInputReader input, object value)
 {
     return(Error <T>(new ParseError(input.GetPosition(), "Unexpected \"" + value.ToString() + "\"")));
 }
Esempio n. 16
0
 public static IEither <T, ParseError> Error <T>(IInputReader input, Func <string> messageFn, ParseError innerError)
 {
     return(Error <T>(new ParseError(input.GetPosition(), messageFn, innerError)));
 }
Esempio n. 17
0
 public static IEither <T, ParseError> Error <T>(IInputReader input, string message)
 {
     return(Error <T>(new ParseError(input.GetPosition(), message)));
 }