1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#!/usr/bin/python2
# xml.g
#
# Amit J. Patel, August 2003
#
# Simple (non-conforming, non-validating) parsing of XML documents,
# based on Robert D. Cameron's "REX" shallow parser. It doesn't
# handle CDATA and lots of other stuff; it's meant to demonstrate
# Yapps, not replace a proper XML parser.
%%
parser xml:
token nodetext: r'[^<>]+'
token attrtext_singlequote: "[^']*"
token attrtext_doublequote: '[^"]*'
token SP: r'\s'
token id: r'[a-zA-Z_:][a-zA-Z0-9_:.-]*'
rule node:
r'<!--.*?-->' {{ return ['!--comment'] }}
| r'<!\[CDATA\[.*?\]\]>' {{ return ['![CDATA['] }}
| r'<!' SP* id '[^>]*>' {{ return ['!doctype'] }}
| '<' SP* id SP* attributes SP* {{ startid = id }}
( '>' nodes '</' SP* id SP* '>' {{ assert startid == id, 'Mismatched tags <%s> ... </%s>' % (startid, id) }}
{{ return [id, attributes] + nodes }}
| '/\s*>' {{ return [id, attributes] }}
)
| nodetext {{ return nodetext }}
rule nodes: {{ result = [] }}
( node {{ result.append(node) }}
) * {{ return result }}
rule attribute: id SP* '=' SP*
( '"' attrtext_doublequote '"' {{ return (id, attrtext_doublequote) }}
| "'" attrtext_singlequote "'" {{ return (id, attrtext_singlequote) }}
)
rule attributes: {{ result = {} }}
( attribute SP* {{ result[attribute[0]] = attribute[1] }}
) * {{ return result }}
%%
if __name__ == '__main__':
tests = ['<!-- hello -->',
'some text',
'< bad xml',
'<br />',
'< spacey a = "foo" / >',
'<a href="foo">text ... </a>',
'<begin> middle </end>',
'<begin> <nested attr=\'baz\' another="hey"> foo </nested> <nested> bar </nested> </begin>',
]
print()
print('____Running tests_______________________________________')
for test in tests:
print()
try:
parser = xml(xmlScanner(test))
output = '%s ==> %s' % (repr(test), repr(parser.node()))
except (runtime.SyntaxError, AssertionError) as e:
output = '%s ==> FAILED ==> %s' % (repr(test), e)
print(output)
|