예제 #1
0
        public static Parser <List <T>, TToken> Many <T, TToken>(this Parser <T, TToken> parser, int least = 0, int most = int.MaxValue)
        {
            if (parser is null)
            {
                throw new ArgumentNullException(nameof(parser));
            }

            return(input =>
            {
                List <T> elements = new List <T>();
                LexemeString <TToken> remainder = input;

                for (int i = 0; i <= most; i++)
                {
                    var result = parser(remainder);

                    if (!result.IsSuccessful)
                    {
                        if (i < least)
                        {
                            return ParserResult.Error <T, List <T>, TToken>(result);
                            //return ParserResult.Error<List<T>, TToken>("Too few elements. Expected at least " + least, remainder);
                        }

                        return ParserResult.Success(elements, remainder);
                    }

                    elements.Add(result.Result);

                    remainder = result.RemainingLexemes;
                }

                return ParserResult.Success(elements, remainder);
            });
        }
예제 #2
0
 public ParserError(string errorMessage, LexemeString <TToken> remainingLexemes)
 {
     ErrorMessage     = errorMessage;
     RemainingLexemes = remainingLexemes;
 }
예제 #3
0
        private static IParserResult <T, Token> ParseEoF <T>(Parser <T, Token> parser, LexemeString <Token> lexemes)
        {
            var result = parser(lexemes);

            if (!result.IsSuccessful)
            {
                return(result);
            }

            if (result.RemainingLexemes.Length > 1)
            {
                var remaining = result.RemainingLexemes[0] + (result.RemainingLexemes.Length > 1 ? " " + result.RemainingLexemes[1] : "");
                return(ParserResult.Error <T, Token>("Error matching " + remaining + " found " + result.Result, result.RemainingLexemes));
            }

            return(result);
        }
예제 #4
0
 public static IParserResult <T, TToken> Success <T, TToken>(T result, LexemeString <TToken> remainingLexemes) =>
 new ParserSuccess <T, TToken>(result, remainingLexemes);
예제 #5
0
 public static IParserResult <T, TToken> Error <T, TToken>(string errorMessage, LexemeString <TToken> remainingLexemes) =>
 new ParserError <T, TToken>(errorMessage, remainingLexemes);
예제 #6
0
 public ParserSuccess(T result, LexemeString <TToken> remainingLexemeString)
 {
     Result           = result;
     RemainingLexemes = remainingLexemeString;
 }