-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootbase.py
72 lines (64 loc) · 2.41 KB
/
bootbase.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
63
64
65
66
67
68
69
70
71
72
from .runtime import OMetaBase, _MaybeParseError, ParseError, EOFError
class BootBaseTraits(object):
def parseGrammar(self, name, builder, *args):
"""
Entry point for converting a grammar to code (of some variety).
@param name: The name for this grammar.
@param builder: A class that implements the grammar-building interface
(interface to be explicitly defined later)
"""
self.builder = builder(name, self, *args)
res, err = self.apply("grammar")
try:
x = self.input.head()
except EOFError:
pass
else:
raise ParseError("Grammar parse failed.\n%s" % self.currentError.formatError(''.join(self.input.data)))
return res
def applicationArgs(self):
"""
Collect rule arguments, a list of Python expressions separated by
spaces.
"""
args = []
while True:
try:
(arg, endchar), err = self.pythonExpr(" )}")
if not arg:
break
args.append(self.builder.expr(arg))
if endchar == ')' or endchar == '}':
break
except _MaybeParseError:
break
if args:
return args
else:
x = str(''.join(self.input.data[max(0, self.input.position-1):]))
raise _MaybeParseError(self.input.position, None, "Grammar parse failed.\nLeftover bits:\n%s" % x)
def ruleValueExpr(self):
"""
Find and generate code for a Python expression terminated by a close
paren/brace or end of line.
"""
(expr, endchar), err = self.pythonExpr(endChars="\r\n)]")
if endchar:
self.input = self.input.prev()
return self.builder.expr(expr)
def semanticActionExpr(self):
"""
Find and generate code for a Python expression terminated by a
close-paren, whose return value is ignored.
"""
return self.builder.action(self.pythonExpr(')')[0][0])
def semanticPredicateExpr(self):
"""
Find and generate code for a Python expression terminated by a
close-paren, whose return value determines the success of the pattern
it's in.
"""
expr = self.builder.expr(self.pythonExpr(')')[0][0])
return self.builder.pred(expr)
class BootBase(BootBaseTraits, OMetaBase):
pass