jmespath.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. (function(exports) {
  2. "use strict";
  3. function isArray(obj) {
  4. if (obj !== null) {
  5. return Object.prototype.toString.call(obj) === "[object Array]";
  6. } else {
  7. return false;
  8. }
  9. }
  10. function isObject(obj) {
  11. if (obj !== null) {
  12. return Object.prototype.toString.call(obj) === "[object Object]";
  13. } else {
  14. return false;
  15. }
  16. }
  17. function strictDeepEqual(first, second) {
  18. // Check the scalar case first.
  19. if (first === second) {
  20. return true;
  21. }
  22. // Check if they are the same type.
  23. var firstType = Object.prototype.toString.call(first);
  24. if (firstType !== Object.prototype.toString.call(second)) {
  25. return false;
  26. }
  27. // We know that first and second have the same type so we can just check the
  28. // first type from now on.
  29. if (isArray(first) === true) {
  30. // Short circuit if they're not the same length;
  31. if (first.length !== second.length) {
  32. return false;
  33. }
  34. for (var i = 0; i < first.length; i++) {
  35. if (strictDeepEqual(first[i], second[i]) === false) {
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41. if (isObject(first) === true) {
  42. // An object is equal if it has the same key/value pairs.
  43. var keysSeen = {};
  44. for (var key in first) {
  45. if (hasOwnProperty.call(first, key)) {
  46. if (strictDeepEqual(first[key], second[key]) === false) {
  47. return false;
  48. }
  49. keysSeen[key] = true;
  50. }
  51. }
  52. // Now check that there aren't any keys in second that weren't
  53. // in first.
  54. for (var key2 in second) {
  55. if (hasOwnProperty.call(second, key2)) {
  56. if (keysSeen[key2] !== true) {
  57. return false;
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. function isFalse(obj) {
  66. // From the spec:
  67. // A false value corresponds to the following values:
  68. // Empty list
  69. // Empty object
  70. // Empty string
  71. // False boolean
  72. // null value
  73. // First check the scalar values.
  74. if (obj === "" || obj === false || obj === null) {
  75. return true;
  76. } else if (isArray(obj) && obj.length === 0) {
  77. // Check for an empty array.
  78. return true;
  79. } else if (isObject(obj)) {
  80. // Check for an empty object.
  81. for (var key in obj) {
  82. // If there are any keys, then
  83. // the object is not empty so the object
  84. // is not false.
  85. if (obj.hasOwnProperty(key)) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. function objValues(obj) {
  95. var keys = Object.keys(obj);
  96. var values = [];
  97. for (var i = 0; i < keys.length; i++) {
  98. values.push(obj[keys[i]]);
  99. }
  100. return values;
  101. }
  102. function merge(a, b) {
  103. var merged = {};
  104. for (var key in a) {
  105. merged[key] = a[key];
  106. }
  107. for (var key2 in b) {
  108. merged[key2] = b[key2];
  109. }
  110. return merged;
  111. }
  112. var trimLeft;
  113. if (typeof String.prototype.trimLeft === "function") {
  114. trimLeft = function(str) {
  115. return str.trimLeft();
  116. };
  117. } else {
  118. trimLeft = function(str) {
  119. return str.match(/^\s*(.*)/)[1];
  120. };
  121. }
  122. // Type constants used to define functions.
  123. var TYPE_NUMBER = 0;
  124. var TYPE_ANY = 1;
  125. var TYPE_STRING = 2;
  126. var TYPE_ARRAY = 3;
  127. var TYPE_OBJECT = 4;
  128. var TYPE_BOOLEAN = 5;
  129. var TYPE_EXPREF = 6;
  130. var TYPE_NULL = 7;
  131. var TYPE_ARRAY_NUMBER = 8;
  132. var TYPE_ARRAY_STRING = 9;
  133. var TYPE_NAME_TABLE = {
  134. 0: 'number',
  135. 1: 'any',
  136. 2: 'string',
  137. 3: 'array',
  138. 4: 'object',
  139. 5: 'boolean',
  140. 6: 'expression',
  141. 7: 'null',
  142. 8: 'Array<number>',
  143. 9: 'Array<string>'
  144. };
  145. var TOK_EOF = "EOF";
  146. var TOK_UNQUOTEDIDENTIFIER = "UnquotedIdentifier";
  147. var TOK_QUOTEDIDENTIFIER = "QuotedIdentifier";
  148. var TOK_RBRACKET = "Rbracket";
  149. var TOK_RPAREN = "Rparen";
  150. var TOK_COMMA = "Comma";
  151. var TOK_COLON = "Colon";
  152. var TOK_RBRACE = "Rbrace";
  153. var TOK_NUMBER = "Number";
  154. var TOK_CURRENT = "Current";
  155. var TOK_EXPREF = "Expref";
  156. var TOK_PIPE = "Pipe";
  157. var TOK_OR = "Or";
  158. var TOK_AND = "And";
  159. var TOK_EQ = "EQ";
  160. var TOK_GT = "GT";
  161. var TOK_LT = "LT";
  162. var TOK_GTE = "GTE";
  163. var TOK_LTE = "LTE";
  164. var TOK_NE = "NE";
  165. var TOK_FLATTEN = "Flatten";
  166. var TOK_STAR = "Star";
  167. var TOK_FILTER = "Filter";
  168. var TOK_DOT = "Dot";
  169. var TOK_NOT = "Not";
  170. var TOK_LBRACE = "Lbrace";
  171. var TOK_LBRACKET = "Lbracket";
  172. var TOK_LPAREN= "Lparen";
  173. var TOK_LITERAL= "Literal";
  174. // The "&", "[", "<", ">" tokens
  175. // are not in basicToken because
  176. // there are two token variants
  177. // ("&&", "[?", "<=", ">="). This is specially handled
  178. // below.
  179. var basicTokens = {
  180. ".": TOK_DOT,
  181. "*": TOK_STAR,
  182. ",": TOK_COMMA,
  183. ":": TOK_COLON,
  184. "{": TOK_LBRACE,
  185. "}": TOK_RBRACE,
  186. "]": TOK_RBRACKET,
  187. "(": TOK_LPAREN,
  188. ")": TOK_RPAREN,
  189. "@": TOK_CURRENT
  190. };
  191. var operatorStartToken = {
  192. "<": true,
  193. ">": true,
  194. "=": true,
  195. "!": true
  196. };
  197. var skipChars = {
  198. " ": true,
  199. "\t": true,
  200. "\n": true
  201. };
  202. function isAlpha(ch) {
  203. return (ch >= "a" && ch <= "z") ||
  204. (ch >= "A" && ch <= "Z") ||
  205. ch === "_";
  206. }
  207. function isNum(ch) {
  208. return (ch >= "0" && ch <= "9") ||
  209. ch === "-";
  210. }
  211. function isAlphaNum(ch) {
  212. return (ch >= "a" && ch <= "z") ||
  213. (ch >= "A" && ch <= "Z") ||
  214. (ch >= "0" && ch <= "9") ||
  215. ch === "_";
  216. }
  217. function Lexer() {
  218. }
  219. Lexer.prototype = {
  220. tokenize: function(stream) {
  221. var tokens = [];
  222. this._current = 0;
  223. var start;
  224. var identifier;
  225. var token;
  226. while (this._current < stream.length) {
  227. if (isAlpha(stream[this._current])) {
  228. start = this._current;
  229. identifier = this._consumeUnquotedIdentifier(stream);
  230. tokens.push({type: TOK_UNQUOTEDIDENTIFIER,
  231. value: identifier,
  232. start: start});
  233. } else if (basicTokens[stream[this._current]] !== undefined) {
  234. tokens.push({type: basicTokens[stream[this._current]],
  235. value: stream[this._current],
  236. start: this._current});
  237. this._current++;
  238. } else if (isNum(stream[this._current])) {
  239. token = this._consumeNumber(stream);
  240. tokens.push(token);
  241. } else if (stream[this._current] === "[") {
  242. // No need to increment this._current. This happens
  243. // in _consumeLBracket
  244. token = this._consumeLBracket(stream);
  245. tokens.push(token);
  246. } else if (stream[this._current] === "\"") {
  247. start = this._current;
  248. identifier = this._consumeQuotedIdentifier(stream);
  249. tokens.push({type: TOK_QUOTEDIDENTIFIER,
  250. value: identifier,
  251. start: start});
  252. } else if (stream[this._current] === "'") {
  253. start = this._current;
  254. identifier = this._consumeRawStringLiteral(stream);
  255. tokens.push({type: TOK_LITERAL,
  256. value: identifier,
  257. start: start});
  258. } else if (stream[this._current] === "`") {
  259. start = this._current;
  260. var literal = this._consumeLiteral(stream);
  261. tokens.push({type: TOK_LITERAL,
  262. value: literal,
  263. start: start});
  264. } else if (operatorStartToken[stream[this._current]] !== undefined) {
  265. tokens.push(this._consumeOperator(stream));
  266. } else if (skipChars[stream[this._current]] !== undefined) {
  267. // Ignore whitespace.
  268. this._current++;
  269. } else if (stream[this._current] === "&") {
  270. start = this._current;
  271. this._current++;
  272. if (stream[this._current] === "&") {
  273. this._current++;
  274. tokens.push({type: TOK_AND, value: "&&", start: start});
  275. } else {
  276. tokens.push({type: TOK_EXPREF, value: "&", start: start});
  277. }
  278. } else if (stream[this._current] === "|") {
  279. start = this._current;
  280. this._current++;
  281. if (stream[this._current] === "|") {
  282. this._current++;
  283. tokens.push({type: TOK_OR, value: "||", start: start});
  284. } else {
  285. tokens.push({type: TOK_PIPE, value: "|", start: start});
  286. }
  287. } else {
  288. var error = new Error("Unknown character:" + stream[this._current]);
  289. error.name = "LexerError";
  290. throw error;
  291. }
  292. }
  293. return tokens;
  294. },
  295. _consumeUnquotedIdentifier: function(stream) {
  296. var start = this._current;
  297. this._current++;
  298. while (this._current < stream.length && isAlphaNum(stream[this._current])) {
  299. this._current++;
  300. }
  301. return stream.slice(start, this._current);
  302. },
  303. _consumeQuotedIdentifier: function(stream) {
  304. var start = this._current;
  305. this._current++;
  306. var maxLength = stream.length;
  307. while (stream[this._current] !== "\"" && this._current < maxLength) {
  308. // You can escape a double quote and you can escape an escape.
  309. var current = this._current;
  310. if (stream[current] === "\\" && (stream[current + 1] === "\\" ||
  311. stream[current + 1] === "\"")) {
  312. current += 2;
  313. } else {
  314. current++;
  315. }
  316. this._current = current;
  317. }
  318. this._current++;
  319. return JSON.parse(stream.slice(start, this._current));
  320. },
  321. _consumeRawStringLiteral: function(stream) {
  322. var start = this._current;
  323. this._current++;
  324. var maxLength = stream.length;
  325. while (stream[this._current] !== "'" && this._current < maxLength) {
  326. // You can escape a single quote and you can escape an escape.
  327. var current = this._current;
  328. if (stream[current] === "\\" && (stream[current + 1] === "\\" ||
  329. stream[current + 1] === "'")) {
  330. current += 2;
  331. } else {
  332. current++;
  333. }
  334. this._current = current;
  335. }
  336. this._current++;
  337. var literal = stream.slice(start + 1, this._current - 1);
  338. return literal.replace("\\'", "'");
  339. },
  340. _consumeNumber: function(stream) {
  341. var start = this._current;
  342. this._current++;
  343. var maxLength = stream.length;
  344. while (isNum(stream[this._current]) && this._current < maxLength) {
  345. this._current++;
  346. }
  347. var value = parseInt(stream.slice(start, this._current));
  348. return {type: TOK_NUMBER, value: value, start: start};
  349. },
  350. _consumeLBracket: function(stream) {
  351. var start = this._current;
  352. this._current++;
  353. if (stream[this._current] === "?") {
  354. this._current++;
  355. return {type: TOK_FILTER, value: "[?", start: start};
  356. } else if (stream[this._current] === "]") {
  357. this._current++;
  358. return {type: TOK_FLATTEN, value: "[]", start: start};
  359. } else {
  360. return {type: TOK_LBRACKET, value: "[", start: start};
  361. }
  362. },
  363. _consumeOperator: function(stream) {
  364. var start = this._current;
  365. var startingChar = stream[start];
  366. this._current++;
  367. if (startingChar === "!") {
  368. if (stream[this._current] === "=") {
  369. this._current++;
  370. return {type: TOK_NE, value: "!=", start: start};
  371. } else {
  372. return {type: TOK_NOT, value: "!", start: start};
  373. }
  374. } else if (startingChar === "<") {
  375. if (stream[this._current] === "=") {
  376. this._current++;
  377. return {type: TOK_LTE, value: "<=", start: start};
  378. } else {
  379. return {type: TOK_LT, value: "<", start: start};
  380. }
  381. } else if (startingChar === ">") {
  382. if (stream[this._current] === "=") {
  383. this._current++;
  384. return {type: TOK_GTE, value: ">=", start: start};
  385. } else {
  386. return {type: TOK_GT, value: ">", start: start};
  387. }
  388. } else if (startingChar === "=") {
  389. if (stream[this._current] === "=") {
  390. this._current++;
  391. return {type: TOK_EQ, value: "==", start: start};
  392. }
  393. }
  394. },
  395. _consumeLiteral: function(stream) {
  396. this._current++;
  397. var start = this._current;
  398. var maxLength = stream.length;
  399. var literal;
  400. while(stream[this._current] !== "`" && this._current < maxLength) {
  401. // You can escape a literal char or you can escape the escape.
  402. var current = this._current;
  403. if (stream[current] === "\\" && (stream[current + 1] === "\\" ||
  404. stream[current + 1] === "`")) {
  405. current += 2;
  406. } else {
  407. current++;
  408. }
  409. this._current = current;
  410. }
  411. var literalString = trimLeft(stream.slice(start, this._current));
  412. literalString = literalString.replace("\\`", "`");
  413. if (this._looksLikeJSON(literalString)) {
  414. literal = JSON.parse(literalString);
  415. } else {
  416. // Try to JSON parse it as "<literal>"
  417. literal = JSON.parse("\"" + literalString + "\"");
  418. }
  419. // +1 gets us to the ending "`", +1 to move on to the next char.
  420. this._current++;
  421. return literal;
  422. },
  423. _looksLikeJSON: function(literalString) {
  424. var startingChars = "[{\"";
  425. var jsonLiterals = ["true", "false", "null"];
  426. var numberLooking = "-0123456789";
  427. if (literalString === "") {
  428. return false;
  429. } else if (startingChars.indexOf(literalString[0]) >= 0) {
  430. return true;
  431. } else if (jsonLiterals.indexOf(literalString) >= 0) {
  432. return true;
  433. } else if (numberLooking.indexOf(literalString[0]) >= 0) {
  434. try {
  435. JSON.parse(literalString);
  436. return true;
  437. } catch (ex) {
  438. return false;
  439. }
  440. } else {
  441. return false;
  442. }
  443. }
  444. };
  445. var bindingPower = {};
  446. bindingPower[TOK_EOF] = 0;
  447. bindingPower[TOK_UNQUOTEDIDENTIFIER] = 0;
  448. bindingPower[TOK_QUOTEDIDENTIFIER] = 0;
  449. bindingPower[TOK_RBRACKET] = 0;
  450. bindingPower[TOK_RPAREN] = 0;
  451. bindingPower[TOK_COMMA] = 0;
  452. bindingPower[TOK_RBRACE] = 0;
  453. bindingPower[TOK_NUMBER] = 0;
  454. bindingPower[TOK_CURRENT] = 0;
  455. bindingPower[TOK_EXPREF] = 0;
  456. bindingPower[TOK_PIPE] = 1;
  457. bindingPower[TOK_OR] = 2;
  458. bindingPower[TOK_AND] = 3;
  459. bindingPower[TOK_EQ] = 5;
  460. bindingPower[TOK_GT] = 5;
  461. bindingPower[TOK_LT] = 5;
  462. bindingPower[TOK_GTE] = 5;
  463. bindingPower[TOK_LTE] = 5;
  464. bindingPower[TOK_NE] = 5;
  465. bindingPower[TOK_FLATTEN] = 9;
  466. bindingPower[TOK_STAR] = 20;
  467. bindingPower[TOK_FILTER] = 21;
  468. bindingPower[TOK_DOT] = 40;
  469. bindingPower[TOK_NOT] = 45;
  470. bindingPower[TOK_LBRACE] = 50;
  471. bindingPower[TOK_LBRACKET] = 55;
  472. bindingPower[TOK_LPAREN] = 60;
  473. function Parser() {
  474. }
  475. Parser.prototype = {
  476. parse: function(expression) {
  477. this._loadTokens(expression);
  478. this.index = 0;
  479. var ast = this.expression(0);
  480. if (this._lookahead(0) !== TOK_EOF) {
  481. var t = this._lookaheadToken(0);
  482. var error = new Error(
  483. "Unexpected token type: " + t.type + ", value: " + t.value);
  484. error.name = "ParserError";
  485. throw error;
  486. }
  487. return ast;
  488. },
  489. _loadTokens: function(expression) {
  490. var lexer = new Lexer();
  491. var tokens = lexer.tokenize(expression);
  492. tokens.push({type: TOK_EOF, value: "", start: expression.length});
  493. this.tokens = tokens;
  494. },
  495. expression: function(rbp) {
  496. var leftToken = this._lookaheadToken(0);
  497. this._advance();
  498. var left = this.nud(leftToken);
  499. var currentToken = this._lookahead(0);
  500. while (rbp < bindingPower[currentToken]) {
  501. this._advance();
  502. left = this.led(currentToken, left);
  503. currentToken = this._lookahead(0);
  504. }
  505. return left;
  506. },
  507. _lookahead: function(number) {
  508. return this.tokens[this.index + number].type;
  509. },
  510. _lookaheadToken: function(number) {
  511. return this.tokens[this.index + number];
  512. },
  513. _advance: function() {
  514. this.index++;
  515. },
  516. nud: function(token) {
  517. var left;
  518. var right;
  519. var expression;
  520. switch (token.type) {
  521. case TOK_LITERAL:
  522. return {type: "Literal", value: token.value};
  523. case TOK_UNQUOTEDIDENTIFIER:
  524. return {type: "Field", name: token.value};
  525. case TOK_QUOTEDIDENTIFIER:
  526. var node = {type: "Field", name: token.value};
  527. if (this._lookahead(0) === TOK_LPAREN) {
  528. throw new Error("Quoted identifier not allowed for function names.");
  529. }
  530. return node;
  531. case TOK_NOT:
  532. right = this.expression(bindingPower.Not);
  533. return {type: "NotExpression", children: [right]};
  534. case TOK_STAR:
  535. left = {type: "Identity"};
  536. right = null;
  537. if (this._lookahead(0) === TOK_RBRACKET) {
  538. // This can happen in a multiselect,
  539. // [a, b, *]
  540. right = {type: "Identity"};
  541. } else {
  542. right = this._parseProjectionRHS(bindingPower.Star);
  543. }
  544. return {type: "ValueProjection", children: [left, right]};
  545. case TOK_FILTER:
  546. return this.led(token.type, {type: "Identity"});
  547. case TOK_LBRACE:
  548. return this._parseMultiselectHash();
  549. case TOK_FLATTEN:
  550. left = {type: TOK_FLATTEN, children: [{type: "Identity"}]};
  551. right = this._parseProjectionRHS(bindingPower.Flatten);
  552. return {type: "Projection", children: [left, right]};
  553. case TOK_LBRACKET:
  554. if (this._lookahead(0) === TOK_NUMBER || this._lookahead(0) === TOK_COLON) {
  555. right = this._parseIndexExpression();
  556. return this._projectIfSlice({type: "Identity"}, right);
  557. } else if (this._lookahead(0) === TOK_STAR &&
  558. this._lookahead(1) === TOK_RBRACKET) {
  559. this._advance();
  560. this._advance();
  561. right = this._parseProjectionRHS(bindingPower.Star);
  562. return {type: "Projection",
  563. children: [{type: "Identity"}, right]};
  564. }
  565. return this._parseMultiselectList();
  566. case TOK_CURRENT:
  567. return {type: TOK_CURRENT};
  568. case TOK_EXPREF:
  569. expression = this.expression(bindingPower.Expref);
  570. return {type: "ExpressionReference", children: [expression]};
  571. case TOK_LPAREN:
  572. var args = [];
  573. while (this._lookahead(0) !== TOK_RPAREN) {
  574. if (this._lookahead(0) === TOK_CURRENT) {
  575. expression = {type: TOK_CURRENT};
  576. this._advance();
  577. } else {
  578. expression = this.expression(0);
  579. }
  580. args.push(expression);
  581. }
  582. this._match(TOK_RPAREN);
  583. return args[0];
  584. default:
  585. this._errorToken(token);
  586. }
  587. },
  588. led: function(tokenName, left) {
  589. var right;
  590. switch(tokenName) {
  591. case TOK_DOT:
  592. var rbp = bindingPower.Dot;
  593. if (this._lookahead(0) !== TOK_STAR) {
  594. right = this._parseDotRHS(rbp);
  595. return {type: "Subexpression", children: [left, right]};
  596. }
  597. // Creating a projection.
  598. this._advance();
  599. right = this._parseProjectionRHS(rbp);
  600. return {type: "ValueProjection", children: [left, right]};
  601. case TOK_PIPE:
  602. right = this.expression(bindingPower.Pipe);
  603. return {type: TOK_PIPE, children: [left, right]};
  604. case TOK_OR:
  605. right = this.expression(bindingPower.Or);
  606. return {type: "OrExpression", children: [left, right]};
  607. case TOK_AND:
  608. right = this.expression(bindingPower.And);
  609. return {type: "AndExpression", children: [left, right]};
  610. case TOK_LPAREN:
  611. var name = left.name;
  612. var args = [];
  613. var expression, node;
  614. while (this._lookahead(0) !== TOK_RPAREN) {
  615. if (this._lookahead(0) === TOK_CURRENT) {
  616. expression = {type: TOK_CURRENT};
  617. this._advance();
  618. } else {
  619. expression = this.expression(0);
  620. }
  621. if (this._lookahead(0) === TOK_COMMA) {
  622. this._match(TOK_COMMA);
  623. }
  624. args.push(expression);
  625. }
  626. this._match(TOK_RPAREN);
  627. node = {type: "Function", name: name, children: args};
  628. return node;
  629. case TOK_FILTER:
  630. var condition = this.expression(0);
  631. this._match(TOK_RBRACKET);
  632. if (this._lookahead(0) === TOK_FLATTEN) {
  633. right = {type: "Identity"};
  634. } else {
  635. right = this._parseProjectionRHS(bindingPower.Filter);
  636. }
  637. return {type: "FilterProjection", children: [left, right, condition]};
  638. case TOK_FLATTEN:
  639. var leftNode = {type: TOK_FLATTEN, children: [left]};
  640. var rightNode = this._parseProjectionRHS(bindingPower.Flatten);
  641. return {type: "Projection", children: [leftNode, rightNode]};
  642. case TOK_EQ:
  643. case TOK_NE:
  644. case TOK_GT:
  645. case TOK_GTE:
  646. case TOK_LT:
  647. case TOK_LTE:
  648. return this._parseComparator(left, tokenName);
  649. case TOK_LBRACKET:
  650. var token = this._lookaheadToken(0);
  651. if (token.type === TOK_NUMBER || token.type === TOK_COLON) {
  652. right = this._parseIndexExpression();
  653. return this._projectIfSlice(left, right);
  654. }
  655. this._match(TOK_STAR);
  656. this._match(TOK_RBRACKET);
  657. right = this._parseProjectionRHS(bindingPower.Star);
  658. return {type: "Projection", children: [left, right]};
  659. default:
  660. this._errorToken(this._lookaheadToken(0));
  661. }
  662. },
  663. _match: function(tokenType) {
  664. if (this._lookahead(0) === tokenType) {
  665. this._advance();
  666. } else {
  667. var t = this._lookaheadToken(0);
  668. var error = new Error("Expected " + tokenType + ", got: " + t.type);
  669. error.name = "ParserError";
  670. throw error;
  671. }
  672. },
  673. _errorToken: function(token) {
  674. var error = new Error("Invalid token (" +
  675. token.type + "): \"" +
  676. token.value + "\"");
  677. error.name = "ParserError";
  678. throw error;
  679. },
  680. _parseIndexExpression: function() {
  681. if (this._lookahead(0) === TOK_COLON || this._lookahead(1) === TOK_COLON) {
  682. return this._parseSliceExpression();
  683. } else {
  684. var node = {
  685. type: "Index",
  686. value: this._lookaheadToken(0).value};
  687. this._advance();
  688. this._match(TOK_RBRACKET);
  689. return node;
  690. }
  691. },
  692. _projectIfSlice: function(left, right) {
  693. var indexExpr = {type: "IndexExpression", children: [left, right]};
  694. if (right.type === "Slice") {
  695. return {
  696. type: "Projection",
  697. children: [indexExpr, this._parseProjectionRHS(bindingPower.Star)]
  698. };
  699. } else {
  700. return indexExpr;
  701. }
  702. },
  703. _parseSliceExpression: function() {
  704. // [start:end:step] where each part is optional, as well as the last
  705. // colon.
  706. var parts = [null, null, null];
  707. var index = 0;
  708. var currentToken = this._lookahead(0);
  709. while (currentToken !== TOK_RBRACKET && index < 3) {
  710. if (currentToken === TOK_COLON) {
  711. index++;
  712. this._advance();
  713. } else if (currentToken === TOK_NUMBER) {
  714. parts[index] = this._lookaheadToken(0).value;
  715. this._advance();
  716. } else {
  717. var t = this._lookahead(0);
  718. var error = new Error("Syntax error, unexpected token: " +
  719. t.value + "(" + t.type + ")");
  720. error.name = "Parsererror";
  721. throw error;
  722. }
  723. currentToken = this._lookahead(0);
  724. }
  725. this._match(TOK_RBRACKET);
  726. return {
  727. type: "Slice",
  728. children: parts
  729. };
  730. },
  731. _parseComparator: function(left, comparator) {
  732. var right = this.expression(bindingPower[comparator]);
  733. return {type: "Comparator", name: comparator, children: [left, right]};
  734. },
  735. _parseDotRHS: function(rbp) {
  736. var lookahead = this._lookahead(0);
  737. var exprTokens = [TOK_UNQUOTEDIDENTIFIER, TOK_QUOTEDIDENTIFIER, TOK_STAR];
  738. if (exprTokens.indexOf(lookahead) >= 0) {
  739. return this.expression(rbp);
  740. } else if (lookahead === TOK_LBRACKET) {
  741. this._match(TOK_LBRACKET);
  742. return this._parseMultiselectList();
  743. } else if (lookahead === TOK_LBRACE) {
  744. this._match(TOK_LBRACE);
  745. return this._parseMultiselectHash();
  746. }
  747. },
  748. _parseProjectionRHS: function(rbp) {
  749. var right;
  750. if (bindingPower[this._lookahead(0)] < 10) {
  751. right = {type: "Identity"};
  752. } else if (this._lookahead(0) === TOK_LBRACKET) {
  753. right = this.expression(rbp);
  754. } else if (this._lookahead(0) === TOK_FILTER) {
  755. right = this.expression(rbp);
  756. } else if (this._lookahead(0) === TOK_DOT) {
  757. this._match(TOK_DOT);
  758. right = this._parseDotRHS(rbp);
  759. } else {
  760. var t = this._lookaheadToken(0);
  761. var error = new Error("Sytanx error, unexpected token: " +
  762. t.value + "(" + t.type + ")");
  763. error.name = "ParserError";
  764. throw error;
  765. }
  766. return right;
  767. },
  768. _parseMultiselectList: function() {
  769. var expressions = [];
  770. while (this._lookahead(0) !== TOK_RBRACKET) {
  771. var expression = this.expression(0);
  772. expressions.push(expression);
  773. if (this._lookahead(0) === TOK_COMMA) {
  774. this._match(TOK_COMMA);
  775. if (this._lookahead(0) === TOK_RBRACKET) {
  776. throw new Error("Unexpected token Rbracket");
  777. }
  778. }
  779. }
  780. this._match(TOK_RBRACKET);
  781. return {type: "MultiSelectList", children: expressions};
  782. },
  783. _parseMultiselectHash: function() {
  784. var pairs = [];
  785. var identifierTypes = [TOK_UNQUOTEDIDENTIFIER, TOK_QUOTEDIDENTIFIER];
  786. var keyToken, keyName, value, node;
  787. for (;;) {
  788. keyToken = this._lookaheadToken(0);
  789. if (identifierTypes.indexOf(keyToken.type) < 0) {
  790. throw new Error("Expecting an identifier token, got: " +
  791. keyToken.type);
  792. }
  793. keyName = keyToken.value;
  794. this._advance();
  795. this._match(TOK_COLON);
  796. value = this.expression(0);
  797. node = {type: "KeyValuePair", name: keyName, value: value};
  798. pairs.push(node);
  799. if (this._lookahead(0) === TOK_COMMA) {
  800. this._match(TOK_COMMA);
  801. } else if (this._lookahead(0) === TOK_RBRACE) {
  802. this._match(TOK_RBRACE);
  803. break;
  804. }
  805. }
  806. return {type: "MultiSelectHash", children: pairs};
  807. }
  808. };
  809. function TreeInterpreter(runtime) {
  810. this.runtime = runtime;
  811. }
  812. TreeInterpreter.prototype = {
  813. search: function(node, value) {
  814. return this.visit(node, value);
  815. },
  816. visit: function(node, value) {
  817. var matched, current, result, first, second, field, left, right, collected, i;
  818. switch (node.type) {
  819. case "Field":
  820. if (value !== null && isObject(value)) {
  821. field = value[node.name];
  822. if (field === undefined) {
  823. return null;
  824. } else {
  825. return field;
  826. }
  827. }
  828. return null;
  829. case "Subexpression":
  830. result = this.visit(node.children[0], value);
  831. for (i = 1; i < node.children.length; i++) {
  832. result = this.visit(node.children[1], result);
  833. if (result === null) {
  834. return null;
  835. }
  836. }
  837. return result;
  838. case "IndexExpression":
  839. left = this.visit(node.children[0], value);
  840. right = this.visit(node.children[1], left);
  841. return right;
  842. case "Index":
  843. if (!isArray(value)) {
  844. return null;
  845. }
  846. var index = node.value;
  847. if (index < 0) {
  848. index = value.length + index;
  849. }
  850. result = value[index];
  851. if (result === undefined) {
  852. result = null;
  853. }
  854. return result;
  855. case "Slice":
  856. if (!isArray(value)) {
  857. return null;
  858. }
  859. var sliceParams = node.children.slice(0);
  860. var computed = this.computeSliceParams(value.length, sliceParams);
  861. var start = computed[0];
  862. var stop = computed[1];
  863. var step = computed[2];
  864. result = [];
  865. if (step > 0) {
  866. for (i = start; i < stop; i += step) {
  867. result.push(value[i]);
  868. }
  869. } else {
  870. for (i = start; i > stop; i += step) {
  871. result.push(value[i]);
  872. }
  873. }
  874. return result;
  875. case "Projection":
  876. // Evaluate left child.
  877. var base = this.visit(node.children[0], value);
  878. if (!isArray(base)) {
  879. return null;
  880. }
  881. collected = [];
  882. for (i = 0; i < base.length; i++) {
  883. current = this.visit(node.children[1], base[i]);
  884. if (current !== null) {
  885. collected.push(current);
  886. }
  887. }
  888. return collected;
  889. case "ValueProjection":
  890. // Evaluate left child.
  891. base = this.visit(node.children[0], value);
  892. if (!isObject(base)) {
  893. return null;
  894. }
  895. collected = [];
  896. var values = objValues(base);
  897. for (i = 0; i < values.length; i++) {
  898. current = this.visit(node.children[1], values[i]);
  899. if (current !== null) {
  900. collected.push(current);
  901. }
  902. }
  903. return collected;
  904. case "FilterProjection":
  905. base = this.visit(node.children[0], value);
  906. if (!isArray(base)) {
  907. return null;
  908. }
  909. var filtered = [];
  910. var finalResults = [];
  911. for (i = 0; i < base.length; i++) {
  912. matched = this.visit(node.children[2], base[i]);
  913. if (!isFalse(matched)) {
  914. filtered.push(base[i]);
  915. }
  916. }
  917. for (var j = 0; j < filtered.length; j++) {
  918. current = this.visit(node.children[1], filtered[j]);
  919. if (current !== null) {
  920. finalResults.push(current);
  921. }
  922. }
  923. return finalResults;
  924. case "Comparator":
  925. first = this.visit(node.children[0], value);
  926. second = this.visit(node.children[1], value);
  927. switch(node.name) {
  928. case TOK_EQ:
  929. result = strictDeepEqual(first, second);
  930. break;
  931. case TOK_NE:
  932. result = !strictDeepEqual(first, second);
  933. break;
  934. case TOK_GT:
  935. result = first > second;
  936. break;
  937. case TOK_GTE:
  938. result = first >= second;
  939. break;
  940. case TOK_LT:
  941. result = first < second;
  942. break;
  943. case TOK_LTE:
  944. result = first <= second;
  945. break;
  946. default:
  947. throw new Error("Unknown comparator: " + node.name);
  948. }
  949. return result;
  950. case TOK_FLATTEN:
  951. var original = this.visit(node.children[0], value);
  952. if (!isArray(original)) {
  953. return null;
  954. }
  955. var merged = [];
  956. for (i = 0; i < original.length; i++) {
  957. current = original[i];
  958. if (isArray(current)) {
  959. merged.push.apply(merged, current);
  960. } else {
  961. merged.push(current);
  962. }
  963. }
  964. return merged;
  965. case "Identity":
  966. return value;
  967. case "MultiSelectList":
  968. if (value === null) {
  969. return null;
  970. }
  971. collected = [];
  972. for (i = 0; i < node.children.length; i++) {
  973. collected.push(this.visit(node.children[i], value));
  974. }
  975. return collected;
  976. case "MultiSelectHash":
  977. if (value === null) {
  978. return null;
  979. }
  980. collected = {};
  981. var child;
  982. for (i = 0; i < node.children.length; i++) {
  983. child = node.children[i];
  984. collected[child.name] = this.visit(child.value, value);
  985. }
  986. return collected;
  987. case "OrExpression":
  988. matched = this.visit(node.children[0], value);
  989. if (isFalse(matched)) {
  990. matched = this.visit(node.children[1], value);
  991. }
  992. return matched;
  993. case "AndExpression":
  994. first = this.visit(node.children[0], value);
  995. if (isFalse(first) === true) {
  996. return first;
  997. }
  998. return this.visit(node.children[1], value);
  999. case "NotExpression":
  1000. first = this.visit(node.children[0], value);
  1001. return isFalse(first);
  1002. case "Literal":
  1003. return node.value;
  1004. case TOK_PIPE:
  1005. left = this.visit(node.children[0], value);
  1006. return this.visit(node.children[1], left);
  1007. case TOK_CURRENT:
  1008. return value;
  1009. case "Function":
  1010. var resolvedArgs = [];
  1011. for (i = 0; i < node.children.length; i++) {
  1012. resolvedArgs.push(this.visit(node.children[i], value));
  1013. }
  1014. return this.runtime.callFunction(node.name, resolvedArgs);
  1015. case "ExpressionReference":
  1016. var refNode = node.children[0];
  1017. // Tag the node with a specific attribute so the type
  1018. // checker verify the type.
  1019. refNode.jmespathType = TOK_EXPREF;
  1020. return refNode;
  1021. default:
  1022. throw new Error("Unknown node type: " + node.type);
  1023. }
  1024. },
  1025. computeSliceParams: function(arrayLength, sliceParams) {
  1026. var start = sliceParams[0];
  1027. var stop = sliceParams[1];
  1028. var step = sliceParams[2];
  1029. var computed = [null, null, null];
  1030. if (step === null) {
  1031. step = 1;
  1032. } else if (step === 0) {
  1033. var error = new Error("Invalid slice, step cannot be 0");
  1034. error.name = "RuntimeError";
  1035. throw error;
  1036. }
  1037. var stepValueNegative = step < 0 ? true : false;
  1038. if (start === null) {
  1039. start = stepValueNegative ? arrayLength - 1 : 0;
  1040. } else {
  1041. start = this.capSliceRange(arrayLength, start, step);
  1042. }
  1043. if (stop === null) {
  1044. stop = stepValueNegative ? -1 : arrayLength;
  1045. } else {
  1046. stop = this.capSliceRange(arrayLength, stop, step);
  1047. }
  1048. computed[0] = start;
  1049. computed[1] = stop;
  1050. computed[2] = step;
  1051. return computed;
  1052. },
  1053. capSliceRange: function(arrayLength, actualValue, step) {
  1054. if (actualValue < 0) {
  1055. actualValue += arrayLength;
  1056. if (actualValue < 0) {
  1057. actualValue = step < 0 ? -1 : 0;
  1058. }
  1059. } else if (actualValue >= arrayLength) {
  1060. actualValue = step < 0 ? arrayLength - 1 : arrayLength;
  1061. }
  1062. return actualValue;
  1063. }
  1064. };
  1065. function Runtime(interpreter) {
  1066. this._interpreter = interpreter;
  1067. this.functionTable = {
  1068. // name: [function, <signature>]
  1069. // The <signature> can be:
  1070. //
  1071. // {
  1072. // args: [[type1, type2], [type1, type2]],
  1073. // variadic: true|false
  1074. // }
  1075. //
  1076. // Each arg in the arg list is a list of valid types
  1077. // (if the function is overloaded and supports multiple
  1078. // types. If the type is "any" then no type checking
  1079. // occurs on the argument. Variadic is optional
  1080. // and if not provided is assumed to be false.
  1081. abs: {_func: this._functionAbs, _signature: [{types: [TYPE_NUMBER]}]},
  1082. avg: {_func: this._functionAvg, _signature: [{types: [TYPE_ARRAY_NUMBER]}]},
  1083. ceil: {_func: this._functionCeil, _signature: [{types: [TYPE_NUMBER]}]},
  1084. contains: {
  1085. _func: this._functionContains,
  1086. _signature: [{types: [TYPE_STRING, TYPE_ARRAY]},
  1087. {types: [TYPE_ANY]}]},
  1088. "ends_with": {
  1089. _func: this._functionEndsWith,
  1090. _signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}]},
  1091. floor: {_func: this._functionFloor, _signature: [{types: [TYPE_NUMBER]}]},
  1092. length: {
  1093. _func: this._functionLength,
  1094. _signature: [{types: [TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT]}]},
  1095. map: {
  1096. _func: this._functionMap,
  1097. _signature: [{types: [TYPE_EXPREF]}, {types: [TYPE_ARRAY]}]},
  1098. max: {
  1099. _func: this._functionMax,
  1100. _signature: [{types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING]}]},
  1101. "merge": {
  1102. _func: this._functionMerge,
  1103. _signature: [{types: [TYPE_OBJECT], variadic: true}]
  1104. },
  1105. "max_by": {
  1106. _func: this._functionMaxBy,
  1107. _signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_EXPREF]}]
  1108. },
  1109. sum: {_func: this._functionSum, _signature: [{types: [TYPE_ARRAY_NUMBER]}]},
  1110. "starts_with": {
  1111. _func: this._functionStartsWith,
  1112. _signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}]},
  1113. min: {
  1114. _func: this._functionMin,
  1115. _signature: [{types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING]}]},
  1116. "min_by": {
  1117. _func: this._functionMinBy,
  1118. _signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_EXPREF]}]
  1119. },
  1120. type: {_func: this._functionType, _signature: [{types: [TYPE_ANY]}]},
  1121. keys: {_func: this._functionKeys, _signature: [{types: [TYPE_OBJECT]}]},
  1122. values: {_func: this._functionValues, _signature: [{types: [TYPE_OBJECT]}]},
  1123. sort: {_func: this._functionSort, _signature: [{types: [TYPE_ARRAY_STRING, TYPE_ARRAY_NUMBER]}]},
  1124. "sort_by": {
  1125. _func: this._functionSortBy,
  1126. _signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_EXPREF]}]
  1127. },
  1128. join: {
  1129. _func: this._functionJoin,
  1130. _signature: [
  1131. {types: [TYPE_STRING]},
  1132. {types: [TYPE_ARRAY_STRING]}
  1133. ]
  1134. },
  1135. reverse: {
  1136. _func: this._functionReverse,
  1137. _signature: [{types: [TYPE_STRING, TYPE_ARRAY]}]},
  1138. "to_array": {_func: this._functionToArray, _signature: [{types: [TYPE_ANY]}]},
  1139. "to_string": {_func: this._functionToString, _signature: [{types: [TYPE_ANY]}]},
  1140. "to_number": {_func: this._functionToNumber, _signature: [{types: [TYPE_ANY]}]},
  1141. "not_null": {
  1142. _func: this._functionNotNull,
  1143. _signature: [{types: [TYPE_ANY], variadic: true}]
  1144. }
  1145. };
  1146. }
  1147. Runtime.prototype = {
  1148. callFunction: function(name, resolvedArgs) {
  1149. var functionEntry = this.functionTable[name];
  1150. if (functionEntry === undefined) {
  1151. throw new Error("Unknown function: " + name + "()");
  1152. }
  1153. this._validateArgs(name, resolvedArgs, functionEntry._signature);
  1154. return functionEntry._func.call(this, resolvedArgs);
  1155. },
  1156. _validateArgs: function(name, args, signature) {
  1157. // Validating the args requires validating
  1158. // the correct arity and the correct type of each arg.
  1159. // If the last argument is declared as variadic, then we need
  1160. // a minimum number of args to be required. Otherwise it has to
  1161. // be an exact amount.
  1162. var pluralized;
  1163. if (signature[signature.length - 1].variadic) {
  1164. if (args.length < signature.length) {
  1165. pluralized = signature.length === 1 ? " argument" : " arguments";
  1166. throw new Error("ArgumentError: " + name + "() " +
  1167. "takes at least" + signature.length + pluralized +
  1168. " but received " + args.length);
  1169. }
  1170. } else if (args.length !== signature.length) {
  1171. pluralized = signature.length === 1 ? " argument" : " arguments";
  1172. throw new Error("ArgumentError: " + name + "() " +
  1173. "takes " + signature.length + pluralized +
  1174. " but received " + args.length);
  1175. }
  1176. var currentSpec;
  1177. var actualType;
  1178. var typeMatched;
  1179. for (var i = 0; i < signature.length; i++) {
  1180. typeMatched = false;
  1181. currentSpec = signature[i].types;
  1182. actualType = this._getTypeName(args[i]);
  1183. for (var j = 0; j < currentSpec.length; j++) {
  1184. if (this._typeMatches(actualType, currentSpec[j], args[i])) {
  1185. typeMatched = true;
  1186. break;
  1187. }
  1188. }
  1189. if (!typeMatched) {
  1190. var expected = currentSpec
  1191. .map(function(typeIdentifier) {
  1192. return TYPE_NAME_TABLE[typeIdentifier];
  1193. })
  1194. .join(',');
  1195. throw new Error("TypeError: " + name + "() " +
  1196. "expected argument " + (i + 1) +
  1197. " to be type " + expected +
  1198. " but received type " +
  1199. TYPE_NAME_TABLE[actualType] + " instead.");
  1200. }
  1201. }
  1202. },
  1203. _typeMatches: function(actual, expected, argValue) {
  1204. if (expected === TYPE_ANY) {
  1205. return true;
  1206. }
  1207. if (expected === TYPE_ARRAY_STRING ||
  1208. expected === TYPE_ARRAY_NUMBER ||
  1209. expected === TYPE_ARRAY) {
  1210. // The expected type can either just be array,
  1211. // or it can require a specific subtype (array of numbers).
  1212. //
  1213. // The simplest case is if "array" with no subtype is specified.
  1214. if (expected === TYPE_ARRAY) {
  1215. return actual === TYPE_ARRAY;
  1216. } else if (actual === TYPE_ARRAY) {
  1217. // Otherwise we need to check subtypes.
  1218. // I think this has potential to be improved.
  1219. var subtype;
  1220. if (expected === TYPE_ARRAY_NUMBER) {
  1221. subtype = TYPE_NUMBER;
  1222. } else if (expected === TYPE_ARRAY_STRING) {
  1223. subtype = TYPE_STRING;
  1224. }
  1225. for (var i = 0; i < argValue.length; i++) {
  1226. if (!this._typeMatches(
  1227. this._getTypeName(argValue[i]), subtype,
  1228. argValue[i])) {
  1229. return false;
  1230. }
  1231. }
  1232. return true;
  1233. }
  1234. } else {
  1235. return actual === expected;
  1236. }
  1237. },
  1238. _getTypeName: function(obj) {
  1239. switch (Object.prototype.toString.call(obj)) {
  1240. case "[object String]":
  1241. return TYPE_STRING;
  1242. case "[object Number]":
  1243. return TYPE_NUMBER;
  1244. case "[object Array]":
  1245. return TYPE_ARRAY;
  1246. case "[object Boolean]":
  1247. return TYPE_BOOLEAN;
  1248. case "[object Null]":
  1249. return TYPE_NULL;
  1250. case "[object Object]":
  1251. // Check if it's an expref. If it has, it's been
  1252. // tagged with a jmespathType attr of 'Expref';
  1253. if (obj.jmespathType === TOK_EXPREF) {
  1254. return TYPE_EXPREF;
  1255. } else {
  1256. return TYPE_OBJECT;
  1257. }
  1258. }
  1259. },
  1260. _functionStartsWith: function(resolvedArgs) {
  1261. return resolvedArgs[0].lastIndexOf(resolvedArgs[1]) === 0;
  1262. },
  1263. _functionEndsWith: function(resolvedArgs) {
  1264. var searchStr = resolvedArgs[0];
  1265. var suffix = resolvedArgs[1];
  1266. return searchStr.indexOf(suffix, searchStr.length - suffix.length) !== -1;
  1267. },
  1268. _functionReverse: function(resolvedArgs) {
  1269. var typeName = this._getTypeName(resolvedArgs[0]);
  1270. if (typeName === TYPE_STRING) {
  1271. var originalStr = resolvedArgs[0];
  1272. var reversedStr = "";
  1273. for (var i = originalStr.length - 1; i >= 0; i--) {
  1274. reversedStr += originalStr[i];
  1275. }
  1276. return reversedStr;
  1277. } else {
  1278. var reversedArray = resolvedArgs[0].slice(0);
  1279. reversedArray.reverse();
  1280. return reversedArray;
  1281. }
  1282. },
  1283. _functionAbs: function(resolvedArgs) {
  1284. return Math.abs(resolvedArgs[0]);
  1285. },
  1286. _functionCeil: function(resolvedArgs) {
  1287. return Math.ceil(resolvedArgs[0]);
  1288. },
  1289. _functionAvg: function(resolvedArgs) {
  1290. var sum = 0;
  1291. var inputArray = resolvedArgs[0];
  1292. for (var i = 0; i < inputArray.length; i++) {
  1293. sum += inputArray[i];
  1294. }
  1295. return sum / inputArray.length;
  1296. },
  1297. _functionContains: function(resolvedArgs) {
  1298. return resolvedArgs[0].indexOf(resolvedArgs[1]) >= 0;
  1299. },
  1300. _functionFloor: function(resolvedArgs) {
  1301. return Math.floor(resolvedArgs[0]);
  1302. },
  1303. _functionLength: function(resolvedArgs) {
  1304. if (!isObject(resolvedArgs[0])) {
  1305. return resolvedArgs[0].length;
  1306. } else {
  1307. // As far as I can tell, there's no way to get the length
  1308. // of an object without O(n) iteration through the object.
  1309. return Object.keys(resolvedArgs[0]).length;
  1310. }
  1311. },
  1312. _functionMap: function(resolvedArgs) {
  1313. var mapped = [];
  1314. var interpreter = this._interpreter;
  1315. var exprefNode = resolvedArgs[0];
  1316. var elements = resolvedArgs[1];
  1317. for (var i = 0; i < elements.length; i++) {
  1318. mapped.push(interpreter.visit(exprefNode, elements[i]));
  1319. }
  1320. return mapped;
  1321. },
  1322. _functionMerge: function(resolvedArgs) {
  1323. var merged = {};
  1324. for (var i = 0; i < resolvedArgs.length; i++) {
  1325. var current = resolvedArgs[i];
  1326. for (var key in current) {
  1327. merged[key] = current[key];
  1328. }
  1329. }
  1330. return merged;
  1331. },
  1332. _functionMax: function(resolvedArgs) {
  1333. if (resolvedArgs[0].length > 0) {
  1334. var typeName = this._getTypeName(resolvedArgs[0][0]);
  1335. if (typeName === TYPE_NUMBER) {
  1336. return Math.max.apply(Math, resolvedArgs[0]);
  1337. } else {
  1338. var elements = resolvedArgs[0];
  1339. var maxElement = elements[0];
  1340. for (var i = 1; i < elements.length; i++) {
  1341. if (maxElement.localeCompare(elements[i]) < 0) {
  1342. maxElement = elements[i];
  1343. }
  1344. }
  1345. return maxElement;
  1346. }
  1347. } else {
  1348. return null;
  1349. }
  1350. },
  1351. _functionMin: function(resolvedArgs) {
  1352. if (resolvedArgs[0].length > 0) {
  1353. var typeName = this._getTypeName(resolvedArgs[0][0]);
  1354. if (typeName === TYPE_NUMBER) {
  1355. return Math.min.apply(Math, resolvedArgs[0]);
  1356. } else {
  1357. var elements = resolvedArgs[0];
  1358. var minElement = elements[0];
  1359. for (var i = 1; i < elements.length; i++) {
  1360. if (elements[i].localeCompare(minElement) < 0) {
  1361. minElement = elements[i];
  1362. }
  1363. }
  1364. return minElement;
  1365. }
  1366. } else {
  1367. return null;
  1368. }
  1369. },
  1370. _functionSum: function(resolvedArgs) {
  1371. var sum = 0;
  1372. var listToSum = resolvedArgs[0];
  1373. for (var i = 0; i < listToSum.length; i++) {
  1374. sum += listToSum[i];
  1375. }
  1376. return sum;
  1377. },
  1378. _functionType: function(resolvedArgs) {
  1379. switch (this._getTypeName(resolvedArgs[0])) {
  1380. case TYPE_NUMBER:
  1381. return "number";
  1382. case TYPE_STRING:
  1383. return "string";
  1384. case TYPE_ARRAY:
  1385. return "array";
  1386. case TYPE_OBJECT:
  1387. return "object";
  1388. case TYPE_BOOLEAN:
  1389. return "boolean";
  1390. case TYPE_EXPREF:
  1391. return "expref";
  1392. case TYPE_NULL:
  1393. return "null";
  1394. }
  1395. },
  1396. _functionKeys: function(resolvedArgs) {
  1397. return Object.keys(resolvedArgs[0]);
  1398. },
  1399. _functionValues: function(resolvedArgs) {
  1400. var obj = resolvedArgs[0];
  1401. var keys = Object.keys(obj);
  1402. var values = [];
  1403. for (var i = 0; i < keys.length; i++) {
  1404. values.push(obj[keys[i]]);
  1405. }
  1406. return values;
  1407. },
  1408. _functionJoin: function(resolvedArgs) {
  1409. var joinChar = resolvedArgs[0];
  1410. var listJoin = resolvedArgs[1];
  1411. return listJoin.join(joinChar);
  1412. },
  1413. _functionToArray: function(resolvedArgs) {
  1414. if (this._getTypeName(resolvedArgs[0]) === TYPE_ARRAY) {
  1415. return resolvedArgs[0];
  1416. } else {
  1417. return [resolvedArgs[0]];
  1418. }
  1419. },
  1420. _functionToString: function(resolvedArgs) {
  1421. if (this._getTypeName(resolvedArgs[0]) === TYPE_STRING) {
  1422. return resolvedArgs[0];
  1423. } else {
  1424. return JSON.stringify(resolvedArgs[0]);
  1425. }
  1426. },
  1427. _functionToNumber: function(resolvedArgs) {
  1428. var typeName = this._getTypeName(resolvedArgs[0]);
  1429. var convertedValue;
  1430. if (typeName === TYPE_NUMBER) {
  1431. return resolvedArgs[0];
  1432. } else if (typeName === TYPE_STRING) {
  1433. convertedValue = +resolvedArgs[0];
  1434. if (!isNaN(convertedValue)) {
  1435. return convertedValue;
  1436. }
  1437. }
  1438. return null;
  1439. },
  1440. _functionNotNull: function(resolvedArgs) {
  1441. for (var i = 0; i < resolvedArgs.length; i++) {
  1442. if (this._getTypeName(resolvedArgs[i]) !== TYPE_NULL) {
  1443. return resolvedArgs[i];
  1444. }
  1445. }
  1446. return null;
  1447. },
  1448. _functionSort: function(resolvedArgs) {
  1449. var sortedArray = resolvedArgs[0].slice(0);
  1450. sortedArray.sort();
  1451. return sortedArray;
  1452. },
  1453. _functionSortBy: function(resolvedArgs) {
  1454. var sortedArray = resolvedArgs[0].slice(0);
  1455. if (sortedArray.length === 0) {
  1456. return sortedArray;
  1457. }
  1458. var interpreter = this._interpreter;
  1459. var exprefNode = resolvedArgs[1];
  1460. var requiredType = this._getTypeName(
  1461. interpreter.visit(exprefNode, sortedArray[0]));
  1462. if ([TYPE_NUMBER, TYPE_STRING].indexOf(requiredType) < 0) {
  1463. throw new Error("TypeError");
  1464. }
  1465. var that = this;
  1466. // In order to get a stable sort out of an unstable
  1467. // sort algorithm, we decorate/sort/undecorate (DSU)
  1468. // by creating a new list of [index, element] pairs.
  1469. // In the cmp function, if the evaluated elements are
  1470. // equal, then the index will be used as the tiebreaker.
  1471. // After the decorated list has been sorted, it will be
  1472. // undecorated to extract the original elements.
  1473. var decorated = [];
  1474. for (var i = 0; i < sortedArray.length; i++) {
  1475. decorated.push([i, sortedArray[i]]);
  1476. }
  1477. decorated.sort(function(a, b) {
  1478. var exprA = interpreter.visit(exprefNode, a[1]);
  1479. var exprB = interpreter.visit(exprefNode, b[1]);
  1480. if (that._getTypeName(exprA) !== requiredType) {
  1481. throw new Error(
  1482. "TypeError: expected " + requiredType + ", received " +
  1483. that._getTypeName(exprA));
  1484. } else if (that._getTypeName(exprB) !== requiredType) {
  1485. throw new Error(
  1486. "TypeError: expected " + requiredType + ", received " +
  1487. that._getTypeName(exprB));
  1488. }
  1489. if (exprA > exprB) {
  1490. return 1;
  1491. } else if (exprA < exprB) {
  1492. return -1;
  1493. } else {
  1494. // If they're equal compare the items by their
  1495. // order to maintain relative order of equal keys
  1496. // (i.e. to get a stable sort).
  1497. return a[0] - b[0];
  1498. }
  1499. });
  1500. // Undecorate: extract out the original list elements.
  1501. for (var j = 0; j < decorated.length; j++) {
  1502. sortedArray[j] = decorated[j][1];
  1503. }
  1504. return sortedArray;
  1505. },
  1506. _functionMaxBy: function(resolvedArgs) {
  1507. var exprefNode = resolvedArgs[1];
  1508. var resolvedArray = resolvedArgs[0];
  1509. var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
  1510. var maxNumber = -Infinity;
  1511. var maxRecord;
  1512. var current;
  1513. for (var i = 0; i < resolvedArray.length; i++) {
  1514. current = keyFunction(resolvedArray[i]);
  1515. if (current > maxNumber) {
  1516. maxNumber = current;
  1517. maxRecord = resolvedArray[i];
  1518. }
  1519. }
  1520. return maxRecord;
  1521. },
  1522. _functionMinBy: function(resolvedArgs) {
  1523. var exprefNode = resolvedArgs[1];
  1524. var resolvedArray = resolvedArgs[0];
  1525. var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
  1526. var minNumber = Infinity;
  1527. var minRecord;
  1528. var current;
  1529. for (var i = 0; i < resolvedArray.length; i++) {
  1530. current = keyFunction(resolvedArray[i]);
  1531. if (current < minNumber) {
  1532. minNumber = current;
  1533. minRecord = resolvedArray[i];
  1534. }
  1535. }
  1536. return minRecord;
  1537. },
  1538. createKeyFunction: function(exprefNode, allowedTypes) {
  1539. var that = this;
  1540. var interpreter = this._interpreter;
  1541. var keyFunc = function(x) {
  1542. var current = interpreter.visit(exprefNode, x);
  1543. if (allowedTypes.indexOf(that._getTypeName(current)) < 0) {
  1544. var msg = "TypeError: expected one of " + allowedTypes +
  1545. ", received " + that._getTypeName(current);
  1546. throw new Error(msg);
  1547. }
  1548. return current;
  1549. };
  1550. return keyFunc;
  1551. }
  1552. };
  1553. function compile(stream) {
  1554. var parser = new Parser();
  1555. var ast = parser.parse(stream);
  1556. return ast;
  1557. }
  1558. function tokenize(stream) {
  1559. var lexer = new Lexer();
  1560. return lexer.tokenize(stream);
  1561. }
  1562. function search(data, expression) {
  1563. var parser = new Parser();
  1564. // This needs to be improved. Both the interpreter and runtime depend on
  1565. // each other. The runtime needs the interpreter to support exprefs.
  1566. // There's likely a clean way to avoid the cyclic dependency.
  1567. var runtime = new Runtime();
  1568. var interpreter = new TreeInterpreter(runtime);
  1569. runtime._interpreter = interpreter;
  1570. var node = parser.parse(expression);
  1571. return interpreter.search(node, data);
  1572. }
  1573. exports.tokenize = tokenize;
  1574. exports.compile = compile;
  1575. exports.search = search;
  1576. exports.strictDeepEqual = strictDeepEqual;
  1577. })(typeof exports === "undefined" ? this.jmespath = {} : exports);