-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwizard.pegjs
180 lines (143 loc) · 4.94 KB
/
wizard.pegjs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Parser for human readable OSM geo data search queries.
*/
start
= _ x:geo_query _ { return x }
geo_query
= x:query whitespace+ ( "in bbox" / "IN BBOX" )
{ return { bounds:"bbox", query:x } }
/ x:query whitespace+ ( "in" / "IN" ) whitespace+ y:string
{ return { bounds:"area", query:x, area:y } }
/ x:query whitespace+ ( "around" / "AROUND" ) whitespace+ y:string
{ return { bounds:"around", query:x, area:y } }
/ x:query whitespace+ ( "global" / "GLOBAL" )
{ return { bounds:"global", query:x } }
/ x:query
{ return { bounds:"bbox", query:x } }
query
= logical_or
logical_or
= x:logical_and whitespace+ ( "or" / "OR" / "||" / "|" ) whitespace+ y:logical_or
{ return { logical:"or", queries:[x,y] } }
/*
/ x:logical_and whitespace+ ( "xor" / "XOR" ) whitespace+ y:logical_and
{ return { logical:"xor", queries:[x,y] } }
/ x:logical_and whitespace+ ( "except" / "EXCEPT" ) whitespace+ y:logical_and
{ return { logical:"minus", queries:[x,y] } }
*/
/ x:logical_and
logical_and
= x:braces whitespace+ ( "and" / "AND" / "&&" / "&" ) whitespace+ y:logical_and
{ return { logical:"and", queries:[x,y] } }
/ x:braces
/*logical_not
= TODO? */
braces
= statement
/ "(" _ x:logical_or _ ")" { return x; }
statement
= type
/ meta
/ key_eq_val
/ key_not_eq_val
/ key_present
/ key_not_present
/ key_like_val
/ like_key_like_val
/ key_not_like_val
/ key_substr_val
/ free_form
key_eq_val
= x:key_string _ ( "==" / "=" ) _ y:string
{ return { query:"eq", key:x, val:y } }
key_not_eq_val
= x:key_string _ ( "!=" / "<>" ) _ y:string
{ return { query:"neq", key:x, val:y } }
key_present
= x:key_string _ ( "==" / "=" ) _ "*"
{ return { query:"key", key:x } }
/ x:string whitespace+ ("is" whitespace+ "not" whitespace+ "null" / "IS" whitespace+ "NOT" whitespace+ "NULL")
{ return { query:"key", key:x } }
key_not_present
= x:key_string _ ( "!=" / "<>" ) _ "*"
{ return { query:"nokey", key:x } }
/ x:string whitespace+ ("is" whitespace+ "null" / "IS" whitespace+ "NULL")
{ return { query:"nokey", key:x } }
key_like_val
= x:key_string _ ( "~=" / "~" / "=~" ) _ y:(string / regexstring )
{ return { query:"like", key:x, val:y.regex?y:{regex:y} } }
/ x:string whitespace+ ("like" / "LIKE") whitespace+ y:(string / regexstring )
{ return { query:"like", key:x, val:y.regex?y:{regex:y} } }
like_key_like_val
= "~" _ x:(string / regexstring) _ ( "~=" / "~" / "=~" ) _ y:(string / regexstring )
{ return { query:"likelike", key:x.regex?x:{regex:x}, val:y.regex?y:{regex:y} } }
key_not_like_val
= x:key_string _ ( "!~" ) _ y:(string / regexstring )
{ return { query:"notlike", key:x, val:y.regex?y:{regex:y} } }
/ x:string whitespace+ ("not" whitespace+ "like" / "NOT" whitespace+ "LIKE") whitespace+ y:(string / regexstring )
{ return { query:"notlike", key:x, val:y.regex?y:{regex:y} } }
key_substr_val
= x:string _ ( ":" ) _ y:string
{ return { query:"substr", key:x, val:y } }
type
= "type" _ ":" _ x:string
{ return { query:"type", type:x } }
meta // TODO?
= x:("user" / "uid" / "newer" / "id") _ ":" _ y:string
{ return { query:"meta", meta:x, val:y } }
free_form
= x:string
{ return { query:"free form", free:x } }
/* ==== strings ==== */
key_string "Key"
= s:[a-zA-Z0-9_:-]+ { return s.join(''); }
/ parts:('"' DoubleStringCharacters '"' / "'" SingleStringCharacters "'") {
return parts[1];
}
string "string"
= s:[^'" ()~=!*/:<>&|[\]{}#+@$%?^.,]+ { return s.join(''); }
/ parts:('"' DoubleStringCharacters '"' / "'" SingleStringCharacters "'") {
return parts[1];
}
DoubleStringCharacters
= chars:DoubleStringCharacter* { return chars.join(""); }
SingleStringCharacters
= chars:SingleStringCharacter* { return chars.join(""); }
DoubleStringCharacter
= !('"' / "\\") char_:. { return char_; }
/ "\\" sequence:EscapeSequence { return sequence; }
SingleStringCharacter
= !("'" / "\\") char_:. { return char_; }
/ "\\" sequence:EscapeSequence { return sequence; }
EscapeSequence
= CharacterEscapeSequence
// / "0" !DecimalDigit { return "\0"; }
// / HexEscapeSequence
// / UnicodeEscapeSequence //TODO?
CharacterEscapeSequence
= SingleEscapeCharacter
SingleEscapeCharacter
= char_:['"\\bfnrtv] {
return char_
.replace("b", "\b")
.replace("f", "\f")
.replace("n", "\n")
.replace("r", "\r")
.replace("t", "\t")
.replace("v", "\x0B") // IE does not recognize "\v".
}
/* ==== regexes ==== */
regexstring "string"
= parts:('/' (RegexStringCharacters) '/' ('i'/'')?) {
return { regex: parts[1], modifier: parts[3] };
}
RegexStringCharacters
= chars:RegexStringCharacter+ { return chars.join(""); }
RegexStringCharacter
= !('/' / "\\/") char_:. { return char_; }
/ "\\/" { return "/"; }
/* ===== Whitespace ===== */
_ "whitespace"
= whitespace*
whitespace "whitespace"
= [ \t\n\r]