Welcome to parsemon2’s documentation!¶
parsemon¶
-
exception
parsemon.
ParsingFailed
[source]¶ Base exception class for all possible ways a parser can fail
-
class
parsemon.
ParsingResult
(value: Any, remaining_input: str)[source]¶ -
remaining_input
: str¶
-
value
: Any¶
-
-
parsemon.
chain
(first, second, *rest)[source]¶ Combine to parsers and only use the result of the second parser
- Parameters
first – a parser that consumes input, the result will be discarded
second – a parser that is applied after first, the result of this parser will be returned by the resulting parser
-
parsemon.
choice
(first_parser, second_parser)[source]¶ Try one parser and try a second one if the first one fails
-
parsemon.
do
(f)[source]¶ Use this as a decorator
Do allows you to combine parsers in a more pythonic way.
It expects a generator that returns something in the end and takes no arguments.
It lets you write parsers like the following:
@do def three_letters(): first = yield character() second = yield character() third = yield character() return first + second + third
-
parsemon.
enclosed_by
(parser, prefix_parser, suffix_parser=None)[source]¶ Parse a string enclosed by delimeters
The parser
enclosed_by(many(none_of('"')),literal('"'))
will consume the string"example"
and return the python string'example'
.
-
parsemon.
end_of_file
()[source]¶ Returns a parser that only succeeds with a value of None if there would be no further input to consume
-
parsemon.
floating_point
(delimiter: str = '.')[source]¶ Parse a floating point number.
- Parameters
delimiter – defaults to ., is expected token to seperate integer part from rational part
-
parsemon.
literal
(expected)[source]¶ Parses exactly the string given and nothing else.
If the parser already fails on the first element, no input will be consumed.
-
parsemon.
many
(original_parser)[source]¶ Apply a parser 0 or more times
The resulting parser is greedy, which means that it will be applied as often as possible, which also includes 0 times. Think of this as Kleene-Star.
- Parameters
original_parser – this parser will be applied as often as possible by the resulting new parser
-
parsemon.
many1
(original_parser)[source]¶ Apply a parser 1 or more times
The resulting parser is greedy, which means that it will be applied as often as possible. Think of this as an equivalent to the ‘+’ operator in regular expressions.
- original_parser – this parser will be applied 1 or more times by the
resulting parser
-
parsemon.
none_of
(chars: str)[source]¶ Parse any character except the ones in
chars
This parser will fail if it finds a character that is in
chars
.
-
parsemon.
run_parser
(p, input_string: str, stream_implementation: Type[parsemon.stream.Stream] = <class 'parsemon.stream.StringStream'>)[source]¶ Parse string input_string with parser p
-
parsemon.
seperated_by
(parser, seperator)[source]¶ Apply the input parser as often as possible, where occurences are seperated by input that can be parsed by ‘seperator’.
This can be useful to parse lists with seperators in between. The parser
seperated_by(many(none_of(',')), literal(','))
will parse the string1,2,3,4
and return the list['1','2','3','4']
.
-
parsemon.
until
(repeating_parser, delimiter_parser)[source]¶ Parse repeating_parser until delimiter_parser is found.
delimiter_parser is has always precedence over repeating_parser. You can think about it this way: First we check if delimiter_parser matches the input succesfully, if this is not the case, we try repeating_parser. If repeating_parser fails to match, then until(repeating_parser, delimiter_parser) as a whole fails. If repeating_parser matches, then we start over again. When delimiter_parser matches eventually, we return all results of repeating_parser in a list.
Note that both, delimiter_parser and repeating_parser consume input. This is especially important if both parser have overlap on the characters they consume, e.g. until(character(), literal(‘end’)). Make use of try_parser and look_ahead as you see fit for your usecase.
-
parsemon.
whitespace
(stream, cont)¶
parsemon.basic¶
Implement basic parsers that should be generally useful
parsemon.parser¶
This module contains the basic building blocks for implementing parsers
-
parsemon.parser.
chain
(first, second, *rest)[source]¶ Combine to parsers and only use the result of the second parser
- Parameters
first – a parser that consumes input, the result will be discarded
second – a parser that is applied after first, the result of this parser will be returned by the resulting parser
-
parsemon.parser.
choice
(first_parser, second_parser)[source]¶ Try one parser and try a second one if the first one fails
-
parsemon.parser.
choices
(parser, *parsers)[source]¶ Try the given parsers one at a time until one succeeds
-
parsemon.parser.
enclosed_by
(parser, prefix_parser, suffix_parser=None)[source]¶ Parse a string enclosed by delimeters
The parser
enclosed_by(many(none_of('"')),literal('"'))
will consume the string"example"
and return the python string'example'
.
-
parsemon.parser.
many
(original_parser)[source]¶ Apply a parser 0 or more times
The resulting parser is greedy, which means that it will be applied as often as possible, which also includes 0 times. Think of this as Kleene-Star.
- Parameters
original_parser – this parser will be applied as often as possible by the resulting new parser
-
parsemon.parser.
many1
(original_parser)[source]¶ Apply a parser 1 or more times
The resulting parser is greedy, which means that it will be applied as often as possible. Think of this as an equivalent to the ‘+’ operator in regular expressions.
- original_parser – this parser will be applied 1 or more times by the
resulting parser
-
parsemon.parser.
run_parser
(p, input_string: str, stream_implementation: Type[parsemon.stream.Stream] = <class 'parsemon.stream.StringStream'>)[source]¶ Parse string input_string with parser p
-
parsemon.parser.
seperated_by
(parser, seperator)[source]¶ Apply the input parser as often as possible, where occurences are seperated by input that can be parsed by ‘seperator’.
This can be useful to parse lists with seperators in between. The parser
seperated_by(many(none_of(',')), literal(','))
will parse the string1,2,3,4
and return the list['1','2','3','4']
.
-
parsemon.parser.
until
(repeating_parser, delimiter_parser)[source]¶ Parse repeating_parser until delimiter_parser is found.
delimiter_parser is has always precedence over repeating_parser. You can think about it this way: First we check if delimiter_parser matches the input succesfully, if this is not the case, we try repeating_parser. If repeating_parser fails to match, then until(repeating_parser, delimiter_parser) as a whole fails. If repeating_parser matches, then we start over again. When delimiter_parser matches eventually, we return all results of repeating_parser in a list.
Note that both, delimiter_parser and repeating_parser consume input. This is especially important if both parser have overlap on the characters they consume, e.g. until(character(), literal(‘end’)). Make use of try_parser and look_ahead as you see fit for your usecase.
-
parsemon.parser.
whitespace
(stream, cont)¶ Parse any character that is classified as a whitespace character by unicode standard. That includes newline characters.