Greasy Fork is available in English.

rsel-exprparser-basic

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

Verze ze dne 06. 03. 2016. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/17641/111857/rsel-exprparser-basic.js

  1. // ==UserScript==
  2. // @name rsel-exprparser-basic
  3. // @namespace https://greatest.deepsurf.us/users/11629-TheLastTaterTot
  4. // @version 0.3.1
  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. new__EXPR_DEBUGINFO: function(m, exprWord, exprPhrase) {
  18. return {
  19. m: m,
  20. exprMatches: exprWord,
  21. exprMatchPhrases: exprPhrase,
  22. exprBuild: {},
  23. err: null ,
  24. errorMsg: null
  25. };
  26. },
  27. //reset for error debugging
  28. getSelectionIndex: function(selector, selText) {
  29. return selector.map(function(i) {
  30. if (new RegExp(selText,'i').test(this.innerText))
  31. return this.value
  32. }).get(0);
  33. },
  34. getSelectOptions: function(selector) {
  35. var opts = [];
  36. selector.map(function(i, a) {
  37. opts.push(a.innerText.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. rselBtns: {
  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. rselCond: {
  91. country: {
  92. op: function(selText) {
  93. document.getElementById('opRSCountry').value = RSelExprParser.getSelectionIndex($('#opRSCountry option'), selText);
  94. },
  95. val: function(selText) {
  96. document.getElementById('selRSCountry').value = RSelExprParser.getSelectionIndex($('#selRSCountry option'), 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($('#opRSState option'), 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($('#opRSCity option'), 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($('#opRSStreet option'), 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($('#opRSRoadType option'), selText);
  157. },
  158. val: function(selText) {
  159. document.getElementById('selRSRoadType').value = RSelExprParser.getSelectionIndex($('#selRSRoadType option'), 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($('#opRSDirection option'), selText);
  168. },
  169. val: function(selText) {
  170. document.getElementById('selRSDirection').value = RSelExprParser.getSelectionIndex($('#selRSDirection option'), 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($('#opRSElevation option'), selText);
  179. },
  180. val: function(selText) {
  181. document.getElementById('selRSElevation').value = RSelExprParser.getSelectionIndex($('#selRSElevation option'), 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($('#opRSManLock option'), 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($('#opRSTrLock option'), 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: $('#opRSSpeed option'),
  211. op: function(selText) {
  212. document.getElementById('opRSSpeed').value = RSelExprParser.getSelectionIndex($('#opRSSpeed option'), 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($('#opRSClsrStrtEnd option'), selText);
  227. },
  228. val: function(val) {
  229. document.getElementById('inRSClsrDays').value = val;
  230. },
  231. condmod: function(selText) {
  232. document.getElementById('opRSClsrBeforeAter').value = RSelExprParser.getSelectionIndex($('#opRSClsrBeforeAter option'), 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($('#opRSUpdtd option'), 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($('#opRSCrtd option'), 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($('#opRSLastU option'), 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($('#opRSLength option'), selText);
  274. },
  275. val: function(val) {
  276. document.getElementById('inRSLength').value = val;
  277. },
  278. condmod: function(selText) {
  279. document.getElementById('unitRSLength').value = RSelExprParser.getSelectionIndex($('#unitRSLength option'), 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($('#opRSSegId option'), 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.rselCond).map(function(a, i) {
  364. if (a === eb.cond)
  365. checkKeys = true;
  366. });
  367. if (checkKeys) {
  368. try {
  369. this.rselCond[eb.cond].op(eb.op);
  370. if (eb.op2 !== null )
  371. this.rselCond[eb.cond].op2(eb.op2);
  372. if (eb.condmod !== null )
  373. this.rselCond[eb.cond].condmod(eb.condmod);
  374.  
  375. if (eb.val2 === null ) {
  376. if (eb.val !== null )
  377. this.rselCond[eb.cond].val(eb.val);
  378. this.rselCond[eb.cond].add();
  379. } else {
  380. this.rselBtns.lfParens();
  381. this.rselCond[eb.cond].val(eb.val);
  382. this.rselCond[eb.cond].add();
  383. this.rselBtns.or();
  384. this.rselCond[eb.cond].val(eb.val2);
  385. this.rselCond[eb.cond].add();
  386. this.rselBtns.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.rselBtns.lfParens();
  534. return false;
  535. case exprWord === ')':
  536. this.rselBtns.rtParens();
  537. return false;
  538. case 'and' === exprWord:
  539. this.rselBtns.and();
  540. return false;
  541. case 'or' === exprWord:
  542. this.rselBtns.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.rselBtns.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.rselBtns.not();
  568.  
  569. var optionText = RSelExprParser.getSelectOptions(RSelExprParser.rselCond.speed.opOptNodes);
  570. optionText = RegExp(optionText.join('|'), 'i').exec(exprPhrase);
  571. if (optionText)
  572. exprBuild.op = optionText[0];
  573. else
  574. exprBuild.op = 'any';
  575. }
  576.  
  577. var speedVal = exprPhrase.match(/(\d+)\s?mph|(\d+)\s?km/i);
  578. if (speedVal && speedVal.length === 2)
  579. exprBuild.val = speedVal[1];
  580. } catch (err) {
  581. exprBuild.errorCode = 101;
  582. exprBuild.err = err;
  583. return exprBuild;
  584. }
  585. return exprBuild;
  586.  
  587. // BINARY CONDITIONS:
  588. case exprPhrase.length === 0 || //suggests binary
  589. /^(screen|roundabout|toll|tun|new|changed|restrict|editable)/.test(exprBuild.cond) || //binary selection conditions
  590. (/^name.*|^closure/i.test(exprBuild.cond) && exprPhrase.length <= 1):
  591. //selection conditions that have both binary and multiple options
  592.  
  593. exprPhrase = exprPhrase.join(' ');
  594.  
  595. exprBuild.cond = exprBuild.cond.replace(/^name.*/, 'name');
  596. exprBuild.cond = exprBuild.cond.replace(/^toll\s.*/, 'toll');
  597.  
  598. if (/\bnot?\b|!|!=/i.test(exprPhrase)) {
  599. exprBuild.op = false;
  600. } else {
  601. exprBuild.op = true;
  602. }
  603. switch (exprBuild.cond) {
  604. case 'name':
  605. try {
  606. if (/alt/i.test(exprPhrase)) {
  607. exprBuild.cond = 'unnamed';
  608. exprBuild.op = false;
  609. exprBuild.op2 = true;
  610. } else {
  611. exprBuild.cond = 'unnamed';
  612. exprBuild.op = false;
  613. exprBuild.op2 = false;
  614. }
  615. return exprBuild;
  616. } catch (err) {
  617. exprBuild.errorCode = 101;
  618. exprBuild.err = err;
  619. return exprBuild;
  620. }
  621. case 'closure':
  622. exprBuild.op2 = '---';
  623. return exprBuild;
  624. case 'onscreen':
  625. exprBuild.cond = 'screen';
  626. exprBuild.op = true;
  627. return exprBuild;
  628. case 'offscreen':
  629. exprBuild.cond = 'screen';
  630. exprBuild.op = false;
  631. return exprBuild;
  632. case 'roundabout':
  633. case 'toll':
  634. case 'tunnel':
  635. case 'new':
  636. case 'changed':
  637. case 'restriction':
  638. case 'editable':
  639. return exprBuild;
  640. default:
  641. exprBuild.errorCode = 101;
  642. exprBuild.errorMsg = 'Error: Presumed binary selector had no match.';
  643. return exprBuild;
  644. }
  645. //switch
  646.  
  647. //--------------------------------------------------------------------
  648.  
  649. case /^closure/.test(exprBuild.cond):
  650. try {
  651. exprPhrase = exprPhrase.join().toLowerCase();
  652. exprBuild.op = !(/does\s?n['o]t|!|!=/.test(exprPhrase));
  653. //checkbox
  654. exprBuild.op2 = /start|end/.exec(exprPhrase) + 's';
  655. //starts/ends
  656. exprBuild.condmod = /before|after|\bin\b/.exec(exprPhrase) + '';
  657. //in/before/after
  658. if (!exprBuild.condmod)
  659. exprBuild.condmod = 'in';
  660. exprBuild.val = /\d+/.exec(exprPhrase) + '';
  661. //days ago
  662. } catch (err) {
  663. exprBuild.errorCode = 101;
  664. exprBuild.err = err;
  665. return exprBuild;
  666. }
  667. return exprBuild;
  668.  
  669. default:
  670. // CONDITION NAME MATCHING (TYPE OF SELECTION)
  671. try {
  672. if (/^(str.*|cit.*)/.test(exprBuild.cond)) {
  673. exprBuild.cond = exprBuild.cond.replace(/^str.*/, 'street');
  674. exprBuild.cond = exprBuild.cond.replace(/^cit.*/, 'city');
  675. var exprStart = exprPhrase.slice(0, -1), //don't include last element bc it should be the name itself
  676. prim, alt;
  677. if (exprStart) {
  678. //exprStart = exprStart.toString().toLowerCase();
  679. prim = /\bprim?(?:ary|\.)?\b/i.test(exprStart);
  680. alt = /\balt(?:ern\w*|\.)?\b/i.test(exprStart);
  681. exprPhrase = exprStart.filter(function(a) {
  682. return !/^pri|^alt/i.test(a)
  683. }).concat(exprPhrase.slice(-1));
  684. } else {
  685. prim = false;
  686. alt = false;
  687. }
  688. if (prim && alt)
  689. exprBuild.condmod = 2;
  690. else if (prim)
  691. exprBuild.condmod = 0;
  692. else if (alt)
  693. exprBuild.condmod = 1;
  694. else
  695. exprBuild.condmod = 0;
  696. }
  697. } catch (err) {
  698. exprBuild.errorCode = 101;
  699. exprBuild.err = err;
  700. return exprBuild;
  701. }
  702.  
  703. // COMPARATOR OPERATION MATCHING
  704. try {
  705. // Convert natural lang representation to standard comparator operations
  706. var exprPhraseStr = exprPhrase.join(' ').replace(/\bcontains?/i, 'contains').replace(/(?:\bdo(?:es)?\s?n[o']t\s|!\s?)(contains)/i, '! $1');
  707. //.replace(/\b(?:do(?:es)?\s?n[o']t\s|!\s?)contains?/i, '!^').replace(/\bcontains?/i,'\u220b');
  708.  
  709. // Comparator operations with standard representation
  710. exprBuild.op = /(?:! )?contains|[!<>=~]{1,2}/i.exec(exprPhraseStr) + '';
  711.  
  712. } catch (err) {
  713. exprBuild.errorCode = 101;
  714. exprBuild.err = err;
  715. return exprBuild;
  716. }
  717.  
  718. // SELECTION VALUE MATCHING
  719. try {
  720. if (/^length|^last/.test(exprBuild.cond)) {
  721. exprBuild.val = exprPhraseStr.match(/\b\d+/) + ''
  722. } else {
  723. try {
  724. // The following line is kind of elaborate bc it needed to grab text between parens/quotes while keeping the inner quotes
  725. 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');
  726.  
  727. } catch (err) {
  728. exprBuild.errorCode = 2;
  729. exprBuild.err = err;
  730. return exprBuild;
  731. }
  732.  
  733. if (/^direction/.test(exprBuild.cond)) {
  734. exprBuild.val = exprBuild.val.match(/A[<>-\s]*B|B[<>-\s]*A|unknown/i) + '';
  735. //reduce to unique key words...
  736. }
  737. }
  738.  
  739. return exprBuild;
  740.  
  741. } catch (err) {
  742. exprBuild.errorCode = 101;
  743. exprBuild.err = err;
  744. return exprBuild;
  745. }
  746. }
  747. //switch
  748. },
  749. //parseExpr()
  750. updateExpression: function(parseThis) {
  751. this.rselBtns.clear();
  752. if (parseThis) {
  753. console.info('*** Begin parsing expression... ***');
  754.  
  755.  
  756. var parsed = this.parseExpr(parseThis);
  757.  
  758. if (parsed && !parsed.errorCode) {
  759. var exprMatches = parsed.exprMatches,
  760. exprMatchPhrases = parsed.exprMatchPhrases,
  761. exprFragment, exprFragPhrase, mLength, m, __EXPR_DEBUGINFO;
  762.  
  763. mLength = exprMatchPhrases.length;
  764. for (m = 0; m < mLength; m++) {
  765. __EXPR_DEBUGINFO = this.new__EXPR_DEBUGINFO(m, exprMatches[m], exprMatchPhrases[m]);
  766.  
  767. //if (m > 3) debugger;
  768.  
  769. exprFragment = exprMatches[m];
  770. exprFragPhrase = exprMatchPhrases[m];
  771.  
  772. if (exprFragPhrase.constructor !== Array) exprFragPhrase = [exprFragPhrase];
  773.  
  774. var exprBuild = this.buildExpr(exprFragment, exprFragPhrase);
  775.  
  776. if (exprBuild && !exprBuild.errorCode) {
  777. __EXPR_DEBUGINFO.errorStatus = this.addExpr(exprBuild);
  778.  
  779. if (__EXPR_DEBUGINFO.errorStatus && __EXPR_DEBUGINFO.errorStatus.errorCode) {
  780. console.warn('updateExpression() may have partly failed. Check results.');
  781. __EXPR_DEBUGINFO.exprBuild = exprBuild;
  782. console.debug(__EXPR_DEBUGINFO);
  783. return false;
  784. }
  785. } else if (exprBuild && exprBuild.errorCode) {
  786. console.warn('updateExpression() may have partly failed. Check results.');
  787. __EXPR_DEBUGINFO.exprBuild = exprBuild;
  788. console.debug(__EXPR_DEBUGINFO);
  789. return false;
  790. }
  791. } //for each condition matched
  792.  
  793. return this.getCurrentExprText();
  794. } else {
  795. console.debug(parsed);
  796. return false;
  797. }
  798. } else {
  799. return null;
  800. }
  801. }
  802. };