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); }); }
public ParserError(string errorMessage, LexemeString <TToken> remainingLexemes) { ErrorMessage = errorMessage; RemainingLexemes = remainingLexemes; }
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); }
public static IParserResult <T, TToken> Success <T, TToken>(T result, LexemeString <TToken> remainingLexemes) => new ParserSuccess <T, TToken>(result, remainingLexemes);
public static IParserResult <T, TToken> Error <T, TToken>(string errorMessage, LexemeString <TToken> remainingLexemes) => new ParserError <T, TToken>(errorMessage, remainingLexemes);
public ParserSuccess(T result, LexemeString <TToken> remainingLexemeString) { Result = result; RemainingLexemes = remainingLexemeString; }