Added Python versions of base64 examples

These are transliterations of the existing C files.
They're not particularly Pythonic or performant, but they're a start.

Example of usage

```
$ echo '  YW55IGNhcm5hbCBwbGVhcw==' | PYTHONPATH=../build/opt/src/bindings/python/ python base64.py
inputsize=27
input=  YW55IGNhcm5hbCBwbGVhcw==
((((89L, 87L, 53L, 53L), (73L, 71L, 78L, 104L), (99L, 109L, 53L, 104L), (98L, 67L, 66L, 119L), (98L, 71L, 86L, 104L)), (99L, 'w', '=', '=')),)
$ echo '  YW55IGNhcm5hbCBwbGVhcw==' | PYTHONPATH=../build/opt/src/bindings/python/ python base64_sem1.py
inputsize=27
input=  YW55IGNhcm5hbCBwbGVhcw==
(97L, 110L, 121L, 32L, 99L, 97L, 114L, 110L, 97L, 108L, 32L, 112L, 108L, 101L, 97L, 115L)
$ echo '  YW55IGNhcm5hbCBwbGVhcw==' | PYTHONPATH=../build/opt/src/bindings/python/ python base64_sem2.py
inputsize=27
input=  YW55IGNhcm5hbCBwbGVhcw==
(97L, 110L, 121L, 32L, 99L, 97L, 114L, 110L, 97L, 108L, 32L, 112L, 108L, 101L, 97L, 115L)
```
This commit is contained in:
Alex Willmer 2015-12-04 10:20:25 +00:00
parent 7017ea6d91
commit c6280a98bc
3 changed files with 388 additions and 0 deletions

60
examples/base64.py Normal file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env python2
# Example parser: Base64, syntax only.
#
# Demonstrates how to construct a Hammer parser that recognizes valid Base64
# sequences.
#
# Note that no semantic evaluation of the sequence is performed, i.e. the
# byte sequence being represented is not returned, or determined. See
# base64_sem1.py and base64_sem2.py for examples how to attach appropriate
# semantic actions to the grammar.
from __future__ import print_function
import sys
import hammer as h
def init_parser():
# CORE
digit = h.ch_range(0x30, 0x39)
alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
# AUX.
plus = h.ch('+')
slash = h.ch('/')
equals = h.ch('=')
bsfdig = h.choice(alpha, digit, plus, slash)
bsfdig_4bit = h.in_('AEIMQUYcgkosw048')
bsfdig_2bit = h.in_('AQgw')
base64_3 = h.repeat_n(bsfdig, 4)
base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
base64 = h.sequence(h.many(base64_3),
h.optional(h.choice(base64_2, base64_1)))
return h.sequence(h.whitespace(base64), h.whitespace(h.end_p()))
def main():
document = init_parser()
s = sys.stdin.read()
inputsize = len(s)
print('inputsize=%i' % inputsize, file=sys.stderr)
print('input=%s' % s, file=sys.stderr, end='')
result = document.parse(s)
if result:
#print('parsed=%i bytes', result.bit_length/8, file=sys.stderr)
print(result)
if __name__ == '__main__':
import sys
main()