lexer.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. import _ 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,
  12. 170,
  13. 181,
  14. 181,
  15. 186,
  16. 186,
  17. 192,
  18. 214,
  19. 216,
  20. 246,
  21. 248,
  22. 705,
  23. 710,
  24. 721,
  25. 736,
  26. 740,
  27. 748,
  28. 748,
  29. 750,
  30. 750,
  31. 880,
  32. 884,
  33. 886,
  34. 887,
  35. 890,
  36. 893,
  37. 902,
  38. 902,
  39. 904,
  40. 906,
  41. 908,
  42. 908,
  43. 910,
  44. 929,
  45. 931,
  46. 1013,
  47. 1015,
  48. 1153,
  49. 1162,
  50. 1319,
  51. 1329,
  52. 1366,
  53. 1369,
  54. 1369,
  55. 1377,
  56. 1415,
  57. 1488,
  58. 1514,
  59. 1520,
  60. 1522,
  61. 1568,
  62. 1610,
  63. 1646,
  64. 1647,
  65. 1649,
  66. 1747,
  67. 1749,
  68. 1749,
  69. 1765,
  70. 1766,
  71. 1774,
  72. 1775,
  73. 1786,
  74. 1788,
  75. 1791,
  76. 1791,
  77. 1808,
  78. 1808,
  79. 1810,
  80. 1839,
  81. 1869,
  82. 1957,
  83. 1969,
  84. 1969,
  85. 1994,
  86. 2026,
  87. 2036,
  88. 2037,
  89. 2042,
  90. 2042,
  91. 2048,
  92. 2069,
  93. 2074,
  94. 2074,
  95. 2084,
  96. 2084,
  97. 2088,
  98. 2088,
  99. 2112,
  100. 2136,
  101. 2308,
  102. 2361,
  103. 2365,
  104. 2365,
  105. 2384,
  106. 2384,
  107. 2392,
  108. 2401,
  109. 2417,
  110. 2423,
  111. 2425,
  112. 2431,
  113. 2437,
  114. 2444,
  115. 2447,
  116. 2448,
  117. 2451,
  118. 2472,
  119. 2474,
  120. 2480,
  121. 2482,
  122. 2482,
  123. 2486,
  124. 2489,
  125. 2493,
  126. 2493,
  127. 2510,
  128. 2510,
  129. 2524,
  130. 2525,
  131. 2527,
  132. 2529,
  133. 2544,
  134. 2545,
  135. 2565,
  136. 2570,
  137. 2575,
  138. 2576,
  139. 2579,
  140. 2600,
  141. 2602,
  142. 2608,
  143. 2610,
  144. 2611,
  145. 2613,
  146. 2614,
  147. 2616,
  148. 2617,
  149. 2649,
  150. 2652,
  151. 2654,
  152. 2654,
  153. 2674,
  154. 2676,
  155. 2693,
  156. 2701,
  157. 2703,
  158. 2705,
  159. 2707,
  160. 2728,
  161. 2730,
  162. 2736,
  163. 2738,
  164. 2739,
  165. 2741,
  166. 2745,
  167. 2749,
  168. 2749,
  169. 2768,
  170. 2768,
  171. 2784,
  172. 2785,
  173. 2821,
  174. 2828,
  175. 2831,
  176. 2832,
  177. 2835,
  178. 2856,
  179. 2858,
  180. 2864,
  181. 2866,
  182. 2867,
  183. 2869,
  184. 2873,
  185. 2877,
  186. 2877,
  187. 2908,
  188. 2909,
  189. 2911,
  190. 2913,
  191. 2929,
  192. 2929,
  193. 2947,
  194. 2947,
  195. 2949,
  196. 2954,
  197. 2958,
  198. 2960,
  199. 2962,
  200. 2965,
  201. 2969,
  202. 2970,
  203. 2972,
  204. 2972,
  205. 2974,
  206. 2975,
  207. 2979,
  208. 2980,
  209. 2984,
  210. 2986,
  211. 2990,
  212. 3001,
  213. 3024,
  214. 3024,
  215. 3077,
  216. 3084,
  217. 3086,
  218. 3088,
  219. 3090,
  220. 3112,
  221. 3114,
  222. 3123,
  223. 3125,
  224. 3129,
  225. 3133,
  226. 3133,
  227. 3160,
  228. 3161,
  229. 3168,
  230. 3169,
  231. 3205,
  232. 3212,
  233. 3214,
  234. 3216,
  235. 3218,
  236. 3240,
  237. 3242,
  238. 3251,
  239. 3253,
  240. 3257,
  241. 3261,
  242. 3261,
  243. 3294,
  244. 3294,
  245. 3296,
  246. 3297,
  247. 3313,
  248. 3314,
  249. 3333,
  250. 3340,
  251. 3342,
  252. 3344,
  253. 3346,
  254. 3386,
  255. 3389,
  256. 3389,
  257. 3406,
  258. 3406,
  259. 3424,
  260. 3425,
  261. 3450,
  262. 3455,
  263. 3461,
  264. 3478,
  265. 3482,
  266. 3505,
  267. 3507,
  268. 3515,
  269. 3517,
  270. 3517,
  271. 3520,
  272. 3526,
  273. 3585,
  274. 3632,
  275. 3634,
  276. 3635,
  277. 3648,
  278. 3654,
  279. 3713,
  280. 3714,
  281. 3716,
  282. 3716,
  283. 3719,
  284. 3720,
  285. 3722,
  286. 3722,
  287. 3725,
  288. 3725,
  289. 3732,
  290. 3735,
  291. 3737,
  292. 3743,
  293. 3745,
  294. 3747,
  295. 3749,
  296. 3749,
  297. 3751,
  298. 3751,
  299. 3754,
  300. 3755,
  301. 3757,
  302. 3760,
  303. 3762,
  304. 3763,
  305. 3773,
  306. 3773,
  307. 3776,
  308. 3780,
  309. 3782,
  310. 3782,
  311. 3804,
  312. 3805,
  313. 3840,
  314. 3840,
  315. 3904,
  316. 3911,
  317. 3913,
  318. 3948,
  319. 3976,
  320. 3980,
  321. 4096,
  322. 4138,
  323. 4159,
  324. 4159,
  325. 4176,
  326. 4181,
  327. 4186,
  328. 4189,
  329. 4193,
  330. 4193,
  331. 4197,
  332. 4198,
  333. 4206,
  334. 4208,
  335. 4213,
  336. 4225,
  337. 4238,
  338. 4238,
  339. 4256,
  340. 4293,
  341. 4304,
  342. 4346,
  343. 4348,
  344. 4348,
  345. 4352,
  346. 4680,
  347. 4682,
  348. 4685,
  349. 4688,
  350. 4694,
  351. 4696,
  352. 4696,
  353. 4698,
  354. 4701,
  355. 4704,
  356. 4744,
  357. 4746,
  358. 4749,
  359. 4752,
  360. 4784,
  361. 4786,
  362. 4789,
  363. 4792,
  364. 4798,
  365. 4800,
  366. 4800,
  367. 4802,
  368. 4805,
  369. 4808,
  370. 4822,
  371. 4824,
  372. 4880,
  373. 4882,
  374. 4885,
  375. 4888,
  376. 4954,
  377. 4992,
  378. 5007,
  379. 5024,
  380. 5108,
  381. 5121,
  382. 5740,
  383. 5743,
  384. 5759,
  385. 5761,
  386. 5786,
  387. 5792,
  388. 5866,
  389. 5870,
  390. 5872,
  391. 5888,
  392. 5900,
  393. 5902,
  394. 5905,
  395. 5920,
  396. 5937,
  397. 5952,
  398. 5969,
  399. 5984,
  400. 5996,
  401. 5998,
  402. 6000,
  403. 6016,
  404. 6067,
  405. 6103,
  406. 6103,
  407. 6108,
  408. 6108,
  409. 6176,
  410. 6263,
  411. 6272,
  412. 6312,
  413. 6314,
  414. 6314,
  415. 6320,
  416. 6389,
  417. 6400,
  418. 6428,
  419. 6480,
  420. 6509,
  421. 6512,
  422. 6516,
  423. 6528,
  424. 6571,
  425. 6593,
  426. 6599,
  427. 6656,
  428. 6678,
  429. 6688,
  430. 6740,
  431. 6823,
  432. 6823,
  433. 6917,
  434. 6963,
  435. 6981,
  436. 6987,
  437. 7043,
  438. 7072,
  439. 7086,
  440. 7087,
  441. 7104,
  442. 7141,
  443. 7168,
  444. 7203,
  445. 7245,
  446. 7247,
  447. 7258,
  448. 7293,
  449. 7401,
  450. 7404,
  451. 7406,
  452. 7409,
  453. 7424,
  454. 7615,
  455. 7680,
  456. 7957,
  457. 7960,
  458. 7965,
  459. 7968,
  460. 8005,
  461. 8008,
  462. 8013,
  463. 8016,
  464. 8023,
  465. 8025,
  466. 8025,
  467. 8027,
  468. 8027,
  469. 8029,
  470. 8029,
  471. 8031,
  472. 8061,
  473. 8064,
  474. 8116,
  475. 8118,
  476. 8124,
  477. 8126,
  478. 8126,
  479. 8130,
  480. 8132,
  481. 8134,
  482. 8140,
  483. 8144,
  484. 8147,
  485. 8150,
  486. 8155,
  487. 8160,
  488. 8172,
  489. 8178,
  490. 8180,
  491. 8182,
  492. 8188,
  493. 8305,
  494. 8305,
  495. 8319,
  496. 8319,
  497. 8336,
  498. 8348,
  499. 8450,
  500. 8450,
  501. 8455,
  502. 8455,
  503. 8458,
  504. 8467,
  505. 8469,
  506. 8469,
  507. 8473,
  508. 8477,
  509. 8484,
  510. 8484,
  511. 8486,
  512. 8486,
  513. 8488,
  514. 8488,
  515. 8490,
  516. 8493,
  517. 8495,
  518. 8505,
  519. 8508,
  520. 8511,
  521. 8517,
  522. 8521,
  523. 8526,
  524. 8526,
  525. 8544,
  526. 8584,
  527. 11264,
  528. 11310,
  529. 11312,
  530. 11358,
  531. 11360,
  532. 11492,
  533. 11499,
  534. 11502,
  535. 11520,
  536. 11557,
  537. 11568,
  538. 11621,
  539. 11631,
  540. 11631,
  541. 11648,
  542. 11670,
  543. 11680,
  544. 11686,
  545. 11688,
  546. 11694,
  547. 11696,
  548. 11702,
  549. 11704,
  550. 11710,
  551. 11712,
  552. 11718,
  553. 11720,
  554. 11726,
  555. 11728,
  556. 11734,
  557. 11736,
  558. 11742,
  559. 11823,
  560. 11823,
  561. 12293,
  562. 12295,
  563. 12321,
  564. 12329,
  565. 12337,
  566. 12341,
  567. 12344,
  568. 12348,
  569. 12353,
  570. 12438,
  571. 12445,
  572. 12447,
  573. 12449,
  574. 12538,
  575. 12540,
  576. 12543,
  577. 12549,
  578. 12589,
  579. 12593,
  580. 12686,
  581. 12704,
  582. 12730,
  583. 12784,
  584. 12799,
  585. 13312,
  586. 13312,
  587. 19893,
  588. 19893,
  589. 19968,
  590. 19968,
  591. 40907,
  592. 40907,
  593. 40960,
  594. 42124,
  595. 42192,
  596. 42237,
  597. 42240,
  598. 42508,
  599. 42512,
  600. 42527,
  601. 42538,
  602. 42539,
  603. 42560,
  604. 42606,
  605. 42623,
  606. 42647,
  607. 42656,
  608. 42735,
  609. 42775,
  610. 42783,
  611. 42786,
  612. 42888,
  613. 42891,
  614. 42894,
  615. 42896,
  616. 42897,
  617. 42912,
  618. 42921,
  619. 43002,
  620. 43009,
  621. 43011,
  622. 43013,
  623. 43015,
  624. 43018,
  625. 43020,
  626. 43042,
  627. 43072,
  628. 43123,
  629. 43138,
  630. 43187,
  631. 43250,
  632. 43255,
  633. 43259,
  634. 43259,
  635. 43274,
  636. 43301,
  637. 43312,
  638. 43334,
  639. 43360,
  640. 43388,
  641. 43396,
  642. 43442,
  643. 43471,
  644. 43471,
  645. 43520,
  646. 43560,
  647. 43584,
  648. 43586,
  649. 43588,
  650. 43595,
  651. 43616,
  652. 43638,
  653. 43642,
  654. 43642,
  655. 43648,
  656. 43695,
  657. 43697,
  658. 43697,
  659. 43701,
  660. 43702,
  661. 43705,
  662. 43709,
  663. 43712,
  664. 43712,
  665. 43714,
  666. 43714,
  667. 43739,
  668. 43741,
  669. 43777,
  670. 43782,
  671. 43785,
  672. 43790,
  673. 43793,
  674. 43798,
  675. 43808,
  676. 43814,
  677. 43816,
  678. 43822,
  679. 43968,
  680. 44002,
  681. 44032,
  682. 44032,
  683. 55203,
  684. 55203,
  685. 55216,
  686. 55238,
  687. 55243,
  688. 55291,
  689. 63744,
  690. 64045,
  691. 64048,
  692. 64109,
  693. 64112,
  694. 64217,
  695. 64256,
  696. 64262,
  697. 64275,
  698. 64279,
  699. 64285,
  700. 64285,
  701. 64287,
  702. 64296,
  703. 64298,
  704. 64310,
  705. 64312,
  706. 64316,
  707. 64318,
  708. 64318,
  709. 64320,
  710. 64321,
  711. 64323,
  712. 64324,
  713. 64326,
  714. 64433,
  715. 64467,
  716. 64829,
  717. 64848,
  718. 64911,
  719. 64914,
  720. 64967,
  721. 65008,
  722. 65019,
  723. 65136,
  724. 65140,
  725. 65142,
  726. 65276,
  727. 65313,
  728. 65338,
  729. 65345,
  730. 65370,
  731. 65382,
  732. 65470,
  733. 65474,
  734. 65479,
  735. 65482,
  736. 65487,
  737. 65490,
  738. 65495,
  739. 65498,
  740. 65500,
  741. 65536,
  742. 65547,
  743. 65549,
  744. 65574,
  745. 65576,
  746. 65594,
  747. 65596,
  748. 65597,
  749. 65599,
  750. 65613,
  751. 65616,
  752. 65629,
  753. 65664,
  754. 65786,
  755. 65856,
  756. 65908,
  757. 66176,
  758. 66204,
  759. 66208,
  760. 66256,
  761. 66304,
  762. 66334,
  763. 66352,
  764. 66378,
  765. 66432,
  766. 66461,
  767. 66464,
  768. 66499,
  769. 66504,
  770. 66511,
  771. 66513,
  772. 66517,
  773. 66560,
  774. 66717,
  775. 67584,
  776. 67589,
  777. 67592,
  778. 67592,
  779. 67594,
  780. 67637,
  781. 67639,
  782. 67640,
  783. 67644,
  784. 67644,
  785. 67647,
  786. 67669,
  787. 67840,
  788. 67861,
  789. 67872,
  790. 67897,
  791. 68096,
  792. 68096,
  793. 68112,
  794. 68115,
  795. 68117,
  796. 68119,
  797. 68121,
  798. 68147,
  799. 68192,
  800. 68220,
  801. 68352,
  802. 68405,
  803. 68416,
  804. 68437,
  805. 68448,
  806. 68466,
  807. 68608,
  808. 68680,
  809. 69635,
  810. 69687,
  811. 69763,
  812. 69807,
  813. 73728,
  814. 74606,
  815. 74752,
  816. 74850,
  817. 77824,
  818. 78894,
  819. 92160,
  820. 92728,
  821. 110592,
  822. 110593,
  823. 119808,
  824. 119892,
  825. 119894,
  826. 119964,
  827. 119966,
  828. 119967,
  829. 119970,
  830. 119970,
  831. 119973,
  832. 119974,
  833. 119977,
  834. 119980,
  835. 119982,
  836. 119993,
  837. 119995,
  838. 119995,
  839. 119997,
  840. 120003,
  841. 120005,
  842. 120069,
  843. 120071,
  844. 120074,
  845. 120077,
  846. 120084,
  847. 120086,
  848. 120092,
  849. 120094,
  850. 120121,
  851. 120123,
  852. 120126,
  853. 120128,
  854. 120132,
  855. 120134,
  856. 120134,
  857. 120138,
  858. 120144,
  859. 120146,
  860. 120485,
  861. 120488,
  862. 120512,
  863. 120514,
  864. 120538,
  865. 120540,
  866. 120570,
  867. 120572,
  868. 120596,
  869. 120598,
  870. 120628,
  871. 120630,
  872. 120654,
  873. 120656,
  874. 120686,
  875. 120688,
  876. 120712,
  877. 120714,
  878. 120744,
  879. 120746,
  880. 120770,
  881. 120772,
  882. 120779,
  883. 131072,
  884. 131072,
  885. 173782,
  886. 173782,
  887. 173824,
  888. 173824,
  889. 177972,
  890. 177972,
  891. 177984,
  892. 177984,
  893. 178205,
  894. 178205,
  895. 194560,
  896. 195101,
  897. ];
  898. const identifierStartTable = [];
  899. for (let i = 0; i < 128; i++) {
  900. identifierStartTable[i] =
  901. (i >= 48 && i <= 57) || // 0-9
  902. i === 36 || // $
  903. i === 126 || // ~
  904. i === 124 || // |
  905. (i >= 65 && i <= 90) || // A-Z
  906. i === 95 || // _
  907. i === 45 || // -
  908. i === 42 || // *
  909. i === 58 || // :
  910. i === 91 || // templateStart [
  911. i === 93 || // templateEnd ]
  912. i === 63 || // ?
  913. i === 37 || // %
  914. i === 35 || // #
  915. i === 61 || // =
  916. (i >= 97 && i <= 122); // a-z
  917. }
  918. const identifierPartTable = identifierStartTable;
  919. export function Lexer(this: any, expression) {
  920. this.input = expression;
  921. this.char = 1;
  922. this.from = 1;
  923. }
  924. Lexer.prototype = {
  925. peek: function(i) {
  926. return this.input.charAt(i || 0);
  927. },
  928. skip: function(i) {
  929. i = i || 1;
  930. this.char += i;
  931. this.input = this.input.slice(i);
  932. },
  933. tokenize: function() {
  934. const list = [];
  935. let token = this.next();
  936. while (token) {
  937. list.push(token);
  938. token = this.next();
  939. }
  940. return list;
  941. },
  942. next: function() {
  943. this.from = this.char;
  944. // Move to the next non-space character.
  945. if (/\s/.test(this.peek())) {
  946. while (/\s/.test(this.peek())) {
  947. this.from += 1;
  948. this.skip();
  949. }
  950. if (this.peek() === '') {
  951. // EOL
  952. return null;
  953. }
  954. }
  955. let match = this.scanStringLiteral();
  956. if (match) {
  957. return match;
  958. }
  959. match = this.scanPunctuator() || this.scanNumericLiteral() || this.scanIdentifier() || this.scanTemplateSequence();
  960. if (match) {
  961. this.skip(match.value.length);
  962. return match;
  963. }
  964. // No token could be matched, give up.
  965. return null;
  966. },
  967. scanTemplateSequence: function() {
  968. if (this.peek() === '[' && this.peek(1) === '[') {
  969. return {
  970. type: 'templateStart',
  971. value: '[[',
  972. pos: this.char,
  973. };
  974. }
  975. if (this.peek() === ']' && this.peek(1) === ']') {
  976. return {
  977. type: 'templateEnd',
  978. value: '[[',
  979. pos: this.char,
  980. };
  981. }
  982. return null;
  983. },
  984. /*
  985. * Extract a JavaScript identifier out of the next sequence of
  986. * characters or return 'null' if its not possible. In addition,
  987. * to Identifier this method can also produce BooleanLiteral
  988. * (true/false) and NullLiteral (null).
  989. */
  990. scanIdentifier: function() {
  991. let id = '';
  992. let index = 0;
  993. let type, char;
  994. // Detects any character in the Unicode categories "Uppercase
  995. // letter (Lu)", "Lowercase letter (Ll)", "Titlecase letter
  996. // (Lt)", "Modifier letter (Lm)", "Other letter (Lo)", or
  997. // "Letter number (Nl)".
  998. //
  999. // Both approach and unicodeLetterTable were borrowed from
  1000. // Google's Traceur.
  1001. function isUnicodeLetter(code) {
  1002. for (let i = 0; i < unicodeLetterTable.length; ) {
  1003. if (code < unicodeLetterTable[i++]) {
  1004. return false;
  1005. }
  1006. if (code <= unicodeLetterTable[i++]) {
  1007. return true;
  1008. }
  1009. }
  1010. return false;
  1011. }
  1012. function isHexDigit(str) {
  1013. return /^[0-9a-fA-F]$/.test(str);
  1014. }
  1015. const readUnicodeEscapeSequence = _.bind(function(this: any) {
  1016. /*jshint validthis:true */
  1017. index += 1;
  1018. if (this.peek(index) !== 'u') {
  1019. return null;
  1020. }
  1021. const ch1 = this.peek(index + 1);
  1022. const ch2 = this.peek(index + 2);
  1023. const ch3 = this.peek(index + 3);
  1024. const ch4 = this.peek(index + 4);
  1025. let code;
  1026. if (isHexDigit(ch1) && isHexDigit(ch2) && isHexDigit(ch3) && isHexDigit(ch4)) {
  1027. code = parseInt(ch1 + ch2 + ch3 + ch4, 16);
  1028. if (isUnicodeLetter(code)) {
  1029. index += 5;
  1030. return '\\u' + ch1 + ch2 + ch3 + ch4;
  1031. }
  1032. return null;
  1033. }
  1034. return null;
  1035. }, this);
  1036. const getIdentifierStart = _.bind(function(this: any) {
  1037. /*jshint validthis:true */
  1038. const chr = this.peek(index);
  1039. const code = chr.charCodeAt(0);
  1040. if (chr === '*') {
  1041. index += 1;
  1042. return chr;
  1043. }
  1044. if (code === 92) {
  1045. return readUnicodeEscapeSequence();
  1046. }
  1047. if (code < 128) {
  1048. if (identifierStartTable[code]) {
  1049. index += 1;
  1050. return chr;
  1051. }
  1052. return null;
  1053. }
  1054. if (isUnicodeLetter(code)) {
  1055. index += 1;
  1056. return chr;
  1057. }
  1058. return null;
  1059. }, this);
  1060. const getIdentifierPart = _.bind(function(this: any) {
  1061. /*jshint validthis:true */
  1062. const chr = this.peek(index);
  1063. const code = chr.charCodeAt(0);
  1064. if (code === 92) {
  1065. return readUnicodeEscapeSequence();
  1066. }
  1067. if (code < 128) {
  1068. if (identifierPartTable[code]) {
  1069. index += 1;
  1070. return chr;
  1071. }
  1072. return null;
  1073. }
  1074. if (isUnicodeLetter(code)) {
  1075. index += 1;
  1076. return chr;
  1077. }
  1078. return null;
  1079. }, this);
  1080. char = getIdentifierStart();
  1081. if (char === null) {
  1082. return null;
  1083. }
  1084. id = char;
  1085. for (;;) {
  1086. char = getIdentifierPart();
  1087. if (char === null) {
  1088. break;
  1089. }
  1090. id += char;
  1091. }
  1092. switch (id) {
  1093. case 'true': {
  1094. type = 'bool';
  1095. break;
  1096. }
  1097. case 'false': {
  1098. type = 'bool';
  1099. break;
  1100. }
  1101. default:
  1102. type = 'identifier';
  1103. }
  1104. return {
  1105. type: type,
  1106. value: id,
  1107. pos: this.char,
  1108. };
  1109. },
  1110. /*
  1111. * Extract a numeric literal out of the next sequence of
  1112. * characters or return 'null' if its not possible. This method
  1113. * supports all numeric literals described in section 7.8.3
  1114. * of the EcmaScript 5 specification.
  1115. *
  1116. * This method's implementation was heavily influenced by the
  1117. * scanNumericLiteral function in the Esprima parser's source code.
  1118. */
  1119. scanNumericLiteral: function(): any {
  1120. let index = 0;
  1121. let value = '';
  1122. const length = this.input.length;
  1123. let char = this.peek(index);
  1124. let bad;
  1125. function isDecimalDigit(str) {
  1126. return /^[0-9]$/.test(str);
  1127. }
  1128. function isOctalDigit(str) {
  1129. return /^[0-7]$/.test(str);
  1130. }
  1131. function isHexDigit(str) {
  1132. return /^[0-9a-fA-F]$/.test(str);
  1133. }
  1134. function isIdentifierStart(ch) {
  1135. return ch === '$' || ch === '_' || ch === '\\' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
  1136. }
  1137. // handle negative num literals
  1138. if (char === '-') {
  1139. value += char;
  1140. index += 1;
  1141. char = this.peek(index);
  1142. }
  1143. // Numbers must start either with a decimal digit or a point.
  1144. if (char !== '.' && !isDecimalDigit(char)) {
  1145. return null;
  1146. }
  1147. if (char !== '.') {
  1148. value += this.peek(index);
  1149. index += 1;
  1150. char = this.peek(index);
  1151. if (value === '0') {
  1152. // Base-16 numbers.
  1153. if (char === 'x' || char === 'X') {
  1154. index += 1;
  1155. value += char;
  1156. while (index < length) {
  1157. char = this.peek(index);
  1158. if (!isHexDigit(char)) {
  1159. break;
  1160. }
  1161. value += char;
  1162. index += 1;
  1163. }
  1164. if (value.length <= 2) {
  1165. // 0x
  1166. return {
  1167. type: 'number',
  1168. value: value,
  1169. isMalformed: true,
  1170. pos: this.char,
  1171. };
  1172. }
  1173. if (index < length) {
  1174. char = this.peek(index);
  1175. if (isIdentifierStart(char)) {
  1176. return null;
  1177. }
  1178. }
  1179. return {
  1180. type: 'number',
  1181. value: value,
  1182. base: 16,
  1183. isMalformed: false,
  1184. pos: this.char,
  1185. };
  1186. }
  1187. // Base-8 numbers.
  1188. if (isOctalDigit(char)) {
  1189. index += 1;
  1190. value += char;
  1191. bad = false;
  1192. while (index < length) {
  1193. char = this.peek(index);
  1194. // Numbers like '019' (note the 9) are not valid octals
  1195. // but we still parse them and mark as malformed.
  1196. if (isDecimalDigit(char)) {
  1197. bad = true;
  1198. }
  1199. if (!isOctalDigit(char)) {
  1200. // if the char is a non punctuator then its not a valid number
  1201. if (!this.isPunctuator(char)) {
  1202. return null;
  1203. }
  1204. break;
  1205. }
  1206. value += char;
  1207. index += 1;
  1208. }
  1209. if (index < length) {
  1210. char = this.peek(index);
  1211. if (isIdentifierStart(char)) {
  1212. return null;
  1213. }
  1214. }
  1215. return {
  1216. type: 'number',
  1217. value: value,
  1218. base: 8,
  1219. isMalformed: bad,
  1220. };
  1221. }
  1222. // Decimal numbers that start with '0' such as '09' are illegal
  1223. // but we still parse them and return as malformed.
  1224. if (isDecimalDigit(char)) {
  1225. index += 1;
  1226. value += char;
  1227. }
  1228. }
  1229. while (index < length) {
  1230. char = this.peek(index);
  1231. if (!isDecimalDigit(char)) {
  1232. break;
  1233. }
  1234. value += char;
  1235. index += 1;
  1236. }
  1237. }
  1238. // Decimal digits.
  1239. if (char === '.') {
  1240. value += char;
  1241. index += 1;
  1242. while (index < length) {
  1243. char = this.peek(index);
  1244. if (!isDecimalDigit(char)) {
  1245. break;
  1246. }
  1247. value += char;
  1248. index += 1;
  1249. }
  1250. }
  1251. // Exponent part.
  1252. if (char === 'e' || char === 'E') {
  1253. value += char;
  1254. index += 1;
  1255. char = this.peek(index);
  1256. if (char === '+' || char === '-') {
  1257. value += this.peek(index);
  1258. index += 1;
  1259. }
  1260. char = this.peek(index);
  1261. if (isDecimalDigit(char)) {
  1262. value += char;
  1263. index += 1;
  1264. while (index < length) {
  1265. char = this.peek(index);
  1266. if (!isDecimalDigit(char)) {
  1267. break;
  1268. }
  1269. value += char;
  1270. index += 1;
  1271. }
  1272. } else {
  1273. return null;
  1274. }
  1275. }
  1276. if (index < length) {
  1277. char = this.peek(index);
  1278. if (!this.isPunctuator(char)) {
  1279. return null;
  1280. }
  1281. }
  1282. return {
  1283. type: 'number',
  1284. value: value,
  1285. base: 10,
  1286. pos: this.char,
  1287. isMalformed: !isFinite(+value),
  1288. };
  1289. },
  1290. isPunctuator: ch1 => {
  1291. switch (ch1) {
  1292. case '.':
  1293. case '(':
  1294. case ')':
  1295. case ',':
  1296. case '{':
  1297. case '}':
  1298. return true;
  1299. }
  1300. return false;
  1301. },
  1302. scanPunctuator: function() {
  1303. const ch1 = this.peek();
  1304. if (this.isPunctuator(ch1)) {
  1305. return {
  1306. type: ch1,
  1307. value: ch1,
  1308. pos: this.char,
  1309. };
  1310. }
  1311. return null;
  1312. },
  1313. /*
  1314. * Extract a string out of the next sequence of characters and/or
  1315. * lines or return 'null' if its not possible. Since strings can
  1316. * span across multiple lines this method has to move the char
  1317. * pointer.
  1318. *
  1319. * This method recognizes pseudo-multiline JavaScript strings:
  1320. *
  1321. * var str = "hello\
  1322. * world";
  1323. */
  1324. scanStringLiteral: function() {
  1325. /*jshint loopfunc:true */
  1326. const quote = this.peek();
  1327. // String must start with a quote.
  1328. if (quote !== '"' && quote !== "'") {
  1329. return null;
  1330. }
  1331. let value = '';
  1332. this.skip();
  1333. while (this.peek() !== quote) {
  1334. if (this.peek() === '') {
  1335. // End Of Line
  1336. return {
  1337. type: 'string',
  1338. value: value,
  1339. isUnclosed: true,
  1340. quote: quote,
  1341. pos: this.char,
  1342. };
  1343. }
  1344. const char = this.peek();
  1345. const jump = 1; // A length of a jump, after we're done
  1346. // parsing this character.
  1347. value += char;
  1348. this.skip(jump);
  1349. }
  1350. this.skip();
  1351. return {
  1352. type: 'string',
  1353. value: value,
  1354. isUnclosed: false,
  1355. quote: quote,
  1356. pos: this.char,
  1357. };
  1358. },
  1359. };