lexer.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. // This is auto generated from the unicode tables.
  4. // The tables are at:
  5. // http://www.fileformat.info/info/unicode/category/Lu/list.htm
  6. // http://www.fileformat.info/info/unicode/category/Ll/list.htm
  7. // http://www.fileformat.info/info/unicode/category/Lt/list.htm
  8. // http://www.fileformat.info/info/unicode/category/Lm/list.htm
  9. // http://www.fileformat.info/info/unicode/category/Lo/list.htm
  10. // http://www.fileformat.info/info/unicode/category/Nl/list.htm
  11. var unicodeLetterTable = [
  12. 170, 170, 181, 181, 186, 186, 192, 214,
  13. 216, 246, 248, 705, 710, 721, 736, 740, 748, 748, 750, 750,
  14. 880, 884, 886, 887, 890, 893, 902, 902, 904, 906, 908, 908,
  15. 910, 929, 931, 1013, 1015, 1153, 1162, 1319, 1329, 1366,
  16. 1369, 1369, 1377, 1415, 1488, 1514, 1520, 1522, 1568, 1610,
  17. 1646, 1647, 1649, 1747, 1749, 1749, 1765, 1766, 1774, 1775,
  18. 1786, 1788, 1791, 1791, 1808, 1808, 1810, 1839, 1869, 1957,
  19. 1969, 1969, 1994, 2026, 2036, 2037, 2042, 2042, 2048, 2069,
  20. 2074, 2074, 2084, 2084, 2088, 2088, 2112, 2136, 2308, 2361,
  21. 2365, 2365, 2384, 2384, 2392, 2401, 2417, 2423, 2425, 2431,
  22. 2437, 2444, 2447, 2448, 2451, 2472, 2474, 2480, 2482, 2482,
  23. 2486, 2489, 2493, 2493, 2510, 2510, 2524, 2525, 2527, 2529,
  24. 2544, 2545, 2565, 2570, 2575, 2576, 2579, 2600, 2602, 2608,
  25. 2610, 2611, 2613, 2614, 2616, 2617, 2649, 2652, 2654, 2654,
  26. 2674, 2676, 2693, 2701, 2703, 2705, 2707, 2728, 2730, 2736,
  27. 2738, 2739, 2741, 2745, 2749, 2749, 2768, 2768, 2784, 2785,
  28. 2821, 2828, 2831, 2832, 2835, 2856, 2858, 2864, 2866, 2867,
  29. 2869, 2873, 2877, 2877, 2908, 2909, 2911, 2913, 2929, 2929,
  30. 2947, 2947, 2949, 2954, 2958, 2960, 2962, 2965, 2969, 2970,
  31. 2972, 2972, 2974, 2975, 2979, 2980, 2984, 2986, 2990, 3001,
  32. 3024, 3024, 3077, 3084, 3086, 3088, 3090, 3112, 3114, 3123,
  33. 3125, 3129, 3133, 3133, 3160, 3161, 3168, 3169, 3205, 3212,
  34. 3214, 3216, 3218, 3240, 3242, 3251, 3253, 3257, 3261, 3261,
  35. 3294, 3294, 3296, 3297, 3313, 3314, 3333, 3340, 3342, 3344,
  36. 3346, 3386, 3389, 3389, 3406, 3406, 3424, 3425, 3450, 3455,
  37. 3461, 3478, 3482, 3505, 3507, 3515, 3517, 3517, 3520, 3526,
  38. 3585, 3632, 3634, 3635, 3648, 3654, 3713, 3714, 3716, 3716,
  39. 3719, 3720, 3722, 3722, 3725, 3725, 3732, 3735, 3737, 3743,
  40. 3745, 3747, 3749, 3749, 3751, 3751, 3754, 3755, 3757, 3760,
  41. 3762, 3763, 3773, 3773, 3776, 3780, 3782, 3782, 3804, 3805,
  42. 3840, 3840, 3904, 3911, 3913, 3948, 3976, 3980, 4096, 4138,
  43. 4159, 4159, 4176, 4181, 4186, 4189, 4193, 4193, 4197, 4198,
  44. 4206, 4208, 4213, 4225, 4238, 4238, 4256, 4293, 4304, 4346,
  45. 4348, 4348, 4352, 4680, 4682, 4685, 4688, 4694, 4696, 4696,
  46. 4698, 4701, 4704, 4744, 4746, 4749, 4752, 4784, 4786, 4789,
  47. 4792, 4798, 4800, 4800, 4802, 4805, 4808, 4822, 4824, 4880,
  48. 4882, 4885, 4888, 4954, 4992, 5007, 5024, 5108, 5121, 5740,
  49. 5743, 5759, 5761, 5786, 5792, 5866, 5870, 5872, 5888, 5900,
  50. 5902, 5905, 5920, 5937, 5952, 5969, 5984, 5996, 5998, 6000,
  51. 6016, 6067, 6103, 6103, 6108, 6108, 6176, 6263, 6272, 6312,
  52. 6314, 6314, 6320, 6389, 6400, 6428, 6480, 6509, 6512, 6516,
  53. 6528, 6571, 6593, 6599, 6656, 6678, 6688, 6740, 6823, 6823,
  54. 6917, 6963, 6981, 6987, 7043, 7072, 7086, 7087, 7104, 7141,
  55. 7168, 7203, 7245, 7247, 7258, 7293, 7401, 7404, 7406, 7409,
  56. 7424, 7615, 7680, 7957, 7960, 7965, 7968, 8005, 8008, 8013,
  57. 8016, 8023, 8025, 8025, 8027, 8027, 8029, 8029, 8031, 8061,
  58. 8064, 8116, 8118, 8124, 8126, 8126, 8130, 8132, 8134, 8140,
  59. 8144, 8147, 8150, 8155, 8160, 8172, 8178, 8180, 8182, 8188,
  60. 8305, 8305, 8319, 8319, 8336, 8348, 8450, 8450, 8455, 8455,
  61. 8458, 8467, 8469, 8469, 8473, 8477, 8484, 8484, 8486, 8486,
  62. 8488, 8488, 8490, 8493, 8495, 8505, 8508, 8511, 8517, 8521,
  63. 8526, 8526, 8544, 8584, 11264, 11310, 11312, 11358,
  64. 11360, 11492, 11499, 11502, 11520, 11557, 11568, 11621,
  65. 11631, 11631, 11648, 11670, 11680, 11686, 11688, 11694,
  66. 11696, 11702, 11704, 11710, 11712, 11718, 11720, 11726,
  67. 11728, 11734, 11736, 11742, 11823, 11823, 12293, 12295,
  68. 12321, 12329, 12337, 12341, 12344, 12348, 12353, 12438,
  69. 12445, 12447, 12449, 12538, 12540, 12543, 12549, 12589,
  70. 12593, 12686, 12704, 12730, 12784, 12799, 13312, 13312,
  71. 19893, 19893, 19968, 19968, 40907, 40907, 40960, 42124,
  72. 42192, 42237, 42240, 42508, 42512, 42527, 42538, 42539,
  73. 42560, 42606, 42623, 42647, 42656, 42735, 42775, 42783,
  74. 42786, 42888, 42891, 42894, 42896, 42897, 42912, 42921,
  75. 43002, 43009, 43011, 43013, 43015, 43018, 43020, 43042,
  76. 43072, 43123, 43138, 43187, 43250, 43255, 43259, 43259,
  77. 43274, 43301, 43312, 43334, 43360, 43388, 43396, 43442,
  78. 43471, 43471, 43520, 43560, 43584, 43586, 43588, 43595,
  79. 43616, 43638, 43642, 43642, 43648, 43695, 43697, 43697,
  80. 43701, 43702, 43705, 43709, 43712, 43712, 43714, 43714,
  81. 43739, 43741, 43777, 43782, 43785, 43790, 43793, 43798,
  82. 43808, 43814, 43816, 43822, 43968, 44002, 44032, 44032,
  83. 55203, 55203, 55216, 55238, 55243, 55291, 63744, 64045,
  84. 64048, 64109, 64112, 64217, 64256, 64262, 64275, 64279,
  85. 64285, 64285, 64287, 64296, 64298, 64310, 64312, 64316,
  86. 64318, 64318, 64320, 64321, 64323, 64324, 64326, 64433,
  87. 64467, 64829, 64848, 64911, 64914, 64967, 65008, 65019,
  88. 65136, 65140, 65142, 65276, 65313, 65338, 65345, 65370,
  89. 65382, 65470, 65474, 65479, 65482, 65487, 65490, 65495,
  90. 65498, 65500, 65536, 65547, 65549, 65574, 65576, 65594,
  91. 65596, 65597, 65599, 65613, 65616, 65629, 65664, 65786,
  92. 65856, 65908, 66176, 66204, 66208, 66256, 66304, 66334,
  93. 66352, 66378, 66432, 66461, 66464, 66499, 66504, 66511,
  94. 66513, 66517, 66560, 66717, 67584, 67589, 67592, 67592,
  95. 67594, 67637, 67639, 67640, 67644, 67644, 67647, 67669,
  96. 67840, 67861, 67872, 67897, 68096, 68096, 68112, 68115,
  97. 68117, 68119, 68121, 68147, 68192, 68220, 68352, 68405,
  98. 68416, 68437, 68448, 68466, 68608, 68680, 69635, 69687,
  99. 69763, 69807, 73728, 74606, 74752, 74850, 77824, 78894,
  100. 92160, 92728, 110592, 110593, 119808, 119892, 119894, 119964,
  101. 119966, 119967, 119970, 119970, 119973, 119974, 119977, 119980,
  102. 119982, 119993, 119995, 119995, 119997, 120003, 120005, 120069,
  103. 120071, 120074, 120077, 120084, 120086, 120092, 120094, 120121,
  104. 120123, 120126, 120128, 120132, 120134, 120134, 120138, 120144,
  105. 120146, 120485, 120488, 120512, 120514, 120538, 120540, 120570,
  106. 120572, 120596, 120598, 120628, 120630, 120654, 120656, 120686,
  107. 120688, 120712, 120714, 120744, 120746, 120770, 120772, 120779,
  108. 131072, 131072, 173782, 173782, 173824, 173824, 177972, 177972,
  109. 177984, 177984, 178205, 178205, 194560, 195101
  110. ];
  111. var identifierStartTable = [];
  112. for (var i = 0; i < 128; i++) {
  113. identifierStartTable[i] =
  114. i >= 48 && i <= 57 || // 0-9
  115. i === 36 || // $
  116. i === 126 || // ~
  117. i === 124 || // |
  118. i >= 65 && i <= 90 || // A-Z
  119. i === 95 || // _
  120. i === 45 || // -
  121. i === 42 || // *
  122. i === 58 || // :
  123. i === 91 || // templateStart [
  124. i === 93 || // templateEnd ]
  125. i === 63 || // ?
  126. i === 37 || // %
  127. i === 35 || // #
  128. i === 61 || // =
  129. i >= 97 && i <= 122; // a-z
  130. }
  131. var identifierPartTable = identifierStartTable;
  132. export function Lexer(expression) {
  133. this.input = expression;
  134. this.char = 1;
  135. this.from = 1;
  136. }
  137. Lexer.prototype = {
  138. peek: function (i) {
  139. return this.input.charAt(i || 0);
  140. },
  141. skip: function (i) {
  142. i = i || 1;
  143. this.char += i;
  144. this.input = this.input.slice(i);
  145. },
  146. tokenize: function() {
  147. var list = [];
  148. var token;
  149. while (token = this.next()) {
  150. list.push(token);
  151. }
  152. return list;
  153. },
  154. next: function() {
  155. this.from = this.char;
  156. // Move to the next non-space character.
  157. if (/\s/.test(this.peek())) {
  158. while (/\s/.test(this.peek())) {
  159. this.from += 1;
  160. this.skip();
  161. }
  162. if (this.peek() === "") { // EOL
  163. return null;
  164. }
  165. }
  166. var match = this.scanStringLiteral();
  167. if (match) {
  168. return match;
  169. }
  170. match =
  171. this.scanPunctuator() ||
  172. this.scanNumericLiteral() ||
  173. this.scanIdentifier() ||
  174. this.scanTemplateSequence();
  175. if (match) {
  176. this.skip(match.value.length);
  177. return match;
  178. }
  179. // No token could be matched, give up.
  180. return null;
  181. },
  182. scanTemplateSequence: function() {
  183. if (this.peek() === '[' && this.peek(1) === '[') {
  184. return {
  185. type: 'templateStart',
  186. value: '[[',
  187. pos: this.char
  188. };
  189. }
  190. if (this.peek() === ']' && this.peek(1) === ']') {
  191. return {
  192. type: 'templateEnd',
  193. value: '[[',
  194. pos: this.char
  195. };
  196. }
  197. return null;
  198. },
  199. /*
  200. * Extract a JavaScript identifier out of the next sequence of
  201. * characters or return 'null' if its not possible. In addition,
  202. * to Identifier this method can also produce BooleanLiteral
  203. * (true/false) and NullLiteral (null).
  204. */
  205. scanIdentifier: function() {
  206. var id = "";
  207. var index = 0;
  208. var type, char;
  209. // Detects any character in the Unicode categories "Uppercase
  210. // letter (Lu)", "Lowercase letter (Ll)", "Titlecase letter
  211. // (Lt)", "Modifier letter (Lm)", "Other letter (Lo)", or
  212. // "Letter number (Nl)".
  213. //
  214. // Both approach and unicodeLetterTable were borrowed from
  215. // Google's Traceur.
  216. function isUnicodeLetter(code) {
  217. for (var i = 0; i < unicodeLetterTable.length;) {
  218. if (code < unicodeLetterTable[i++]) {
  219. return false;
  220. }
  221. if (code <= unicodeLetterTable[i++]) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. function isHexDigit(str) {
  228. return (/^[0-9a-fA-F]$/).test(str);
  229. }
  230. var readUnicodeEscapeSequence = _.bind(function () {
  231. /*jshint validthis:true */
  232. index += 1;
  233. if (this.peek(index) !== "u") {
  234. return null;
  235. }
  236. var ch1 = this.peek(index + 1);
  237. var ch2 = this.peek(index + 2);
  238. var ch3 = this.peek(index + 3);
  239. var ch4 = this.peek(index + 4);
  240. var code;
  241. if (isHexDigit(ch1) && isHexDigit(ch2) && isHexDigit(ch3) && isHexDigit(ch4)) {
  242. code = parseInt(ch1 + ch2 + ch3 + ch4, 16);
  243. if (isUnicodeLetter(code)) {
  244. index += 5;
  245. return "\\u" + ch1 + ch2 + ch3 + ch4;
  246. }
  247. return null;
  248. }
  249. return null;
  250. }, this);
  251. var getIdentifierStart = _.bind(function () {
  252. /*jshint validthis:true */
  253. var chr = this.peek(index);
  254. var code = chr.charCodeAt(0);
  255. if (chr === '*') {
  256. index += 1;
  257. return chr;
  258. }
  259. if (code === 92) {
  260. return readUnicodeEscapeSequence();
  261. }
  262. if (code < 128) {
  263. if (identifierStartTable[code]) {
  264. index += 1;
  265. return chr;
  266. }
  267. return null;
  268. }
  269. if (isUnicodeLetter(code)) {
  270. index += 1;
  271. return chr;
  272. }
  273. return null;
  274. }, this);
  275. var getIdentifierPart = _.bind(function () {
  276. /*jshint validthis:true */
  277. var chr = this.peek(index);
  278. var code = chr.charCodeAt(0);
  279. if (code === 92) {
  280. return readUnicodeEscapeSequence();
  281. }
  282. if (code < 128) {
  283. if (identifierPartTable[code]) {
  284. index += 1;
  285. return chr;
  286. }
  287. return null;
  288. }
  289. if (isUnicodeLetter(code)) {
  290. index += 1;
  291. return chr;
  292. }
  293. return null;
  294. }, this);
  295. char = getIdentifierStart();
  296. if (char === null) {
  297. return null;
  298. }
  299. id = char;
  300. for (;;) {
  301. char = getIdentifierPart();
  302. if (char === null) {
  303. break;
  304. }
  305. id += char;
  306. }
  307. switch (id) {
  308. case 'true': {
  309. type = 'bool';
  310. break;
  311. }
  312. case 'false': {
  313. type = 'bool';
  314. break;
  315. }
  316. default:
  317. type = "identifier";
  318. }
  319. return {
  320. type: type,
  321. value: id,
  322. pos: this.char
  323. };
  324. },
  325. /*
  326. * Extract a numeric literal out of the next sequence of
  327. * characters or return 'null' if its not possible. This method
  328. * supports all numeric literals described in section 7.8.3
  329. * of the EcmaScript 5 specification.
  330. *
  331. * This method's implementation was heavily influenced by the
  332. * scanNumericLiteral function in the Esprima parser's source code.
  333. */
  334. scanNumericLiteral: function (): any {
  335. var index = 0;
  336. var value = "";
  337. var length = this.input.length;
  338. var char = this.peek(index);
  339. var bad;
  340. function isDecimalDigit(str) {
  341. return (/^[0-9]$/).test(str);
  342. }
  343. function isOctalDigit(str) {
  344. return (/^[0-7]$/).test(str);
  345. }
  346. function isHexDigit(str) {
  347. return (/^[0-9a-fA-F]$/).test(str);
  348. }
  349. function isIdentifierStart(ch) {
  350. return (ch === "$") || (ch === "_") || (ch === "\\") ||
  351. (ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z");
  352. }
  353. // handle negative num literals
  354. if (char === '-') {
  355. value += char;
  356. index += 1;
  357. char = this.peek(index);
  358. }
  359. // Numbers must start either with a decimal digit or a point.
  360. if (char !== "." && !isDecimalDigit(char)) {
  361. return null;
  362. }
  363. if (char !== ".") {
  364. value += this.peek(index);
  365. index += 1;
  366. char = this.peek(index);
  367. if (value === "0") {
  368. // Base-16 numbers.
  369. if (char === "x" || char === "X") {
  370. index += 1;
  371. value += char;
  372. while (index < length) {
  373. char = this.peek(index);
  374. if (!isHexDigit(char)) {
  375. break;
  376. }
  377. value += char;
  378. index += 1;
  379. }
  380. if (value.length <= 2) { // 0x
  381. return {
  382. type: 'number',
  383. value: value,
  384. isMalformed: true,
  385. pos: this.char
  386. };
  387. }
  388. if (index < length) {
  389. char = this.peek(index);
  390. if (isIdentifierStart(char)) {
  391. return null;
  392. }
  393. }
  394. return {
  395. type: 'number',
  396. value: value,
  397. base: 16,
  398. isMalformed: false,
  399. pos: this.char
  400. };
  401. }
  402. // Base-8 numbers.
  403. if (isOctalDigit(char)) {
  404. index += 1;
  405. value += char;
  406. bad = false;
  407. while (index < length) {
  408. char = this.peek(index);
  409. // Numbers like '019' (note the 9) are not valid octals
  410. // but we still parse them and mark as malformed.
  411. if (isDecimalDigit(char)) {
  412. bad = true;
  413. } if (!isOctalDigit(char)) {
  414. // if the char is a non punctuator then its not a valid number
  415. if (!this.isPunctuator(char)) {
  416. return null;
  417. }
  418. break;
  419. }
  420. value += char;
  421. index += 1;
  422. }
  423. if (index < length) {
  424. char = this.peek(index);
  425. if (isIdentifierStart(char)) {
  426. return null;
  427. }
  428. }
  429. return {
  430. type: 'number',
  431. value: value,
  432. base: 8,
  433. isMalformed: bad
  434. };
  435. }
  436. // Decimal numbers that start with '0' such as '09' are illegal
  437. // but we still parse them and return as malformed.
  438. if (isDecimalDigit(char)) {
  439. index += 1;
  440. value += char;
  441. }
  442. }
  443. while (index < length) {
  444. char = this.peek(index);
  445. if (!isDecimalDigit(char)) {
  446. break;
  447. }
  448. value += char;
  449. index += 1;
  450. }
  451. }
  452. // Decimal digits.
  453. if (char === ".") {
  454. value += char;
  455. index += 1;
  456. while (index < length) {
  457. char = this.peek(index);
  458. if (!isDecimalDigit(char)) {
  459. break;
  460. }
  461. value += char;
  462. index += 1;
  463. }
  464. }
  465. // Exponent part.
  466. if (char === "e" || char === "E") {
  467. value += char;
  468. index += 1;
  469. char = this.peek(index);
  470. if (char === "+" || char === "-") {
  471. value += this.peek(index);
  472. index += 1;
  473. }
  474. char = this.peek(index);
  475. if (isDecimalDigit(char)) {
  476. value += char;
  477. index += 1;
  478. while (index < length) {
  479. char = this.peek(index);
  480. if (!isDecimalDigit(char)) {
  481. break;
  482. }
  483. value += char;
  484. index += 1;
  485. }
  486. } else {
  487. return null;
  488. }
  489. }
  490. if (index < length) {
  491. char = this.peek(index);
  492. if (!this.isPunctuator(char)) {
  493. return null;
  494. }
  495. }
  496. return {
  497. type: 'number',
  498. value: value,
  499. base: 10,
  500. pos: this.char,
  501. isMalformed: !isFinite(+value)
  502. };
  503. },
  504. isPunctuator: function (ch1) {
  505. switch (ch1) {
  506. case ".":
  507. case "(":
  508. case ")":
  509. case ",":
  510. case "{":
  511. case "}":
  512. return true;
  513. }
  514. return false;
  515. },
  516. scanPunctuator: function () {
  517. var ch1 = this.peek();
  518. if (this.isPunctuator(ch1)) {
  519. return {
  520. type: ch1,
  521. value: ch1,
  522. pos: this.char
  523. };
  524. }
  525. return null;
  526. },
  527. /*
  528. * Extract a string out of the next sequence of characters and/or
  529. * lines or return 'null' if its not possible. Since strings can
  530. * span across multiple lines this method has to move the char
  531. * pointer.
  532. *
  533. * This method recognizes pseudo-multiline JavaScript strings:
  534. *
  535. * var str = "hello\
  536. * world";
  537. */
  538. scanStringLiteral: function () {
  539. /*jshint loopfunc:true */
  540. var quote = this.peek();
  541. // String must start with a quote.
  542. if (quote !== "\"" && quote !== "'") {
  543. return null;
  544. }
  545. var value = "";
  546. this.skip();
  547. while (this.peek() !== quote) {
  548. if (this.peek() === "") { // End Of Line
  549. return {
  550. type: 'string',
  551. value: value,
  552. isUnclosed: true,
  553. quote: quote,
  554. pos: this.char
  555. };
  556. }
  557. var char = this.peek();
  558. var jump = 1; // A length of a jump, after we're done
  559. // parsing this character.
  560. value += char;
  561. this.skip(jump);
  562. }
  563. this.skip();
  564. return {
  565. type: 'string',
  566. value: value,
  567. isUnclosed: false,
  568. quote: quote,
  569. pos: this.char
  570. };
  571. },
  572. };