lexer.ts 18 KB

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