Skip to content

Instantly share code, notes, and snippets.

@habnabit
Forked from jml/gist:6875518
Created October 8, 2013 14:45
Show Gist options
  • Save habnabit/6885876 to your computer and use it in GitHub Desktop.
Save habnabit/6885876 to your computer and use it in GitHub Desktop.
from ometa.grammar import OMeta
from ometa.interp import TrampolinedGrammarInterpreter
from parsley import makeGrammar
import sys
grammar = """
hex_digit = digit | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
byte = <hex_digit hex_digit>:b -> int(b, 16)
"""
hex = makeGrammar(grammar, {}, name='hex', unwrap=True)
def iter_grammar(grammar, rule, input_stream, name='Grammar', buffer_size=1):
tokens = [] # Should really be an explicit queue.
interp = None
while True:
data = input_stream.read(buffer_size)
if not data:
break
if interp is None or interp.ended:
interp = TrampolinedGrammarInterpreter(
OMeta(grammar).parseGrammar(name), rule, callback=lambda *a: tokens.append(a))
interp.receive(data)
for token in tokens:
yield token
tokens[:] = []
interp.end()
for token in iter_grammar(grammar, 'byte', sys.stdin):
print token
A0B0FFCD010283
$ python streaming.py < eg.data
(160, ParseError(2, None))
(176, ParseError(2, None))
(255, ParseError(2, None))
(205, ParseError(2, None))
(1, ParseError(2, None))
(2, ParseError(2, None))
(131, ParseError(2, None))
Traceback (most recent call last):
File "gistfile1.py", line 34, in <module>
for token in iter_grammar(grammar, 'byte', sys.stdin):
File "gistfile1.py", line 27, in iter_grammar
interp.receive(data)
File "/usr/local/lib/python2.7/site-packages/ometa/interp.py", line 48, in receive
for x in self.next:
File "/usr/local/lib/python2.7/site-packages/ometa/interp.py", line 166, in apply
for x in self._apply(f, ruleName, argvals):
File "/usr/local/lib/python2.7/site-packages/ometa/interp.py", line 104, in _apply
for x in rule():
File "/usr/local/lib/python2.7/site-packages/ometa/interp.py", line 255, in parse_Or
raise self.err(joinErrors(errors))
File "/usr/local/lib/python2.7/site-packages/ometa/interp.py", line 482, in err
raise e
ometa.runtime.ParseError:
^
Parse error at line 2, column -1: expected one of 'A', 'B', 'C', 'D', 'E', 'F', or a digit. trail: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment