rads.config.xml_parsers module

Parser combinators for reading XML files.

This module is heavily based on PEGTL, a parser combinator library for C++.

Exceptions

Name

Description

TerminalXMLParseError

Parse error that fails parsing of XML file.

XMLParseError

Parse error that fails a single parser.

Parser Combinators (class based API)

Name

Description

Parser

Base class of all parser combinators.

Apply

Apply a function the value result of a parser.

Lazy

Delay parser construction until evaluation.

Must

Require parser to succeed.

At

Non consuming match.

Not

Invert a parser match, non consuming.

Repeat

Match zero or more times (greedily).

Sequence

Chain parsers together, all or nothing match.

Alternate

Chain parsers together, taking first success.

Success

Always succeeds, non consuming.

Failure

Always fails, non consuming.

Start

Match beginning of XML block.

End

Match end of XML block.

AnyElement

Match any XML element.

Tag

Match an XML element by it’s tag name.

Parser Combinators (function based API)

Name

Description

lazy()

Delay parser construction until evaluation.

at()

Non consuming match.

not_at()

Invert a parser match, non consuming.

opt()

Optional match, always succeeds.

plus()

Match one or more times (greedily).

seq()

Match a sequence of parsers, all or nothing.

sor()

Use first matching parser.

star()

Match zero ore more times (greedily).

must()

Require parser to succeed.

rep()

Match N times.

until()

Consume elements until a match, match not consumed.

failure()

Always fails, non consuming.

success()

Always succeeds, non consuming.

start()

Match beginning of XML block.

end()

Match end of XML block.

any()

Match any XML element.

tag()

Match an XML element by it’s tag name.

exception rads.config.xml_parsers.TerminalXMLParseError(file: Optional[str], line: Optional[int], message: Optional[str] = None)[source]

Bases: Exception

A parse error that should fail parsing of the entire file.

Parameters
  • file – Name of file that was being parsed when the error occurred.

  • line – Line number in the file that was being parsed when the error occurred.

  • message – An optional message (instead of the default ‘parsing failed’) detailing why the parse failed.

file = None

Filename where the error occurred.

line = None

Line number in the file where the error occurred.

message = 'parsing failed'

The message provided when the error was constructed.

exception rads.config.xml_parsers.XMLParseError(file: Optional[str], line: Optional[int], message: Optional[str] = None)[source]

Bases: rads.config.xml_parsers.TerminalXMLParseError

A parse error that signals that a given parser failed.

Unlike TerminalXMLParseError is expected and simply signals that the parser did not match and another parser should be tried.

Parameters
  • file – Name of file that was being parsed when the error occurred.

  • line – Line number in the file that was being parsed when the error occurred.

  • message – An optional message (instead of the default ‘parsing failed’) detailing why the parse failed.

terminal(message: Optional[str] = None) → rads.config.xml_parsers.TerminalXMLParseError[source]

Raise this local failure to a global failure.

Parameters

message – Optionally a new message.

Returns

A global parse failure with the same file and line, and possibly message as this exception.

rads.config.xml_parsers.next_element(pos: rads.xml.base.Element) → rads.xml.base.Element[source]

Get next element lazily.

Parameters

pos – Current element.

Returns

Next sibling XML element.

Raises

XMLParseError – If there is no next sibling element.

rads.config.xml_parsers.first_child(pos: rads.xml.base.Element) → rads.xml.base.Element[source]

Get first child of element, lazily.

class rads.config.xml_parsers.Parser[source]

Bases: abc.ABC

Base parser combinator.

