marsh.parse
¶
Tools for parsing strings into elements and/or commands.
- string(source, terminal_chars='')[source]¶
Parse a single string.
Reserved characters are allowed by being escaped (`) or for the string (or part of the string) to be enclosed in quotes (`”`` or
'
).Quotes are discarded before returning the result.
- Parameters:
source (
Union
[str
,MutableSequence
[str
]]) – The string source to parseterminal_chars (
str
) – Any characters that should terminate the parsing process ( unless escaped or within a set of quotes). If none are given the entire input is consumed.
- Return type:
- Returns:
The parsed string.
- terminal(source, terminal_chars='')[source]¶
Parse a terminal value.
None | int | float | string | bool
- Parameters:
source (
Union
[str
,MutableSequence
[str
]]) – The string source to parseterminal_chars (
str
) – Any characters that should terminate the parsing process ( unless escaped or within a set of quotes). If none are given the entire input is consumed.
- Return type:
- Returns:
The parsed value.
- sequence(source, terminal_chars='', element_parser=<function element>)[source]¶
Parse a single sequence of elements.
See
element()
for defenition of an element.Elements should be separated by
,
. The sequence should be enclosed in()
or[]
.- Parameters:
source (
Union
[str
,MutableSequence
[str
]]) – The string source to parseterminal_chars (
str
) – Expected characters that can succeed the part of the input string that contains the sequence. If not given, an exception is raised if there is anything other than whitespace after the sequence.element_parser (
_ParseFunction
) – Each element of the sequence will be parsed using this parser.
- Return type:
- Returns:
Sequence of elements.
- mapping(source, terminal_chars='', key_parser=<function string>, value_parser=<function element>)[source]¶
Parse a single mapping of strings to elements.
See
element()
for defenition of an element.Keys and values should be separated by
:
Key-value pairs should be separated by,
. The mapping should be enclosed in{}
.- Parameters:
source (
Union
[str
,MutableSequence
[str
]]) – The string source to parseterminal_chars (
str
) – Expected characters that can succeed the part of the input string that contains the mapping. If not given, an exception is raised if there is anything other than whitespace after the mapping.key_parser (
_ParseFunction
) – Each key of the mapping will be parsed using this parser.value_parser (
_ParseFunction
) – Each value of the mapping will be parsed using this parser.
- Return type:
- Returns:
The mapping of strings to elements.
- element(source, terminal_chars='')[source]¶
Parse a single element.
Returns a mapping, sequence or terminal element.
See
mapping()
,sequence()
andterminal()
for more information parsing rules.- Parameters:
source (
Union
[str
,MutableSequence
[str
]]) – The string source to parseterminal_chars (
str
) – Expected characters that can succeed the part of the input string that contains the element. If not given, an exception is raised if there is anything other than whitespace after the element.
- Return type:
Union
[None
,int
,float
,bool
,str
,Sequence
[Any
],Mapping
[str
,Any
]]- Returns:
The parsed element.