def any_of(input): if input.is_eof: return Result.failure(ParseException('EOF'), input) current = input.current if not predicate(current): return Result.failure( ParseException( 'Bad symbol at position {pos}, got \'{curr}\'{exp}'. format(pos=input.position, curr=current, exp='' if expected_str is None else ', expected: {}'.format(expected_str))), input) return Result.success(current, input.advance())
def one_or_many(input): parsed = Result.failure(None, input) result = [] for i in xrange(0, start): parsed = self(parsed.reminder) if parsed.is_failed: return Result.failure(parsed.error, input) else: result.append(parsed.value) while parsed.is_successful: parsed = self(parsed.reminder) if parsed.is_successful: result.append(parsed.value) return Result.success(result, parsed.reminder)
def string(input): reminder = input builder = [] for idx in xrange(0, len(arg)): if not reminder.is_eof: builder.append(reminder.current) reminder = reminder.advance() else: return Result.failure( ParseException( 'Got EOF while parsing \'{}\''.format(arg)), input) builder = ''.join(builder) if arg == builder: return Result.success(builder, reminder) else: return Result.failure( ParseException('\'{}\' not found'.format(arg)), input)
def prior(input): parsed = self(input) if parsed.is_successful: next_parsed = other(parsed.reminder) if next_parsed.is_successful: return Result.success(parsed.value, next_parsed.reminder) else: return Result.failure(next_parsed.error, input) else: return parsed
def eof(input): if input.is_eof: return Result.success(None, input) else: return Result.failure( ParseException('Expected end of file (EOF)'), input)