abstract __call__(position: rads.xml.base.Element) → Tuple[Any, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
__add__(other: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Sequence[source]

Combine two parsers, matching the first followed by the second.

Multiple consecutive uses of ‘+’ will result in a single Sequence because the Sequence class automatically flattens itself.

Parameters

other – The parser to match after this one.

Returns

A new parser that will match this parser followed by the other parser (if the this parser matched).

__or__(other: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Alternate[source]

Combine two parsers, matching the first or the second.

Multiple consecutive uses of ‘|’ will result in a single Alternate because the Alternate class automatically flattens itself.

Parameters

other – The parser to match if this parser does not.

Returns

A new parser that will match either this parser or the other parser (if the this parser did not match).

__xor__(func: Callable[[Any], Any]) → rads.config.xml_parsers.Apply[source]

Apply a function to the value result of this parser.

Parameters

func

The function to apply to the value result of matching this parser.

Note

This will not be ran until this parser is matched.

Returns

A new parser that will match this parser and upon a successful match apply the given func to the value result.

__invert__() → rads.config.xml_parsers.Not[source]

Invert this parser.

If this parser would match a given position, now it will not. If it would not match now it will, but it will not consume any elements.

Returns

A new parser that will not match whenever this parser does, and will match whenever this parser does not. However, it will not consume any elements.

__lshift__(message: str) → rads.config.xml_parsers.Must[source]

Require the parser to succeed.

This will convert all XMLParseError s to TerminalXMLParseError s.

Parameters

message – The message that will be raised if the parser does not match.

Returns

A new parser that will elevate any local parse failures to global failures and overwrite their message with message.

class rads.config.xml_parsers.Apply(parser: rads.config.xml_parsers.Parser, func: Callable[[Any], Any], catch: Optional[Collection[type]] = None)[source]

Bases: rads.config.xml_parsers.Parser

Apply a function to the value result of the parser.

Parameters
  • parser – The parser whose value result to apply the given func to the value result of.

  • func – The function to apply.

  • catch

    An exception or iterable of exceptions to convert into XMLParseError s. The default is not to catch any exceptions. To catch all exceptions simply pass Exception as it is the base class of all exceptions that should be caught.

    Note

    Any exceptions that are derived from the exceptions given will also be caught.

__call__(position: rads.xml.base.Element) → Tuple[Any, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Lazy(parser_func: Callable[[], rads.config.xml_parsers.Parser])[source]

Bases: rads.config.xml_parsers.Parser

Delay construction of parser until evaluated.

Note

This lazy behavior is useful when constructing recursive parsers in order to avoid infinite recursion.

Parameters

parser_func – A zero argument function that returns a parser when called. This will be used to delay construction of the parser.

__call__(position: rads.xml.base.Element) → Tuple[Any, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Must(parser: rads.config.xml_parsers.Parser, message: Optional[str] = None)[source]

Bases: rads.config.xml_parsers.Parser

Raise a XMLParseError to a TerminalXMLParseError ending parsing.

Parameters
  • parser – Parser that must match.

  • message – New message to apply to the GlobalParserFailure if the parser does not match.

__call__(position: rads.xml.base.Element) → Tuple[Any, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.At(parser: rads.config.xml_parsers.Parser)[source]

Bases: rads.config.xml_parsers.Parser

Match a parser, consuming nothing.

Parameters

parser – Parser to match.

__call__(position: rads.xml.base.Element) → Tuple[Any, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Not(parser: rads.config.xml_parsers.Parser)[source]

Bases: rads.config.xml_parsers.Parser

Invert a parser match, consuming nothing.

Parameters

parser – Parser to invert the match of.

__call__(position: rads.xml.base.Element) → Tuple[None, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Repeat(parser: rads.config.xml_parsers.Parser)[source]

Bases: rads.config.xml_parsers.Parser

Match a parser zero or more times (greedily).

Parameters

parser – Parser to match repeatedly.

__call__(position: rads.xml.base.Element) → Tuple[List[Any], rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Sequence(*parsers: rads.config.xml_parsers.Parser)[source]

Bases: rads.config.xml_parsers._MultiParser

Chain parsers together, succeeding only if all succeed in order.

Note

Consecutive Sequence’s are automatically flattened.

Parameters

*parsers – Parsers to match in sequence.

__call__(position: rads.xml.base.Element) → Tuple[List[Any], rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
__add__(other: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Sequence[source]

Combine this sequence and a parser, returning a new sequence.

Note

If the other parser is a Sequence then the parsers in the other Sequence will be unwrapped and appended individually.

Parameters

other – The parser to combine with this sequence to form the new sequence.

Returns

A new sequence which matches this sequence followed by the given parser (if the sequence matched).

__iadd__(other: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Sequence[source]

Combine this sequence with the given parser (in place).

Note

If the other parser is a Sequence then the parsers in the other Sequence will be unwrapped and appended to this sequence individually.

Parameters

other – The parser to combine with (append to) this sequence.

Returns

This sequence parser.

class rads.config.xml_parsers.Alternate(*parsers: rads.config.xml_parsers.Parser)[source]

Bases: rads.config.xml_parsers._MultiParser

Match any one of the parsers, stops on first match.

Note

Consecutive Alternate’s are automatically flattened.

Parameters

*parsers – Pool of parsers to find a match in.

__call__(position: rads.xml.base.Element) → Tuple[Any, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
__or__(other: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Alternate[source]

Combine this alternate and a parser, returning a new alternate.

Note

If the other parser is a Alternate then the parsers in the other Alternate will be unwrapped and added individually.

Parameters

other – The parser to combine with this alternate to form the new alternate.

Returns

A new alternate which matches any parser from this alternate or the given parser (if no parser of this alternate matches).

class rads.config.xml_parsers.Success[source]

Bases: rads.config.xml_parsers.Parser

Parser that always succeeds, consuming nothing.

__call__(position: rads.xml.base.Element) → Tuple[None, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Failure[source]

Bases: rads.config.xml_parsers.Parser

Parser that always fails, consuming nothing.

__call__(position: rads.xml.base.Element) → NoReturn[source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Start[source]

Bases: rads.config.xml_parsers.Parser

Match start of an element, consuming nothing.

__call__(position: rads.xml.base.Element) → Tuple[None, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.End[source]

Bases: rads.config.xml_parsers.Parser

Match end of an element, consuming nothing.

__call__(position: rads.xml.base.Element) → Tuple[None, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.AnyElement[source]

Bases: rads.config.xml_parsers.Parser

Parser that matches any element.

__call__(position: rads.xml.base.Element) → Tuple[rads.xml.base.Element, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
class rads.config.xml_parsers.Tag(name: str)[source]

Bases: rads.config.xml_parsers.Parser

Match an element by it’s tag name.

Parameters

name – Tag name to match.

__call__(position: rads.xml.base.Element) → Tuple[rads.xml.base.Element, rads.xml.base.Element][source]

Call the parser, trying to match at the given position.

If the match fails a XMLParseError will be raised. This call will only return if the parser matches at the given position.

Parameters

position – An XML element that the parser should attempt to match at.

Returns

A tuple giving the value result of the match (which depends on the particular parser) and the element to match at.

The next element can be the same element as given in position (a non consuming parser) or any later sibling element.

Further, it will actually be a yzal.Thunk and will therefore delay it’s construction until it is needed. Therefore, any XMLParseError that may be generated by moving to a later element will occur when the returned element is used.

Raises
rads.config.xml_parsers.lazy(parser_func: Callable[[], rads.config.xml_parsers.Parser]) → rads.config.xml_parsers.Parser[source]

Delays construction of parser until evaluated.

Parameters

parser_func – A zero argument function that returns a parser when called. This will be used to delay construction of the parser.

Returns

A new parser that is equivalent to the parser returned by parser_func.

rads.config.xml_parsers.at(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Succeeds if and only if the given parser succeeds, consumes nothing.

Parameters

parser – The parser that must succeed.

Returns

A new parser that succeeds if and only if parser succeeds, but does not consume input.

rads.config.xml_parsers.not_at(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Succeeds if and only if the given parser fails, consumes nothing.

Parameters

parser – The parser that must fail.

Returns

A new parser that succeeds if and only if parser fails, but does not consume input.

rads.config.xml_parsers.opt(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Parser that always succeeds, regardless of the given parser.

Parameters

parser – An optional parser that can succeed or fail.

Returns

A new parser that optionally matches parser. If parser succeeds this parser will be transparent, as if parser was called directly. If parser fails this opt() returns None as the result and does not consume anything.

rads.config.xml_parsers.plus(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Match the given parser as much as possible, must match at least once.

Parameters

parser – Parser to match one or more times (greedily).

Returns

A new parser that matches parser one or more times. Failing if no matches are made.

rads.config.xml_parsers.seq(*parsers: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Match sequence of parsers in order, succeeding if and only if all succeed.

Parameters

*parsers – One or more parsers to match in order.

Returns

A new parser that matches all the given parser’s in order, failing if any one of the parser’s fail.

rads.config.xml_parsers.sor(*parsers: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Match the first of the given parsers, failing if all fail.

Parameters

*parsers – One or more parsers to match. The first parser that succeeds will take the place of this parser. If all fail then this parser will also fail.

Returns

A new parser that matches the first parser that succeeds or fails if all parser’s fail.

rads.config.xml_parsers.star(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Match the given parser as much as possible, can match zero times.

Parameters

parser – Parser to match zero or more times (greedily).

Returns

A new parser that matches parser one or more times. Failing if no matches are made.

rads.config.xml_parsers.must(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Raise a local parse failure to a global parse failure.

Local parse failures (XMLParseError) are typically caught by Alternate or other such parsers that allow some parser’s to fail. In particular, local failures are an expected part of parser combinators and simply signal that a particular parser could not parse the given elements. A global parse failure (TerminalXMLParseError) should only be caught at the top level and signals that the entire parse is a failure.

Parameters

parser – A parser that must match, else the entire parse is failed.

Returns

A parser that must succeed, if it fails a GlobalParserFailure is raised.

rads.config.xml_parsers.rep(parser: rads.config.xml_parsers.Parser, times: int) → rads.config.xml_parsers.Parser[source]

Match the given parser a given number of times.

Fails if the parser does not succeed the given number of times.

Parameters
  • parser – The parser to match times.

  • times – Number of times the parser must succeed.

Returns

A parser that succeeds only if the given parser matches the given number of times.

rads.config.xml_parsers.until(parser: rads.config.xml_parsers.Parser) → rads.config.xml_parsers.Parser[source]

Match all elements until the given parser matches.

Does not consume the elements that the given parser matches.

Parameters

parser – The parser to end matching with.

Returns

A parser that will consume all elements until the given parser matches. It will not consume the elements that the given parser matched.

rads.config.xml_parsers.failure() → rads.config.xml_parsers.Parser[source]

Parser that always fails.

Returns

A new parser that always fails, consuming nothing.

rads.config.xml_parsers.success() → rads.config.xml_parsers.Parser[source]

Parser that always succeeds.

Returns

A new parser that always succeeds, consuming nothing.

rads.config.xml_parsers.start() → rads.config.xml_parsers.Parser[source]

Match the beginning of an element.

Returns

A new parser that matches the beginning of an element, consuming nothing.

rads.config.xml_parsers.end() → rads.config.xml_parsers.Parser[source]

Match the end of an element.

Returns

A new parser that matches the end of an element, consuming nothing.

rads.config.xml_parsers.any() → rads.config.xml_parsers.Parser[source]

Match any element.

Returns

A new parser that matches any single element.

rads.config.xml_parsers.tag(name: str) → rads.config.xml_parsers.Parser[source]

Match an element by tag name.

Parameters

name – Tag name to match.

Returns

Parser matching the given tag name.