Greasy Fork is available in English.

rsel-exprparser-basic

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

Fra 11.03.2016. Se den seneste versjonen.

Dette scriptet burde ikke installeres direkte. Det er et bibliotek for andre script å inkludere med det nye metadirektivet // @require https://update.greatest.deepsurf.us/scripts/17641/112721/rsel-exprparser-basic.js

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