lexer.ts 19 KB

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