rsel-exprparser-basic

Parses RSel-specific expression text and rebuilds it in the UI.

Pada tanggal 11 Maret 2016. Lihat %(latest_version_link).

Skrip ini tidak untuk dipasang secara langsung. Ini adalah pustaka skrip lain untuk disertakan dengan direktif meta // @require https://update.greatest.deepsurf.us/scripts/17641/112718/rsel-exprparser-basic.js

  1. // ==UserScript==
  2. // @name rsel-exprparser-basic
  3. // @namespace https://greatest.deepsurf.us/users/11629-TheLastTaterTot
  4. // @version 0.3.7
  5. // @description Parses RSel-specific expression text and rebuilds it in the UI.
  6. // @author TheLastTaterTot
  7. // @include https://editor-beta.waze.com/*editor/*
  8. // @include https://www.waze.com/*editor/*
  9. // @exclude https://www.waze.com/*user/editor/*
  10. // @grant none
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. // Main usage: RSelExprParser.updateExpression(<rsel expression text>)
  15.  
  16. var RSelExprParser = {
  17. version: '0.3.7',
  18. new__EXPR_DEBUGINFO: function(m, exprWord, exprPhrase) {
  19. return {
  20. m: m,
  21. exprMatches: exprWord,
  22. exprMatchPhrases: exprPhrase,
  23. exprBuild: {},
  24. err: null ,
  25. errorMsg: null
  26. };
  27. },
  28. _getSelectionIndex: function(selector, selText) {
  29. for (var s = 0, sLength = selector.length; s < sLength; s++) {
  30. if (new RegExp(selText,'i').test(selector[s].text)) {
  31. return selector[s].value;
  32. }
  33. }
  34. },
  35. _getSelectOptions: function(selector) {
  36. for (var opts = [], s = 0, sLength = selector.length; s < sLength; s++) {
  37. opts[s] = selector[s].text.toLowerCase();
  38. }
  39. return opts;
  40. },
  41. _getNewExprBuild: function() {
  42. return {
  43. cond: null ,
  44. op: null ,
  45. op2: null ,
  46. val: null ,
  47. val2: null ,
  48. condmod: null ,
  49. errorCode: 0
  50.  
  51. }
  52. },
  53. getCurrentExprText: function(){
  54. return document.getElementById('outRSExpr').value;
  55. },
  56. /*Using RSel DOM elements rather than requesting dev to provide direct modifiction of RSel's expr object.
  57. This is so the RSel dev can feel free to significantly change his object storage structure if needed. */
  58. rselButtons: {
  59. lfParens: function() {
  60. try {
  61. document.getElementById('btnRSLBkt').click();
  62. } catch (err) {}
  63. },
  64. rtParens: function() {
  65. try {
  66. document.getElementById('btnRSRBkt').click();
  67. } catch (err) {}
  68. },
  69. and: function() {
  70. try {
  71. document.getElementById('btnRSAnd').click()
  72. } catch (err) {}
  73. },
  74. or: function() {
  75. try {
  76. document.getElementById('btnRSOr').click()
  77. } catch (err) {}
  78. },
  79. not: function() {
  80. try {
  81. document.getElementById('btnRSNot').click()
  82. } catch (err) {}
  83. },
  84. clear: function() {
  85. try {
  86. document.getElementById('btnRSClear').click()
  87. } catch (err) {}
  88. }
  89. },
  90. rselConditions: {
  91. country: {
  92. op: function(selText) {
  93. document.getElementById('opRSCountry').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSCountry').options, selText);
  94. },
  95. val: function(selText) {
  96. document.getElementById('selRSCountry').value = RSelExprParser._getSelectionIndex(document.getElementById('selRSCountry').options, selText);
  97. },
  98. add: function() {
  99. document.getElementById('btnRSAddCountry').click();
  100. }
  101. },
  102. state: {
  103. op: function(selText) {
  104. document.getElementById('opRSState').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSState').options, selText);
  105. },
  106. val: function(val) {
  107. document.getElementById('inRSState').value = val;
  108. },
  109. add: function() {
  110. document.getElementById('btnRSAddState').click();
  111. }
  112. },
  113. city: {
  114. op: function(selText) {
  115. document.getElementById('opRSCity').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSCity').options, selText);
  116. },
  117. val: function(val) {
  118. document.getElementById('inRSCity').value = val;
  119. },
  120. condmod: function(val) {
  121. document.getElementById('selRSAltCity').value = val;
  122. },
  123. add: function() {
  124. document.getElementById('btnRSAddCity').click();
  125. }
  126. },
  127. street: {
  128. op: function(selText) {
  129. document.getElementById('opRSStreet').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSStreet').options, selText);
  130. },
  131. val: function(val) {
  132. document.getElementById('inRSStreet').value = val;
  133. },
  134. condmod: function(val) {
  135. document.getElementById('selRSAlttStreet').value = val;
  136. },
  137. add: function() {
  138. document.getElementById('btnRSAddStreet').click();
  139. }
  140. },
  141. unnamed: {
  142. op: function(checked) {
  143. document.getElementById('cbRSNoName').checked = checked;
  144. },
  145. //checked - has no name
  146. op2: function(checked) {
  147. document.getElementById('cbRSAltNoName').checked = checked;
  148. },
  149. //checked - alt name
  150. add: function() {
  151. document.getElementById('btnRSAddNoName').click();
  152. }
  153. },
  154. road: {
  155. op: function(selText) {
  156. document.getElementById('opRSRoadType').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSRoadType').options, selText);
  157. },
  158. val: function(selText) {
  159. document.getElementById('selRSRoadType').value = RSelExprParser._getSelectionIndex(document.getElementById('selRSRoadType').options, selText);
  160. },
  161. add: function() {
  162. document.getElementById('btnRSAddRoadType').click();
  163. }
  164. },
  165. direction: {
  166. op: function(selText) {
  167. document.getElementById('opRSDirection').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSDirection').options, selText);
  168. },
  169. val: function(selText) {
  170. document.getElementById('selRSDirection').value = RSelExprParser._getSelectionIndex(document.getElementById('selRSDirection').options, selText);
  171. },
  172. add: function() {
  173. document.getElementById('btnRSAddDirection').click();
  174. }
  175. },
  176. elevation: {
  177. op: function(selText) {
  178. document.getElementById('opRSElevation').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSElevation').options, selText);
  179. },
  180. val: function(selText) {
  181. document.getElementById('selRSElevation').value = RSelExprParser._getSelectionIndex(document.getElementById('selRSElevation').options, selText);
  182. },
  183. add: function() {
  184. document.getElementById('btnRSAddElevation').click();
  185. }
  186. },
  187. manlock: {
  188. op: function(selText) {
  189. document.getElementById('opRSManLock').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSManLock').options, selText);
  190. },
  191. val: function(val) {
  192. document.getElementById('selRSManLock').value = val;
  193. },
  194. add: function() {
  195. document.getElementById('btnRSAddManLock').click();
  196. }
  197. },
  198. traflock: {
  199. op: function(selText) {
  200. document.getElementById('opRSTrLock').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSTrLock').options, selText);
  201. },
  202. val: function(val) {
  203. document.getElementById('selRSTrLock').value = val;
  204. },
  205. add: function() {
  206. document.getElementById('btnRSAddTrLock').click();
  207. }
  208. },
  209. speed: {
  210. opOptNodes: function() { return document.getElementById('opRSSpeed').options },
  211. op: function(selText) {
  212. document.getElementById('opRSSpeed').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSSpeed').options, selText);
  213. },
  214. val: function(val) {
  215. document.getElementById('inRSSpeed').value = val;
  216. },
  217. add: function() {
  218. document.getElementById('btnRSAddSpeed').click();
  219. }
  220. },
  221. closure: {
  222. op: function(checked) {
  223. document.getElementById('cbRSClsr').checked = checked;
  224. },
  225. op2: function(selText) {
  226. document.getElementById('opRSClsrStrtEnd').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSClsrStrtEnd').options, selText);
  227. },
  228. val: function(val) {
  229. document.getElementById('inRSClsrDays').value = val;
  230. },
  231. condmod: function(selText) {
  232. document.getElementById('opRSClsrBeforeAter').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSClsrBeforeAter').options, selText);
  233. },
  234. add: function() {
  235. document.getElementById('btnRSAddClsr').click();
  236. }
  237. },
  238. updatedby: {
  239. op: function(selText) {
  240. document.getElementById('opRSUpdtd').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSUpdtd').options, selText);
  241. },
  242. val: function(val) {
  243. document.getElementById('inRSUpdtd').value = val;
  244. },
  245. add: function() {
  246. document.getElementById('btnRSAddUpdtd').click();
  247. }
  248. },
  249. createdby: {
  250. op: function(selText) {
  251. document.getElementById('opRSCrtd').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSCrtd').options, selText);
  252. },
  253. val: function(val) {
  254. document.getElementById('inRSCrtd').value = val;
  255. },
  256. add: function() {
  257. document.getElementById('btnRSAddCrtd').click();
  258. }
  259. },
  260. last: {
  261. op: function(selText) {
  262. document.getElementById('opRSLastU').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSLastU').options, selText);
  263. },
  264. val: function(val) {
  265. document.getElementById('inRSLastU').value = val;
  266. },
  267. add: function() {
  268. document.getElementById('btnRSAddLastU').click();
  269. }
  270. },
  271. length: {
  272. op: function(selText) {
  273. document.getElementById('opRSLength').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSLength').options, selText);
  274. },
  275. val: function(val) {
  276. document.getElementById('inRSLength').value = val;
  277. },
  278. condmod: function(selText) {
  279. document.getElementById('unitRSLength').value = RSelExprParser._getSelectionIndex(document.getElementById('unitRSLength').options, selText);
  280. },
  281. add: function() {
  282. document.getElementById('btnRSAddLength').click();
  283. }
  284. },
  285. id: {
  286. op: function(selText) {
  287. document.getElementById('opRSSegId').value = RSelExprParser._getSelectionIndex(document.getElementById('opRSSegId').options, selText);
  288. },
  289. val: function(val) {
  290. document.getElementById('inRSSegId').value = val;
  291. },
  292. add: function() {
  293. document.getElementById('btnRSAddSegId').click();
  294. }
  295. },
  296. roundabout: {
  297. op: function(checked) {
  298. document.getElementById('cbRSIsRound').checked = checked;
  299. },
  300. add: function() {
  301. document.getElementById('btnRSAddIsRound').click();
  302. }
  303. },
  304. toll: {
  305. op: function(checked) {
  306. document.getElementById('cbRSIsToll').checked = checked;
  307. },
  308. add: function() {
  309. document.getElementById('btnRSAddIsToll').click();
  310. }
  311. },
  312. tunnel: {
  313. op: function(checked) {
  314. document.getElementById('cbRSTunnel').checked = checked;
  315. },
  316. add: function() {
  317. document.getElementById('btnRSAddTunnel').click();
  318. }
  319. },
  320. new: {
  321. op: function(checked) {
  322. document.getElementById('cbRSIsNew').checked = checked;
  323. },
  324. add: function() {
  325. document.getElementById('btnRSAddIsNew').click();
  326. }
  327. },
  328. changed: {
  329. op: function(checked) {
  330. document.getElementById('cbRSIsChngd').checked = checked;
  331. },
  332. add: function() {
  333. document.getElementById('btnRSAddIsChngd').click();
  334. }
  335. },
  336. screen: {
  337. op: function(checked) {
  338. document.getElementById('cbRSOnScr').checked = checked;
  339. },
  340. add: function() {
  341. document.getElementById('btnRSAddOnScr').click();
  342. }
  343. },
  344. restriction: {
  345. op: function(checked) {
  346. document.getElementById('cbRSRestr').checked = checked;
  347. },
  348. add: function() {
  349. document.getElementById('btnRSAddRestr').click();
  350. }
  351. },
  352. editable: {
  353. op: function(checked) {
  354. document.getElementById('cbRSEdtbl').checked = checked;
  355. },
  356. add: function() {
  357. document.getElementById('btnRSAddEdtbl').click();
  358. }
  359. }
  360. },
  361. addExpr: function(eb) {
  362. var checkKeys = false;
  363. Object.keys(this.rselConditions).map(function(a, i) {
  364. if (a === eb.cond)
  365. checkKeys = true;
  366. });
  367. if (checkKeys) {
  368. try {
  369. this.rselConditions[eb.cond].op(eb.op);
  370. if (eb.op2 !== null )
  371. this.rselConditions[eb.cond].op2(eb.op2);
  372. if (eb.condmod !== null )
  373. this.rselConditions[eb.cond].condmod(eb.condmod);
  374.  
  375. if (eb.val2 === null ) {
  376. if (eb.val !== null )
  377. this.rselConditions[eb.cond].val(eb.val);
  378. this.rselConditions[eb.cond].add();
  379. } else {
  380. this.rselButtons.lfParens();
  381. this.rselConditions[eb.cond].val(eb.val);
  382. this.rselConditions[eb.cond].add();
  383. this.rselButtons.or();
  384. this.rselConditions[eb.cond].val(eb.val2);
  385. this.rselConditions[eb.cond].add();
  386. this.rselButtons.rtParens();
  387. }
  388.  
  389. } catch (err) {
  390. return {
  391. errorCode: 101,
  392. errorMsg: 'Error: Unable to parse expression text.',
  393. err: err
  394. };
  395. }
  396. } else {
  397. return {
  398. errorCode: 3,
  399. errorMsg: 'Selection condition was not recognized'
  400. };
  401. //
  402. }
  403. return {
  404. errorCode: 0
  405. };
  406. },
  407. //=============================================================================
  408. parseExpr: function(parseThis) {
  409. //---------------------------------------------------------------
  410. parseThis = parseThis.replace(/\bpri?m?(?:ary|\.)?\s?(?:or)\s?alt(?:ern|s)?(?:\.)?/ig, 'any');
  411. parseThis = parseThis.replace(/\b((?:un)?name[ds]?)\b|\b(road) type\b|\b(last) update\b|\b(speed) limits?\b/ig, '$1$2$3$4')
  412. parseThis = parseThis.replace(/\b(man)ual (lock)s?\b|\b(traf)[fic]* (lock)s?\b/ig, '$1$2$3$4');
  413. parseThis = parseThis.replace(/\b(created|updated)\s(by)\b/ig, '$1$2');
  414. parseThis = parseThis.replace(/\bon screen/ig, 'onscreen');
  415. //\b(?:in|on|off|out|outside)(?: of)?[- ]?screen\b
  416. parseThis = parseThis.replace(/\b(?:off|out)(?: of)?[- ]?screen/ig, 'offscreen');
  417.  
  418. var parseExprArray = parseThis.match(
  419. /(\(['"].*?['"]\)|".*?"|'.*?')|\bno[\s-]alt|\b(?:street[\s-]?)?name\(s\)|\bstreet(?:\snames?)\b|\btoll(?:[-\s]?ro?a?d)?\b|\bdoes(?:\s?n[o']t)\b|(?:!\s?)?contains?\b|!=|>=|<=|[ab][<->]{2}[ab]|\w+(\(s\))?|&&|\|\||!=|[|&<>=()!~]/gi
  420. ),
  421. parseExprHistory = [],
  422. condMatches = [],
  423. condMatchPhrases = [],
  424. exprMatches = [],
  425. exprMatchPhrases = [],
  426. exprFragment, unwantedWordsSearch,
  427. e, f, b, fLength;
  428.  
  429. // The following parses the expression text into unique chunks within separate array elements
  430. e = parseExprArray.length;
  431. while (e-- > 0) {
  432. try {
  433. exprFragment = parseExprArray.shift();
  434. //console.info(exprFragment);
  435.  
  436. // Find operators that join individual expressions (AND|OR|!|parenthesis)
  437. if (/^(?:and|or|&&|\|\||!=|[=&|()!])$/i.test(exprFragment)) {
  438. exprMatches.push(exprFragment.toLowerCase());
  439. exprMatchPhrases.push(exprFragment.toLowerCase());
  440. }
  441.  
  442. // Identify elements that contain selection condition names
  443. if (
  444. /^country|^state|^city|^street|^(?:un|street[\s-]?)?name|^road|^round|^toll|^speed|^dir|^elevation|^tun|^manlock|^traflock|^speed|^new|^changed|screen$|^restrict|^clos|^createdby|^last|^updatedby|^length|^id|^editable/i
  445. .test(exprFragment)) {
  446. condMatches.push(exprFragment.toLowerCase());
  447. // lists specific selection conditions
  448. exprMatches.push(exprFragment.toLowerCase());
  449. //same as condMatches, but includes operations as separate array elements
  450.  
  451. try {
  452. //search phrase fowards
  453. fLength = parseExprArray.length;
  454. f = 0;
  455. while (!(/^(and|or|&&|\|\||[&|)])$/i.test(parseExprArray[f])) && (++f < fLength)) {}
  456. //search phrase backwards
  457. b = parseExprHistory.length;
  458. while (!(/^(and|or|&&|\|\||[&|(])$/i.test(parseExprHistory[b - 1])) && (--b > 0)) {}
  459.  
  460. condMatchPhrases.push(parseExprHistory.slice(b).concat(exprFragment, parseExprArray.slice(0, f)));
  461. //list specific selection conditions and its criteria
  462.  
  463. unwantedWordsSearch = parseExprHistory.slice(b);
  464. if (unwantedWordsSearch && unwantedWordsSearch.length) {
  465. unwantedWordsSearch = unwantedWordsSearch.filter(function(a) {
  466. return !/\b(has|have|is|=|are|does|was|were)\b/i.test(a)
  467. });
  468. }
  469. if (/!|!=/.test(unwantedWordsSearch[0]))
  470. unwantedWordsSearch.splice(0, 1);
  471.  
  472. exprMatchPhrases.push(unwantedWordsSearch.concat(parseExprArray.slice(0, f)));
  473. //excludes the match cond
  474.  
  475. parseExprHistory = parseExprHistory.concat(exprFragment, parseExprArray.slice(0, f));
  476. parseExprArray = parseExprArray.slice(f);
  477. e -= f;
  478. } catch (err) {
  479. return {
  480. errorCode: 101,
  481. errorMsg: 'Error parsing expression at ' + exprFragment,
  482. err: err
  483. };
  484. }
  485. } else {
  486. parseExprHistory.push(exprFragment);
  487. }
  488. } catch (err) {
  489. return {
  490. errorCode: 101,
  491. errdebug: 'Error parsing expression at ' + exprFragment,
  492. err: err
  493. };
  494. }
  495. }
  496. //while
  497.  
  498.  
  499. //---------------------------------------------------------------
  500. // Quick crude check for unmatched parentheses
  501. var nOpenParens = exprMatches.toString().match(/\(/g),
  502. nCloseParens = exprMatches.toString().match(/\)/g);
  503. if (!nOpenParens) nOpenParens = [];
  504. if (!nCloseParens) nCloseParens = [];
  505. if (nOpenParens.length !== nCloseParens.length)
  506. return {
  507. errorCode: 1,
  508. errorMsg: 'Warning: Open and close paretheses may be unmatched.'
  509. };
  510.  
  511. //---------------------------------------------------------------
  512.  
  513. return {
  514. errorCode: 0,
  515. exprMatches: exprMatches,
  516. exprMatchPhrases: exprMatchPhrases,
  517. condMatches: condMatches,
  518. condMatchPhrases: condMatchPhrases
  519. };
  520. },
  521. buildExpr: function(exprWord, exprPhrase) {
  522.  
  523. var exprBuild = RSelExprParser._getNewExprBuild();
  524. exprBuild.cond = exprWord;
  525.  
  526. //if (m===10) debugger;
  527.  
  528. //============================================================
  529. // Where the magic happens... sort of.
  530. //============================================================
  531. switch (true) {
  532. case exprWord === '(':
  533. this.rselButtons.lfParens();
  534. return false;
  535. case exprWord === ')':
  536. this.rselButtons.rtParens();
  537. return false;
  538. case 'and' === exprWord:
  539. this.rselButtons.and();
  540. return false;
  541. case 'or' === exprWord:
  542. this.rselButtons.or();
  543. return false;
  544. case /no alt/i.test(exprPhrase):
  545. exprBuild.cond = 'unnamed';
  546. exprBuild.op = true;
  547. exprBuild.op2 = true;
  548. return exprBuild;
  549. case '!' === exprWord:
  550. this.rselButtons.not();
  551. return false;
  552. case /^unnamed/.test(exprBuild.cond):
  553. exprBuild.cond = 'unnamed';
  554. exprBuild.op = true;
  555. exprBuild.op2 = false;
  556. return exprBuild;
  557.  
  558. // SPEED LIMITS
  559. case 'speed' === exprBuild.cond:
  560. try {
  561. if (exprPhrase.length < 2 && /\bnot?\b|!|!=/i.test(exprPhrase[0])) {
  562. exprBuild.op = 'none';
  563. } else {
  564. exprPhrase = exprPhrase.join(' ');
  565.  
  566. if (/\bnot?\b|!|!=/i.test(exprPhrase)) {
  567. RSelExprParser.rselButtons.not();
  568. }
  569.  
  570. var optionText = RSelExprParser._getSelectOptions(RSelExprParser.rselConditions.speed.opOptNodes());
  571. optionText = new RegExp(optionText.join('|'), 'i')
  572. optionText = optionText.exec(exprPhrase);
  573.  
  574. if (optionText) {
  575. exprBuild.op = optionText+'';
  576. } else {
  577. exprBuild.op = 'any';
  578. }
  579. }
  580.  
  581. if (exprPhrase) {
  582. var speedVal = exprPhrase.match(/(\d+)\s?mph|(\d+)\s?km/i);
  583. if (speedVal && speedVal.length === 2) {
  584. exprBuild.val = speedVal[1];
  585. }
  586. }
  587. } catch (err) {
  588. exprBuild.errorCode = 101;
  589. exprBuild.err = err;
  590. return exprBuild;
  591. }
  592. return exprBuild;
  593.  
  594. // BINARY CONDITIONS:
  595. case exprPhrase.length === 0 || //suggests binary
  596. /^(screen|roundabout|toll|tun|new|changed|restrict|editable)/.test(exprBuild.cond) || //binary selection conditions
  597. (/^name.*|^closure/i.test(exprBuild.cond) && exprPhrase.length <= 1):
  598. //selection conditions that have both binary and multiple options
  599.  
  600. exprPhrase = exprPhrase.join(' ');
  601.  
  602. exprBuild.cond = exprBuild.cond.replace(/^name.*/, 'name');
  603. exprBuild.cond = exprBuild.cond.replace(/^toll\s.*/, 'toll');
  604.  
  605. if (/\bnot?\b|!|!=/i.test(exprPhrase)) {
  606. exprBuild.op = false;
  607. } else {
  608. exprBuild.op = true;
  609. }
  610. switch (exprBuild.cond) {
  611. case 'name':
  612. try {
  613. if (/alt/i.test(exprPhrase)) {
  614. exprBuild.cond = 'unnamed';
  615. exprBuild.op = false;
  616. exprBuild.op2 = true;
  617. } else {
  618. exprBuild.cond = 'unnamed';
  619. exprBuild.op = false;
  620. exprBuild.op2 = false;
  621. }
  622. return exprBuild;
  623. } catch (err) {
  624. exprBuild.errorCode = 101;
  625. exprBuild.err = err;
  626. return exprBuild;
  627. }
  628. case 'closure':
  629. exprBuild.op2 = '---';
  630. return exprBuild;
  631. case 'onscreen':
  632. exprBuild.cond = 'screen';
  633. exprBuild.op = true;
  634. return exprBuild;
  635. case 'offscreen':
  636. exprBuild.cond = 'screen';
  637. exprBuild.op = false;
  638. return exprBuild;
  639. case 'roundabout':
  640. case 'toll':
  641. case 'tunnel':
  642. case 'new':
  643. case 'changed':
  644. case 'restriction':
  645. case 'editable':
  646. return exprBuild;
  647. default:
  648. exprBuild.errorCode = 101;
  649. exprBuild.errorMsg = 'Error: Presumed binary selector had no match.';
  650. return exprBuild;
  651. }
  652. //switch
  653.  
  654. //--------------------------------------------------------------------
  655.  
  656. case /^closure/.test(exprBuild.cond):
  657. try {
  658. exprPhrase = exprPhrase.join().toLowerCase();
  659. exprBuild.op = !(/does\s?n['o]t|!|!=/.test(exprPhrase));
  660. //checkbox
  661. exprBuild.op2 = /start|end/.exec(exprPhrase) + 's';
  662. //starts/ends
  663. exprBuild.condmod = /before|after|\bin\b/.exec(exprPhrase) + '';
  664. //in/before/after
  665. if (!exprBuild.condmod)
  666. exprBuild.condmod = 'in';
  667. exprBuild.val = /\d+/.exec(exprPhrase) + '';
  668. //days ago
  669. } catch (err) {
  670. exprBuild.errorCode = 101;
  671. exprBuild.err = err;
  672. return exprBuild;
  673. }
  674. return exprBuild;
  675.  
  676. default:
  677. // CONDITION NAME MATCHING (TYPE OF SELECTION)
  678. try {
  679. if (/^(str.*|cit.*)/.test(exprBuild.cond)) {
  680. exprBuild.cond = exprBuild.cond.replace(/^str.*/, 'street');
  681. exprBuild.cond = exprBuild.cond.replace(/^cit.*/, 'city');
  682. var exprStart = exprPhrase.slice(0, -1), //don't include last element bc it should be the name itself
  683. prim, alt;
  684. if (exprStart) {
  685. //exprStart = exprStart.toString().toLowerCase();
  686. prim = /\bprim?(?:ary|\.)?\b/i.test(exprStart);
  687. alt = /\balt(?:ern\w*|\.)?\b/i.test(exprStart);
  688. exprPhrase = exprStart.filter(function(a) {
  689. return !/^pri|^alt/i.test(a)
  690. }).concat(exprPhrase.slice(-1));
  691. } else {
  692. prim = false;
  693. alt = false;
  694. }
  695. if (prim && alt)
  696. exprBuild.condmod = 2;
  697. else if (prim)
  698. exprBuild.condmod = 0;
  699. else if (alt)
  700. exprBuild.condmod = 1;
  701. else
  702. exprBuild.condmod = 0;
  703. }
  704. } catch (err) {
  705. exprBuild.errorCode = 101;
  706. exprBuild.err = err;
  707. return exprBuild;
  708. }
  709.  
  710. // COMPARATOR OPERATION MATCHING
  711. try {
  712. // Convert natural lang representation to standard comparator operations
  713. var exprPhraseStr = exprPhrase.join(' ').replace(/\bcontains?/i, 'contains').replace(/(?:\bdo(?:es)?\s?n[o']t\s|!\s?)(contains)/i, '! $1');
  714. //.replace(/\b(?:do(?:es)?\s?n[o']t\s|!\s?)contains?/i, '!^').replace(/\bcontains?/i,'\u220b');
  715.  
  716. // Comparator operations with standard representation
  717. exprBuild.op = /(?:! )?contains|[!<>=~]{1,2}/i.exec(exprPhraseStr) + '';
  718.  
  719. } catch (err) {
  720. exprBuild.errorCode = 101;
  721. exprBuild.err = err;
  722. return exprBuild;
  723. }
  724.  
  725. // SELECTION VALUE MATCHING
  726. try {
  727. if (/^length|^last/.test(exprBuild.cond)) {
  728. exprBuild.val = exprPhraseStr.match(/\b\d+/) + ''
  729. } else {
  730. try {
  731. // The following line is kind of elaborate bc it needed to grab text between parens/quotes while keeping the inner quotes
  732. exprBuild.val = exprPhraseStr.replace(new RegExp('^(?:\\s?' + exprBuild.op + '\\s)(.*)','i'), '$1').replace(/^\(["'](.*?)['"]\)$|^\s?["'](.*)["']$|^["'](.*)['"]$|\b(\w*?)\b/, '$1$2$3$4').replace(/(") (\w) (")/, '$1$2$3');
  733.  
  734. } catch (err) {
  735. exprBuild.errorCode = 2;
  736. exprBuild.err = err;
  737. return exprBuild;
  738. }
  739.  
  740. if (/^direction/.test(exprBuild.cond)) {
  741. exprBuild.val = exprBuild.val.match(/A[<>-\s]*B|B[<>-\s]*A|unknown/i) + '';
  742. //reduce to unique key words...
  743. }
  744. }
  745.  
  746. return exprBuild;
  747.  
  748. } catch (err) {
  749. exprBuild.errorCode = 101;
  750. exprBuild.err = err;
  751. return exprBuild;
  752. }
  753. }
  754. //switch
  755. },
  756. //parseExpr()
  757. updateExpression: function(parseThis) {
  758. this.rselButtons.clear();
  759. if (parseThis) {
  760. //console.info('*** Begin parsing expression... ***');
  761.  
  762.  
  763. var parsed = this.parseExpr(parseThis);
  764.  
  765. if (parsed && !parsed.errorCode) {
  766. var exprMatches = parsed.exprMatches,
  767. exprMatchPhrases = parsed.exprMatchPhrases,
  768. exprFragment, exprFragPhrase, mLength, m, __EXPR_DEBUGINFO;
  769.  
  770. mLength = exprMatchPhrases.length;
  771. for (m = 0; m < mLength; m++) {
  772. __EXPR_DEBUGINFO = this.new__EXPR_DEBUGINFO(m, exprMatches[m], exprMatchPhrases[m]);
  773.  
  774. //if (m > 3) debugger;
  775.  
  776. exprFragment = exprMatches[m];
  777. exprFragPhrase = exprMatchPhrases[m];
  778.  
  779. if (exprFragPhrase.constructor !== Array) exprFragPhrase = [exprFragPhrase];
  780.  
  781. var exprBuild = this.buildExpr(exprFragment, exprFragPhrase);
  782.  
  783. if (exprBuild && !exprBuild.errorCode) {
  784. __EXPR_DEBUGINFO.errorStatus = this.addExpr(exprBuild);
  785.  
  786. if (__EXPR_DEBUGINFO.errorStatus && __EXPR_DEBUGINFO.errorStatus.errorCode) {
  787. console.warn('updateExpression() may have partly failed. Check results.');
  788. __EXPR_DEBUGINFO.exprBuild = exprBuild;
  789. console.debug(__EXPR_DEBUGINFO);
  790. return false;
  791. }
  792. } else if (exprBuild && exprBuild.errorCode) {
  793. console.warn('updateExpression() may have partly failed. Check results.');
  794. __EXPR_DEBUGINFO.exprBuild = exprBuild;
  795. console.debug(__EXPR_DEBUGINFO);
  796. return false;
  797. }
  798. } //for each condition matched
  799.  
  800. return this.getCurrentExprText();
  801. } else {
  802. console.debug(parsed);
  803. return false;
  804. }
  805. } else {
  806. return null;
  807. }
  808. }
  809. };