forked from igrigorik/textquery
/
textquery.treetop
64 lines (54 loc) · 870 Bytes
/
textquery.treetop
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
grammar TextQuery
rule expression
logical / value
end
rule logical
op1:value space operator:binary space op2:expression {
def eval(text)
operator.eval(op1.eval(text), op2.eval(text))
end
}
end
rule binary
'AND' {
def eval(a,b)
a && b
end
}
/
'OR' {
def eval(a,b)
a || b
end
}
end
rule unary
('-' / 'NOT') {
def eval(a)
not a
end
}
end
rule space
[\s]*
end
rule word
[^\s\(\)]+ {
def eval(text)
not text.match(/^#{query}\s|\s#{query}\s|\s#{query}$|^#{query}$/).nil?
end
def query
Regexp.escape(text_value)
end
}
end
rule value
operator:unary space value {
def eval(text)
operator.eval(value.eval(text))
end
}
/
word
end
end