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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
from pyparsing import *
class Polyp:
# BuiltinFunction = Keyword("Render")
Point = Literal('.')
e = CaselessLiteral('E')
PlusOrMinus = Literal('+') | Literal('-')
Number = Word(nums)
Integer = Combine( Optional(PlusOrMinus) + Number )
Float = Combine( Integer + Optional( Point + Optional(Number) ) + Optional( e + Integer ) )
String = quotedString
Comment = cppStyleComment
Identifier = Forward()
Identifier << (Word(alphas, alphanums + '_') + ZeroOrMore(':' + Identifier))
Variable = Word(alphas, alphanums + '_')
PrimaryExpr = Forward()
UnaryExpr = Forward()
Expr = Forward()
Expression = Forward()
MathOperator = oneOf("+ - * / % ^ **")
RelationOperator = oneOf("= != < > <= >= <>")
UnaryOperator = oneOf("+ - ! ~")
LogicalOperator = oneOf("& |")
BinaryOperator = RelationOperator | MathOperator
VariableList = delimitedList(Variable)
ExpressionList = delimitedList(Expression)
Operand = Integer | Float | Identifier | String
UnaryExpr << ((UnaryOperator + UnaryExpr)
| PrimaryExpr
| nestedExpr(content=PrimaryExpr)
)
Expr << ( UnaryExpr + ZeroOrMore(BinaryOperator + Expr) )
Expression << (
(
(Expr | nestedExpr(content=Expr) )
+ ZeroOrMore(
LogicalOperator + ( Expr
| nestedExpr(content=Expr)
)
)
) | nestedExpr(content=Expression) )
Call = Identifier + Suppress("(") + ExpressionList + Suppress(")")
ParameterList = delimitedList(Variable)
Parameters = Suppress("(") + Optional(ParameterList) + Suppress(")")
Function = Identifier + Parameters
VariableDeclaration = Optional(Suppress(Keyword("var"))) + VariableList + Suppress(":=") + ExpressionList
FunctionDeclaration = Optional(Suppress(Keyword("def"))) + Function + Suppress(":=") + ( nestedExpr(content=Expression) | Expression)
Declaration = VariableDeclaration | FunctionDeclaration
PrimaryExpr << (Call | Operand)
Statement = Declaration | Call
EndOfStatement = Suppress(";")
Polyps = OneOrMore(Statement + EndOfStatement) + StringEnd()
Polyps.ignore(Comment)
def __init__(self):
self.FunctionDeclaration.setParseAction(self.FunctionRegister)
self.VariableDeclaration.setParseAction(self.VariableRegister)
self.EndOfStatement.setParseAction(self.StatementEnd)
self.Call.setParseAction(self.CallAction)
self.VariableTable = {} # Contains bound variables (lazy evaluation)
self.FunctionTable = {} # Contains defined functions (lazy evaluation :)
self.StatementEnd()
def StatementEnd(self, s = None, loc = None, toks = None):
self.function_registration = False
self.variable_registration = False
def CallAction(self, s, loc, toks):
print "Call to '%s'" % toks[0]
def FunctionRegister(self, s, loc, toks):
print "Registering Function:", toks[0]
self.function_registration = True
FunctionName = toks[0]
FunctionTrace = toks[1:]
self.FunctionTable[FunctionName] = [FunctionTrace]
def VariableRegister(self, s, loc, toks):
print "Registering Variable:", toks[0]
self.variable_registration = True
self.VariableTable[toks[0]] = None
def Identifier(self, s, loc, toks):
if self.function_registration or self.variable_registration:
pass
else:
if not self.VariableTable.has_key(toks[0]):
print "Unresolved variable at ", loc
def Parse(self, string, given_table={}):
self.VariableTable.clear()
self.VariableTable.update(given_table)
self.FunctionTable.clear()
try:
print "Parsing string '%s':" % string
self.Polyps.parseString(string)
except ParseException, E:
print "Polyps: Parse error: ", E
print string
print (" " * E.loc) + "^"
print E.parserElement
return
def ParseFile(self, file, given_table={}):
self.VariableTable.clear()
self.VariableTable.update(given_table)
self.FunctionTable.clear()
try:
print "Parsing file '%s':" % file
self.Polyps.parseFile(file)
except ParseException, E:
print "Polyps: Parse error: ", E
print (" " * E.loc) + "^"
# print
print E.parserElement
return
p = Polyp()
# parse("Foostring := \"Foo\";") # (x < a+s) & (x > a-s) & (y < b+s) & (y > b-s) & (z < c+s) & (z > c-s);")
p.Parse("""Cube(a, b, c, s) := (x < a+s) & (x > a-s) & (y < b+s) & (y > b-s) & (z < c+s) & (z > c-s);""")
# p.Parse("var MyVar := 3;")
p.ParseFile("howdy.polyp")
|