-
Notifications
You must be signed in to change notification settings - Fork 6
/
tube.py
62 lines (62 loc) · 2.24 KB
/
tube.py
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
# from ometa.interp import TrampolinedGrammarInterpreter, _feed_me
#
# class TrampolinedParser:
# """
# A parser that incrementally parses incoming data.
# """
#
# currentRule = 'initial'
# def __init__(self, grammar, receiver, bindings):
# """
# Initializes the parser.
#
# @param grammar: The grammar used to parse the incoming data.
# @param receiver: Responsible for logic operation on the parsed data.
# Typically, the logic operation will be invoked inside the grammar,
# e.g., rule = expr1 expr2 (-> receiver.doSomeStuff())
# @param bindings: The namespace that can be accessed inside the grammar.
# """
# self.grammar = grammar
# self.bindings = dict(bindings)
# self.bindings['receiver'] = self.receiver = receiver
# self._setupInterp()
#
#
# def _setupInterp(self):
# """
# Resets the parser. The parser will begin parsing with the rule named
# 'initial'.
# """
# if isinstance(self.currentRule, basestring):
# self.currentRule = (self.currentRule, None, ())
# elif isinstance(self.currentRule, (tuple, list)):
# self.currentRule = (self.currentRule[0], None, self.currentRule[-1])
# else:
# raise ValueError("Wrong rule format.")
# self._interp = TrampolinedGrammarInterpreter(
# grammar=self.grammar, rule=self.currentRule, callback=None,
# globals=self.bindings)
#
#
# def setNextRule(self, nextRule):
# self.currentRule = nextRule
#
#
# def receive(self, data):
# """
# Receive the incoming data and begin parsing. The parser will parse the
# data incrementally according to the 'currentRule' rule in the grammar.
#
# @param data: The raw data received.
# """
# while data:
# # try:
# status = self._interp.receive(data)
# # except Exception as e:
# # maybe we should raise it?
# # raise e
# # else:
# if status is _feed_me:
# return
# data = ''.join(self._interp.input.data[self._interp.input.position:])
# self._setupInterp()