-
Notifications
You must be signed in to change notification settings - Fork 8
/
CQPParser.peggy
153 lines (128 loc) · 2.81 KB
/
CQPParser.peggy
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
{
function makeObj(type, op, val, flags) {
return {type : type, op : op, val: val, flags: flags}
}
function unpack(target, source) {
var output = [].concat(target)
source.forEach(function(item) {
output.push(item[1])
})
return output
}
}
start
= tokens
tokens
= t1:token t2:(" " t2:token)*
{
return unpack([t1], t2)
}
token
= "[" "]" repeat:repeat? {
var result = {"and_block":[[{type:"word",op:"=",val:""}]]}
if(repeat)
result.repeat = repeat
return result
}
/ "[" left:and right:(" & " and)* "]" repeat:repeat? {
var output = left
right.forEach(function(item) {
var val = item[1]
output = _.mergeWith(output, val, function(a, b) {
return _.isArray(a) ? a.concat(b) : undefined;
})
})
if(repeat)
output.repeat = repeat
return output
}
/ "<" end:"/"? tag:[A-Za-z0-9_-]+ ">" {
return { struct: tag.join(""), start: end ? false : true }
}
repeat
= "{" from:[0-9]+ "," to:[0-9]* "}"
{
var output = [Number(from.join(""))]
if(to.length)
output.push(Number(to.join("")))
return output
}
bound = val:("lbound" / "rbound") "(" "sentence" ")" {
return val
}
bound_block
= first:bound rest:(" & " bound)*
{
return unpack([first], rest)
}
and
= left:or right:(" | " or)* {
return {and_block : [unpack([left], right)]}
}
/ bound:bound_block {
bound = _.fromPairs(bound.map(function(item) {
return [item, true]
}))
return {"bound" : bound, "and_block" : []}
}
/ "(" and:and ")" {return and}
or
= lhs:attribute " "? infix_op:infix_op " "? rhs:value_expr flags:(" %"[lcd]+)? {
var prefix = ""
if(lhs[0])
prefix = lhs[0]
if(flags) {
flags = _.zipObject(flags[1], _.map(flags[1], function() { return true; }));
}
return makeObj(prefix + lhs[1].join(""), infix_op, rhs, flags)
}
/ date
attribute
= "_."? [A-Za-z0-9_-]+
value_expr
= ["] rhs:('""' / [^"])* ["] {
return rhs.join("");
}
/ ['] rhs:("\\'" / [^'])* ['] {
return rhs.join("");
}
// date_expr
date
= "$" "date_interval" " "? op:("!=" / "=") " "? ["'] val:([0-9]+ "," [0-9]+ "," [0-9]+ "," [0-9]+) ['"]
{
val = _.filter(val, _.isArray).map(function(item) {
return item.join("")
})
return makeObj("date_interval", op, val)
}
infix_op
= "^="
/ "&="
/ "_="
/ "*="
/ "!="
/ "="
/ "!*="
/ "not contains"
/ "contains"
/ "highest_rank"
/ "not_highest_rank"
/ "rank_contains"
/ "not_rank_contains"
/ "regexp_contains"
/ "not_regexp_contains"
/ "starts_with_contains"
/ "ends_with_contains"
/ "incontains_contains"
/ "not_starts_with_contains"
/ "not_ends_with_contains"
/ "not_incontains_contains"
date_op
= " <= "
/ " => "
/ " > "
/ " = "
/ " < "
bool
= " & "
/ " | "