blob: b249fd29482067091e733b043a6ef1ac385f70c6 (
plain)
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
|
from lepl import *
from lepl.graph import SimpleWalker
class Table(Node): pass
class Variable(Node): pass
class Value(Node): pass
class Query(Node): pass
class Logical(Node): pass
class Boolean(Node): pass
class Statement(Node): pass
statement = Delayed()
logicalstatement = Delayed()
booleanstatement = Delayed()
number = Digit()[1:,...]
variable = Regexp(r'[a-zA-Z][a-zA-Z0-9]*') > Variable
table = Regexp(r'[a-zA-Z][a-zA-Z0-9]*') > Table
string = Regexp(r'[\s\w\d]*')
spaces = Drop(Regexp(r'\s*'))
with Separator(spaces):
value = number | '"' & string & '"' > Value
term = value | variable
booleanstatement += (statement & '&' & booleanstatement) | (statement & '|' & booleanstatement) | statement > Boolean
logicalstatement += term & Any('<>=~') & logicalstatement | term > Logical
statement += '(' & booleanstatement & ')' | logicalstatement
query = table & '?' & booleanstatement > Query
parser = query.parse_string
ast = parser("materials ? 30 < shearmodulus < 50 & meltingtemp > 460 & (tensilestrength > 4000 | poissonsmodulus < 14)")
# print parser("objects ? name ~ \"bicycle\" ")[0]
# print SimpleWalker(ast)
# print "Table is '%s'" % ast[0].Table[0]
table = ast[0].Table[0][0]
print "Searching table structure '%s'" % table
matchneeded = False
value1 = 0
value2 = 0
comparator = 0
logic = " AND "
def extr(a):
global matchneeded
global value1
global value2
global comparator
global logic
s = ""
for child in a:
if type(child) in (Boolean, Logical, Statement):
s += extr(child)
elif type(child) in (Variable, Value):
if matchneeded:
value2 = child[0]
matchneeded = False
s += " %s ( %s %s %s ) " % (logic, value1, comparator, value2)
value1 = value2
else:
value1 = child[0]
elif child == '&':
logic = " AND "
elif child == '|':
logic = " OR "
elif child in ('<', '>', '~', '='):
matchneeded = True
comparator = child
elif child in ('(', ')'):
s += child
else:
print "Unrecognized token: '%s'" % child
return s
print "arr: " + extr(ast[0].Boolean[0])
# print [child for child in ast[0].Boolean[0]]
|