| | 1 | // ffs/wizard module |
| | 2 | if (typeof turbo === "undefined") turbo={}; |
| | 3 | turbo.ffs = function() { |
| | 4 | var ffs = {}; |
| | 5 | var freeFormQuery; |
| | 6 | |
| | 7 | /* this converts a random boolean expression into a normalized form: |
| | 8 | * A∧B∧… ∨ C∧D∧… ∨ … |
| | 9 | * for example: A∧(B∨C) ⇔ (A∧B)∨(A∧C) |
| | 10 | */ |
| | 11 | function normalize(query) { |
| | 12 | var normalized_query = { |
| | 13 | logical:"or", |
| | 14 | queries:[] |
| | 15 | }; |
| | 16 | function normalize_recursive(rem_query) { |
| | 17 | if (!rem_query.logical) { |
| | 18 | return [{ |
| | 19 | logical: "and", |
| | 20 | queries: [rem_query] |
| | 21 | }]; |
| | 22 | } else if (rem_query.logical === "and") { |
| | 23 | var c1 = normalize_recursive( rem_query.queries[0] ); |
| | 24 | var c2 = normalize_recursive( rem_query.queries[1] ); |
| | 25 | // return cross product of c1 and c2 |
| | 26 | var c = []; |
| | 27 | for (var i=0; i<c1.length; i++) |
| | 28 | for (var j=0; j<c2.length; j++) { |
| | 29 | c.push({ |
| | 30 | logical: "and", |
| | 31 | queries: c1[i].queries.concat(c2[j].queries) |
| | 32 | }); |
| | 33 | } |
| | 34 | return c; |
| | 35 | } else if (rem_query.logical === "or") { |
| | 36 | var c1 = normalize_recursive( rem_query.queries[0] ); |
| | 37 | var c2 = normalize_recursive( rem_query.queries[1] ); |
| | 38 | return [].concat(c1,c2); |
| | 39 | |
| | 40 | } else { |
| | 41 | alert("unsupported boolean operator: "+rem_query.logical); |
| | 42 | } |
| | 43 | } |
| | 44 | normalized_query.queries = normalize_recursive(query); |
| | 45 | return normalized_query; |
| | 46 | } |
| | 47 | |
| | 48 | function escRegexp(str) { |
| | 49 | return str.replace(/([()[{*+.$^\\|?])/g, '\\$1'); |
| | 50 | } |
| | 51 | |
| | 52 | ffs.construct_query = function(search, comment) { |
| | 53 | function quote_comment_str(s) { |
| | 54 | // quote strings that are to be used within c-style comments |
| | 55 | // replace any comment-ending sequences in these strings that would break the resulting query |
| | 56 | return s.replace(/\*\//g,'[…]').replace(/\n/g,'\\n'); |
| | 57 | } |
| | 58 | |
| | 59 | try { |
| | 60 | ffs = turbo.ffs.parser.parse(search); |
| | 61 | } catch(e) { |
| | 62 | console.log("ffs parse error"); |
| | 63 | return false; |
| | 64 | } |
| | 65 | |
| | 66 | var query_parts = []; |
| | 67 | var bounds_part; |
| | 68 | |
| | 69 | query_parts.push('/*'); |
| | 70 | if (comment) { |
| | 71 | query_parts.push(comment) |
| | 72 | } else { |
| | 73 | query_parts.push('This has been generated by the overpass-turbo wizard.'); |
| | 74 | query_parts.push('The original search was:'); |
| | 75 | query_parts.push('“'+quote_comment_str(search)+'”'); |
| | 76 | } |
| | 77 | query_parts.push('*/'); |
| | 78 | query_parts.push('[out:json][timeout:25];'); |
| | 79 | |
| | 80 | switch(ffs.bounds) { |
| | 81 | case "area": |
| | 82 | query_parts.push('// fetch area “'+ffs.area+'” to search in'); |
| | 83 | query_parts.push('{{geocodeArea:'+ffs.area+'}}->.searchArea;'); |
| | 84 | bounds_part = '(area.searchArea)'; |
| | 85 | break; |
| | 86 | case "around": |
| | 87 | query_parts.push('// adjust the search radius (in meters) here'); |
| | 88 | query_parts.push('{{radius=1000}}'); |
| | 89 | bounds_part = '(around:{{radius}},{{geocodeCoords:'+ffs.area+'}})'; |
| | 90 | break; |
| | 91 | case "bbox": |
| | 92 | bounds_part = '({{bbox}})'; |
| | 93 | break; |
| | 94 | case "global": |
| | 95 | bounds_part = undefined; |
| | 96 | break; |
| | 97 | default: |
| | 98 | alert("unknown bounds condition: "+ffs.bounds); |
| | 99 | return false; |
| | 100 | break; |
| | 101 | } |
| | 102 | |
| | 103 | function get_query_clause(condition) { |
| | 104 | function esc(str) { |
| | 105 | if (typeof str !== "string") return; |
| | 106 | // see http://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#Escaping |
| | 107 | return str.replace(/\\/g,"\\\\").replace(/"/g,"\\\"") // need to escape those |
| | 108 | .replace(/\t/g,"\\t").replace(/\n/g,"\\n"); // also escape newlines an tabs for better readability of the query |
| | 109 | } |
| | 110 | var key = esc(condition.key); |
| | 111 | var val = esc(condition.val); |
| | 112 | // convert substring searches into matching regexp ones |
| | 113 | if (condition.query === "substr") { |
| | 114 | condition.query = "like"; |
| | 115 | condition.val={regex:escRegexp(condition.val)}; |
| | 116 | } |
| | 117 | // special case for empty values |
| | 118 | // see https://github.com/drolbr/Overpass-API/issues/53 |
| | 119 | if (val === '') { |
| | 120 | if (condition.query === "eq") { |
| | 121 | condition.query = "like"; |
| | 122 | condition.val={regex:'^$'}; |
| | 123 | } else if (condition.query === "neq") { |
| | 124 | condition.query = "notlike"; |
| | 125 | condition.val={regex:'^$'}; |
| | 126 | } |
| | 127 | } |
| | 128 | // special case for empty keys |
| | 129 | // see https://github.com/drolbr/Overpass-API/issues/53#issuecomment-26325122 |
| | 130 | if (key === '') { |
| | 131 | if (condition.query === "key") { |
| | 132 | condition.query = "likelike"; |
| | 133 | key='^$'; |
| | 134 | condition.val={regex: '.*'}; |
| | 135 | } else if (condition.query === "eq") { |
| | 136 | condition.query = "likelike"; |
| | 137 | key='^$'; |
| | 138 | condition.val={regex: '^'+escRegexp(condition.val)+'$'}; |
| | 139 | } else if (condition.query === "like") { |
| | 140 | condition.query = "likelike"; |
| | 141 | key='^$'; |
| | 142 | } |
| | 143 | } |
| | 144 | // construct the query clause |
| | 145 | switch(condition.query) { |
| | 146 | case "key": |
| | 147 | return '["'+key+'"]'; |
| | 148 | case "nokey": |
| | 149 | return '["'+key+'"!~".*"]'; |
| | 150 | case "eq": |
| | 151 | return '["'+key+'"="'+val+'"]'; |
| | 152 | case "neq": |
| | 153 | return '["'+key+'"!="'+val+'"]'; |
| | 154 | case "like": |
| | 155 | return '["'+key+'"~"'+esc(condition.val.regex)+'"' |
| | 156 | +(condition.val.modifier==="i"?',i':'') |
| | 157 | +']'; |
| | 158 | case "likelike": |
| | 159 | return '[~"'+key+'"~"'+esc(condition.val.regex)+'"' |
| | 160 | +(condition.val.modifier==="i"?',i':'') |
| | 161 | +']'; |
| | 162 | case "notlike": |
| | 163 | return '["'+key+'"!~"'+esc(condition.val.regex)+'"' |
| | 164 | +(condition.val.modifier==="i"?',i':'') |
| | 165 | +']'; |
| | 166 | case "meta": |
| | 167 | switch(condition.meta) { |
| | 168 | case "id": |
| | 169 | return '('+val+')'; |
| | 170 | case "newer": |
| | 171 | if (condition.val.match(/^-?\d+ ?(seconds?|minutes?|hours?|days?|weeks?|months?|years?)?$/)) |
| | 172 | return '(newer:"{{date:'+val+'}}")'; |
| | 173 | return '(newer:"'+val+'")'; |
| | 174 | case "user": |
| | 175 | return '(user:"'+val+'")'; |
| | 176 | case "uid": |
| | 177 | return '(uid:'+val+')'; |
| | 178 | default: |
| | 179 | console.log("unknown query type: meta/"+condition.meta); |
| | 180 | return false; |
| | 181 | } |
| | 182 | case "free form": |
| | 183 | // own module, special cased below |
| | 184 | default: |
| | 185 | console.log("unknown query type: "+condition.query); |
| | 186 | return false; |
| | 187 | } |
| | 188 | } |
| | 189 | function get_query_clause_str(condition) { |
| | 190 | function quotes(s) { |
| | 191 | if (s.match(/^[a-zA-Z0-9_]+$/) === null) |
| | 192 | return '"'+s.replace(/"/g,'\\"')+'"'; |
| | 193 | return s; |
| | 194 | } |
| | 195 | function quoteRegex(s) { |
| | 196 | if (s.regex.match(/^[a-zA-Z0-9_]+$/) === null || s.modifier) |
| | 197 | return '/'+s.regex.replace(/\//g,'\\/')+'/'+(s.modifier||''); |
| | 198 | return s.regex; |
| | 199 | } |
| | 200 | switch(condition.query) { |
| | 201 | case "key": |
| | 202 | return quote_comment_str(quotes(condition.key)+'=*'); |
| | 203 | case "nokey": |
| | 204 | return quote_comment_str(quotes(condition.key)+'!=*'); |
| | 205 | case "eq": |
| | 206 | return quote_comment_str(quotes(condition.key)+'='+quotes(condition.val)); |
| | 207 | case "neq": |
| | 208 | return quote_comment_str(quotes(condition.key)+'!='+quotes(condition.val)); |
| | 209 | case "like": |
| | 210 | return quote_comment_str(quotes(condition.key)+'~'+quoteRegex(condition.val)); |
| | 211 | case "likelike": |
| | 212 | return quote_comment_str('~'+quotes(condition.key)+'~'+quoteRegex(condition.val)); |
| | 213 | case "notlike": |
| | 214 | return quote_comment_str(quotes(condition.key)+'!~'+quoteRegex(condition.val)); |
| | 215 | case "substr": |
| | 216 | return quote_comment_str(quotes(condition.key)+':'+quotes(condition.val)); |
| | 217 | case "meta": |
| | 218 | switch(condition.meta) { |
| | 219 | case "id": |
| | 220 | return quote_comment_str('id:'+quotes(condition.val)); |
| | 221 | case "newer": |
| | 222 | return quote_comment_str('newer:'+quotes(condition.val)); |
| | 223 | case "user": |
| | 224 | return quote_comment_str('user:'+quotes(condition.val)); |
| | 225 | case "uid": |
| | 226 | return quote_comment_str('uid:'+quotes(condition.val)); |
| | 227 | default: |
| | 228 | return ''; |
| | 229 | } |
| | 230 | case "free form": |
| | 231 | return quote_comment_str(quotes(condition.free)); |
| | 232 | default: |
| | 233 | return ''; |
| | 234 | } |
| | 235 | } |
| | 236 | |
| | 237 | ffs.query = normalize(ffs.query); |
| | 238 | |
| | 239 | query_parts.push('// gather results'); |
| | 240 | query_parts.push('('); |
| | 241 | for (var i=0; i<ffs.query.queries.length; i++) { |
| | 242 | var and_query = ffs.query.queries[i]; |
| | 243 | |
| | 244 | var types = ['node','way','relation']; |
| | 245 | var clauses = []; |
| | 246 | var clauses_str = []; |
| | 247 | for (var j=0; j<and_query.queries.length; j++) { |
| | 248 | var cond_query = and_query.queries[j]; |
| | 249 | // todo: looks like some code duplication here could be reduced by refactoring |
| | 250 | if (cond_query.query === "free form") { |
| | 251 | // eventually load free form query module |
| | 252 | if (!freeFormQuery) freeFormQuery = turbo.ffs.free(); |
| | 253 | var ffs_clause = freeFormQuery.get_query_clause(cond_query); |
| | 254 | if (ffs_clause === false) |
| | 255 | return false; |
| | 256 | // restrict possible data types |
| | 257 | types = types.filter(function(t) { |
| | 258 | return ffs_clause.types.indexOf(t) != -1; |
| | 259 | }); |
| | 260 | // add clauses |
| | 261 | clauses_str.push(get_query_clause_str(cond_query)); |
| | 262 | clauses = clauses.concat(ffs_clause.conditions.map(function(condition) { |
| | 263 | return get_query_clause(condition); |
| | 264 | })); |
| | 265 | } else if (cond_query.query === "type") { |
| | 266 | // restrict possible data types |
| | 267 | types = types.indexOf(cond_query.type) != -1 ? [cond_query.type] : []; |
| | 268 | } else { |
| | 269 | // add another query clause |
| | 270 | clauses_str.push(get_query_clause_str(cond_query)); |
| | 271 | var clause = get_query_clause(cond_query); |
| | 272 | if (clause === false) return false; |
| | 273 | clauses.push(clause); |
| | 274 | } |
| | 275 | } |
| | 276 | clauses_str = clauses_str.join(' and '); |
| | 277 | |
| | 278 | // construct query |
| | 279 | query_parts.push(' // query part for: “'+clauses_str+'”') |
| | 280 | for (var t=0; t<types.length; t++) { |
| | 281 | var buffer = ' '+types[t]; |
| | 282 | for (var c=0; c<clauses.length; c++) |
| | 283 | buffer += clauses[c]; |
| | 284 | if (bounds_part) |
| | 285 | buffer += bounds_part; |
| | 286 | buffer += ';'; |
| | 287 | query_parts.push(buffer); |
| | 288 | } |
| | 289 | } |
| | 290 | query_parts.push(');'); |
| | 291 | |
| | 292 | query_parts.push('// print results'); |
| | 293 | query_parts.push('out body;'); |
| | 294 | query_parts.push('>;'); |
| | 295 | query_parts.push('out skel qt;'); |
| | 296 | |
| | 297 | return query_parts.join('\n'); |
| | 298 | } |
| | 299 | |
| | 300 | // this is a "did you mean …" mechanism against typos in preset names |
| | 301 | ffs.repair_search = function(search) { |
| | 302 | try { |
| | 303 | ffs = turbo.ffs.parser.parse(search); |
| | 304 | } catch(e) { |
| | 305 | return false; |
| | 306 | } |
| | 307 | |
| | 308 | function quotes(s) { |
| | 309 | if (s.match(/^[a-zA-Z0-9_]+$/) === null) |
| | 310 | return '"'+s.replace(/"/g,'\\"')+'"'; |
| | 311 | return s; |
| | 312 | } |
| | 313 | |
| | 314 | var search_parts = []; |
| | 315 | var repaired = false; |
| | 316 | |
| | 317 | ffs.query = normalize(ffs.query); |
| | 318 | ffs.query.queries.forEach(function (q) { |
| | 319 | q.queries.forEach(validateQuery); |
| | 320 | }); |
| | 321 | function validateQuery(cond_query) { |
| | 322 | if (cond_query.query === "free form") { |
| | 323 | // eventually load free form query module |
| | 324 | if (!freeFormQuery) freeFormQuery = turbo.ffs.free(); |
| | 325 | var ffs_clause = freeFormQuery.get_query_clause(cond_query); |
| | 326 | if (ffs_clause === false) { |
| | 327 | // try to find suggestions for occasional typos |
| | 328 | var fuzzy = freeFormQuery.fuzzy_search(cond_query); |
| | 329 | var free_regex = null; |
| | 330 | try { free_regex = new RegExp("['\"]?"+escRegexp(cond_query.free)+"['\"]?"); } catch(e) {} |
| | 331 | if (fuzzy && search.match(free_regex)) { |
| | 332 | search_parts = search_parts.concat(search.split(free_regex)); |
| | 333 | search = search_parts.pop(); |
| | 334 | var replacement = quotes(fuzzy); |
| | 335 | search_parts.push(replacement); |
| | 336 | repaired = true; |
| | 337 | } |
| | 338 | } |
| | 339 | } |
| | 340 | } |
| | 341 | search_parts.push(search); |
| | 342 | |
| | 343 | if (!repaired) |
| | 344 | return false; |
| | 345 | return search_parts; |
| | 346 | } |
| | 347 | |
| | 348 | ffs.invalidateCache = function() { |
| | 349 | freeFormQuery = undefined; |
| | 350 | } |
| | 351 | |
| | 352 | return ffs; |
| | 353 | }; |
| | 354 | // ffs/wizard module |
| | 355 | if (typeof turbo === "undefined") turbo={}; |
| | 356 | |
| | 357 | turbo.ffs.free = function() { |
| | 358 | |
| | 359 | var freeFormQuery = {}; |
| | 360 | var presets = {}; |
| | 361 | |
| | 362 | // load presets |
| | 363 | (function loadPresets() { |
| | 364 | if (typeof $ === "undefined") return; |
| | 365 | var presets_file = "data/iD_presets.json"; |
| | 366 | try { |
| | 367 | $.ajax(presets_file,{async:false,dataType:"json"}).success(function(data){ |
| | 368 | presets = data; |
| | 369 | Object.keys(presets).map(function(key) { |
| | 370 | var preset = presets[key]; |
| | 371 | preset.nameCased = preset.name; |
| | 372 | preset.name = preset.name.toLowerCase(); |
| | 373 | preset.terms = !preset.terms ? [] : preset.terms.map(function(term) {return term.toLowerCase();}); |
| | 374 | }); |
| | 375 | }).error(function(){ |
| | 376 | throw new Error(); |
| | 377 | }); |
| | 378 | } catch(e) { |
| | 379 | console.log("failed to load presets file", presets_file, e); |
| | 380 | } |
| | 381 | })(); |
| | 382 | // load preset translations |
| | 383 | (function loadPresetTranslations() { |
| | 384 | if (typeof $ === "undefined" || typeof i18n === "undefined") return; |
| | 385 | var language = i18n.getLanguage(); |
| | 386 | if (language == "en") return; |
| | 387 | var translation_file = "data/iD_presets_"+language+".json"; |
| | 388 | try { |
| | 389 | $.ajax(translation_file,{async:false,dataType:"json"}).success(function(data){ |
| | 390 | // load translated names and terms into presets object |
| | 391 | Object.keys(data).map(function(preset) { |
| | 392 | var translation = data[preset]; |
| | 393 | preset = presets[preset]; |
| | 394 | preset.translated = true; |
| | 395 | // save original preset name under alternative terms |
| | 396 | var oriPresetName = preset.name; |
| | 397 | // save translated preset name |
| | 398 | preset.nameCased = translation.name; |
| | 399 | preset.name = translation.name.toLowerCase(); |
| | 400 | // add new terms |
| | 401 | if (translation.terms) |
| | 402 | preset.terms = translation.terms.split(",") |
| | 403 | .map(function(term) { return term.trim().toLowerCase(); }) |
| | 404 | .concat(preset.terms); |
| | 405 | // add this to the front to allow exact (english) preset names to match before terms |
| | 406 | preset.terms.unshift(oriPresetName); |
| | 407 | }); |
| | 408 | }).error(function(){ |
| | 409 | throw new Error(); |
| | 410 | }); |
| | 411 | } catch(e) { |
| | 412 | console.log("failed to load preset translations file: "+translation_file); |
| | 413 | } |
| | 414 | })(); |
| | 415 | |
| | 416 | freeFormQuery.get_query_clause = function(condition) { |
| | 417 | // search presets for ffs term |
| | 418 | var search = condition.free.toLowerCase(); |
| | 419 | var candidates = Object.keys(presets).map(function(key) { |
| | 420 | return presets[key]; |
| | 421 | }).filter(function(preset) { |
| | 422 | if (preset.searchable===false) return false; |
| | 423 | if (preset.name === search) return true; |
| | 424 | preset._termsIndex = preset.terms.indexOf(search); |
| | 425 | return preset._termsIndex != -1; |
| | 426 | }); |
| | 427 | if (candidates.length === 0) |
| | 428 | return false; |
| | 429 | // sort candidates |
| | 430 | candidates.sort(function(a,b) { |
| | 431 | // prefer exact name matches |
| | 432 | if (a.name === search) return -1; |
| | 433 | if (b.name === search) return 1; |
| | 434 | return a._termsIndex - b._termsIndex; |
| | 435 | }); |
| | 436 | var preset = candidates[0]; |
| | 437 | var types = []; |
| | 438 | preset.geometry.forEach(function(g) { |
| | 439 | switch (g) { |
| | 440 | case "point": |
| | 441 | case "vertex": |
| | 442 | types.push("node"); |
| | 443 | break; |
| | 444 | case "line": |
| | 445 | types.push("way"); |
| | 446 | break; |
| | 447 | case "area": |
| | 448 | types.push("way"); |
| | 449 | types.push("relation"); // todo: additionally add type=multipolygon? |
| | 450 | break; |
| | 451 | case "relation": |
| | 452 | types.push("relation"); |
| | 453 | break; |
| | 454 | default: |
| | 455 | console.log("unknown geometry type "+g+" of preset "+preset.name); |
| | 456 | } |
| | 457 | }); |
| | 458 | function onlyUnique(value, index, self) { |
| | 459 | return self.indexOf(value) === index; |
| | 460 | } |
| | 461 | return { |
| | 462 | types: types.filter(onlyUnique), |
| | 463 | conditions: Object.keys(preset.tags).map(function(k) { |
| | 464 | var v = preset.tags[k]; |
| | 465 | return { |
| | 466 | query: v==="*" ? "key" : "eq", |
| | 467 | key: k, |
| | 468 | val: v |
| | 469 | }; |
| | 470 | }) |
| | 471 | }; |
| | 472 | } |
| | 473 | |
| | 474 | freeFormQuery.fuzzy_search = function(condition) { |
| | 475 | // search presets for ffs term |
| | 476 | var search = condition.free.toLowerCase(); |
| | 477 | // fuzzyness: max lev.dist allowed to still match |
| | 478 | var fuzzyness = 2+Math.floor(search.length/7); |
| | 479 | function fuzzyMatch(term) { |
| | 480 | return levenshteinDistance(term, search) <= fuzzyness; |
| | 481 | } |
| | 482 | var candidates = Object.keys(presets).map(function(key) { |
| | 483 | return presets[key]; |
| | 484 | }).filter(function(preset) { |
| | 485 | if (preset.searchable===false) return false; |
| | 486 | if (fuzzyMatch(preset.name)) return true; |
| | 487 | return preset.terms.some(fuzzyMatch); |
| | 488 | }); |
| | 489 | if (candidates.length === 0) |
| | 490 | return false; |
| | 491 | // sort candidates |
| | 492 | function preset_weight(preset) { |
| | 493 | return [preset.name].concat(preset.terms).map(function(term, index) { |
| | 494 | return levenshteinDistance(term,search); |
| | 495 | }).reduce(function min(a, b) { |
| | 496 | return a <= b ? a : b; |
| | 497 | }); |
| | 498 | }; |
| | 499 | candidates.sort(function(a,b) { |
| | 500 | return preset_weight(a) - preset_weight(b); |
| | 501 | }); |
| | 502 | var preset = candidates[0]; |
| | 503 | return preset.nameCased; |
| | 504 | } |
| | 505 | |
| | 506 | |
| | 507 | return freeFormQuery; |
| | 508 | }; |
| | 509 | turbo.ffs.parser = (function() { |
| | 510 | /* |
| | 511 | * Generated by PEG.js 0.8.0. |
| | 512 | * |
| | 513 | * http://pegjs.majda.cz/ |
| | 514 | */ |
| | 515 | |
| | 516 | function peg$subclass(child, parent) { |
| | 517 | function ctor() { this.constructor = child; } |
| | 518 | ctor.prototype = parent.prototype; |
| | 519 | child.prototype = new ctor(); |
| | 520 | } |
| | 521 | |
| | 522 | function SyntaxError(message, expected, found, offset, line, column) { |
| | 523 | this.message = message; |
| | 524 | this.expected = expected; |
| | 525 | this.found = found; |
| | 526 | this.offset = offset; |
| | 527 | this.line = line; |
| | 528 | this.column = column; |
| | 529 | |
| | 530 | this.name = "SyntaxError"; |
| | 531 | } |
| | 532 | |
| | 533 | peg$subclass(SyntaxError, Error); |
| | 534 | |
| | 535 | function parse(input) { |
| | 536 | var options = arguments.length > 1 ? arguments[1] : {}, |
| | 537 | |
| | 538 | peg$FAILED = {}, |
| | 539 | |
| | 540 | peg$startRuleIndices = { start: 0 }, |
| | 541 | peg$startRuleIndex = 0, |
| | 542 | |
| | 543 | peg$consts = [ |
| | 544 | peg$FAILED, |
| | 545 | function(x) { return x }, |
| | 546 | [], |
| | 547 | "in bbox", |
| | 548 | { type: "literal", value: "in bbox", description: "\"in bbox\"" }, |
| | 549 | "IN BBOX", |
| | 550 | { type: "literal", value: "IN BBOX", description: "\"IN BBOX\"" }, |
| | 551 | function(x) { return { bounds:"bbox", query:x } }, |
| | 552 | "in", |
| | 553 | { type: "literal", value: "in", description: "\"in\"" }, |
| | 554 | "IN", |
| | 555 | { type: "literal", value: "IN", description: "\"IN\"" }, |
| | 556 | function(x, y) { return { bounds:"area", query:x, area:y } }, |
| | 557 | "around", |
| | 558 | { type: "literal", value: "around", description: "\"around\"" }, |
| | 559 | "AROUND", |
| | 560 | { type: "literal", value: "AROUND", description: "\"AROUND\"" }, |
| | 561 | function(x, y) { return { bounds:"around", query:x, area:y } }, |
| | 562 | "global", |
| | 563 | { type: "literal", value: "global", description: "\"global\"" }, |
| | 564 | "GLOBAL", |
| | 565 | { type: "literal", value: "GLOBAL", description: "\"GLOBAL\"" }, |
| | 566 | function(x) { return { bounds:"global", query:x } }, |
| | 567 | "or", |
| | 568 | { type: "literal", value: "or", description: "\"or\"" }, |
| | 569 | "OR", |
| | 570 | { type: "literal", value: "OR", description: "\"OR\"" }, |
| | 571 | "||", |
| | 572 | { type: "literal", value: "||", description: "\"||\"" }, |
| | 573 | "|", |
| | 574 | { type: "literal", value: "|", description: "\"|\"" }, |
| | 575 | function(x, y) { return { logical:"or", queries:[x,y] } }, |
| | 576 | "and", |
| | 577 | { type: "literal", value: "and", description: "\"and\"" }, |
| | 578 | "AND", |
| | 579 | { type: "literal", value: "AND", description: "\"AND\"" }, |
| | 580 | "&&", |
| | 581 | { type: "literal", value: "&&", description: "\"&&\"" }, |
| | 582 | "&", |
| | 583 | { type: "literal", value: "&", description: "\"&\"" }, |
| | 584 | function(x, y) { return { logical:"and", queries:[x,y] } }, |
| | 585 | "(", |
| | 586 | { type: "literal", value: "(", description: "\"(\"" }, |
| | 587 | ")", |
| | 588 | { type: "literal", value: ")", description: "\")\"" }, |
| | 589 | function(x) { return x; }, |
| | 590 | "=", |
| | 591 | { type: "literal", value: "=", description: "\"=\"" }, |
| | 592 | "==", |
| | 593 | { type: "literal", value: "==", description: "\"==\"" }, |
| | 594 | function(x, y) { return { query:"eq", key:x, val:y } }, |
| | 595 | "!=", |
| | 596 | { type: "literal", value: "!=", description: "\"!=\"" }, |
| | 597 | "<>", |
| | 598 | { type: "literal", value: "<>", description: "\"<>\"" }, |
| | 599 | function(x, y) { return { query:"neq", key:x, val:y } }, |
| | 600 | "*", |
| | 601 | { type: "literal", value: "*", description: "\"*\"" }, |
| | 602 | function(x) { return { query:"key", key:x } }, |
| | 603 | "is", |
| | 604 | { type: "literal", value: "is", description: "\"is\"" }, |
| | 605 | "not", |
| | 606 | { type: "literal", value: "not", description: "\"not\"" }, |
| | 607 | "null", |
| | 608 | { type: "literal", value: "null", description: "\"null\"" }, |
| | 609 | "IS", |
| | 610 | { type: "literal", value: "IS", description: "\"IS\"" }, |
| | 611 | "NOT", |
| | 612 | { type: "literal", value: "NOT", description: "\"NOT\"" }, |
| | 613 | "NULL", |
| | 614 | { type: "literal", value: "NULL", description: "\"NULL\"" }, |
| | 615 | function(x) { return { query:"nokey", key:x } }, |
| | 616 | "~=", |
| | 617 | { type: "literal", value: "~=", description: "\"~=\"" }, |
| | 618 | "~", |
| | 619 | { type: "literal", value: "~", description: "\"~\"" }, |
| | 620 | "=~", |
| | 621 | { type: "literal", value: "=~", description: "\"=~\"" }, |
| | 622 | function(x, y) { return { query:"like", key:x, val:y.regex?y:{regex:y} } }, |
| | 623 | "like", |
| | 624 | { type: "literal", value: "like", description: "\"like\"" }, |
| | 625 | "LIKE", |
| | 626 | { type: "literal", value: "LIKE", description: "\"LIKE\"" }, |
| | 627 | function(x, y) { return { query:"likelike", key:x, val:y.regex?y:{regex:y} } }, |
| | 628 | "!~", |
| | 629 | { type: "literal", value: "!~", description: "\"!~\"" }, |
| | 630 | function(x, y) { return { query:"notlike", key:x, val:y.regex?y:{regex:y} } }, |
| | 631 | ":", |
| | 632 | { type: "literal", value: ":", description: "\":\"" }, |
| | 633 | function(x, y) { return { query:"substr", key:x, val:y } }, |
| | 634 | "type", |
| | 635 | { type: "literal", value: "type", description: "\"type\"" }, |
| | 636 | function(x) { return { query:"type", type:x } }, |
| | 637 | "user", |
| | 638 | { type: "literal", value: "user", description: "\"user\"" }, |
| | 639 | "uid", |
| | 640 | { type: "literal", value: "uid", description: "\"uid\"" }, |
| | 641 | "newer", |
| | 642 | { type: "literal", value: "newer", description: "\"newer\"" }, |
| | 643 | "id", |
| | 644 | { type: "literal", value: "id", description: "\"id\"" }, |
| | 645 | function(x, y) { return { query:"meta", meta:x, val:y } }, |
| | 646 | function(x) { return { query:"free form", free:x } }, |
| | 647 | { type: "other", description: "Key" }, |
| | 648 | /^[a-zA-Z0-9_:\-]/, |
| | 649 | { type: "class", value: "[a-zA-Z0-9_:\\-]", description: "[a-zA-Z0-9_:\\-]" }, |
| | 650 | function(s) { return s.join(''); }, |
| | 651 | "\"", |
| | 652 | { type: "literal", value: "\"", description: "\"\\\"\"" }, |
| | 653 | "'", |
| | 654 | { type: "literal", value: "'", description: "\"'\"" }, |
| | 655 | function(parts) { |
| | 656 | return parts[1]; |
| | 657 | }, |
| | 658 | { type: "other", description: "string" }, |
| | 659 | /^[^'" ()~=!*\/:<>&|[\]{}#+@$%?\^.,]/, |
| | 660 | { type: "class", value: "[^'\" ()~=!*\\/:<>&|[\\]{}#+@$%?\\^.,]", description: "[^'\" ()~=!*\\/:<>&|[\\]{}#+@$%?\\^.,]" }, |
| | 661 | function(chars) { return chars.join(""); }, |
| | 662 | void 0, |
| | 663 | "\\", |
| | 664 | { type: "literal", value: "\\", description: "\"\\\\\"" }, |
| | 665 | { type: "any", description: "any character" }, |
| | 666 | function(char_) { return char_; }, |
| | 667 | function(sequence) { return sequence; }, |
| | 668 | /^['"\\bfnrtv]/, |
| | 669 | { type: "class", value: "['\"\\\\bfnrtv]", description: "['\"\\\\bfnrtv]" }, |
| | 670 | function(char_) { |
| | 671 | return char_ |
| | 672 | .replace("b", "\b") |
| | 673 | .replace("f", "\f") |
| | 674 | .replace("n", "\n") |
| | 675 | .replace("r", "\r") |
| | 676 | .replace("t", "\t") |
| | 677 | .replace("v", "\x0B") // IE does not recognize "\v". |
| | 678 | }, |
| | 679 | "/", |
| | 680 | { type: "literal", value: "/", description: "\"/\"" }, |
| | 681 | null, |
| | 682 | "i", |
| | 683 | { type: "literal", value: "i", description: "\"i\"" }, |
| | 684 | "", |
| | 685 | function(parts) { |
| | 686 | return { regex: parts[1], modifier: parts[3] }; |
| | 687 | }, |
| | 688 | "\\/", |
| | 689 | { type: "literal", value: "\\/", description: "\"\\\\/\"" }, |
| | 690 | function() { return "/"; }, |
| | 691 | { type: "other", description: "whitespace" }, |
| | 692 | /^[ \t\n\r]/, |
| | 693 | { type: "class", value: "[ \\t\\n\\r]", description: "[ \\t\\n\\r]" } |
| | 694 | ], |
| | 695 | |
| | 696 | peg$bytecode = [ |
| | 697 | peg$decode("!7;+<$7!+2%7;+(%4#6!#!!%$## $\"# \"# "), |
| | 698 | peg$decode("!7\"+]$ \"7<+&$,#&7<\"\"\" +D%.#\"\"2#3$*) \".%\"\"2%3&+(%4#6'#!\"%$## $\"# \"# *\u0158 \"!7\"+\x81$ \"7<+&$,#&7<\"\"\" +h%.(\"\"2(3)*) \".*\"\"2*3++L% \"7<+&$,#&7<\"\"\" +3%72+)%4%6,%\"$ %$%# $$# $## $\"# \"# *\xE9 \"!7\"+\x81$ \"7<+&$,#&7<\"\"\" +h%.-\"\"2-3.*) \"./\"\"2/30+L% \"7<+&$,#&7<\"\"\" +3%72+)%4%61%\"$ %$%# $$# $## $\"# \"# *z \"!7\"+]$ \"7<+&$,#&7<\"\"\" +D%.2\"\"2233*) \".4\"\"2435+(%4#66#!\"%$## $\"# \"# */ \"!7\"+' 4!6'!! %"), |
| | 699 | peg$decode("!7#+\x99$ \"7<+&$,#&7<\"\"\" +\x80%.7\"\"2738*A \".9\"\"293:*5 \".;\"\"2;3<*) \".=\"\"2=3>+L% \"7<+&$,#&7<\"\"\" +3%7\"+)%4%6?%\"$ %$%# $$# $## $\"# \"# *# \"7#"), |
| | 700 | peg$decode("!7$+\x99$ \"7<+&$,#&7<\"\"\" +\x80%.@\"\"2@3A*A \".B\"\"2B3C*5 \".D\"\"2D3E*) \".F\"\"2F3G+L% \"7<+&$,#&7<\"\"\" +3%7#+)%4%6H%\"$ %$%# $$# $## $\"# \"# *# \"7$"), |
| | 701 | peg$decode("7%*g \"!.I\"\"2I3J+V$7;+L%7\"+B%7;+8%.K\"\"2K3L+(%4%6M%!\"%$%# $$# $## $\"# \"# "), |
| | 702 | peg$decode("7.*Y \"7/*S \"7&*M \"7'*G \"7(*A \"7)*; \"7**5 \"7+*/ \"7,*) \"7-*# \"70"), |
| | 703 | peg$decode("!71+c$7;+Y%.N\"\"2N3O*) \".P\"\"2P3Q+=%7;+3%72+)%4%6R%\"$ %$%# $$# $## $\"# \"# "), |
| | 704 | peg$decode("!71+c$7;+Y%.S\"\"2S3T*) \".U\"\"2U3V+=%7;+3%72+)%4%6W%\"$ %$%# $$# $## $\"# \"# "), |
| | 705 | peg$decode("!71+h$7;+^%.N\"\"2N3O*) \".P\"\"2P3Q+B%7;+8%.X\"\"2X3Y+(%4%6Z%!$%$%# $$# $## $\"# \"# *\u0122 \"!72+\u0117$ \"7<+&$,#&7<\"\"\" +\xFE%!.[\"\"2[3\\+u$ \"7<+&$,#&7<\"\"\" +\\%.]\"\"2]3^+L% \"7<+&$,#&7<\"\"\" +3%._\"\"2_3`+#%'%%$%# $$# $## $\"# \"# *\x86 \"!.a\"\"2a3b+u$ \"7<+&$,#&7<\"\"\" +\\%.c\"\"2c3d+L% \"7<+&$,#&7<\"\"\" +3%.e\"\"2e3f+#%'%%$%# $$# $## $\"# \"# +(%4#6Z#!\"%$## $\"# \"# "), |
| | 706 | peg$decode("!71+h$7;+^%.S\"\"2S3T*) \".U\"\"2U3V+B%7;+8%.X\"\"2X3Y+(%4%6g%!$%$%# $$# $## $\"# \"# *\xD0 \"!72+\xC5$ \"7<+&$,#&7<\"\"\" +\xAC%!.[\"\"2[3\\+L$ \"7<+&$,#&7<\"\"\" +3%._\"\"2_3`+#%'#%$## $\"# \"# *] \"!.a\"\"2a3b+L$ \"7<+&$,#&7<\"\"\" +3%.e\"\"2e3f+#%'#%$## $\"# \"# +(%4#6g#!\"%$## $\"# \"# "), |
| | 707 | peg$decode("!71+u$7;+k%.h\"\"2h3i*5 \".j\"\"2j3k*) \".l\"\"2l3m+C%7;+9%72*# \"78+)%4%6n%\"$ %$%# $$# $## $\"# \"# *\x92 \"!72+\x87$ \"7<+&$,#&7<\"\"\" +n%.o\"\"2o3p*) \".q\"\"2q3r+R% \"7<+&$,#&7<\"\"\" +9%72*# \"78+)%4%6n%\"$ %$%# $$# $## $\"# \"# "), |
| | 708 | peg$decode("!.j\"\"2j3k+\x89$7;+%72+u%7;+k%.h\"\"2h3i*5 \".j\"\"2j3k*) \".l\"\"2l3m+C%7;+9%72*# \"78+)%4'6s'\"$ %$'# $&# $%# $$# $## $\"# \"# "), |
| | 709 | peg$decode("!71+]$7;+S%.t\"\"2t3u+C%7;+9%72*# \"78+)%4%6v%\"$ %$%# $$# $## $\"# \"# *\xFA \"!72+\xEF$ \"7<+&$,#&7<\"\"\" +\xD6%!.]\"\"2]3^+L$ \"7<+&$,#&7<\"\"\" +3%.o\"\"2o3p+#%'#%$## $\"# \"# *] \"!.c\"\"2c3d+L$ \"7<+&$,#&7<\"\"\" +3%.q\"\"2q3r+#%'#%$## $\"# \"# +R% \"7<+&$,#&7<\"\"\" +9%72*# \"78+)%4%6v%\"$ %$%# $$# $## $\"# \"# "), |
| | 710 | peg$decode("!72+W$7;+M%.w\"\"2w3x+=%7;+3%72+)%4%6y%\"$ %$%# $$# $## $\"# \"# "), |
| | 711 | peg$decode("!.z\"\"2z3{+V$7;+L%.w\"\"2w3x+<%7;+2%72+(%4%6|%! %$%# $$# $## $\"# \"# "), |
| | 712 | peg$decode("!.}\"\"2}3~*A \".\"\"23\x80*5 \".\x81\"\"2\x813\x82*) \".\x83\"\"2\x833\x84+W$7;+M%.w\"\"2w3x+=%7;+3%72+)%4%6\x85%\"$ %$%# $$# $## $\"# \"# "), |
| | 713 | peg$decode("!72+' 4!6\x86!! %"), |
| | 714 | peg$decode("8! \"0\x88\"\"1!3\x89+,$,)&0\x88\"\"1!3\x89\"\"\" +' 4!6\x8A!! %*\x8B \"!!.\x8B\"\"2\x8B3\x8C+=$73+3%.\x8B\"\"2\x8B3\x8C+#%'#%$## $\"# \"# *N \"!.\x8D\"\"2\x8D3\x8E+=$74+3%.\x8D\"\"2\x8D3\x8E+#%'#%$## $\"# \"# +' 4!6\x8F!! %9*\" 3\x87"), |
| | 715 | peg$decode("8! \"0\x91\"\"1!3\x92+,$,)&0\x91\"\"1!3\x92\"\"\" +' 4!6\x8A!! %*\x8B \"!!.\x8B\"\"2\x8B3\x8C+=$73+3%.\x8B\"\"2\x8B3\x8C+#%'#%$## $\"# \"# *N \"!.\x8D\"\"2\x8D3\x8E+=$74+3%.\x8D\"\"2\x8D3\x8E+#%'#%$## $\"# \"# +' 4!6\x8F!! %9*\" 3\x90"), |
| | 716 | peg$decode("! \"75,#&75\"+' 4!6\x93!! %"), |
| | 717 | peg$decode("! \"76,#&76\"+' 4!6\x93!! %"), |
| | 718 | peg$decode("!!8.\x8B\"\"2\x8B3\x8C*) \".\x95\"\"2\x953\x969*$$\"\" \x94\"# +7$-\"\"1!3\x97+(%4\"6\x98\"! %$\"# \"# *C \"!.\x95\"\"2\x953\x96+2$77+(%4\"6\x99\"! %$\"# \"# "), |
| | 719 | peg$decode("!!8.\x8D\"\"2\x8D3\x8E*) \".\x95\"\"2\x953\x969*$$\"\" \x94\"# +7$-\"\"1!3\x97+(%4\"6\x98\"! %$\"# \"# *C \"!.\x95\"\"2\x953\x96+2$77+(%4\"6\x99\"! %$\"# \"# "), |
| | 720 | peg$decode("!0\x9A\"\"1!3\x9B+' 4!6\x9C!! %"), |
| | 721 | peg$decode("8!!.\x9D\"\"2\x9D3\x9E+Y$79+O%.\x9D\"\"2\x9D3\x9E+?%.\xA0\"\"2\xA03\xA1*# \" \xA2*# \" \x9F+#%'$%$$# $## $\"# \"# +' 4!6\xA3!! %9*\" 3\x90"), |
| | 722 | peg$decode("! \"7:+&$,#&7:\"\"\" +' 4!6\x93!! %"), |
| | 723 | peg$decode("!!8.\x9D\"\"2\x9D3\x9E*) \".\xA4\"\"2\xA43\xA59*$$\"\" \x94\"# +7$-\"\"1!3\x97+(%4\"6\x98\"! %$\"# \"# *4 \"!.\xA4\"\"2\xA43\xA5+& 4!6\xA6! %"), |
| | 724 | peg$decode("8 \"7<,#&7<\"9*\" 3\xA7"), |
| | 725 | peg$decode("80\xA8\"\"1!3\xA99*\" 3\xA7") |
| | 726 | ], |
| | 727 | |
| | 728 | peg$currPos = 0, |
| | 729 | peg$reportedPos = 0, |
| | 730 | peg$cachedPos = 0, |
| | 731 | peg$cachedPosDetails = { line: 1, column: 1, seenCR: false }, |
| | 732 | peg$maxFailPos = 0, |
| | 733 | peg$maxFailExpected = [], |
| | 734 | peg$silentFails = 0, |
| | 735 | |
| | 736 | peg$result; |
| | 737 | |
| | 738 | if ("startRule" in options) { |
| | 739 | if (!(options.startRule in peg$startRuleIndices)) { |
| | 740 | throw new Error("Can't start parsing from rule \"" + options.startRule + "\"."); |
| | 741 | } |
| | 742 | |
| | 743 | peg$startRuleIndex = peg$startRuleIndices[options.startRule]; |
| | 744 | } |
| | 745 | |
| | 746 | function text() { |
| | 747 | return input.substring(peg$reportedPos, peg$currPos); |
| | 748 | } |
| | 749 | |
| | 750 | function offset() { |
| | 751 | return peg$reportedPos; |
| | 752 | } |
| | 753 | |
| | 754 | function line() { |
| | 755 | return peg$computePosDetails(peg$reportedPos).line; |
| | 756 | } |
| | 757 | |
| | 758 | function column() { |
| | 759 | return peg$computePosDetails(peg$reportedPos).column; |
| | 760 | } |
| | 761 | |
| | 762 | function expected(description) { |
| | 763 | throw peg$buildException( |
| | 764 | null, |
| | 765 | [{ type: "other", description: description }], |
| | 766 | peg$reportedPos |
| | 767 | ); |
| | 768 | } |
| | 769 | |
| | 770 | function error(message) { |
| | 771 | throw peg$buildException(message, null, peg$reportedPos); |
| | 772 | } |
| | 773 | |
| | 774 | function peg$computePosDetails(pos) { |
| | 775 | function advance(details, startPos, endPos) { |
| | 776 | var p, ch; |
| | 777 | |
| | 778 | for (p = startPos; p < endPos; p++) { |
| | 779 | ch = input.charAt(p); |
| | 780 | if (ch === "\n") { |
| | 781 | if (!details.seenCR) { details.line++; } |
| | 782 | details.column = 1; |
| | 783 | details.seenCR = false; |
| | 784 | } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") { |
| | 785 | details.line++; |
| | 786 | details.column = 1; |
| | 787 | details.seenCR = true; |
| | 788 | } else { |
| | 789 | details.column++; |
| | 790 | details.seenCR = false; |
| | 791 | } |
| | 792 | } |
| | 793 | } |
| | 794 | |
| | 795 | if (peg$cachedPos !== pos) { |
| | 796 | if (peg$cachedPos > pos) { |
| | 797 | peg$cachedPos = 0; |
| | 798 | peg$cachedPosDetails = { line: 1, column: 1, seenCR: false }; |
| | 799 | } |
| | 800 | advance(peg$cachedPosDetails, peg$cachedPos, pos); |
| | 801 | peg$cachedPos = pos; |
| | 802 | } |
| | 803 | |
| | 804 | return peg$cachedPosDetails; |
| | 805 | } |
| | 806 | |
| | 807 | function peg$fail(expected) { |
| | 808 | if (peg$currPos < peg$maxFailPos) { return; } |
| | 809 | |
| | 810 | if (peg$currPos > peg$maxFailPos) { |
| | 811 | peg$maxFailPos = peg$currPos; |
| | 812 | peg$maxFailExpected = []; |
| | 813 | } |
| | 814 | |
| | 815 | peg$maxFailExpected.push(expected); |
| | 816 | } |
| | 817 | |
| | 818 | function peg$buildException(message, expected, pos) { |
| | 819 | function cleanupExpected(expected) { |
| | 820 | var i = 1; |
| | 821 | |
| | 822 | expected.sort(function(a, b) { |
| | 823 | if (a.description < b.description) { |
| | 824 | return -1; |
| | 825 | } else if (a.description > b.description) { |
| | 826 | return 1; |
| | 827 | } else { |
| | 828 | return 0; |
| | 829 | } |
| | 830 | }); |
| | 831 | |
| | 832 | while (i < expected.length) { |
| | 833 | if (expected[i - 1] === expected[i]) { |
| | 834 | expected.splice(i, 1); |
| | 835 | } else { |
| | 836 | i++; |
| | 837 | } |
| | 838 | } |
| | 839 | } |
| | 840 | |
| | 841 | function buildMessage(expected, found) { |
| | 842 | function stringEscape(s) { |
| | 843 | function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); } |
| | 844 | |
| | 845 | return s |
| | 846 | .replace(/\\/g, '\\\\') |
| | 847 | .replace(/"/g, '\\"') |
| | 848 | .replace(/\x08/g, '\\b') |
| | 849 | .replace(/\t/g, '\\t') |
| | 850 | .replace(/\n/g, '\\n') |
| | 851 | .replace(/\f/g, '\\f') |
| | 852 | .replace(/\r/g, '\\r') |
| | 853 | .replace(/[\x00-\x07\x0B\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) |
| | 854 | .replace(/[\x10-\x1F\x80-\xFF]/g, function(ch) { return '\\x' + hex(ch); }) |
| | 855 | .replace(/[\u0180-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); }) |
| | 856 | .replace(/[\u1080-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); }); |
| | 857 | } |
| | 858 | |
| | 859 | var expectedDescs = new Array(expected.length), |
| | 860 | expectedDesc, foundDesc, i; |
| | 861 | |
| | 862 | for (i = 0; i < expected.length; i++) { |
| | 863 | expectedDescs[i] = expected[i].description; |
| | 864 | } |
| | 865 | |
| | 866 | expectedDesc = expected.length > 1 |
| | 867 | ? expectedDescs.slice(0, -1).join(", ") |
| | 868 | + " or " |
| | 869 | + expectedDescs[expected.length - 1] |
| | 870 | : expectedDescs[0]; |
| | 871 | |
| | 872 | foundDesc = found ? "\"" + stringEscape(found) + "\"" : "end of input"; |
| | 873 | |
| | 874 | return "Expected " + expectedDesc + " but " + foundDesc + " found."; |
| | 875 | } |
| | 876 | |
| | 877 | var posDetails = peg$computePosDetails(pos), |
| | 878 | found = pos < input.length ? input.charAt(pos) : null; |
| | 879 | |
| | 880 | if (expected !== null) { |
| | 881 | cleanupExpected(expected); |
| | 882 | } |
| | 883 | |
| | 884 | return new SyntaxError( |
| | 885 | message !== null ? message : buildMessage(expected, found), |
| | 886 | expected, |
| | 887 | found, |
| | 888 | pos, |
| | 889 | posDetails.line, |
| | 890 | posDetails.column |
| | 891 | ); |
| | 892 | } |
| | 893 | |
| | 894 | function peg$decode(s) { |
| | 895 | var bc = new Array(s.length), i; |
| | 896 | |
| | 897 | for (i = 0; i < s.length; i++) { |
| | 898 | bc[i] = s.charCodeAt(i) - 32; |
| | 899 | } |
| | 900 | |
| | 901 | return bc; |
| | 902 | } |
| | 903 | |
| | 904 | function peg$parseRule(index) { |
| | 905 | var bc = peg$bytecode[index], |
| | 906 | ip = 0, |
| | 907 | ips = [], |
| | 908 | end = bc.length, |
| | 909 | ends = [], |
| | 910 | stack = [], |
| | 911 | params, i; |
| | 912 | |
| | 913 | function protect(object) { |
| | 914 | return Object.prototype.toString.apply(object) === "[object Array]" ? [] : object; |
| | 915 | } |
| | 916 | |
| | 917 | while (true) { |
| | 918 | while (ip < end) { |
| | 919 | switch (bc[ip]) { |
| | 920 | case 0: |
| | 921 | stack.push(protect(peg$consts[bc[ip + 1]])); |
| | 922 | ip += 2; |
| | 923 | break; |
| | 924 | |
| | 925 | case 1: |
| | 926 | stack.push(peg$currPos); |
| | 927 | ip++; |
| | 928 | break; |
| | 929 | |
| | 930 | case 2: |
| | 931 | stack.pop(); |
| | 932 | ip++; |
| | 933 | break; |
| | 934 | |
| | 935 | case 3: |
| | 936 | peg$currPos = stack.pop(); |
| | 937 | ip++; |
| | 938 | break; |
| | 939 | |
| | 940 | case 4: |
| | 941 | stack.length -= bc[ip + 1]; |
| | 942 | ip += 2; |
| | 943 | break; |
| | 944 | |
| | 945 | case 5: |
| | 946 | stack.splice(-2, 1); |
| | 947 | ip++; |
| | 948 | break; |
| | 949 | |
| | 950 | case 6: |
| | 951 | stack[stack.length - 2].push(stack.pop()); |
| | 952 | ip++; |
| | 953 | break; |
| | 954 | |
| | 955 | case 7: |
| | 956 | stack.push(stack.splice(stack.length - bc[ip + 1], bc[ip + 1])); |
| | 957 | ip += 2; |
| | 958 | break; |
| | 959 | |
| | 960 | case 8: |
| | 961 | stack.pop(); |
| | 962 | stack.push(input.substring(stack[stack.length - 1], peg$currPos)); |
| | 963 | ip++; |
| | 964 | break; |
| | 965 | |
| | 966 | case 9: |
| | 967 | ends.push(end); |
| | 968 | ips.push(ip + 3 + bc[ip + 1] + bc[ip + 2]); |
| | 969 | |
| | 970 | if (stack[stack.length - 1]) { |
| | 971 | end = ip + 3 + bc[ip + 1]; |
| | 972 | ip += 3; |
| | 973 | } else { |
| | 974 | end = ip + 3 + bc[ip + 1] + bc[ip + 2]; |
| | 975 | ip += 3 + bc[ip + 1]; |
| | 976 | } |
| | 977 | |
| | 978 | break; |
| | 979 | |
| | 980 | case 10: |
| | 981 | ends.push(end); |
| | 982 | ips.push(ip + 3 + bc[ip + 1] + bc[ip + 2]); |
| | 983 | |
| | 984 | if (stack[stack.length - 1] === peg$FAILED) { |
| | 985 | end = ip + 3 + bc[ip + 1]; |
| | 986 | ip += 3; |
| | 987 | } else { |
| | 988 | end = ip + 3 + bc[ip + 1] + bc[ip + 2]; |
| | 989 | ip += 3 + bc[ip + 1]; |
| | 990 | } |
| | 991 | |
| | 992 | break; |
| | 993 | |
| | 994 | case 11: |
| | 995 | ends.push(end); |
| | 996 | ips.push(ip + 3 + bc[ip + 1] + bc[ip + 2]); |
| | 997 | |
| | 998 | if (stack[stack.length - 1] !== peg$FAILED) { |
| | 999 | end = ip + 3 + bc[ip + 1]; |
| | 1000 | ip += 3; |
| | 1001 | } else { |
| | 1002 | end = ip + 3 + bc[ip + 1] + bc[ip + 2]; |
| | 1003 | ip += 3 + bc[ip + 1]; |
| | 1004 | } |
| | 1005 | |
| | 1006 | break; |
| | 1007 | |
| | 1008 | case 12: |
| | 1009 | if (stack[stack.length - 1] !== peg$FAILED) { |
| | 1010 | ends.push(end); |
| | 1011 | ips.push(ip); |
| | 1012 | |
| | 1013 | end = ip + 2 + bc[ip + 1]; |
| | 1014 | ip += 2; |
| | 1015 | } else { |
| | 1016 | ip += 2 + bc[ip + 1]; |
| | 1017 | } |
| | 1018 | |
| | 1019 | break; |
| | 1020 | |
| | 1021 | case 13: |
| | 1022 | ends.push(end); |
| | 1023 | ips.push(ip + 3 + bc[ip + 1] + bc[ip + 2]); |
| | 1024 | |
| | 1025 | if (input.length > peg$currPos) { |
| | 1026 | end = ip + 3 + bc[ip + 1]; |
| | 1027 | ip += 3; |
| | 1028 | } else { |
| | 1029 | end = ip + 3 + bc[ip + 1] + bc[ip + 2]; |
| | 1030 | ip += 3 + bc[ip + 1]; |
| | 1031 | } |
| | 1032 | |
| | 1033 | break; |
| | 1034 | |
| | 1035 | case 14: |
| | 1036 | ends.push(end); |
| | 1037 | ips.push(ip + 4 + bc[ip + 2] + bc[ip + 3]); |
| | 1038 | |
| | 1039 | if (input.substr(peg$currPos, peg$consts[bc[ip + 1]].length) === peg$consts[bc[ip + 1]]) { |
| | 1040 | end = ip + 4 + bc[ip + 2]; |
| | 1041 | ip += 4; |
| | 1042 | } else { |
| | 1043 | end = ip + 4 + bc[ip + 2] + bc[ip + 3]; |
| | 1044 | ip += 4 + bc[ip + 2]; |
| | 1045 | } |
| | 1046 | |
| | 1047 | break; |
| | 1048 | |
| | 1049 | case 15: |
| | 1050 | ends.push(end); |
| | 1051 | ips.push(ip + 4 + bc[ip + 2] + bc[ip + 3]); |
| | 1052 | |
| | 1053 | if (input.substr(peg$currPos, peg$consts[bc[ip + 1]].length).toLowerCase() === peg$consts[bc[ip + 1]]) { |
| | 1054 | end = ip + 4 + bc[ip + 2]; |
| | 1055 | ip += 4; |
| | 1056 | } else { |
| | 1057 | end = ip + 4 + bc[ip + 2] + bc[ip + 3]; |
| | 1058 | ip += 4 + bc[ip + 2]; |
| | 1059 | } |
| | 1060 | |
| | 1061 | break; |
| | 1062 | |
| | 1063 | case 16: |
| | 1064 | ends.push(end); |
| | 1065 | ips.push(ip + 4 + bc[ip + 2] + bc[ip + 3]); |
| | 1066 | |
| | 1067 | if (peg$consts[bc[ip + 1]].test(input.charAt(peg$currPos))) { |
| | 1068 | end = ip + 4 + bc[ip + 2]; |
| | 1069 | ip += 4; |
| | 1070 | } else { |
| | 1071 | end = ip + 4 + bc[ip + 2] + bc[ip + 3]; |
| | 1072 | ip += 4 + bc[ip + 2]; |
| | 1073 | } |
| | 1074 | |
| | 1075 | break; |
| | 1076 | |
| | 1077 | case 17: |
| | 1078 | stack.push(input.substr(peg$currPos, bc[ip + 1])); |
| | 1079 | peg$currPos += bc[ip + 1]; |
| | 1080 | ip += 2; |
| | 1081 | break; |
| | 1082 | |
| | 1083 | case 18: |
| | 1084 | stack.push(peg$consts[bc[ip + 1]]); |
| | 1085 | peg$currPos += peg$consts[bc[ip + 1]].length; |
| | 1086 | ip += 2; |
| | 1087 | break; |
| | 1088 | |
| | 1089 | case 19: |
| | 1090 | stack.push(peg$FAILED); |
| | 1091 | if (peg$silentFails === 0) { |
| | 1092 | peg$fail(peg$consts[bc[ip + 1]]); |
| | 1093 | } |
| | 1094 | ip += 2; |
| | 1095 | break; |
| | 1096 | |
| | 1097 | case 20: |
| | 1098 | peg$reportedPos = stack[stack.length - 1 - bc[ip + 1]]; |
| | 1099 | ip += 2; |
| | 1100 | break; |
| | 1101 | |
| | 1102 | case 21: |
| | 1103 | peg$reportedPos = peg$currPos; |
| | 1104 | ip++; |
| | 1105 | break; |
| | 1106 | |
| | 1107 | case 22: |
| | 1108 | params = bc.slice(ip + 4, ip + 4 + bc[ip + 3]); |
| | 1109 | for (i = 0; i < bc[ip + 3]; i++) { |
| | 1110 | params[i] = stack[stack.length - 1 - params[i]]; |
| | 1111 | } |
| | 1112 | |
| | 1113 | stack.splice( |
| | 1114 | stack.length - bc[ip + 2], |
| | 1115 | bc[ip + 2], |
| | 1116 | peg$consts[bc[ip + 1]].apply(null, params) |
| | 1117 | ); |
| | 1118 | |
| | 1119 | ip += 4 + bc[ip + 3]; |
| | 1120 | break; |
| | 1121 | |
| | 1122 | case 23: |
| | 1123 | stack.push(peg$parseRule(bc[ip + 1])); |
| | 1124 | ip += 2; |
| | 1125 | break; |
| | 1126 | |
| | 1127 | case 24: |
| | 1128 | peg$silentFails++; |
| | 1129 | ip++; |
| | 1130 | break; |
| | 1131 | |
| | 1132 | case 25: |
| | 1133 | peg$silentFails--; |
| | 1134 | ip++; |
| | 1135 | break; |
| | 1136 | |
| | 1137 | default: |
| | 1138 | throw new Error("Invalid opcode: " + bc[ip] + "."); |
| | 1139 | } |
| | 1140 | } |
| | 1141 | |
| | 1142 | if (ends.length > 0) { |
| | 1143 | end = ends.pop(); |
| | 1144 | ip = ips.pop(); |
| | 1145 | } else { |
| | 1146 | break; |
| | 1147 | } |
| | 1148 | } |
| | 1149 | |
| | 1150 | return stack[0]; |
| | 1151 | } |
| | 1152 | |
| | 1153 | peg$result = peg$parseRule(peg$startRuleIndex); |
| | 1154 | |
| | 1155 | if (peg$result !== peg$FAILED && peg$currPos === input.length) { |
| | 1156 | return peg$result; |
| | 1157 | } else { |
| | 1158 | if (peg$result !== peg$FAILED && peg$currPos < input.length) { |
| | 1159 | peg$fail({ type: "end", description: "end of input" }); |
| | 1160 | } |
| | 1161 | |
| | 1162 | throw peg$buildException(null, peg$maxFailExpected, peg$maxFailPos); |
| | 1163 | } |
| | 1164 | } |
| | 1165 | |
| | 1166 | return { |
| | 1167 | SyntaxError: SyntaxError, |
| | 1168 | parse: parse |
| | 1169 | }; |
| | 1170 | })(); |