lexer.ts 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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,
  13. 170,
  14. 181,
  15. 181,
  16. 186,
  17. 186,
  18. 192,
  19. 214,
  20. 216,
  21. 246,
  22. 248,
  23. 705,
  24. 710,
  25. 721,
  26. 736,
  27. 740,
  28. 748,
  29. 748,
  30. 750,
  31. 750,
  32. 880,
  33. 884,
  34. 886,
  35. 887,
  36. 890,
  37. 893,
  38. 902,
  39. 902,
  40. 904,
  41. 906,
  42. 908,
  43. 908,
  44. 910,
  45. 929,
  46. 931,
  47. 1013,
  48. 1015,
  49. 1153,
  50. 1162,
  51. 1319,
  52. 1329,
  53. 1366,
  54. 1369,
  55. 1369,
  56. 1377,
  57. 1415,
  58. 1488,
  59. 1514,
  60. 1520,
  61. 1522,
  62. 1568,
  63. 1610,
  64. 1646,
  65. 1647,
  66. 1649,
  67. 1747,
  68. 1749,
  69. 1749,
  70. 1765,
  71. 1766,
  72. 1774,
  73. 1775,
  74. 1786,
  75. 1788,
  76. 1791,
  77. 1791,
  78. 1808,
  79. 1808,
  80. 1810,
  81. 1839,
  82. 1869,
  83. 1957,
  84. 1969,
  85. 1969,
  86. 1994,
  87. 2026,
  88. 2036,
  89. 2037,
  90. 2042,
  91. 2042,
  92. 2048,
  93. 2069,
  94. 2074,
  95. 2074,
  96. 2084,
  97. 2084,
  98. 2088,
  99. 2088,
  100. 2112,
  101. 2136,
  102. 2308,
  103. 2361,
  104. 2365,
  105. 2365,
  106. 2384,
  107. 2384,
  108. 2392,
  109. 2401,
  110. 2417,
  111. 2423,
  112. 2425,
  113. 2431,
  114. 2437,
  115. 2444,
  116. 2447,
  117. 2448,
  118. 2451,
  119. 2472,
  120. 2474,
  121. 2480,
  122. 2482,
  123. 2482,
  124. 2486,
  125. 2489,
  126. 2493,
  127. 2493,
  128. 2510,
  129. 2510,
  130. 2524,
  131. 2525,
  132. 2527,
  133. 2529,
  134. 2544,
  135. 2545,
  136. 2565,
  137. 2570,
  138. 2575,
  139. 2576,
  140. 2579,
  141. 2600,
  142. 2602,
  143. 2608,
  144. 2610,
  145. 2611,
  146. 2613,
  147. 2614,
  148. 2616,
  149. 2617,
  150. 2649,
  151. 2652,
  152. 2654,
  153. 2654,
  154. 2674,
  155. 2676,
  156. 2693,
  157. 2701,
  158. 2703,
  159. 2705,
  160. 2707,
  161. 2728,
  162. 2730,
  163. 2736,
  164. 2738,
  165. 2739,
  166. 2741,
  167. 2745,
  168. 2749,
  169. 2749,
  170. 2768,
  171. 2768,
  172. 2784,
  173. 2785,
  174. 2821,
  175. 2828,
  176. 2831,
  177. 2832,
  178. 2835,
  179. 2856,
  180. 2858,
  181. 2864,
  182. 2866,
  183. 2867,
  184. 2869,
  185. 2873,
  186. 2877,
  187. 2877,
  188. 2908,
  189. 2909,
  190. 2911,
  191. 2913,
  192. 2929,
  193. 2929,
  194. 2947,
  195. 2947,
  196. 2949,
  197. 2954,
  198. 2958,
  199. 2960,
  200. 2962,
  201. 2965,
  202. 2969,
  203. 2970,
  204. 2972,
  205. 2972,
  206. 2974,
  207. 2975,
  208. 2979,
  209. 2980,
  210. 2984,
  211. 2986,
  212. 2990,
  213. 3001,
  214. 3024,
  215. 3024,
  216. 3077,
  217. 3084,
  218. 3086,
  219. 3088,
  220. 3090,
  221. 3112,
  222. 3114,
  223. 3123,
  224. 3125,
  225. 3129,
  226. 3133,
  227. 3133,
  228. 3160,
  229. 3161,
  230. 3168,
  231. 3169,
  232. 3205,
  233. 3212,
  234. 3214,
  235. 3216,
  236. 3218,
  237. 3240,
  238. 3242,
  239. 3251,
  240. 3253,
  241. 3257,
  242. 3261,
  243. 3261,
  244. 3294,
  245. 3294,
  246. 3296,
  247. 3297,
  248. 3313,
  249. 3314,
  250. 3333,
  251. 3340,
  252. 3342,
  253. 3344,
  254. 3346,
  255. 3386,
  256. 3389,
  257. 3389,
  258. 3406,
  259. 3406,
  260. 3424,
  261. 3425,
  262. 3450,
  263. 3455,
  264. 3461,
  265. 3478,
  266. 3482,
  267. 3505,
  268. 3507,
  269. 3515,
  270. 3517,
  271. 3517,
  272. 3520,
  273. 3526,
  274. 3585,
  275. 3632,
  276. 3634,
  277. 3635,
  278. 3648,
  279. 3654,
  280. 3713,
  281. 3714,
  282. 3716,
  283. 3716,
  284. 3719,
  285. 3720,
  286. 3722,
  287. 3722,
  288. 3725,
  289. 3725,
  290. 3732,
  291. 3735,
  292. 3737,
  293. 3743,
  294. 3745,
  295. 3747,
  296. 3749,
  297. 3749,
  298. 3751,
  299. 3751,
  300. 3754,
  301. 3755,
  302. 3757,
  303. 3760,
  304. 3762,
  305. 3763,
  306. 3773,
  307. 3773,
  308. 3776,
  309. 3780,
  310. 3782,
  311. 3782,
  312. 3804,
  313. 3805,
  314. 3840,
  315. 3840,
  316. 3904,
  317. 3911,
  318. 3913,
  319. 3948,
  320. 3976,
  321. 3980,
  322. 4096,
  323. 4138,
  324. 4159,
  325. 4159,
  326. 4176,
  327. 4181,
  328. 4186,
  329. 4189,
  330. 4193,
  331. 4193,
  332. 4197,
  333. 4198,
  334. 4206,
  335. 4208,
  336. 4213,
  337. 4225,
  338. 4238,
  339. 4238,
  340. 4256,
  341. 4293,
  342. 4304,
  343. 4346,
  344. 4348,
  345. 4348,
  346. 4352,
  347. 4680,
  348. 4682,
  349. 4685,
  350. 4688,
  351. 4694,
  352. 4696,
  353. 4696,
  354. 4698,
  355. 4701,
  356. 4704,
  357. 4744,
  358. 4746,
  359. 4749,
  360. 4752,
  361. 4784,
  362. 4786,
  363. 4789,
  364. 4792,
  365. 4798,
  366. 4800,
  367. 4800,
  368. 4802,
  369. 4805,
  370. 4808,
  371. 4822,
  372. 4824,
  373. 4880,
  374. 4882,
  375. 4885,
  376. 4888,
  377. 4954,
  378. 4992,
  379. 5007,
  380. 5024,
  381. 5108,
  382. 5121,
  383. 5740,
  384. 5743,
  385. 5759,
  386. 5761,
  387. 5786,
  388. 5792,
  389. 5866,
  390. 5870,
  391. 5872,
  392. 5888,
  393. 5900,
  394. 5902,
  395. 5905,
  396. 5920,
  397. 5937,
  398. 5952,
  399. 5969,
  400. 5984,
  401. 5996,
  402. 5998,
  403. 6000,
  404. 6016,
  405. 6067,
  406. 6103,
  407. 6103,
  408. 6108,
  409. 6108,
  410. 6176,
  411. 6263,
  412. 6272,
  413. 6312,
  414. 6314,
  415. 6314,
  416. 6320,
  417. 6389,
  418. 6400,
  419. 6428,
  420. 6480,
  421. 6509,
  422. 6512,
  423. 6516,
  424. 6528,
  425. 6571,
  426. 6593,
  427. 6599,
  428. 6656,
  429. 6678,
  430. 6688,
  431. 6740,
  432. 6823,
  433. 6823,
  434. 6917,
  435. 6963,
  436. 6981,
  437. 6987,
  438. 7043,
  439. 7072,
  440. 7086,
  441. 7087,
  442. 7104,
  443. 7141,
  444. 7168,
  445. 7203,
  446. 7245,
  447. 7247,
  448. 7258,
  449. 7293,
  450. 7401,
  451. 7404,
  452. 7406,
  453. 7409,
  454. 7424,
  455. 7615,
  456. 7680,
  457. 7957,
  458. 7960,
  459. 7965,
  460. 7968,
  461. 8005,
  462. 8008,
  463. 8013,
  464. 8016,
  465. 8023,
  466. 8025,
  467. 8025,
  468. 8027,
  469. 8027,
  470. 8029,
  471. 8029,
  472. 8031,
  473. 8061,
  474. 8064,
  475. 8116,
  476. 8118,
  477. 8124,
  478. 8126,
  479. 8126,
  480. 8130,
  481. 8132,
  482. 8134,
  483. 8140,
  484. 8144,
  485. 8147,
  486. 8150,
  487. 8155,
  488. 8160,
  489. 8172,
  490. 8178,
  491. 8180,
  492. 8182,
  493. 8188,
  494. 8305,
  495. 8305,
  496. 8319,
  497. 8319,
  498. 8336,
  499. 8348,
  500. 8450,
  501. 8450,
  502. 8455,
  503. 8455,
  504. 8458,
  505. 8467,
  506. 8469,
  507. 8469,
  508. 8473,
  509. 8477,
  510. 8484,
  511. 8484,
  512. 8486,
  513. 8486,
  514. 8488,
  515. 8488,
  516. 8490,
  517. 8493,
  518. 8495,
  519. 8505,
  520. 8508,
  521. 8511,
  522. 8517,
  523. 8521,
  524. 8526,
  525. 8526,
  526. 8544,
  527. 8584,
  528. 11264,
  529. 11310,
  530. 11312,
  531. 11358,
  532. 11360,
  533. 11492,
  534. 11499,
  535. 11502,
  536. 11520,
  537. 11557,
  538. 11568,
  539. 11621,
  540. 11631,
  541. 11631,
  542. 11648,
  543. 11670,
  544. 11680,
  545. 11686,
  546. 11688,
  547. 11694,
  548. 11696,
  549. 11702,
  550. 11704,
  551. 11710,
  552. 11712,
  553. 11718,
  554. 11720,
  555. 11726,
  556. 11728,
  557. 11734,
  558. 11736,
  559. 11742,
  560. 11823,
  561. 11823,
  562. 12293,
  563. 12295,
  564. 12321,
  565. 12329,
  566. 12337,
  567. 12341,
  568. 12344,
  569. 12348,
  570. 12353,
  571. 12438,
  572. 12445,
  573. 12447,
  574. 12449,
  575. 12538,
  576. 12540,
  577. 12543,
  578. 12549,
  579. 12589,
  580. 12593,
  581. 12686,
  582. 12704,
  583. 12730,
  584. 12784,
  585. 12799,
  586. 13312,
  587. 13312,
  588. 19893,
  589. 19893,
  590. 19968,
  591. 19968,
  592. 40907,
  593. 40907,
  594. 40960,
  595. 42124,
  596. 42192,
  597. 42237,
  598. 42240,
  599. 42508,
  600. 42512,
  601. 42527,
  602. 42538,
  603. 42539,
  604. 42560,
  605. 42606,
  606. 42623,
  607. 42647,
  608. 42656,
  609. 42735,
  610. 42775,
  611. 42783,
  612. 42786,
  613. 42888,
  614. 42891,
  615. 42894,
  616. 42896,
  617. 42897,
  618. 42912,
  619. 42921,
  620. 43002,
  621. 43009,
  622. 43011,
  623. 43013,
  624. 43015,
  625. 43018,
  626. 43020,
  627. 43042,
  628. 43072,
  629. 43123,
  630. 43138,
  631. 43187,
  632. 43250,
  633. 43255,
  634. 43259,
  635. 43259,
  636. 43274,
  637. 43301,
  638. 43312,
  639. 43334,
  640. 43360,
  641. 43388,
  642. 43396,
  643. 43442,
  644. 43471,
  645. 43471,
  646. 43520,
  647. 43560,
  648. 43584,
  649. 43586,
  650. 43588,
  651. 43595,
  652. 43616,
  653. 43638,
  654. 43642,
  655. 43642,
  656. 43648,
  657. 43695,
  658. 43697,
  659. 43697,
  660. 43701,
  661. 43702,
  662. 43705,
  663. 43709,
  664. 43712,
  665. 43712,
  666. 43714,
  667. 43714,
  668. 43739,
  669. 43741,
  670. 43777,
  671. 43782,
  672. 43785,
  673. 43790,
  674. 43793,
  675. 43798,
  676. 43808,
  677. 43814,
  678. 43816,
  679. 43822,
  680. 43968,
  681. 44002,
  682. 44032,
  683. 44032,
  684. 55203,
  685. 55203,
  686. 55216,
  687. 55238,
  688. 55243,
  689. 55291,
  690. 63744,
  691. 64045,
  692. 64048,
  693. 64109,
  694. 64112,
  695. 64217,
  696. 64256,
  697. 64262,
  698. 64275,
  699. 64279,
  700. 64285,
  701. 64285,
  702. 64287,
  703. 64296,
  704. 64298,
  705. 64310,
  706. 64312,
  707. 64316,
  708. 64318,
  709. 64318,
  710. 64320,
  711. 64321,
  712. 64323,
  713. 64324,
  714. 64326,
  715. 64433,
  716. 64467,
  717. 64829,
  718. 64848,
  719. 64911,
  720. 64914,
  721. 64967,
  722. 65008,
  723. 65019,
  724. 65136,
  725. 65140,
  726. 65142,
  727. 65276,
  728. 65313,
  729. 65338,
  730. 65345,
  731. 65370,
  732. 65382,
  733. 65470,
  734. 65474,
  735. 65479,
  736. 65482,
  737. 65487,
  738. 65490,
  739. 65495,
  740. 65498,
  741. 65500,
  742. 65536,
  743. 65547,
  744. 65549,
  745. 65574,
  746. 65576,
  747. 65594,
  748. 65596,
  749. 65597,
  750. 65599,
  751. 65613,
  752. 65616,
  753. 65629,
  754. 65664,
  755. 65786,
  756. 65856,
  757. 65908,
  758. 66176,
  759. 66204,
  760. 66208,
  761. 66256,
  762. 66304,
  763. 66334,
  764. 66352,
  765. 66378,
  766. 66432,
  767. 66461,
  768. 66464,
  769. 66499,
  770. 66504,
  771. 66511,
  772. 66513,
  773. 66517,
  774. 66560,
  775. 66717,
  776. 67584,
  777. 67589,
  778. 67592,
  779. 67592,
  780. 67594,
  781. 67637,
  782. 67639,
  783. 67640,
  784. 67644,
  785. 67644,
  786. 67647,
  787. 67669,
  788. 67840,
  789. 67861,
  790. 67872,
  791. 67897,
  792. 68096,
  793. 68096,
  794. 68112,
  795. 68115,
  796. 68117,
  797. 68119,
  798. 68121,
  799. 68147,
  800. 68192,
  801. 68220,
  802. 68352,
  803. 68405,
  804. 68416,
  805. 68437,
  806. 68448,
  807. 68466,
  808. 68608,
  809. 68680,
  810. 69635,
  811. 69687,
  812. 69763,
  813. 69807,
  814. 73728,
  815. 74606,
  816. 74752,
  817. 74850,
  818. 77824,
  819. 78894,
  820. 92160,
  821. 92728,
  822. 110592,
  823. 110593,
  824. 119808,
  825. 119892,
  826. 119894,
  827. 119964,
  828. 119966,
  829. 119967,
  830. 119970,
  831. 119970,
  832. 119973,
  833. 119974,
  834. 119977,
  835. 119980,
  836. 119982,
  837. 119993,
  838. 119995,
  839. 119995,
  840. 119997,
  841. 120003,
  842. 120005,
  843. 120069,
  844. 120071,
  845. 120074,
  846. 120077,
  847. 120084,
  848. 120086,
  849. 120092,
  850. 120094,
  851. 120121,
  852. 120123,
  853. 120126,
  854. 120128,
  855. 120132,
  856. 120134,
  857. 120134,
  858. 120138,
  859. 120144,
  860. 120146,
  861. 120485,
  862. 120488,
  863. 120512,
  864. 120514,
  865. 120538,
  866. 120540,
  867. 120570,
  868. 120572,
  869. 120596,
  870. 120598,
  871. 120628,
  872. 120630,
  873. 120654,
  874. 120656,
  875. 120686,
  876. 120688,
  877. 120712,
  878. 120714,
  879. 120744,
  880. 120746,
  881. 120770,
  882. 120772,
  883. 120779,
  884. 131072,
  885. 131072,
  886. 173782,
  887. 173782,
  888. 173824,
  889. 173824,
  890. 177972,
  891. 177972,
  892. 177984,
  893. 177984,
  894. 178205,
  895. 178205,
  896. 194560,
  897. 195101,
  898. ];
  899. var identifierStartTable = [];
  900. for (var i = 0; i < 128; i++) {
  901. identifierStartTable[i] =
  902. (i >= 48 && i <= 57) || // 0-9
  903. i === 36 || // $
  904. i === 126 || // ~
  905. i === 124 || // |
  906. (i >= 65 && i <= 90) || // A-Z
  907. i === 95 || // _
  908. i === 45 || // -
  909. i === 42 || // *
  910. i === 58 || // :
  911. i === 91 || // templateStart [
  912. i === 93 || // templateEnd ]
  913. i === 63 || // ?
  914. i === 37 || // %
  915. i === 35 || // #
  916. i === 61 || // =
  917. (i >= 97 && i <= 122); // a-z
  918. }
  919. var identifierPartTable = identifierStartTable;
  920. export function Lexer(expression) {
  921. this.input = expression;
  922. this.char = 1;
  923. this.from = 1;
  924. }
  925. Lexer.prototype = {
  926. peek: function(i) {
  927. return this.input.charAt(i || 0);
  928. },
  929. skip: function(i) {
  930. i = i || 1;
  931. this.char += i;
  932. this.input = this.input.slice(i);
  933. },
  934. tokenize: function() {
  935. var list = [];
  936. var token;
  937. while ((token = this.next())) {
  938. list.push(token);
  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. var 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. var id = '';
  992. var index = 0;
  993. var 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 (var 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. var readUnicodeEscapeSequence = _.bind(function() {
  1016. /*jshint validthis:true */
  1017. index += 1;
  1018. if (this.peek(index) !== 'u') {
  1019. return null;
  1020. }
  1021. var ch1 = this.peek(index + 1);
  1022. var ch2 = this.peek(index + 2);
  1023. var ch3 = this.peek(index + 3);
  1024. var ch4 = this.peek(index + 4);
  1025. var 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. var getIdentifierStart = _.bind(function() {
  1037. /*jshint validthis:true */
  1038. var chr = this.peek(index);
  1039. var 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. var getIdentifierPart = _.bind(function() {
  1061. /*jshint validthis:true */
  1062. var chr = this.peek(index);
  1063. var 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. var index = 0;
  1121. var value = '';
  1122. var length = this.input.length;
  1123. var char = this.peek(index);
  1124. var 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: function(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. var 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. var quote = this.peek();
  1327. // String must start with a quote.
  1328. if (quote !== '"' && quote !== "'") {
  1329. return null;
  1330. }
  1331. var 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. var char = this.peek();
  1345. var 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. };