def between_(open, close): open, close = sequence(open, many(space)), sequence(many(space), close) return lambda parser: between(open, close, parser)
semi = trailing_space(char(';')) colon = trailing_space(char(':')) dot = trailing_space(char('.')) sepBy_ = lambda sep: lambda parser: sepBy(parser, sep) sepBy1_ = lambda sep: lambda parser: sepBy1(parser, sep) semiSep = sepBy_(semi) semiSep1 = sepBy1_(semi) commaSep = sepBy_(comma) commaSep1 = sepBy1_(comma) def updateSign(sign, num): return num * (-1 if sign == '-' else 1) natural = fmap(int, digits) integer = fmap(lambda x: updateSign(*x), sequence(option('', oneOf('-+')), natural)) doublePos = fmap(lambda x: float(''.join(x)), sequence(digits, char('.'), option('0', digits))) double = fmap(lambda x: updateSign(*x), sequence(option('', oneOf('-+')), doublePos)) integerOrDouble = tryP(double) | integer
def string(s): '''Parses a string 's'. Returns it. ''' return syntax_tree(mkString)(sequence(*map(char, s)))
sepBy_ = lambda sep: lambda parser: sepBy(parser, sep) sepBy1_ = lambda sep: lambda parser: sepBy1(parser, sep) semiSep = sepBy_(semi) semiSep1 = sepBy1_(semi) commaSep = sepBy_(comma) commaSep1 = sepBy1_(comma) def updateSign(sign, num): return num * (-1 if sign == '-' else 1) natural = fmap(int, digits) integer = fmap(lambda x: updateSign(*x), sequence(option('', oneOf('-+')), natural)) doublePos = fmap(lambda x: float(''.join(x)), sequence(digits, char('.'), option('0', digits))) double = fmap(lambda x: updateSign(*x), sequence(option('', oneOf('-+')), doublePos)) integerOrDouble = tryP(double) | integer