lexer.ts 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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: any[] = [];
  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 class Lexer {
  920. input: any;
  921. char: number;
  922. from: number;
  923. constructor(expression: any) {
  924. this.input = expression;
  925. this.char = 1;
  926. this.from = 1;
  927. }
  928. peek(i?: number) {
  929. return this.input.charAt(i || 0);
  930. }
  931. skip(i?: number) {
  932. i = i || 1;
  933. this.char += i;
  934. this.input = this.input.slice(i);
  935. }
  936. tokenize() {
  937. const list = [];
  938. let token = this.next();
  939. while (token) {
  940. list.push(token);
  941. token = this.next();
  942. }
  943. return list;
  944. }
  945. next() {
  946. this.from = this.char;
  947. // Move to the next non-space character.
  948. if (/\s/.test(this.peek())) {
  949. while (/\s/.test(this.peek())) {
  950. this.from += 1;
  951. this.skip();
  952. }
  953. if (this.peek() === '') {
  954. // EOL
  955. return null;
  956. }
  957. }
  958. let match = this.scanStringLiteral();
  959. if (match) {
  960. return match;
  961. }
  962. match = this.scanPunctuator() || this.scanNumericLiteral() || this.scanIdentifier() || this.scanTemplateSequence();
  963. if (match) {
  964. this.skip(match.value.length);
  965. return match;
  966. }
  967. // No token could be matched, give up.
  968. return null;
  969. }
  970. scanTemplateSequence() {
  971. if (this.peek() === '[' && this.peek(1) === '[') {
  972. return {
  973. type: 'templateStart',
  974. value: '[[',
  975. pos: this.char,
  976. };
  977. }
  978. if (this.peek() === ']' && this.peek(1) === ']') {
  979. return {
  980. type: 'templateEnd',
  981. value: '[[',
  982. pos: this.char,
  983. };
  984. }
  985. return null;
  986. }
  987. /*
  988. * Extract a JavaScript identifier out of the next sequence of
  989. * characters or return 'null' if its not possible. In addition,
  990. * to Identifier this method can also produce BooleanLiteral
  991. * (true/false) and NullLiteral (null).
  992. */
  993. scanIdentifier() {
  994. let id = '';
  995. let index = 0;
  996. let type, char;
  997. // Detects any character in the Unicode categories "Uppercase
  998. // letter (Lu)", "Lowercase letter (Ll)", "Titlecase letter
  999. // (Lt)", "Modifier letter (Lm)", "Other letter (Lo)", or
  1000. // "Letter number (Nl)".
  1001. //
  1002. // Both approach and unicodeLetterTable were borrowed from
  1003. // Google's Traceur.
  1004. function isUnicodeLetter(code: number) {
  1005. for (let i = 0; i < unicodeLetterTable.length; ) {
  1006. if (code < unicodeLetterTable[i++]) {
  1007. return false;
  1008. }
  1009. if (code <= unicodeLetterTable[i++]) {
  1010. return true;
  1011. }
  1012. }
  1013. return false;
  1014. }
  1015. function isHexDigit(str: string) {
  1016. return /^[0-9a-fA-F]$/.test(str);
  1017. }
  1018. const readUnicodeEscapeSequence = _.bind(function(this: any) {
  1019. /*jshint validthis:true */
  1020. index += 1;
  1021. if (this.peek(index) !== 'u') {
  1022. return null;
  1023. }
  1024. const ch1 = this.peek(index + 1);
  1025. const ch2 = this.peek(index + 2);
  1026. const ch3 = this.peek(index + 3);
  1027. const ch4 = this.peek(index + 4);
  1028. let code;
  1029. if (isHexDigit(ch1) && isHexDigit(ch2) && isHexDigit(ch3) && isHexDigit(ch4)) {
  1030. code = parseInt(ch1 + ch2 + ch3 + ch4, 16);
  1031. if (isUnicodeLetter(code)) {
  1032. index += 5;
  1033. return '\\u' + ch1 + ch2 + ch3 + ch4;
  1034. }
  1035. return null;
  1036. }
  1037. return null;
  1038. }, this);
  1039. const getIdentifierStart = _.bind(function(this: any) {
  1040. /*jshint validthis:true */
  1041. const chr = this.peek(index);
  1042. const code = chr.charCodeAt(0);
  1043. if (chr === '*') {
  1044. index += 1;
  1045. return chr;
  1046. }
  1047. if (code === 92) {
  1048. return readUnicodeEscapeSequence();
  1049. }
  1050. if (code < 128) {
  1051. if (identifierStartTable[code]) {
  1052. index += 1;
  1053. return chr;
  1054. }
  1055. return null;
  1056. }
  1057. if (isUnicodeLetter(code)) {
  1058. index += 1;
  1059. return chr;
  1060. }
  1061. return null;
  1062. }, this);
  1063. const getIdentifierPart = _.bind(function(this: any) {
  1064. /*jshint validthis:true */
  1065. const chr = this.peek(index);
  1066. const code = chr.charCodeAt(0);
  1067. if (code === 92) {
  1068. return readUnicodeEscapeSequence();
  1069. }
  1070. if (code < 128) {
  1071. if (identifierPartTable[code]) {
  1072. index += 1;
  1073. return chr;
  1074. }
  1075. return null;
  1076. }
  1077. if (isUnicodeLetter(code)) {
  1078. index += 1;
  1079. return chr;
  1080. }
  1081. return null;
  1082. }, this);
  1083. char = getIdentifierStart();
  1084. if (char === null) {
  1085. return null;
  1086. }
  1087. id = char;
  1088. for (;;) {
  1089. char = getIdentifierPart();
  1090. if (char === null) {
  1091. break;
  1092. }
  1093. id += char;
  1094. }
  1095. switch (id) {
  1096. case 'true': {
  1097. type = 'bool';
  1098. break;
  1099. }
  1100. case 'false': {
  1101. type = 'bool';
  1102. break;
  1103. }
  1104. default:
  1105. type = 'identifier';
  1106. }
  1107. return {
  1108. type: type,
  1109. value: id,
  1110. pos: this.char,
  1111. };
  1112. }
  1113. /*
  1114. * Extract a numeric literal out of the next sequence of
  1115. * characters or return 'null' if its not possible. This method
  1116. * supports all numeric literals described in section 7.8.3
  1117. * of the EcmaScript 5 specification.
  1118. *
  1119. * This method's implementation was heavily influenced by the
  1120. * scanNumericLiteral function in the Esprima parser's source code.
  1121. */
  1122. scanNumericLiteral(): any {
  1123. let index = 0;
  1124. let value = '';
  1125. const length = this.input.length;
  1126. let char = this.peek(index);
  1127. let bad;
  1128. function isDecimalDigit(str: string) {
  1129. return /^[0-9]$/.test(str);
  1130. }
  1131. function isOctalDigit(str: string) {
  1132. return /^[0-7]$/.test(str);
  1133. }
  1134. function isHexDigit(str: string) {
  1135. return /^[0-9a-fA-F]$/.test(str);
  1136. }
  1137. function isIdentifierStart(ch: string) {
  1138. return ch === '$' || ch === '_' || ch === '\\' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
  1139. }
  1140. // handle negative num literals
  1141. if (char === '-') {
  1142. value += char;
  1143. index += 1;
  1144. char = this.peek(index);
  1145. }
  1146. // Numbers must start either with a decimal digit or a point.
  1147. if (char !== '.' && !isDecimalDigit(char)) {
  1148. return null;
  1149. }
  1150. if (char !== '.') {
  1151. value += this.peek(index);
  1152. index += 1;
  1153. char = this.peek(index);
  1154. if (value === '0') {
  1155. // Base-16 numbers.
  1156. if (char === 'x' || char === 'X') {
  1157. index += 1;
  1158. value += char;
  1159. while (index < length) {
  1160. char = this.peek(index);
  1161. if (!isHexDigit(char)) {
  1162. break;
  1163. }
  1164. value += char;
  1165. index += 1;
  1166. }
  1167. if (value.length <= 2) {
  1168. // 0x
  1169. return {
  1170. type: 'number',
  1171. value: value,
  1172. isMalformed: true,
  1173. pos: this.char,
  1174. };
  1175. }
  1176. if (index < length) {
  1177. char = this.peek(index);
  1178. if (isIdentifierStart(char)) {
  1179. return null;
  1180. }
  1181. }
  1182. return {
  1183. type: 'number',
  1184. value: value,
  1185. base: 16,
  1186. isMalformed: false,
  1187. pos: this.char,
  1188. };
  1189. }
  1190. // Base-8 numbers.
  1191. if (isOctalDigit(char)) {
  1192. index += 1;
  1193. value += char;
  1194. bad = false;
  1195. while (index < length) {
  1196. char = this.peek(index);
  1197. // Numbers like '019' (note the 9) are not valid octals
  1198. // but we still parse them and mark as malformed.
  1199. if (isDecimalDigit(char)) {
  1200. bad = true;
  1201. }
  1202. if (!isOctalDigit(char)) {
  1203. // if the char is a non punctuator then its not a valid number
  1204. if (!this.isPunctuator(char)) {
  1205. return null;
  1206. }
  1207. break;
  1208. }
  1209. value += char;
  1210. index += 1;
  1211. }
  1212. if (index < length) {
  1213. char = this.peek(index);
  1214. if (isIdentifierStart(char)) {
  1215. return null;
  1216. }
  1217. }
  1218. return {
  1219. type: 'number',
  1220. value: value,
  1221. base: 8,
  1222. isMalformed: bad,
  1223. };
  1224. }
  1225. // Decimal numbers that start with '0' such as '09' are illegal
  1226. // but we still parse them and return as malformed.
  1227. if (isDecimalDigit(char)) {
  1228. index += 1;
  1229. value += char;
  1230. }
  1231. }
  1232. while (index < length) {
  1233. char = this.peek(index);
  1234. if (!isDecimalDigit(char)) {
  1235. break;
  1236. }
  1237. value += char;
  1238. index += 1;
  1239. }
  1240. }
  1241. // Decimal digits.
  1242. if (char === '.') {
  1243. value += char;
  1244. index += 1;
  1245. while (index < length) {
  1246. char = this.peek(index);
  1247. if (!isDecimalDigit(char)) {
  1248. break;
  1249. }
  1250. value += char;
  1251. index += 1;
  1252. }
  1253. }
  1254. // Exponent part.
  1255. if (char === 'e' || char === 'E') {
  1256. value += char;
  1257. index += 1;
  1258. char = this.peek(index);
  1259. if (char === '+' || char === '-') {
  1260. value += this.peek(index);
  1261. index += 1;
  1262. }
  1263. char = this.peek(index);
  1264. if (isDecimalDigit(char)) {
  1265. value += char;
  1266. index += 1;
  1267. while (index < length) {
  1268. char = this.peek(index);
  1269. if (!isDecimalDigit(char)) {
  1270. break;
  1271. }
  1272. value += char;
  1273. index += 1;
  1274. }
  1275. } else {
  1276. return null;
  1277. }
  1278. }
  1279. if (index < length) {
  1280. char = this.peek(index);
  1281. if (!this.isPunctuator(char)) {
  1282. return null;
  1283. }
  1284. }
  1285. return {
  1286. type: 'number',
  1287. value: value,
  1288. base: 10,
  1289. pos: this.char,
  1290. isMalformed: !isFinite(+value),
  1291. };
  1292. }
  1293. isPunctuator(ch1: string) {
  1294. switch (ch1) {
  1295. case '.':
  1296. case '(':
  1297. case ')':
  1298. case ',':
  1299. case '{':
  1300. case '}':
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. scanPunctuator() {
  1306. const ch1 = this.peek();
  1307. if (this.isPunctuator(ch1)) {
  1308. return {
  1309. type: ch1,
  1310. value: ch1,
  1311. pos: this.char,
  1312. };
  1313. }
  1314. return null;
  1315. }
  1316. /*
  1317. * Extract a string out of the next sequence of characters and/or
  1318. * lines or return 'null' if its not possible. Since strings can
  1319. * span across multiple lines this method has to move the char
  1320. * pointer.
  1321. *
  1322. * This method recognizes pseudo-multiline JavaScript strings:
  1323. *
  1324. * var str = "hello\
  1325. * world";
  1326. */
  1327. scanStringLiteral() {
  1328. /*jshint loopfunc:true */
  1329. const quote = this.peek();
  1330. // String must start with a quote.
  1331. if (quote !== '"' && quote !== "'") {
  1332. return null;
  1333. }
  1334. let value = '';
  1335. this.skip();
  1336. while (this.peek() !== quote) {
  1337. if (this.peek() === '') {
  1338. // End Of Line
  1339. return {
  1340. type: 'string',
  1341. value: value,
  1342. isUnclosed: true,
  1343. quote: quote,
  1344. pos: this.char,
  1345. };
  1346. }
  1347. const char = this.peek();
  1348. const jump = 1; // A length of a jump, after we're done
  1349. // parsing this character.
  1350. value += char;
  1351. this.skip(jump);
  1352. }
  1353. this.skip();
  1354. return {
  1355. type: 'string',
  1356. value: value,
  1357. isUnclosed: false,
  1358. quote: quote,
  1359. pos: this.char,
  1360. };
  1361. }
  1362. }