pyPEG – a PEG Parser-Interpreter in Python
Requires Python 3.x or 2.7
Older versions: pyPEG 1.x
The pyPEG XML Backend uses Python's etree
semantic. This way it can easily be integrated into existing working code using XML. The usage of lxml is recommended. If the module lxml
is installed, pyPEG uses it automatically.
create_tree(thing, parent=None, object_names=False)
Create an XML etree from a thing.
|
|
|
|
| experimental feature: if |
etree.Element
instance created
Example:
>>> from pypeg2.xmlast import create_tree
>>> from pypeg2 import name, restline
>>> class Key(str):
... grammar = name(), "=", restline
...
>>> k = Key("world")
>>> k.name = "hello"
>>> t = create_tree(k)
>>> t.attrib["name"]
'hello'
>>> t.text
'world'
>>> type(t)
<class 'lxml.etree._Element'>
create_thing(element, symbol_table)
Create thing from an XML element.
|
|
| symbol table where the classes can be found; usually call |
thing
created
Example:
>>> from pypeg2.xmlast import create_thing, etree
>>> from pypeg2 import name, restline
>>> class Key(str):
... grammar = name(), "=", restline
...
>>> e = etree.fromstring("<Key name='hello'>world</Key>")
>>> k = create_thing(e, globals())
>>> k.name
Symbol('hello')
>>> k
'world'
>>> type(k)
<class '__main__.Key'>
thing2xml(thing, pretty=False, object_names=False)
Create XML text from a thing.
|
|
|
|
| experimental feature: if |
bytes
with encoded XML
Example:
>>> from pypeg2 import name, restline
>>> from pypeg2.xmlast import thing2xml
>>> class Key(str):
... grammar = name(), "=", restline
...
>>> k = Key("world")
>>> k.name = "hello"
>>> thing2xml(k)
b'<Key name="hello">world</Key>'
xml2thing(xml, symbol_table)
Create thing
from XML text.
|
|
| symbol table where the classes can be found; usually call |
created thing
Example:
>>> from pypeg2 import name, restline
>>> from pypeg2.xmlast import xml2thing
>>> class Key(str):
... grammar = name(), "=", restline
...
>>> k = xml2thing(b"<Key name='hello'>world</Key>", globals())
>>> k.name
Symbol('hello')
>>> k
'world'