esprima.js 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740
  1. /*
  2. Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. (function (root, factory) {
  22. 'use strict';
  23. // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
  24. // Rhino, and plain browser loading.
  25. /* istanbul ignore next */
  26. if (typeof define === 'function' && define.amd) {
  27. define(['exports'], factory);
  28. } else if (typeof exports !== 'undefined') {
  29. factory(exports);
  30. } else {
  31. factory((root.esprima = {}));
  32. }
  33. }(this, function (exports) {
  34. 'use strict';
  35. var Token,
  36. TokenName,
  37. FnExprTokens,
  38. Syntax,
  39. PlaceHolders,
  40. Messages,
  41. Regex,
  42. source,
  43. strict,
  44. index,
  45. lineNumber,
  46. lineStart,
  47. hasLineTerminator,
  48. lastIndex,
  49. lastLineNumber,
  50. lastLineStart,
  51. startIndex,
  52. startLineNumber,
  53. startLineStart,
  54. scanning,
  55. length,
  56. lookahead,
  57. state,
  58. extra,
  59. isBindingElement,
  60. isAssignmentTarget,
  61. firstCoverInitializedNameError;
  62. Token = {
  63. BooleanLiteral: 1,
  64. EOF: 2,
  65. Identifier: 3,
  66. Keyword: 4,
  67. NullLiteral: 5,
  68. NumericLiteral: 6,
  69. Punctuator: 7,
  70. StringLiteral: 8,
  71. RegularExpression: 9,
  72. Template: 10
  73. };
  74. TokenName = {};
  75. TokenName[Token.BooleanLiteral] = 'Boolean';
  76. TokenName[Token.EOF] = '<end>';
  77. TokenName[Token.Identifier] = 'Identifier';
  78. TokenName[Token.Keyword] = 'Keyword';
  79. TokenName[Token.NullLiteral] = 'Null';
  80. TokenName[Token.NumericLiteral] = 'Numeric';
  81. TokenName[Token.Punctuator] = 'Punctuator';
  82. TokenName[Token.StringLiteral] = 'String';
  83. TokenName[Token.RegularExpression] = 'RegularExpression';
  84. TokenName[Token.Template] = 'Template';
  85. // A function following one of those tokens is an expression.
  86. FnExprTokens = ['(', '{', '[', 'in', 'typeof', 'instanceof', 'new',
  87. 'return', 'case', 'delete', 'throw', 'void',
  88. // assignment operators
  89. '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=',
  90. '&=', '|=', '^=', ',',
  91. // binary/unary operators
  92. '+', '-', '*', '/', '%', '++', '--', '<<', '>>', '>>>', '&',
  93. '|', '^', '!', '~', '&&', '||', '?', ':', '===', '==', '>=',
  94. '<=', '<', '>', '!=', '!=='];
  95. Syntax = {
  96. AssignmentExpression: 'AssignmentExpression',
  97. AssignmentPattern: 'AssignmentPattern',
  98. ArrayExpression: 'ArrayExpression',
  99. ArrayPattern: 'ArrayPattern',
  100. ArrowFunctionExpression: 'ArrowFunctionExpression',
  101. BlockStatement: 'BlockStatement',
  102. BinaryExpression: 'BinaryExpression',
  103. BreakStatement: 'BreakStatement',
  104. CallExpression: 'CallExpression',
  105. CatchClause: 'CatchClause',
  106. ClassBody: 'ClassBody',
  107. ClassDeclaration: 'ClassDeclaration',
  108. ClassExpression: 'ClassExpression',
  109. ConditionalExpression: 'ConditionalExpression',
  110. ContinueStatement: 'ContinueStatement',
  111. DoWhileStatement: 'DoWhileStatement',
  112. DebuggerStatement: 'DebuggerStatement',
  113. EmptyStatement: 'EmptyStatement',
  114. ExportAllDeclaration: 'ExportAllDeclaration',
  115. ExportDefaultDeclaration: 'ExportDefaultDeclaration',
  116. ExportNamedDeclaration: 'ExportNamedDeclaration',
  117. ExportSpecifier: 'ExportSpecifier',
  118. ExpressionStatement: 'ExpressionStatement',
  119. ForStatement: 'ForStatement',
  120. ForOfStatement: 'ForOfStatement',
  121. ForInStatement: 'ForInStatement',
  122. FunctionDeclaration: 'FunctionDeclaration',
  123. FunctionExpression: 'FunctionExpression',
  124. Identifier: 'Identifier',
  125. IfStatement: 'IfStatement',
  126. ImportDeclaration: 'ImportDeclaration',
  127. ImportDefaultSpecifier: 'ImportDefaultSpecifier',
  128. ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
  129. ImportSpecifier: 'ImportSpecifier',
  130. Literal: 'Literal',
  131. LabeledStatement: 'LabeledStatement',
  132. LogicalExpression: 'LogicalExpression',
  133. MemberExpression: 'MemberExpression',
  134. MetaProperty: 'MetaProperty',
  135. MethodDefinition: 'MethodDefinition',
  136. NewExpression: 'NewExpression',
  137. ObjectExpression: 'ObjectExpression',
  138. ObjectPattern: 'ObjectPattern',
  139. Program: 'Program',
  140. Property: 'Property',
  141. RestElement: 'RestElement',
  142. ReturnStatement: 'ReturnStatement',
  143. SequenceExpression: 'SequenceExpression',
  144. SpreadElement: 'SpreadElement',
  145. Super: 'Super',
  146. SwitchCase: 'SwitchCase',
  147. SwitchStatement: 'SwitchStatement',
  148. TaggedTemplateExpression: 'TaggedTemplateExpression',
  149. TemplateElement: 'TemplateElement',
  150. TemplateLiteral: 'TemplateLiteral',
  151. ThisExpression: 'ThisExpression',
  152. ThrowStatement: 'ThrowStatement',
  153. TryStatement: 'TryStatement',
  154. UnaryExpression: 'UnaryExpression',
  155. UpdateExpression: 'UpdateExpression',
  156. VariableDeclaration: 'VariableDeclaration',
  157. VariableDeclarator: 'VariableDeclarator',
  158. WhileStatement: 'WhileStatement',
  159. WithStatement: 'WithStatement',
  160. YieldExpression: 'YieldExpression'
  161. };
  162. PlaceHolders = {
  163. ArrowParameterPlaceHolder: 'ArrowParameterPlaceHolder'
  164. };
  165. // Error messages should be identical to V8.
  166. Messages = {
  167. UnexpectedToken: 'Unexpected token %0',
  168. UnexpectedNumber: 'Unexpected number',
  169. UnexpectedString: 'Unexpected string',
  170. UnexpectedIdentifier: 'Unexpected identifier',
  171. UnexpectedReserved: 'Unexpected reserved word',
  172. UnexpectedTemplate: 'Unexpected quasi %0',
  173. UnexpectedEOS: 'Unexpected end of input',
  174. NewlineAfterThrow: 'Illegal newline after throw',
  175. InvalidRegExp: 'Invalid regular expression',
  176. UnterminatedRegExp: 'Invalid regular expression: missing /',
  177. InvalidLHSInAssignment: 'Invalid left-hand side in assignment',
  178. InvalidLHSInForIn: 'Invalid left-hand side in for-in',
  179. InvalidLHSInForLoop: 'Invalid left-hand side in for-loop',
  180. MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
  181. NoCatchOrFinally: 'Missing catch or finally after try',
  182. UnknownLabel: 'Undefined label \'%0\'',
  183. Redeclaration: '%0 \'%1\' has already been declared',
  184. IllegalContinue: 'Illegal continue statement',
  185. IllegalBreak: 'Illegal break statement',
  186. IllegalReturn: 'Illegal return statement',
  187. StrictModeWith: 'Strict mode code may not include a with statement',
  188. StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
  189. StrictVarName: 'Variable name may not be eval or arguments in strict mode',
  190. StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
  191. StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
  192. StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
  193. StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
  194. StrictDelete: 'Delete of an unqualified identifier in strict mode.',
  195. StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
  196. StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
  197. StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
  198. StrictReservedWord: 'Use of future reserved word in strict mode',
  199. TemplateOctalLiteral: 'Octal literals are not allowed in template strings.',
  200. ParameterAfterRestParameter: 'Rest parameter must be last formal parameter',
  201. DefaultRestParameter: 'Unexpected token =',
  202. ObjectPatternAsRestParameter: 'Unexpected token {',
  203. DuplicateProtoProperty: 'Duplicate __proto__ fields are not allowed in object literals',
  204. ConstructorSpecialMethod: 'Class constructor may not be an accessor',
  205. DuplicateConstructor: 'A class may only have one constructor',
  206. StaticPrototype: 'Classes may not have static property named prototype',
  207. MissingFromClause: 'Unexpected token',
  208. NoAsAfterImportNamespace: 'Unexpected token',
  209. InvalidModuleSpecifier: 'Unexpected token',
  210. IllegalImportDeclaration: 'Unexpected token',
  211. IllegalExportDeclaration: 'Unexpected token',
  212. DuplicateBinding: 'Duplicate binding %0'
  213. };
  214. // See also tools/generate-unicode-regex.js.
  215. Regex = {
  216. // ECMAScript 6/Unicode v7.0.0 NonAsciiIdentifierStart:
  217. NonAsciiIdentifierStart: /[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0-\u08B2\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309B-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48]|\uD804[\uDC03-\uDC37\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDE00-\uDE11\uDE13-\uDE2B\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF5D-\uDF61]|\uD805[\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDE00-\uDE2F\uDE44\uDE80-\uDEAA]|\uD806[\uDCA0-\uDCDF\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF98]|\uD809[\uDC00-\uDC6E]|[\uD80C\uD840-\uD868\uD86A-\uD86C][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50\uDF93-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD83A[\uDC00-\uDCC4]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D]|\uD87E[\uDC00-\uDE1D]/,
  218. // ECMAScript 6/Unicode v7.0.0 NonAsciiIdentifierPart:
  219. NonAsciiIdentifierPart: /[\xAA\xB5\xB7\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0-\u08B2\u08E4-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58\u0C59\u0C60-\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D01-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D60-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1369-\u1371\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1CF8\u1CF9\u1D00-\u1DF5\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA69D\uA69F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\uABC0-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE2D\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDDFD\uDE80-\uDE9C\uDEA0-\uDED0\uDEE0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF7A\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCA0-\uDCA9\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00-\uDE03\uDE05\uDE06\uDE0C-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE38-\uDE3A\uDE3F\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE6\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48]|\uD804[\uDC00-\uDC46\uDC66-\uDC6F\uDC7F-\uDCBA\uDCD0-\uDCE8\uDCF0-\uDCF9\uDD00-\uDD34\uDD36-\uDD3F\uDD50-\uDD73\uDD76\uDD80-\uDDC4\uDDD0-\uDDDA\uDE00-\uDE11\uDE13-\uDE37\uDEB0-\uDEEA\uDEF0-\uDEF9\uDF01-\uDF03\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3C-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF5D-\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC80-\uDCC5\uDCC7\uDCD0-\uDCD9\uDD80-\uDDB5\uDDB8-\uDDC0\uDE00-\uDE40\uDE44\uDE50-\uDE59\uDE80-\uDEB7\uDEC0-\uDEC9]|\uD806[\uDCA0-\uDCE9\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF98]|\uD809[\uDC00-\uDC6E]|[\uD80C\uD840-\uD868\uD86A-\uD86C][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDED0-\uDEED\uDEF0-\uDEF4\uDF00-\uDF36\uDF40-\uDF43\uDF50-\uDF59\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50-\uDF7E\uDF8F-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD83A[\uDC00-\uDCC4\uDCD0-\uDCD6]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D]|\uD87E[\uDC00-\uDE1D]|\uDB40[\uDD00-\uDDEF]/
  220. };
  221. // Ensure the condition is true, otherwise throw an error.
  222. // This is only to have a better contract semantic, i.e. another safety net
  223. // to catch a logic error. The condition shall be fulfilled in normal case.
  224. // Do NOT use this to enforce a certain condition on any user input.
  225. function assert(condition, message) {
  226. /* istanbul ignore if */
  227. if (!condition) {
  228. throw new Error('ASSERT: ' + message);
  229. }
  230. }
  231. function isDecimalDigit(ch) {
  232. return (ch >= 0x30 && ch <= 0x39); // 0..9
  233. }
  234. function isHexDigit(ch) {
  235. return '0123456789abcdefABCDEF'.indexOf(ch) >= 0;
  236. }
  237. function isOctalDigit(ch) {
  238. return '01234567'.indexOf(ch) >= 0;
  239. }
  240. function octalToDecimal(ch) {
  241. // \0 is not octal escape sequence
  242. var octal = (ch !== '0'), code = '01234567'.indexOf(ch);
  243. if (index < length && isOctalDigit(source[index])) {
  244. octal = true;
  245. code = code * 8 + '01234567'.indexOf(source[index++]);
  246. // 3 digits are only allowed when string starts
  247. // with 0, 1, 2, 3
  248. if ('0123'.indexOf(ch) >= 0 &&
  249. index < length &&
  250. isOctalDigit(source[index])) {
  251. code = code * 8 + '01234567'.indexOf(source[index++]);
  252. }
  253. }
  254. return {
  255. code: code,
  256. octal: octal
  257. };
  258. }
  259. // ECMA-262 11.2 White Space
  260. function isWhiteSpace(ch) {
  261. return (ch === 0x20) || (ch === 0x09) || (ch === 0x0B) || (ch === 0x0C) || (ch === 0xA0) ||
  262. (ch >= 0x1680 && [0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x202F, 0x205F, 0x3000, 0xFEFF].indexOf(ch) >= 0);
  263. }
  264. // ECMA-262 11.3 Line Terminators
  265. function isLineTerminator(ch) {
  266. return (ch === 0x0A) || (ch === 0x0D) || (ch === 0x2028) || (ch === 0x2029);
  267. }
  268. // ECMA-262 11.6 Identifier Names and Identifiers
  269. function fromCodePoint(cp) {
  270. return (cp < 0x10000) ? String.fromCharCode(cp) :
  271. String.fromCharCode(0xD800 + ((cp - 0x10000) >> 10)) +
  272. String.fromCharCode(0xDC00 + ((cp - 0x10000) & 1023));
  273. }
  274. function isIdentifierStart(ch) {
  275. return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore)
  276. (ch >= 0x41 && ch <= 0x5A) || // A..Z
  277. (ch >= 0x61 && ch <= 0x7A) || // a..z
  278. (ch === 0x5C) || // \ (backslash)
  279. ((ch >= 0x80) && Regex.NonAsciiIdentifierStart.test(fromCodePoint(ch)));
  280. }
  281. function isIdentifierPart(ch) {
  282. return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore)
  283. (ch >= 0x41 && ch <= 0x5A) || // A..Z
  284. (ch >= 0x61 && ch <= 0x7A) || // a..z
  285. (ch >= 0x30 && ch <= 0x39) || // 0..9
  286. (ch === 0x5C) || // \ (backslash)
  287. ((ch >= 0x80) && Regex.NonAsciiIdentifierPart.test(fromCodePoint(ch)));
  288. }
  289. // ECMA-262 11.6.2.2 Future Reserved Words
  290. function isFutureReservedWord(id) {
  291. switch (id) {
  292. case 'enum':
  293. case 'export':
  294. case 'import':
  295. case 'super':
  296. return true;
  297. default:
  298. return false;
  299. }
  300. }
  301. function isStrictModeReservedWord(id) {
  302. switch (id) {
  303. case 'implements':
  304. case 'interface':
  305. case 'package':
  306. case 'private':
  307. case 'protected':
  308. case 'public':
  309. case 'static':
  310. case 'yield':
  311. case 'let':
  312. return true;
  313. default:
  314. return false;
  315. }
  316. }
  317. function isRestrictedWord(id) {
  318. return id === 'eval' || id === 'arguments';
  319. }
  320. // ECMA-262 11.6.2.1 Keywords
  321. function isKeyword(id) {
  322. switch (id.length) {
  323. case 2:
  324. return (id === 'if') || (id === 'in') || (id === 'do');
  325. case 3:
  326. return (id === 'var') || (id === 'for') || (id === 'new') ||
  327. (id === 'try') || (id === 'let');
  328. case 4:
  329. return (id === 'this') || (id === 'else') || (id === 'case') ||
  330. (id === 'void') || (id === 'with') || (id === 'enum');
  331. case 5:
  332. return (id === 'while') || (id === 'break') || (id === 'catch') ||
  333. (id === 'throw') || (id === 'const') || (id === 'yield') ||
  334. (id === 'class') || (id === 'super');
  335. case 6:
  336. return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
  337. (id === 'switch') || (id === 'export') || (id === 'import');
  338. case 7:
  339. return (id === 'default') || (id === 'finally') || (id === 'extends');
  340. case 8:
  341. return (id === 'function') || (id === 'continue') || (id === 'debugger');
  342. case 10:
  343. return (id === 'instanceof');
  344. default:
  345. return false;
  346. }
  347. }
  348. // ECMA-262 11.4 Comments
  349. function addComment(type, value, start, end, loc) {
  350. var comment;
  351. assert(typeof start === 'number', 'Comment must have valid position');
  352. state.lastCommentStart = start;
  353. comment = {
  354. type: type,
  355. value: value
  356. };
  357. if (extra.range) {
  358. comment.range = [start, end];
  359. }
  360. if (extra.loc) {
  361. comment.loc = loc;
  362. }
  363. extra.comments.push(comment);
  364. if (extra.attachComment) {
  365. extra.leadingComments.push(comment);
  366. extra.trailingComments.push(comment);
  367. }
  368. if (extra.tokenize) {
  369. comment.type = comment.type + 'Comment';
  370. if (extra.delegate) {
  371. comment = extra.delegate(comment);
  372. }
  373. extra.tokens.push(comment);
  374. }
  375. }
  376. function skipSingleLineComment(offset) {
  377. var start, loc, ch, comment;
  378. start = index - offset;
  379. loc = {
  380. start: {
  381. line: lineNumber,
  382. column: index - lineStart - offset
  383. }
  384. };
  385. while (index < length) {
  386. ch = source.charCodeAt(index);
  387. ++index;
  388. if (isLineTerminator(ch)) {
  389. hasLineTerminator = true;
  390. if (extra.comments) {
  391. comment = source.slice(start + offset, index - 1);
  392. loc.end = {
  393. line: lineNumber,
  394. column: index - lineStart - 1
  395. };
  396. addComment('Line', comment, start, index - 1, loc);
  397. }
  398. if (ch === 13 && source.charCodeAt(index) === 10) {
  399. ++index;
  400. }
  401. ++lineNumber;
  402. lineStart = index;
  403. return;
  404. }
  405. }
  406. if (extra.comments) {
  407. comment = source.slice(start + offset, index);
  408. loc.end = {
  409. line: lineNumber,
  410. column: index - lineStart
  411. };
  412. addComment('Line', comment, start, index, loc);
  413. }
  414. }
  415. function skipMultiLineComment() {
  416. var start, loc, ch, comment;
  417. if (extra.comments) {
  418. start = index - 2;
  419. loc = {
  420. start: {
  421. line: lineNumber,
  422. column: index - lineStart - 2
  423. }
  424. };
  425. }
  426. while (index < length) {
  427. ch = source.charCodeAt(index);
  428. if (isLineTerminator(ch)) {
  429. if (ch === 0x0D && source.charCodeAt(index + 1) === 0x0A) {
  430. ++index;
  431. }
  432. hasLineTerminator = true;
  433. ++lineNumber;
  434. ++index;
  435. lineStart = index;
  436. } else if (ch === 0x2A) {
  437. // Block comment ends with '*/'.
  438. if (source.charCodeAt(index + 1) === 0x2F) {
  439. ++index;
  440. ++index;
  441. if (extra.comments) {
  442. comment = source.slice(start + 2, index - 2);
  443. loc.end = {
  444. line: lineNumber,
  445. column: index - lineStart
  446. };
  447. addComment('Block', comment, start, index, loc);
  448. }
  449. return;
  450. }
  451. ++index;
  452. } else {
  453. ++index;
  454. }
  455. }
  456. // Ran off the end of the file - the whole thing is a comment
  457. if (extra.comments) {
  458. loc.end = {
  459. line: lineNumber,
  460. column: index - lineStart
  461. };
  462. comment = source.slice(start + 2, index);
  463. addComment('Block', comment, start, index, loc);
  464. }
  465. tolerateUnexpectedToken();
  466. }
  467. function skipComment() {
  468. var ch, start;
  469. hasLineTerminator = false;
  470. start = (index === 0);
  471. while (index < length) {
  472. ch = source.charCodeAt(index);
  473. if (isWhiteSpace(ch)) {
  474. ++index;
  475. } else if (isLineTerminator(ch)) {
  476. hasLineTerminator = true;
  477. ++index;
  478. if (ch === 0x0D && source.charCodeAt(index) === 0x0A) {
  479. ++index;
  480. }
  481. ++lineNumber;
  482. lineStart = index;
  483. start = true;
  484. } else if (ch === 0x2F) { // U+002F is '/'
  485. ch = source.charCodeAt(index + 1);
  486. if (ch === 0x2F) {
  487. ++index;
  488. ++index;
  489. skipSingleLineComment(2);
  490. start = true;
  491. } else if (ch === 0x2A) { // U+002A is '*'
  492. ++index;
  493. ++index;
  494. skipMultiLineComment();
  495. } else {
  496. break;
  497. }
  498. } else if (start && ch === 0x2D) { // U+002D is '-'
  499. // U+003E is '>'
  500. if ((source.charCodeAt(index + 1) === 0x2D) && (source.charCodeAt(index + 2) === 0x3E)) {
  501. // '-->' is a single-line comment
  502. index += 3;
  503. skipSingleLineComment(3);
  504. } else {
  505. break;
  506. }
  507. } else if (ch === 0x3C) { // U+003C is '<'
  508. if (source.slice(index + 1, index + 4) === '!--') {
  509. ++index; // `<`
  510. ++index; // `!`
  511. ++index; // `-`
  512. ++index; // `-`
  513. skipSingleLineComment(4);
  514. } else {
  515. break;
  516. }
  517. } else {
  518. break;
  519. }
  520. }
  521. }
  522. function scanHexEscape(prefix) {
  523. var i, len, ch, code = 0;
  524. len = (prefix === 'u') ? 4 : 2;
  525. for (i = 0; i < len; ++i) {
  526. if (index < length && isHexDigit(source[index])) {
  527. ch = source[index++];
  528. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  529. } else {
  530. return '';
  531. }
  532. }
  533. return String.fromCharCode(code);
  534. }
  535. function scanUnicodeCodePointEscape() {
  536. var ch, code;
  537. ch = source[index];
  538. code = 0;
  539. // At least, one hex digit is required.
  540. if (ch === '}') {
  541. throwUnexpectedToken();
  542. }
  543. while (index < length) {
  544. ch = source[index++];
  545. if (!isHexDigit(ch)) {
  546. break;
  547. }
  548. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  549. }
  550. if (code > 0x10FFFF || ch !== '}') {
  551. throwUnexpectedToken();
  552. }
  553. return fromCodePoint(code);
  554. }
  555. function codePointAt(i) {
  556. var cp, first, second;
  557. cp = source.charCodeAt(i);
  558. if (cp >= 0xD800 && cp <= 0xDBFF) {
  559. second = source.charCodeAt(i + 1);
  560. if (second >= 0xDC00 && second <= 0xDFFF) {
  561. first = cp;
  562. cp = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  563. }
  564. }
  565. return cp;
  566. }
  567. function getComplexIdentifier() {
  568. var cp, ch, id;
  569. cp = codePointAt(index);
  570. id = fromCodePoint(cp);
  571. index += id.length;
  572. // '\u' (U+005C, U+0075) denotes an escaped character.
  573. if (cp === 0x5C) {
  574. if (source.charCodeAt(index) !== 0x75) {
  575. throwUnexpectedToken();
  576. }
  577. ++index;
  578. if (source[index] === '{') {
  579. ++index;
  580. ch = scanUnicodeCodePointEscape();
  581. } else {
  582. ch = scanHexEscape('u');
  583. cp = ch.charCodeAt(0);
  584. if (!ch || ch === '\\' || !isIdentifierStart(cp)) {
  585. throwUnexpectedToken();
  586. }
  587. }
  588. id = ch;
  589. }
  590. while (index < length) {
  591. cp = codePointAt(index);
  592. if (!isIdentifierPart(cp)) {
  593. break;
  594. }
  595. ch = fromCodePoint(cp);
  596. id += ch;
  597. index += ch.length;
  598. // '\u' (U+005C, U+0075) denotes an escaped character.
  599. if (cp === 0x5C) {
  600. id = id.substr(0, id.length - 1);
  601. if (source.charCodeAt(index) !== 0x75) {
  602. throwUnexpectedToken();
  603. }
  604. ++index;
  605. if (source[index] === '{') {
  606. ++index;
  607. ch = scanUnicodeCodePointEscape();
  608. } else {
  609. ch = scanHexEscape('u');
  610. cp = ch.charCodeAt(0);
  611. if (!ch || ch === '\\' || !isIdentifierPart(cp)) {
  612. throwUnexpectedToken();
  613. }
  614. }
  615. id += ch;
  616. }
  617. }
  618. return id;
  619. }
  620. function getIdentifier() {
  621. var start, ch;
  622. start = index++;
  623. while (index < length) {
  624. ch = source.charCodeAt(index);
  625. if (ch === 0x5C) {
  626. // Blackslash (U+005C) marks Unicode escape sequence.
  627. index = start;
  628. return getComplexIdentifier();
  629. } else if (ch >= 0xD800 && ch < 0xDFFF) {
  630. // Need to handle surrogate pairs.
  631. index = start;
  632. return getComplexIdentifier();
  633. }
  634. if (isIdentifierPart(ch)) {
  635. ++index;
  636. } else {
  637. break;
  638. }
  639. }
  640. return source.slice(start, index);
  641. }
  642. function scanIdentifier() {
  643. var start, id, type;
  644. start = index;
  645. // Backslash (U+005C) starts an escaped character.
  646. id = (source.charCodeAt(index) === 0x5C) ? getComplexIdentifier() : getIdentifier();
  647. // There is no keyword or literal with only one character.
  648. // Thus, it must be an identifier.
  649. if (id.length === 1) {
  650. type = Token.Identifier;
  651. } else if (isKeyword(id)) {
  652. type = Token.Keyword;
  653. } else if (id === 'null') {
  654. type = Token.NullLiteral;
  655. } else if (id === 'true' || id === 'false') {
  656. type = Token.BooleanLiteral;
  657. } else {
  658. type = Token.Identifier;
  659. }
  660. return {
  661. type: type,
  662. value: id,
  663. lineNumber: lineNumber,
  664. lineStart: lineStart,
  665. start: start,
  666. end: index
  667. };
  668. }
  669. // ECMA-262 11.7 Punctuators
  670. function scanPunctuator() {
  671. var token, str;
  672. token = {
  673. type: Token.Punctuator,
  674. value: '',
  675. lineNumber: lineNumber,
  676. lineStart: lineStart,
  677. start: index,
  678. end: index
  679. };
  680. // Check for most common single-character punctuators.
  681. str = source[index];
  682. switch (str) {
  683. case '(':
  684. if (extra.tokenize) {
  685. extra.openParenToken = extra.tokenValues.length;
  686. }
  687. ++index;
  688. break;
  689. case '{':
  690. if (extra.tokenize) {
  691. extra.openCurlyToken = extra.tokenValues.length;
  692. }
  693. state.curlyStack.push('{');
  694. ++index;
  695. break;
  696. case '.':
  697. ++index;
  698. if (source[index] === '.' && source[index + 1] === '.') {
  699. // Spread operator: ...
  700. index += 2;
  701. str = '...';
  702. }
  703. break;
  704. case '}':
  705. ++index;
  706. state.curlyStack.pop();
  707. break;
  708. case ')':
  709. case ';':
  710. case ',':
  711. case '[':
  712. case ']':
  713. case ':':
  714. case '?':
  715. case '~':
  716. ++index;
  717. break;
  718. default:
  719. // 4-character punctuator.
  720. str = source.substr(index, 4);
  721. if (str === '>>>=') {
  722. index += 4;
  723. } else {
  724. // 3-character punctuators.
  725. str = str.substr(0, 3);
  726. if (str === '===' || str === '!==' || str === '>>>' ||
  727. str === '<<=' || str === '>>=') {
  728. index += 3;
  729. } else {
  730. // 2-character punctuators.
  731. str = str.substr(0, 2);
  732. if (str === '&&' || str === '||' || str === '==' || str === '!=' ||
  733. str === '+=' || str === '-=' || str === '*=' || str === '/=' ||
  734. str === '++' || str === '--' || str === '<<' || str === '>>' ||
  735. str === '&=' || str === '|=' || str === '^=' || str === '%=' ||
  736. str === '<=' || str === '>=' || str === '=>') {
  737. index += 2;
  738. } else {
  739. // 1-character punctuators.
  740. str = source[index];
  741. if ('<>=!+-*%&|^/'.indexOf(str) >= 0) {
  742. ++index;
  743. }
  744. }
  745. }
  746. }
  747. }
  748. if (index === token.start) {
  749. throwUnexpectedToken();
  750. }
  751. token.end = index;
  752. token.value = str;
  753. return token;
  754. }
  755. // ECMA-262 11.8.3 Numeric Literals
  756. function scanHexLiteral(start) {
  757. var number = '';
  758. while (index < length) {
  759. if (!isHexDigit(source[index])) {
  760. break;
  761. }
  762. number += source[index++];
  763. }
  764. if (number.length === 0) {
  765. throwUnexpectedToken();
  766. }
  767. if (isIdentifierStart(source.charCodeAt(index))) {
  768. throwUnexpectedToken();
  769. }
  770. return {
  771. type: Token.NumericLiteral,
  772. value: parseInt('0x' + number, 16),
  773. lineNumber: lineNumber,
  774. lineStart: lineStart,
  775. start: start,
  776. end: index
  777. };
  778. }
  779. function scanBinaryLiteral(start) {
  780. var ch, number;
  781. number = '';
  782. while (index < length) {
  783. ch = source[index];
  784. if (ch !== '0' && ch !== '1') {
  785. break;
  786. }
  787. number += source[index++];
  788. }
  789. if (number.length === 0) {
  790. // only 0b or 0B
  791. throwUnexpectedToken();
  792. }
  793. if (index < length) {
  794. ch = source.charCodeAt(index);
  795. /* istanbul ignore else */
  796. if (isIdentifierStart(ch) || isDecimalDigit(ch)) {
  797. throwUnexpectedToken();
  798. }
  799. }
  800. return {
  801. type: Token.NumericLiteral,
  802. value: parseInt(number, 2),
  803. lineNumber: lineNumber,
  804. lineStart: lineStart,
  805. start: start,
  806. end: index
  807. };
  808. }
  809. function scanOctalLiteral(prefix, start) {
  810. var number, octal;
  811. if (isOctalDigit(prefix)) {
  812. octal = true;
  813. number = '0' + source[index++];
  814. } else {
  815. octal = false;
  816. ++index;
  817. number = '';
  818. }
  819. while (index < length) {
  820. if (!isOctalDigit(source[index])) {
  821. break;
  822. }
  823. number += source[index++];
  824. }
  825. if (!octal && number.length === 0) {
  826. // only 0o or 0O
  827. throwUnexpectedToken();
  828. }
  829. if (isIdentifierStart(source.charCodeAt(index)) || isDecimalDigit(source.charCodeAt(index))) {
  830. throwUnexpectedToken();
  831. }
  832. return {
  833. type: Token.NumericLiteral,
  834. value: parseInt(number, 8),
  835. octal: octal,
  836. lineNumber: lineNumber,
  837. lineStart: lineStart,
  838. start: start,
  839. end: index
  840. };
  841. }
  842. function isImplicitOctalLiteral() {
  843. var i, ch;
  844. // Implicit octal, unless there is a non-octal digit.
  845. // (Annex B.1.1 on Numeric Literals)
  846. for (i = index + 1; i < length; ++i) {
  847. ch = source[i];
  848. if (ch === '8' || ch === '9') {
  849. return false;
  850. }
  851. if (!isOctalDigit(ch)) {
  852. return true;
  853. }
  854. }
  855. return true;
  856. }
  857. function scanNumericLiteral() {
  858. var number, start, ch;
  859. ch = source[index];
  860. assert(isDecimalDigit(ch.charCodeAt(0)) || (ch === '.'),
  861. 'Numeric literal must start with a decimal digit or a decimal point');
  862. start = index;
  863. number = '';
  864. if (ch !== '.') {
  865. number = source[index++];
  866. ch = source[index];
  867. // Hex number starts with '0x'.
  868. // Octal number starts with '0'.
  869. // Octal number in ES6 starts with '0o'.
  870. // Binary number in ES6 starts with '0b'.
  871. if (number === '0') {
  872. if (ch === 'x' || ch === 'X') {
  873. ++index;
  874. return scanHexLiteral(start);
  875. }
  876. if (ch === 'b' || ch === 'B') {
  877. ++index;
  878. return scanBinaryLiteral(start);
  879. }
  880. if (ch === 'o' || ch === 'O') {
  881. return scanOctalLiteral(ch, start);
  882. }
  883. if (isOctalDigit(ch)) {
  884. if (isImplicitOctalLiteral()) {
  885. return scanOctalLiteral(ch, start);
  886. }
  887. }
  888. }
  889. while (isDecimalDigit(source.charCodeAt(index))) {
  890. number += source[index++];
  891. }
  892. ch = source[index];
  893. }
  894. if (ch === '.') {
  895. number += source[index++];
  896. while (isDecimalDigit(source.charCodeAt(index))) {
  897. number += source[index++];
  898. }
  899. ch = source[index];
  900. }
  901. if (ch === 'e' || ch === 'E') {
  902. number += source[index++];
  903. ch = source[index];
  904. if (ch === '+' || ch === '-') {
  905. number += source[index++];
  906. }
  907. if (isDecimalDigit(source.charCodeAt(index))) {
  908. while (isDecimalDigit(source.charCodeAt(index))) {
  909. number += source[index++];
  910. }
  911. } else {
  912. throwUnexpectedToken();
  913. }
  914. }
  915. if (isIdentifierStart(source.charCodeAt(index))) {
  916. throwUnexpectedToken();
  917. }
  918. return {
  919. type: Token.NumericLiteral,
  920. value: parseFloat(number),
  921. lineNumber: lineNumber,
  922. lineStart: lineStart,
  923. start: start,
  924. end: index
  925. };
  926. }
  927. // ECMA-262 11.8.4 String Literals
  928. function scanStringLiteral() {
  929. var str = '', quote, start, ch, unescaped, octToDec, octal = false;
  930. quote = source[index];
  931. assert((quote === '\'' || quote === '"'),
  932. 'String literal must starts with a quote');
  933. start = index;
  934. ++index;
  935. while (index < length) {
  936. ch = source[index++];
  937. if (ch === quote) {
  938. quote = '';
  939. break;
  940. } else if (ch === '\\') {
  941. ch = source[index++];
  942. if (!ch || !isLineTerminator(ch.charCodeAt(0))) {
  943. switch (ch) {
  944. case 'u':
  945. case 'x':
  946. if (source[index] === '{') {
  947. ++index;
  948. str += scanUnicodeCodePointEscape();
  949. } else {
  950. unescaped = scanHexEscape(ch);
  951. if (!unescaped) {
  952. throw throwUnexpectedToken();
  953. }
  954. str += unescaped;
  955. }
  956. break;
  957. case 'n':
  958. str += '\n';
  959. break;
  960. case 'r':
  961. str += '\r';
  962. break;
  963. case 't':
  964. str += '\t';
  965. break;
  966. case 'b':
  967. str += '\b';
  968. break;
  969. case 'f':
  970. str += '\f';
  971. break;
  972. case 'v':
  973. str += '\x0B';
  974. break;
  975. case '8':
  976. case '9':
  977. str += ch;
  978. tolerateUnexpectedToken();
  979. break;
  980. default:
  981. if (isOctalDigit(ch)) {
  982. octToDec = octalToDecimal(ch);
  983. octal = octToDec.octal || octal;
  984. str += String.fromCharCode(octToDec.code);
  985. } else {
  986. str += ch;
  987. }
  988. break;
  989. }
  990. } else {
  991. ++lineNumber;
  992. if (ch === '\r' && source[index] === '\n') {
  993. ++index;
  994. }
  995. lineStart = index;
  996. }
  997. } else if (isLineTerminator(ch.charCodeAt(0))) {
  998. break;
  999. } else {
  1000. str += ch;
  1001. }
  1002. }
  1003. if (quote !== '') {
  1004. index = start;
  1005. throwUnexpectedToken();
  1006. }
  1007. return {
  1008. type: Token.StringLiteral,
  1009. value: str,
  1010. octal: octal,
  1011. lineNumber: startLineNumber,
  1012. lineStart: startLineStart,
  1013. start: start,
  1014. end: index
  1015. };
  1016. }
  1017. // ECMA-262 11.8.6 Template Literal Lexical Components
  1018. function scanTemplate() {
  1019. var cooked = '', ch, start, rawOffset, terminated, head, tail, restore, unescaped;
  1020. terminated = false;
  1021. tail = false;
  1022. start = index;
  1023. head = (source[index] === '`');
  1024. rawOffset = 2;
  1025. ++index;
  1026. while (index < length) {
  1027. ch = source[index++];
  1028. if (ch === '`') {
  1029. rawOffset = 1;
  1030. tail = true;
  1031. terminated = true;
  1032. break;
  1033. } else if (ch === '$') {
  1034. if (source[index] === '{') {
  1035. state.curlyStack.push('${');
  1036. ++index;
  1037. terminated = true;
  1038. break;
  1039. }
  1040. cooked += ch;
  1041. } else if (ch === '\\') {
  1042. ch = source[index++];
  1043. if (!isLineTerminator(ch.charCodeAt(0))) {
  1044. switch (ch) {
  1045. case 'n':
  1046. cooked += '\n';
  1047. break;
  1048. case 'r':
  1049. cooked += '\r';
  1050. break;
  1051. case 't':
  1052. cooked += '\t';
  1053. break;
  1054. case 'u':
  1055. case 'x':
  1056. if (source[index] === '{') {
  1057. ++index;
  1058. cooked += scanUnicodeCodePointEscape();
  1059. } else {
  1060. restore = index;
  1061. unescaped = scanHexEscape(ch);
  1062. if (unescaped) {
  1063. cooked += unescaped;
  1064. } else {
  1065. index = restore;
  1066. cooked += ch;
  1067. }
  1068. }
  1069. break;
  1070. case 'b':
  1071. cooked += '\b';
  1072. break;
  1073. case 'f':
  1074. cooked += '\f';
  1075. break;
  1076. case 'v':
  1077. cooked += '\v';
  1078. break;
  1079. default:
  1080. if (ch === '0') {
  1081. if (isDecimalDigit(source.charCodeAt(index))) {
  1082. // Illegal: \01 \02 and so on
  1083. throwError(Messages.TemplateOctalLiteral);
  1084. }
  1085. cooked += '\0';
  1086. } else if (isOctalDigit(ch)) {
  1087. // Illegal: \1 \2
  1088. throwError(Messages.TemplateOctalLiteral);
  1089. } else {
  1090. cooked += ch;
  1091. }
  1092. break;
  1093. }
  1094. } else {
  1095. ++lineNumber;
  1096. if (ch === '\r' && source[index] === '\n') {
  1097. ++index;
  1098. }
  1099. lineStart = index;
  1100. }
  1101. } else if (isLineTerminator(ch.charCodeAt(0))) {
  1102. ++lineNumber;
  1103. if (ch === '\r' && source[index] === '\n') {
  1104. ++index;
  1105. }
  1106. lineStart = index;
  1107. cooked += '\n';
  1108. } else {
  1109. cooked += ch;
  1110. }
  1111. }
  1112. if (!terminated) {
  1113. throwUnexpectedToken();
  1114. }
  1115. if (!head) {
  1116. state.curlyStack.pop();
  1117. }
  1118. return {
  1119. type: Token.Template,
  1120. value: {
  1121. cooked: cooked,
  1122. raw: source.slice(start + 1, index - rawOffset)
  1123. },
  1124. head: head,
  1125. tail: tail,
  1126. lineNumber: lineNumber,
  1127. lineStart: lineStart,
  1128. start: start,
  1129. end: index
  1130. };
  1131. }
  1132. // ECMA-262 11.8.5 Regular Expression Literals
  1133. function testRegExp(pattern, flags) {
  1134. // The BMP character to use as a replacement for astral symbols when
  1135. // translating an ES6 "u"-flagged pattern to an ES5-compatible
  1136. // approximation.
  1137. // Note: replacing with '\uFFFF' enables false positives in unlikely
  1138. // scenarios. For example, `[\u{1044f}-\u{10440}]` is an invalid
  1139. // pattern that would not be detected by this substitution.
  1140. var astralSubstitute = '\uFFFF',
  1141. tmp = pattern;
  1142. if (flags.indexOf('u') >= 0) {
  1143. tmp = tmp
  1144. // Replace every Unicode escape sequence with the equivalent
  1145. // BMP character or a constant ASCII code point in the case of
  1146. // astral symbols. (See the above note on `astralSubstitute`
  1147. // for more information.)
  1148. .replace(/\\u\{([0-9a-fA-F]+)\}|\\u([a-fA-F0-9]{4})/g, function ($0, $1, $2) {
  1149. var codePoint = parseInt($1 || $2, 16);
  1150. if (codePoint > 0x10FFFF) {
  1151. throwUnexpectedToken(null, Messages.InvalidRegExp);
  1152. }
  1153. if (codePoint <= 0xFFFF) {
  1154. return String.fromCharCode(codePoint);
  1155. }
  1156. return astralSubstitute;
  1157. })
  1158. // Replace each paired surrogate with a single ASCII symbol to
  1159. // avoid throwing on regular expressions that are only valid in
  1160. // combination with the "u" flag.
  1161. .replace(
  1162. /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
  1163. astralSubstitute
  1164. );
  1165. }
  1166. // First, detect invalid regular expressions.
  1167. try {
  1168. RegExp(tmp);
  1169. } catch (e) {
  1170. throwUnexpectedToken(null, Messages.InvalidRegExp);
  1171. }
  1172. // Return a regular expression object for this pattern-flag pair, or
  1173. // `null` in case the current environment doesn't support the flags it
  1174. // uses.
  1175. try {
  1176. return new RegExp(pattern, flags);
  1177. } catch (exception) {
  1178. /* istanbul ignore next */
  1179. return null;
  1180. }
  1181. }
  1182. function scanRegExpBody() {
  1183. var ch, str, classMarker, terminated, body;
  1184. ch = source[index];
  1185. assert(ch === '/', 'Regular expression literal must start with a slash');
  1186. str = source[index++];
  1187. classMarker = false;
  1188. terminated = false;
  1189. while (index < length) {
  1190. ch = source[index++];
  1191. str += ch;
  1192. if (ch === '\\') {
  1193. ch = source[index++];
  1194. // ECMA-262 7.8.5
  1195. if (isLineTerminator(ch.charCodeAt(0))) {
  1196. throwUnexpectedToken(null, Messages.UnterminatedRegExp);
  1197. }
  1198. str += ch;
  1199. } else if (isLineTerminator(ch.charCodeAt(0))) {
  1200. throwUnexpectedToken(null, Messages.UnterminatedRegExp);
  1201. } else if (classMarker) {
  1202. if (ch === ']') {
  1203. classMarker = false;
  1204. }
  1205. } else {
  1206. if (ch === '/') {
  1207. terminated = true;
  1208. break;
  1209. } else if (ch === '[') {
  1210. classMarker = true;
  1211. }
  1212. }
  1213. }
  1214. if (!terminated) {
  1215. throwUnexpectedToken(null, Messages.UnterminatedRegExp);
  1216. }
  1217. // Exclude leading and trailing slash.
  1218. body = str.substr(1, str.length - 2);
  1219. return {
  1220. value: body,
  1221. literal: str
  1222. };
  1223. }
  1224. function scanRegExpFlags() {
  1225. var ch, str, flags, restore;
  1226. str = '';
  1227. flags = '';
  1228. while (index < length) {
  1229. ch = source[index];
  1230. if (!isIdentifierPart(ch.charCodeAt(0))) {
  1231. break;
  1232. }
  1233. ++index;
  1234. if (ch === '\\' && index < length) {
  1235. ch = source[index];
  1236. if (ch === 'u') {
  1237. ++index;
  1238. restore = index;
  1239. ch = scanHexEscape('u');
  1240. if (ch) {
  1241. flags += ch;
  1242. for (str += '\\u'; restore < index; ++restore) {
  1243. str += source[restore];
  1244. }
  1245. } else {
  1246. index = restore;
  1247. flags += 'u';
  1248. str += '\\u';
  1249. }
  1250. tolerateUnexpectedToken();
  1251. } else {
  1252. str += '\\';
  1253. tolerateUnexpectedToken();
  1254. }
  1255. } else {
  1256. flags += ch;
  1257. str += ch;
  1258. }
  1259. }
  1260. return {
  1261. value: flags,
  1262. literal: str
  1263. };
  1264. }
  1265. function scanRegExp() {
  1266. var start, body, flags, value;
  1267. scanning = true;
  1268. lookahead = null;
  1269. skipComment();
  1270. start = index;
  1271. body = scanRegExpBody();
  1272. flags = scanRegExpFlags();
  1273. value = testRegExp(body.value, flags.value);
  1274. scanning = false;
  1275. if (extra.tokenize) {
  1276. return {
  1277. type: Token.RegularExpression,
  1278. value: value,
  1279. regex: {
  1280. pattern: body.value,
  1281. flags: flags.value
  1282. },
  1283. lineNumber: lineNumber,
  1284. lineStart: lineStart,
  1285. start: start,
  1286. end: index
  1287. };
  1288. }
  1289. return {
  1290. literal: body.literal + flags.literal,
  1291. value: value,
  1292. regex: {
  1293. pattern: body.value,
  1294. flags: flags.value
  1295. },
  1296. start: start,
  1297. end: index
  1298. };
  1299. }
  1300. function collectRegex() {
  1301. var pos, loc, regex, token;
  1302. skipComment();
  1303. pos = index;
  1304. loc = {
  1305. start: {
  1306. line: lineNumber,
  1307. column: index - lineStart
  1308. }
  1309. };
  1310. regex = scanRegExp();
  1311. loc.end = {
  1312. line: lineNumber,
  1313. column: index - lineStart
  1314. };
  1315. /* istanbul ignore next */
  1316. if (!extra.tokenize) {
  1317. // Pop the previous token, which is likely '/' or '/='
  1318. if (extra.tokens.length > 0) {
  1319. token = extra.tokens[extra.tokens.length - 1];
  1320. if (token.range[0] === pos && token.type === 'Punctuator') {
  1321. if (token.value === '/' || token.value === '/=') {
  1322. extra.tokens.pop();
  1323. }
  1324. }
  1325. }
  1326. extra.tokens.push({
  1327. type: 'RegularExpression',
  1328. value: regex.literal,
  1329. regex: regex.regex,
  1330. range: [pos, index],
  1331. loc: loc
  1332. });
  1333. }
  1334. return regex;
  1335. }
  1336. function isIdentifierName(token) {
  1337. return token.type === Token.Identifier ||
  1338. token.type === Token.Keyword ||
  1339. token.type === Token.BooleanLiteral ||
  1340. token.type === Token.NullLiteral;
  1341. }
  1342. // Using the following algorithm:
  1343. // https://github.com/mozilla/sweet.js/wiki/design
  1344. function advanceSlash() {
  1345. var regex, previous, check;
  1346. function testKeyword(value) {
  1347. return value && (value.length > 1) && (value[0] >= 'a') && (value[0] <= 'z');
  1348. }
  1349. previous = extra.tokenValues[extra.tokenValues.length - 1];
  1350. regex = (previous !== null);
  1351. switch (previous) {
  1352. case 'this':
  1353. case ']':
  1354. regex = false;
  1355. break;
  1356. case ')':
  1357. check = extra.tokenValues[extra.openParenToken - 1];
  1358. regex = (check === 'if' || check === 'while' || check === 'for' || check === 'with');
  1359. break;
  1360. case '}':
  1361. // Dividing a function by anything makes little sense,
  1362. // but we have to check for that.
  1363. regex = false;
  1364. if (testKeyword(extra.tokenValues[extra.openCurlyToken - 3])) {
  1365. // Anonymous function, e.g. function(){} /42
  1366. check = extra.tokenValues[extra.openCurlyToken - 4];
  1367. regex = check ? (FnExprTokens.indexOf(check) < 0) : false;
  1368. } else if (testKeyword(extra.tokenValues[extra.openCurlyToken - 4])) {
  1369. // Named function, e.g. function f(){} /42/
  1370. check = extra.tokenValues[extra.openCurlyToken - 5];
  1371. regex = check ? (FnExprTokens.indexOf(check) < 0) : true;
  1372. }
  1373. }
  1374. return regex ? collectRegex() : scanPunctuator();
  1375. }
  1376. function advance() {
  1377. var cp, token;
  1378. if (index >= length) {
  1379. return {
  1380. type: Token.EOF,
  1381. lineNumber: lineNumber,
  1382. lineStart: lineStart,
  1383. start: index,
  1384. end: index
  1385. };
  1386. }
  1387. cp = source.charCodeAt(index);
  1388. if (isIdentifierStart(cp)) {
  1389. token = scanIdentifier();
  1390. if (strict && isStrictModeReservedWord(token.value)) {
  1391. token.type = Token.Keyword;
  1392. }
  1393. return token;
  1394. }
  1395. // Very common: ( and ) and ;
  1396. if (cp === 0x28 || cp === 0x29 || cp === 0x3B) {
  1397. return scanPunctuator();
  1398. }
  1399. // String literal starts with single quote (U+0027) or double quote (U+0022).
  1400. if (cp === 0x27 || cp === 0x22) {
  1401. return scanStringLiteral();
  1402. }
  1403. // Dot (.) U+002E can also start a floating-point number, hence the need
  1404. // to check the next character.
  1405. if (cp === 0x2E) {
  1406. if (isDecimalDigit(source.charCodeAt(index + 1))) {
  1407. return scanNumericLiteral();
  1408. }
  1409. return scanPunctuator();
  1410. }
  1411. if (isDecimalDigit(cp)) {
  1412. return scanNumericLiteral();
  1413. }
  1414. // Slash (/) U+002F can also start a regex.
  1415. if (extra.tokenize && cp === 0x2F) {
  1416. return advanceSlash();
  1417. }
  1418. // Template literals start with ` (U+0060) for template head
  1419. // or } (U+007D) for template middle or template tail.
  1420. if (cp === 0x60 || (cp === 0x7D && state.curlyStack[state.curlyStack.length - 1] === '${')) {
  1421. return scanTemplate();
  1422. }
  1423. // Possible identifier start in a surrogate pair.
  1424. if (cp >= 0xD800 && cp < 0xDFFF) {
  1425. cp = codePointAt(index);
  1426. if (isIdentifierStart(cp)) {
  1427. return scanIdentifier();
  1428. }
  1429. }
  1430. return scanPunctuator();
  1431. }
  1432. function collectToken() {
  1433. var loc, token, value, entry;
  1434. loc = {
  1435. start: {
  1436. line: lineNumber,
  1437. column: index - lineStart
  1438. }
  1439. };
  1440. token = advance();
  1441. loc.end = {
  1442. line: lineNumber,
  1443. column: index - lineStart
  1444. };
  1445. if (token.type !== Token.EOF) {
  1446. value = source.slice(token.start, token.end);
  1447. entry = {
  1448. type: TokenName[token.type],
  1449. value: value,
  1450. range: [token.start, token.end],
  1451. loc: loc
  1452. };
  1453. if (token.regex) {
  1454. entry.regex = {
  1455. pattern: token.regex.pattern,
  1456. flags: token.regex.flags
  1457. };
  1458. }
  1459. if (extra.tokenValues) {
  1460. extra.tokenValues.push((entry.type === 'Punctuator' || entry.type === 'Keyword') ? entry.value : null);
  1461. }
  1462. if (extra.tokenize) {
  1463. if (!extra.range) {
  1464. delete entry.range;
  1465. }
  1466. if (!extra.loc) {
  1467. delete entry.loc;
  1468. }
  1469. if (extra.delegate) {
  1470. entry = extra.delegate(entry);
  1471. }
  1472. }
  1473. extra.tokens.push(entry);
  1474. }
  1475. return token;
  1476. }
  1477. function lex() {
  1478. var token;
  1479. scanning = true;
  1480. lastIndex = index;
  1481. lastLineNumber = lineNumber;
  1482. lastLineStart = lineStart;
  1483. skipComment();
  1484. token = lookahead;
  1485. startIndex = index;
  1486. startLineNumber = lineNumber;
  1487. startLineStart = lineStart;
  1488. lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance();
  1489. scanning = false;
  1490. return token;
  1491. }
  1492. function peek() {
  1493. scanning = true;
  1494. skipComment();
  1495. lastIndex = index;
  1496. lastLineNumber = lineNumber;
  1497. lastLineStart = lineStart;
  1498. startIndex = index;
  1499. startLineNumber = lineNumber;
  1500. startLineStart = lineStart;
  1501. lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance();
  1502. scanning = false;
  1503. }
  1504. function Position() {
  1505. this.line = startLineNumber;
  1506. this.column = startIndex - startLineStart;
  1507. }
  1508. function SourceLocation() {
  1509. this.start = new Position();
  1510. this.end = null;
  1511. }
  1512. function WrappingSourceLocation(startToken) {
  1513. this.start = {
  1514. line: startToken.lineNumber,
  1515. column: startToken.start - startToken.lineStart
  1516. };
  1517. this.end = null;
  1518. }
  1519. function Node() {
  1520. if (extra.range) {
  1521. this.range = [startIndex, 0];
  1522. }
  1523. if (extra.loc) {
  1524. this.loc = new SourceLocation();
  1525. }
  1526. }
  1527. function WrappingNode(startToken) {
  1528. if (extra.range) {
  1529. this.range = [startToken.start, 0];
  1530. }
  1531. if (extra.loc) {
  1532. this.loc = new WrappingSourceLocation(startToken);
  1533. }
  1534. }
  1535. WrappingNode.prototype = Node.prototype = {
  1536. processComment: function () {
  1537. var lastChild,
  1538. innerComments,
  1539. leadingComments,
  1540. trailingComments,
  1541. bottomRight = extra.bottomRightStack,
  1542. i,
  1543. comment,
  1544. last = bottomRight[bottomRight.length - 1];
  1545. if (this.type === Syntax.Program) {
  1546. if (this.body.length > 0) {
  1547. return;
  1548. }
  1549. }
  1550. /**
  1551. * patch innnerComments for properties empty block
  1552. * `function a() {/** comments **\/}`
  1553. */
  1554. if (this.type === Syntax.BlockStatement && this.body.length === 0) {
  1555. innerComments = [];
  1556. for (i = extra.leadingComments.length - 1; i >= 0; --i) {
  1557. comment = extra.leadingComments[i];
  1558. if (this.range[1] >= comment.range[1]) {
  1559. innerComments.unshift(comment);
  1560. extra.leadingComments.splice(i, 1);
  1561. extra.trailingComments.splice(i, 1);
  1562. }
  1563. }
  1564. if (innerComments.length) {
  1565. this.innerComments = innerComments;
  1566. //bottomRight.push(this);
  1567. return;
  1568. }
  1569. }
  1570. if (extra.trailingComments.length > 0) {
  1571. trailingComments = [];
  1572. for (i = extra.trailingComments.length - 1; i >= 0; --i) {
  1573. comment = extra.trailingComments[i];
  1574. if (comment.range[0] >= this.range[1]) {
  1575. trailingComments.unshift(comment);
  1576. extra.trailingComments.splice(i, 1);
  1577. }
  1578. }
  1579. extra.trailingComments = [];
  1580. } else {
  1581. if (last && last.trailingComments && last.trailingComments[0].range[0] >= this.range[1]) {
  1582. trailingComments = last.trailingComments;
  1583. delete last.trailingComments;
  1584. }
  1585. }
  1586. // Eating the stack.
  1587. while (last && last.range[0] >= this.range[0]) {
  1588. lastChild = bottomRight.pop();
  1589. last = bottomRight[bottomRight.length - 1];
  1590. }
  1591. if (lastChild) {
  1592. if (lastChild.leadingComments) {
  1593. leadingComments = [];
  1594. for (i = lastChild.leadingComments.length - 1; i >= 0; --i) {
  1595. comment = lastChild.leadingComments[i];
  1596. if (comment.range[1] <= this.range[0]) {
  1597. leadingComments.unshift(comment);
  1598. lastChild.leadingComments.splice(i, 1);
  1599. }
  1600. }
  1601. if (!lastChild.leadingComments.length) {
  1602. lastChild.leadingComments = undefined;
  1603. }
  1604. }
  1605. } else if (extra.leadingComments.length > 0) {
  1606. leadingComments = [];
  1607. for (i = extra.leadingComments.length - 1; i >= 0; --i) {
  1608. comment = extra.leadingComments[i];
  1609. if (comment.range[1] <= this.range[0]) {
  1610. leadingComments.unshift(comment);
  1611. extra.leadingComments.splice(i, 1);
  1612. }
  1613. }
  1614. }
  1615. if (leadingComments && leadingComments.length > 0) {
  1616. this.leadingComments = leadingComments;
  1617. }
  1618. if (trailingComments && trailingComments.length > 0) {
  1619. this.trailingComments = trailingComments;
  1620. }
  1621. bottomRight.push(this);
  1622. },
  1623. finish: function () {
  1624. if (extra.range) {
  1625. this.range[1] = lastIndex;
  1626. }
  1627. if (extra.loc) {
  1628. this.loc.end = {
  1629. line: lastLineNumber,
  1630. column: lastIndex - lastLineStart
  1631. };
  1632. if (extra.source) {
  1633. this.loc.source = extra.source;
  1634. }
  1635. }
  1636. if (extra.attachComment) {
  1637. this.processComment();
  1638. }
  1639. },
  1640. finishArrayExpression: function (elements) {
  1641. this.type = Syntax.ArrayExpression;
  1642. this.elements = elements;
  1643. this.finish();
  1644. return this;
  1645. },
  1646. finishArrayPattern: function (elements) {
  1647. this.type = Syntax.ArrayPattern;
  1648. this.elements = elements;
  1649. this.finish();
  1650. return this;
  1651. },
  1652. finishArrowFunctionExpression: function (params, defaults, body, expression) {
  1653. this.type = Syntax.ArrowFunctionExpression;
  1654. this.id = null;
  1655. this.params = params;
  1656. this.defaults = defaults;
  1657. this.body = body;
  1658. this.generator = false;
  1659. this.expression = expression;
  1660. this.finish();
  1661. return this;
  1662. },
  1663. finishAssignmentExpression: function (operator, left, right) {
  1664. this.type = Syntax.AssignmentExpression;
  1665. this.operator = operator;
  1666. this.left = left;
  1667. this.right = right;
  1668. this.finish();
  1669. return this;
  1670. },
  1671. finishAssignmentPattern: function (left, right) {
  1672. this.type = Syntax.AssignmentPattern;
  1673. this.left = left;
  1674. this.right = right;
  1675. this.finish();
  1676. return this;
  1677. },
  1678. finishBinaryExpression: function (operator, left, right) {
  1679. this.type = (operator === '||' || operator === '&&') ? Syntax.LogicalExpression : Syntax.BinaryExpression;
  1680. this.operator = operator;
  1681. this.left = left;
  1682. this.right = right;
  1683. this.finish();
  1684. return this;
  1685. },
  1686. finishBlockStatement: function (body) {
  1687. this.type = Syntax.BlockStatement;
  1688. this.body = body;
  1689. this.finish();
  1690. return this;
  1691. },
  1692. finishBreakStatement: function (label) {
  1693. this.type = Syntax.BreakStatement;
  1694. this.label = label;
  1695. this.finish();
  1696. return this;
  1697. },
  1698. finishCallExpression: function (callee, args) {
  1699. this.type = Syntax.CallExpression;
  1700. this.callee = callee;
  1701. this.arguments = args;
  1702. this.finish();
  1703. return this;
  1704. },
  1705. finishCatchClause: function (param, body) {
  1706. this.type = Syntax.CatchClause;
  1707. this.param = param;
  1708. this.body = body;
  1709. this.finish();
  1710. return this;
  1711. },
  1712. finishClassBody: function (body) {
  1713. this.type = Syntax.ClassBody;
  1714. this.body = body;
  1715. this.finish();
  1716. return this;
  1717. },
  1718. finishClassDeclaration: function (id, superClass, body) {
  1719. this.type = Syntax.ClassDeclaration;
  1720. this.id = id;
  1721. this.superClass = superClass;
  1722. this.body = body;
  1723. this.finish();
  1724. return this;
  1725. },
  1726. finishClassExpression: function (id, superClass, body) {
  1727. this.type = Syntax.ClassExpression;
  1728. this.id = id;
  1729. this.superClass = superClass;
  1730. this.body = body;
  1731. this.finish();
  1732. return this;
  1733. },
  1734. finishConditionalExpression: function (test, consequent, alternate) {
  1735. this.type = Syntax.ConditionalExpression;
  1736. this.test = test;
  1737. this.consequent = consequent;
  1738. this.alternate = alternate;
  1739. this.finish();
  1740. return this;
  1741. },
  1742. finishContinueStatement: function (label) {
  1743. this.type = Syntax.ContinueStatement;
  1744. this.label = label;
  1745. this.finish();
  1746. return this;
  1747. },
  1748. finishDebuggerStatement: function () {
  1749. this.type = Syntax.DebuggerStatement;
  1750. this.finish();
  1751. return this;
  1752. },
  1753. finishDoWhileStatement: function (body, test) {
  1754. this.type = Syntax.DoWhileStatement;
  1755. this.body = body;
  1756. this.test = test;
  1757. this.finish();
  1758. return this;
  1759. },
  1760. finishEmptyStatement: function () {
  1761. this.type = Syntax.EmptyStatement;
  1762. this.finish();
  1763. return this;
  1764. },
  1765. finishExpressionStatement: function (expression) {
  1766. this.type = Syntax.ExpressionStatement;
  1767. this.expression = expression;
  1768. this.finish();
  1769. return this;
  1770. },
  1771. finishForStatement: function (init, test, update, body) {
  1772. this.type = Syntax.ForStatement;
  1773. this.init = init;
  1774. this.test = test;
  1775. this.update = update;
  1776. this.body = body;
  1777. this.finish();
  1778. return this;
  1779. },
  1780. finishForOfStatement: function (left, right, body) {
  1781. this.type = Syntax.ForOfStatement;
  1782. this.left = left;
  1783. this.right = right;
  1784. this.body = body;
  1785. this.finish();
  1786. return this;
  1787. },
  1788. finishForInStatement: function (left, right, body) {
  1789. this.type = Syntax.ForInStatement;
  1790. this.left = left;
  1791. this.right = right;
  1792. this.body = body;
  1793. this.each = false;
  1794. this.finish();
  1795. return this;
  1796. },
  1797. finishFunctionDeclaration: function (id, params, defaults, body, generator) {
  1798. this.type = Syntax.FunctionDeclaration;
  1799. this.id = id;
  1800. this.params = params;
  1801. this.defaults = defaults;
  1802. this.body = body;
  1803. this.generator = generator;
  1804. this.expression = false;
  1805. this.finish();
  1806. return this;
  1807. },
  1808. finishFunctionExpression: function (id, params, defaults, body, generator) {
  1809. this.type = Syntax.FunctionExpression;
  1810. this.id = id;
  1811. this.params = params;
  1812. this.defaults = defaults;
  1813. this.body = body;
  1814. this.generator = generator;
  1815. this.expression = false;
  1816. this.finish();
  1817. return this;
  1818. },
  1819. finishIdentifier: function (name) {
  1820. this.type = Syntax.Identifier;
  1821. this.name = name;
  1822. this.finish();
  1823. return this;
  1824. },
  1825. finishIfStatement: function (test, consequent, alternate) {
  1826. this.type = Syntax.IfStatement;
  1827. this.test = test;
  1828. this.consequent = consequent;
  1829. this.alternate = alternate;
  1830. this.finish();
  1831. return this;
  1832. },
  1833. finishLabeledStatement: function (label, body) {
  1834. this.type = Syntax.LabeledStatement;
  1835. this.label = label;
  1836. this.body = body;
  1837. this.finish();
  1838. return this;
  1839. },
  1840. finishLiteral: function (token) {
  1841. this.type = Syntax.Literal;
  1842. this.value = token.value;
  1843. this.raw = source.slice(token.start, token.end);
  1844. if (token.regex) {
  1845. this.regex = token.regex;
  1846. }
  1847. this.finish();
  1848. return this;
  1849. },
  1850. finishMemberExpression: function (accessor, object, property) {
  1851. this.type = Syntax.MemberExpression;
  1852. this.computed = accessor === '[';
  1853. this.object = object;
  1854. this.property = property;
  1855. this.finish();
  1856. return this;
  1857. },
  1858. finishMetaProperty: function (meta, property) {
  1859. this.type = Syntax.MetaProperty;
  1860. this.meta = meta;
  1861. this.property = property;
  1862. this.finish();
  1863. return this;
  1864. },
  1865. finishNewExpression: function (callee, args) {
  1866. this.type = Syntax.NewExpression;
  1867. this.callee = callee;
  1868. this.arguments = args;
  1869. this.finish();
  1870. return this;
  1871. },
  1872. finishObjectExpression: function (properties) {
  1873. this.type = Syntax.ObjectExpression;
  1874. this.properties = properties;
  1875. this.finish();
  1876. return this;
  1877. },
  1878. finishObjectPattern: function (properties) {
  1879. this.type = Syntax.ObjectPattern;
  1880. this.properties = properties;
  1881. this.finish();
  1882. return this;
  1883. },
  1884. finishPostfixExpression: function (operator, argument) {
  1885. this.type = Syntax.UpdateExpression;
  1886. this.operator = operator;
  1887. this.argument = argument;
  1888. this.prefix = false;
  1889. this.finish();
  1890. return this;
  1891. },
  1892. finishProgram: function (body, sourceType) {
  1893. this.type = Syntax.Program;
  1894. this.body = body;
  1895. this.sourceType = sourceType;
  1896. this.finish();
  1897. return this;
  1898. },
  1899. finishProperty: function (kind, key, computed, value, method, shorthand) {
  1900. this.type = Syntax.Property;
  1901. this.key = key;
  1902. this.computed = computed;
  1903. this.value = value;
  1904. this.kind = kind;
  1905. this.method = method;
  1906. this.shorthand = shorthand;
  1907. this.finish();
  1908. return this;
  1909. },
  1910. finishRestElement: function (argument) {
  1911. this.type = Syntax.RestElement;
  1912. this.argument = argument;
  1913. this.finish();
  1914. return this;
  1915. },
  1916. finishReturnStatement: function (argument) {
  1917. this.type = Syntax.ReturnStatement;
  1918. this.argument = argument;
  1919. this.finish();
  1920. return this;
  1921. },
  1922. finishSequenceExpression: function (expressions) {
  1923. this.type = Syntax.SequenceExpression;
  1924. this.expressions = expressions;
  1925. this.finish();
  1926. return this;
  1927. },
  1928. finishSpreadElement: function (argument) {
  1929. this.type = Syntax.SpreadElement;
  1930. this.argument = argument;
  1931. this.finish();
  1932. return this;
  1933. },
  1934. finishSwitchCase: function (test, consequent) {
  1935. this.type = Syntax.SwitchCase;
  1936. this.test = test;
  1937. this.consequent = consequent;
  1938. this.finish();
  1939. return this;
  1940. },
  1941. finishSuper: function () {
  1942. this.type = Syntax.Super;
  1943. this.finish();
  1944. return this;
  1945. },
  1946. finishSwitchStatement: function (discriminant, cases) {
  1947. this.type = Syntax.SwitchStatement;
  1948. this.discriminant = discriminant;
  1949. this.cases = cases;
  1950. this.finish();
  1951. return this;
  1952. },
  1953. finishTaggedTemplateExpression: function (tag, quasi) {
  1954. this.type = Syntax.TaggedTemplateExpression;
  1955. this.tag = tag;
  1956. this.quasi = quasi;
  1957. this.finish();
  1958. return this;
  1959. },
  1960. finishTemplateElement: function (value, tail) {
  1961. this.type = Syntax.TemplateElement;
  1962. this.value = value;
  1963. this.tail = tail;
  1964. this.finish();
  1965. return this;
  1966. },
  1967. finishTemplateLiteral: function (quasis, expressions) {
  1968. this.type = Syntax.TemplateLiteral;
  1969. this.quasis = quasis;
  1970. this.expressions = expressions;
  1971. this.finish();
  1972. return this;
  1973. },
  1974. finishThisExpression: function () {
  1975. this.type = Syntax.ThisExpression;
  1976. this.finish();
  1977. return this;
  1978. },
  1979. finishThrowStatement: function (argument) {
  1980. this.type = Syntax.ThrowStatement;
  1981. this.argument = argument;
  1982. this.finish();
  1983. return this;
  1984. },
  1985. finishTryStatement: function (block, handler, finalizer) {
  1986. this.type = Syntax.TryStatement;
  1987. this.block = block;
  1988. this.guardedHandlers = [];
  1989. this.handlers = handler ? [handler] : [];
  1990. this.handler = handler;
  1991. this.finalizer = finalizer;
  1992. this.finish();
  1993. return this;
  1994. },
  1995. finishUnaryExpression: function (operator, argument) {
  1996. this.type = (operator === '++' || operator === '--') ? Syntax.UpdateExpression : Syntax.UnaryExpression;
  1997. this.operator = operator;
  1998. this.argument = argument;
  1999. this.prefix = true;
  2000. this.finish();
  2001. return this;
  2002. },
  2003. finishVariableDeclaration: function (declarations) {
  2004. this.type = Syntax.VariableDeclaration;
  2005. this.declarations = declarations;
  2006. this.kind = 'var';
  2007. this.finish();
  2008. return this;
  2009. },
  2010. finishLexicalDeclaration: function (declarations, kind) {
  2011. this.type = Syntax.VariableDeclaration;
  2012. this.declarations = declarations;
  2013. this.kind = kind;
  2014. this.finish();
  2015. return this;
  2016. },
  2017. finishVariableDeclarator: function (id, init) {
  2018. this.type = Syntax.VariableDeclarator;
  2019. this.id = id;
  2020. this.init = init;
  2021. this.finish();
  2022. return this;
  2023. },
  2024. finishWhileStatement: function (test, body) {
  2025. this.type = Syntax.WhileStatement;
  2026. this.test = test;
  2027. this.body = body;
  2028. this.finish();
  2029. return this;
  2030. },
  2031. finishWithStatement: function (object, body) {
  2032. this.type = Syntax.WithStatement;
  2033. this.object = object;
  2034. this.body = body;
  2035. this.finish();
  2036. return this;
  2037. },
  2038. finishExportSpecifier: function (local, exported) {
  2039. this.type = Syntax.ExportSpecifier;
  2040. this.exported = exported || local;
  2041. this.local = local;
  2042. this.finish();
  2043. return this;
  2044. },
  2045. finishImportDefaultSpecifier: function (local) {
  2046. this.type = Syntax.ImportDefaultSpecifier;
  2047. this.local = local;
  2048. this.finish();
  2049. return this;
  2050. },
  2051. finishImportNamespaceSpecifier: function (local) {
  2052. this.type = Syntax.ImportNamespaceSpecifier;
  2053. this.local = local;
  2054. this.finish();
  2055. return this;
  2056. },
  2057. finishExportNamedDeclaration: function (declaration, specifiers, src) {
  2058. this.type = Syntax.ExportNamedDeclaration;
  2059. this.declaration = declaration;
  2060. this.specifiers = specifiers;
  2061. this.source = src;
  2062. this.finish();
  2063. return this;
  2064. },
  2065. finishExportDefaultDeclaration: function (declaration) {
  2066. this.type = Syntax.ExportDefaultDeclaration;
  2067. this.declaration = declaration;
  2068. this.finish();
  2069. return this;
  2070. },
  2071. finishExportAllDeclaration: function (src) {
  2072. this.type = Syntax.ExportAllDeclaration;
  2073. this.source = src;
  2074. this.finish();
  2075. return this;
  2076. },
  2077. finishImportSpecifier: function (local, imported) {
  2078. this.type = Syntax.ImportSpecifier;
  2079. this.local = local || imported;
  2080. this.imported = imported;
  2081. this.finish();
  2082. return this;
  2083. },
  2084. finishImportDeclaration: function (specifiers, src) {
  2085. this.type = Syntax.ImportDeclaration;
  2086. this.specifiers = specifiers;
  2087. this.source = src;
  2088. this.finish();
  2089. return this;
  2090. },
  2091. finishYieldExpression: function (argument, delegate) {
  2092. this.type = Syntax.YieldExpression;
  2093. this.argument = argument;
  2094. this.delegate = delegate;
  2095. this.finish();
  2096. return this;
  2097. }
  2098. };
  2099. function recordError(error) {
  2100. var e, existing;
  2101. for (e = 0; e < extra.errors.length; e++) {
  2102. existing = extra.errors[e];
  2103. // Prevent duplicated error.
  2104. /* istanbul ignore next */
  2105. if (existing.index === error.index && existing.message === error.message) {
  2106. return;
  2107. }
  2108. }
  2109. extra.errors.push(error);
  2110. }
  2111. function constructError(msg, column) {
  2112. var error = new Error(msg);
  2113. try {
  2114. throw error;
  2115. } catch (base) {
  2116. /* istanbul ignore else */
  2117. if (Object.create && Object.defineProperty) {
  2118. error = Object.create(base);
  2119. Object.defineProperty(error, 'column', { value: column });
  2120. }
  2121. } finally {
  2122. return error;
  2123. }
  2124. }
  2125. function createError(line, pos, description) {
  2126. var msg, column, error;
  2127. msg = 'Line ' + line + ': ' + description;
  2128. column = pos - (scanning ? lineStart : lastLineStart) + 1;
  2129. error = constructError(msg, column);
  2130. error.lineNumber = line;
  2131. error.description = description;
  2132. error.index = pos;
  2133. return error;
  2134. }
  2135. // Throw an exception
  2136. function throwError(messageFormat) {
  2137. var args, msg;
  2138. args = Array.prototype.slice.call(arguments, 1);
  2139. msg = messageFormat.replace(/%(\d)/g,
  2140. function (whole, idx) {
  2141. assert(idx < args.length, 'Message reference must be in range');
  2142. return args[idx];
  2143. }
  2144. );
  2145. throw createError(lastLineNumber, lastIndex, msg);
  2146. }
  2147. function tolerateError(messageFormat) {
  2148. var args, msg, error;
  2149. args = Array.prototype.slice.call(arguments, 1);
  2150. /* istanbul ignore next */
  2151. msg = messageFormat.replace(/%(\d)/g,
  2152. function (whole, idx) {
  2153. assert(idx < args.length, 'Message reference must be in range');
  2154. return args[idx];
  2155. }
  2156. );
  2157. error = createError(lineNumber, lastIndex, msg);
  2158. if (extra.errors) {
  2159. recordError(error);
  2160. } else {
  2161. throw error;
  2162. }
  2163. }
  2164. // Throw an exception because of the token.
  2165. function unexpectedTokenError(token, message) {
  2166. var value, msg = message || Messages.UnexpectedToken;
  2167. if (token) {
  2168. if (!message) {
  2169. msg = (token.type === Token.EOF) ? Messages.UnexpectedEOS :
  2170. (token.type === Token.Identifier) ? Messages.UnexpectedIdentifier :
  2171. (token.type === Token.NumericLiteral) ? Messages.UnexpectedNumber :
  2172. (token.type === Token.StringLiteral) ? Messages.UnexpectedString :
  2173. (token.type === Token.Template) ? Messages.UnexpectedTemplate :
  2174. Messages.UnexpectedToken;
  2175. if (token.type === Token.Keyword) {
  2176. if (isFutureReservedWord(token.value)) {
  2177. msg = Messages.UnexpectedReserved;
  2178. } else if (strict && isStrictModeReservedWord(token.value)) {
  2179. msg = Messages.StrictReservedWord;
  2180. }
  2181. }
  2182. }
  2183. value = (token.type === Token.Template) ? token.value.raw : token.value;
  2184. } else {
  2185. value = 'ILLEGAL';
  2186. }
  2187. msg = msg.replace('%0', value);
  2188. return (token && typeof token.lineNumber === 'number') ?
  2189. createError(token.lineNumber, token.start, msg) :
  2190. createError(scanning ? lineNumber : lastLineNumber, scanning ? index : lastIndex, msg);
  2191. }
  2192. function throwUnexpectedToken(token, message) {
  2193. throw unexpectedTokenError(token, message);
  2194. }
  2195. function tolerateUnexpectedToken(token, message) {
  2196. var error = unexpectedTokenError(token, message);
  2197. if (extra.errors) {
  2198. recordError(error);
  2199. } else {
  2200. throw error;
  2201. }
  2202. }
  2203. // Expect the next token to match the specified punctuator.
  2204. // If not, an exception will be thrown.
  2205. function expect(value) {
  2206. var token = lex();
  2207. if (token.type !== Token.Punctuator || token.value !== value) {
  2208. throwUnexpectedToken(token);
  2209. }
  2210. }
  2211. /**
  2212. * @name expectCommaSeparator
  2213. * @description Quietly expect a comma when in tolerant mode, otherwise delegates
  2214. * to <code>expect(value)</code>
  2215. * @since 2.0
  2216. */
  2217. function expectCommaSeparator() {
  2218. var token;
  2219. if (extra.errors) {
  2220. token = lookahead;
  2221. if (token.type === Token.Punctuator && token.value === ',') {
  2222. lex();
  2223. } else if (token.type === Token.Punctuator && token.value === ';') {
  2224. lex();
  2225. tolerateUnexpectedToken(token);
  2226. } else {
  2227. tolerateUnexpectedToken(token, Messages.UnexpectedToken);
  2228. }
  2229. } else {
  2230. expect(',');
  2231. }
  2232. }
  2233. // Expect the next token to match the specified keyword.
  2234. // If not, an exception will be thrown.
  2235. function expectKeyword(keyword) {
  2236. var token = lex();
  2237. if (token.type !== Token.Keyword || token.value !== keyword) {
  2238. throwUnexpectedToken(token);
  2239. }
  2240. }
  2241. // Return true if the next token matches the specified punctuator.
  2242. function match(value) {
  2243. return lookahead.type === Token.Punctuator && lookahead.value === value;
  2244. }
  2245. // Return true if the next token matches the specified keyword
  2246. function matchKeyword(keyword) {
  2247. return lookahead.type === Token.Keyword && lookahead.value === keyword;
  2248. }
  2249. // Return true if the next token matches the specified contextual keyword
  2250. // (where an identifier is sometimes a keyword depending on the context)
  2251. function matchContextualKeyword(keyword) {
  2252. return lookahead.type === Token.Identifier && lookahead.value === keyword;
  2253. }
  2254. // Return true if the next token is an assignment operator
  2255. function matchAssign() {
  2256. var op;
  2257. if (lookahead.type !== Token.Punctuator) {
  2258. return false;
  2259. }
  2260. op = lookahead.value;
  2261. return op === '=' ||
  2262. op === '*=' ||
  2263. op === '/=' ||
  2264. op === '%=' ||
  2265. op === '+=' ||
  2266. op === '-=' ||
  2267. op === '<<=' ||
  2268. op === '>>=' ||
  2269. op === '>>>=' ||
  2270. op === '&=' ||
  2271. op === '^=' ||
  2272. op === '|=';
  2273. }
  2274. function consumeSemicolon() {
  2275. // Catch the very common case first: immediately a semicolon (U+003B).
  2276. if (source.charCodeAt(startIndex) === 0x3B || match(';')) {
  2277. lex();
  2278. return;
  2279. }
  2280. if (hasLineTerminator) {
  2281. return;
  2282. }
  2283. // FIXME(ikarienator): this is seemingly an issue in the previous location info convention.
  2284. lastIndex = startIndex;
  2285. lastLineNumber = startLineNumber;
  2286. lastLineStart = startLineStart;
  2287. if (lookahead.type !== Token.EOF && !match('}')) {
  2288. throwUnexpectedToken(lookahead);
  2289. }
  2290. }
  2291. // Cover grammar support.
  2292. //
  2293. // When an assignment expression position starts with an left parenthesis, the determination of the type
  2294. // of the syntax is to be deferred arbitrarily long until the end of the parentheses pair (plus a lookahead)
  2295. // or the first comma. This situation also defers the determination of all the expressions nested in the pair.
  2296. //
  2297. // There are three productions that can be parsed in a parentheses pair that needs to be determined
  2298. // after the outermost pair is closed. They are:
  2299. //
  2300. // 1. AssignmentExpression
  2301. // 2. BindingElements
  2302. // 3. AssignmentTargets
  2303. //
  2304. // In order to avoid exponential backtracking, we use two flags to denote if the production can be
  2305. // binding element or assignment target.
  2306. //
  2307. // The three productions have the relationship:
  2308. //
  2309. // BindingElements ⊆ AssignmentTargets ⊆ AssignmentExpression
  2310. //
  2311. // with a single exception that CoverInitializedName when used directly in an Expression, generates
  2312. // an early error. Therefore, we need the third state, firstCoverInitializedNameError, to track the
  2313. // first usage of CoverInitializedName and report it when we reached the end of the parentheses pair.
  2314. //
  2315. // isolateCoverGrammar function runs the given parser function with a new cover grammar context, and it does not
  2316. // effect the current flags. This means the production the parser parses is only used as an expression. Therefore
  2317. // the CoverInitializedName check is conducted.
  2318. //
  2319. // inheritCoverGrammar function runs the given parse function with a new cover grammar context, and it propagates
  2320. // the flags outside of the parser. This means the production the parser parses is used as a part of a potential
  2321. // pattern. The CoverInitializedName check is deferred.
  2322. function isolateCoverGrammar(parser) {
  2323. var oldIsBindingElement = isBindingElement,
  2324. oldIsAssignmentTarget = isAssignmentTarget,
  2325. oldFirstCoverInitializedNameError = firstCoverInitializedNameError,
  2326. result;
  2327. isBindingElement = true;
  2328. isAssignmentTarget = true;
  2329. firstCoverInitializedNameError = null;
  2330. result = parser();
  2331. if (firstCoverInitializedNameError !== null) {
  2332. throwUnexpectedToken(firstCoverInitializedNameError);
  2333. }
  2334. isBindingElement = oldIsBindingElement;
  2335. isAssignmentTarget = oldIsAssignmentTarget;
  2336. firstCoverInitializedNameError = oldFirstCoverInitializedNameError;
  2337. return result;
  2338. }
  2339. function inheritCoverGrammar(parser) {
  2340. var oldIsBindingElement = isBindingElement,
  2341. oldIsAssignmentTarget = isAssignmentTarget,
  2342. oldFirstCoverInitializedNameError = firstCoverInitializedNameError,
  2343. result;
  2344. isBindingElement = true;
  2345. isAssignmentTarget = true;
  2346. firstCoverInitializedNameError = null;
  2347. result = parser();
  2348. isBindingElement = isBindingElement && oldIsBindingElement;
  2349. isAssignmentTarget = isAssignmentTarget && oldIsAssignmentTarget;
  2350. firstCoverInitializedNameError = oldFirstCoverInitializedNameError || firstCoverInitializedNameError;
  2351. return result;
  2352. }
  2353. // ECMA-262 13.3.3 Destructuring Binding Patterns
  2354. function parseArrayPattern(params, kind) {
  2355. var node = new Node(), elements = [], rest, restNode;
  2356. expect('[');
  2357. while (!match(']')) {
  2358. if (match(',')) {
  2359. lex();
  2360. elements.push(null);
  2361. } else {
  2362. if (match('...')) {
  2363. restNode = new Node();
  2364. lex();
  2365. params.push(lookahead);
  2366. rest = parseVariableIdentifier(kind);
  2367. elements.push(restNode.finishRestElement(rest));
  2368. break;
  2369. } else {
  2370. elements.push(parsePatternWithDefault(params, kind));
  2371. }
  2372. if (!match(']')) {
  2373. expect(',');
  2374. }
  2375. }
  2376. }
  2377. expect(']');
  2378. return node.finishArrayPattern(elements);
  2379. }
  2380. function parsePropertyPattern(params, kind) {
  2381. var node = new Node(), key, keyToken, computed = match('['), init;
  2382. if (lookahead.type === Token.Identifier) {
  2383. keyToken = lookahead;
  2384. key = parseVariableIdentifier();
  2385. if (match('=')) {
  2386. params.push(keyToken);
  2387. lex();
  2388. init = parseAssignmentExpression();
  2389. return node.finishProperty(
  2390. 'init', key, false,
  2391. new WrappingNode(keyToken).finishAssignmentPattern(key, init), false, true);
  2392. } else if (!match(':')) {
  2393. params.push(keyToken);
  2394. return node.finishProperty('init', key, false, key, false, true);
  2395. }
  2396. } else {
  2397. key = parseObjectPropertyKey();
  2398. }
  2399. expect(':');
  2400. init = parsePatternWithDefault(params, kind);
  2401. return node.finishProperty('init', key, computed, init, false, false);
  2402. }
  2403. function parseObjectPattern(params, kind) {
  2404. var node = new Node(), properties = [];
  2405. expect('{');
  2406. while (!match('}')) {
  2407. properties.push(parsePropertyPattern(params, kind));
  2408. if (!match('}')) {
  2409. expect(',');
  2410. }
  2411. }
  2412. lex();
  2413. return node.finishObjectPattern(properties);
  2414. }
  2415. function parsePattern(params, kind) {
  2416. if (match('[')) {
  2417. return parseArrayPattern(params, kind);
  2418. } else if (match('{')) {
  2419. return parseObjectPattern(params, kind);
  2420. } else if (matchKeyword('let')) {
  2421. if (kind === 'const' || kind === 'let') {
  2422. tolerateUnexpectedToken(lookahead, Messages.UnexpectedToken);
  2423. }
  2424. }
  2425. params.push(lookahead);
  2426. return parseVariableIdentifier(kind);
  2427. }
  2428. function parsePatternWithDefault(params, kind) {
  2429. var startToken = lookahead, pattern, previousAllowYield, right;
  2430. pattern = parsePattern(params, kind);
  2431. if (match('=')) {
  2432. lex();
  2433. previousAllowYield = state.allowYield;
  2434. state.allowYield = true;
  2435. right = isolateCoverGrammar(parseAssignmentExpression);
  2436. state.allowYield = previousAllowYield;
  2437. pattern = new WrappingNode(startToken).finishAssignmentPattern(pattern, right);
  2438. }
  2439. return pattern;
  2440. }
  2441. // ECMA-262 12.2.5 Array Initializer
  2442. function parseArrayInitializer() {
  2443. var elements = [], node = new Node(), restSpread;
  2444. expect('[');
  2445. while (!match(']')) {
  2446. if (match(',')) {
  2447. lex();
  2448. elements.push(null);
  2449. } else if (match('...')) {
  2450. restSpread = new Node();
  2451. lex();
  2452. restSpread.finishSpreadElement(inheritCoverGrammar(parseAssignmentExpression));
  2453. if (!match(']')) {
  2454. isAssignmentTarget = isBindingElement = false;
  2455. expect(',');
  2456. }
  2457. elements.push(restSpread);
  2458. } else {
  2459. elements.push(inheritCoverGrammar(parseAssignmentExpression));
  2460. if (!match(']')) {
  2461. expect(',');
  2462. }
  2463. }
  2464. }
  2465. lex();
  2466. return node.finishArrayExpression(elements);
  2467. }
  2468. // ECMA-262 12.2.6 Object Initializer
  2469. function parsePropertyFunction(node, paramInfo, isGenerator) {
  2470. var previousStrict, body;
  2471. isAssignmentTarget = isBindingElement = false;
  2472. previousStrict = strict;
  2473. body = isolateCoverGrammar(parseFunctionSourceElements);
  2474. if (strict && paramInfo.firstRestricted) {
  2475. tolerateUnexpectedToken(paramInfo.firstRestricted, paramInfo.message);
  2476. }
  2477. if (strict && paramInfo.stricted) {
  2478. tolerateUnexpectedToken(paramInfo.stricted, paramInfo.message);
  2479. }
  2480. strict = previousStrict;
  2481. return node.finishFunctionExpression(null, paramInfo.params, paramInfo.defaults, body, isGenerator);
  2482. }
  2483. function parsePropertyMethodFunction() {
  2484. var params, method, node = new Node(),
  2485. previousAllowYield = state.allowYield;
  2486. state.allowYield = false;
  2487. params = parseParams();
  2488. state.allowYield = previousAllowYield;
  2489. state.allowYield = false;
  2490. method = parsePropertyFunction(node, params, false);
  2491. state.allowYield = previousAllowYield;
  2492. return method;
  2493. }
  2494. function parseObjectPropertyKey() {
  2495. var token, node = new Node(), expr;
  2496. token = lex();
  2497. // Note: This function is called only from parseObjectProperty(), where
  2498. // EOF and Punctuator tokens are already filtered out.
  2499. switch (token.type) {
  2500. case Token.StringLiteral:
  2501. case Token.NumericLiteral:
  2502. if (strict && token.octal) {
  2503. tolerateUnexpectedToken(token, Messages.StrictOctalLiteral);
  2504. }
  2505. return node.finishLiteral(token);
  2506. case Token.Identifier:
  2507. case Token.BooleanLiteral:
  2508. case Token.NullLiteral:
  2509. case Token.Keyword:
  2510. return node.finishIdentifier(token.value);
  2511. case Token.Punctuator:
  2512. if (token.value === '[') {
  2513. expr = isolateCoverGrammar(parseAssignmentExpression);
  2514. expect(']');
  2515. return expr;
  2516. }
  2517. break;
  2518. }
  2519. throwUnexpectedToken(token);
  2520. }
  2521. function lookaheadPropertyName() {
  2522. switch (lookahead.type) {
  2523. case Token.Identifier:
  2524. case Token.StringLiteral:
  2525. case Token.BooleanLiteral:
  2526. case Token.NullLiteral:
  2527. case Token.NumericLiteral:
  2528. case Token.Keyword:
  2529. return true;
  2530. case Token.Punctuator:
  2531. return lookahead.value === '[';
  2532. }
  2533. return false;
  2534. }
  2535. // This function is to try to parse a MethodDefinition as defined in 14.3. But in the case of object literals,
  2536. // it might be called at a position where there is in fact a short hand identifier pattern or a data property.
  2537. // This can only be determined after we consumed up to the left parentheses.
  2538. //
  2539. // In order to avoid back tracking, it returns `null` if the position is not a MethodDefinition and the caller
  2540. // is responsible to visit other options.
  2541. function tryParseMethodDefinition(token, key, computed, node) {
  2542. var value, options, methodNode, params,
  2543. previousAllowYield = state.allowYield;
  2544. if (token.type === Token.Identifier) {
  2545. // check for `get` and `set`;
  2546. if (token.value === 'get' && lookaheadPropertyName()) {
  2547. computed = match('[');
  2548. key = parseObjectPropertyKey();
  2549. methodNode = new Node();
  2550. expect('(');
  2551. expect(')');
  2552. state.allowYield = false;
  2553. value = parsePropertyFunction(methodNode, {
  2554. params: [],
  2555. defaults: [],
  2556. stricted: null,
  2557. firstRestricted: null,
  2558. message: null
  2559. }, false);
  2560. state.allowYield = previousAllowYield;
  2561. return node.finishProperty('get', key, computed, value, false, false);
  2562. } else if (token.value === 'set' && lookaheadPropertyName()) {
  2563. computed = match('[');
  2564. key = parseObjectPropertyKey();
  2565. methodNode = new Node();
  2566. expect('(');
  2567. options = {
  2568. params: [],
  2569. defaultCount: 0,
  2570. defaults: [],
  2571. firstRestricted: null,
  2572. paramSet: {}
  2573. };
  2574. if (match(')')) {
  2575. tolerateUnexpectedToken(lookahead);
  2576. } else {
  2577. state.allowYield = false;
  2578. parseParam(options);
  2579. state.allowYield = previousAllowYield;
  2580. if (options.defaultCount === 0) {
  2581. options.defaults = [];
  2582. }
  2583. }
  2584. expect(')');
  2585. state.allowYield = false;
  2586. value = parsePropertyFunction(methodNode, options, false);
  2587. state.allowYield = previousAllowYield;
  2588. return node.finishProperty('set', key, computed, value, false, false);
  2589. }
  2590. } else if (token.type === Token.Punctuator && token.value === '*' && lookaheadPropertyName()) {
  2591. computed = match('[');
  2592. key = parseObjectPropertyKey();
  2593. methodNode = new Node();
  2594. state.allowYield = true;
  2595. params = parseParams();
  2596. state.allowYield = previousAllowYield;
  2597. state.allowYield = false;
  2598. value = parsePropertyFunction(methodNode, params, true);
  2599. state.allowYield = previousAllowYield;
  2600. return node.finishProperty('init', key, computed, value, true, false);
  2601. }
  2602. if (key && match('(')) {
  2603. value = parsePropertyMethodFunction();
  2604. return node.finishProperty('init', key, computed, value, true, false);
  2605. }
  2606. // Not a MethodDefinition.
  2607. return null;
  2608. }
  2609. function parseObjectProperty(hasProto) {
  2610. var token = lookahead, node = new Node(), computed, key, maybeMethod, proto, value;
  2611. computed = match('[');
  2612. if (match('*')) {
  2613. lex();
  2614. } else {
  2615. key = parseObjectPropertyKey();
  2616. }
  2617. maybeMethod = tryParseMethodDefinition(token, key, computed, node);
  2618. if (maybeMethod) {
  2619. return maybeMethod;
  2620. }
  2621. if (!key) {
  2622. throwUnexpectedToken(lookahead);
  2623. }
  2624. // Check for duplicated __proto__
  2625. if (!computed) {
  2626. proto = (key.type === Syntax.Identifier && key.name === '__proto__') ||
  2627. (key.type === Syntax.Literal && key.value === '__proto__');
  2628. if (hasProto.value && proto) {
  2629. tolerateError(Messages.DuplicateProtoProperty);
  2630. }
  2631. hasProto.value |= proto;
  2632. }
  2633. if (match(':')) {
  2634. lex();
  2635. value = inheritCoverGrammar(parseAssignmentExpression);
  2636. return node.finishProperty('init', key, computed, value, false, false);
  2637. }
  2638. if (token.type === Token.Identifier) {
  2639. if (match('=')) {
  2640. firstCoverInitializedNameError = lookahead;
  2641. lex();
  2642. value = isolateCoverGrammar(parseAssignmentExpression);
  2643. return node.finishProperty('init', key, computed,
  2644. new WrappingNode(token).finishAssignmentPattern(key, value), false, true);
  2645. }
  2646. return node.finishProperty('init', key, computed, key, false, true);
  2647. }
  2648. throwUnexpectedToken(lookahead);
  2649. }
  2650. function parseObjectInitializer() {
  2651. var properties = [], hasProto = {value: false}, node = new Node();
  2652. expect('{');
  2653. while (!match('}')) {
  2654. properties.push(parseObjectProperty(hasProto));
  2655. if (!match('}')) {
  2656. expectCommaSeparator();
  2657. }
  2658. }
  2659. expect('}');
  2660. return node.finishObjectExpression(properties);
  2661. }
  2662. function reinterpretExpressionAsPattern(expr) {
  2663. var i;
  2664. switch (expr.type) {
  2665. case Syntax.Identifier:
  2666. case Syntax.MemberExpression:
  2667. case Syntax.RestElement:
  2668. case Syntax.AssignmentPattern:
  2669. break;
  2670. case Syntax.SpreadElement:
  2671. expr.type = Syntax.RestElement;
  2672. reinterpretExpressionAsPattern(expr.argument);
  2673. break;
  2674. case Syntax.ArrayExpression:
  2675. expr.type = Syntax.ArrayPattern;
  2676. for (i = 0; i < expr.elements.length; i++) {
  2677. if (expr.elements[i] !== null) {
  2678. reinterpretExpressionAsPattern(expr.elements[i]);
  2679. }
  2680. }
  2681. break;
  2682. case Syntax.ObjectExpression:
  2683. expr.type = Syntax.ObjectPattern;
  2684. for (i = 0; i < expr.properties.length; i++) {
  2685. reinterpretExpressionAsPattern(expr.properties[i].value);
  2686. }
  2687. break;
  2688. case Syntax.AssignmentExpression:
  2689. expr.type = Syntax.AssignmentPattern;
  2690. reinterpretExpressionAsPattern(expr.left);
  2691. break;
  2692. default:
  2693. // Allow other node type for tolerant parsing.
  2694. break;
  2695. }
  2696. }
  2697. // ECMA-262 12.2.9 Template Literals
  2698. function parseTemplateElement(option) {
  2699. var node, token;
  2700. if (lookahead.type !== Token.Template || (option.head && !lookahead.head)) {
  2701. throwUnexpectedToken();
  2702. }
  2703. node = new Node();
  2704. token = lex();
  2705. return node.finishTemplateElement({ raw: token.value.raw, cooked: token.value.cooked }, token.tail);
  2706. }
  2707. function parseTemplateLiteral() {
  2708. var quasi, quasis, expressions, node = new Node();
  2709. quasi = parseTemplateElement({ head: true });
  2710. quasis = [quasi];
  2711. expressions = [];
  2712. while (!quasi.tail) {
  2713. expressions.push(parseExpression());
  2714. quasi = parseTemplateElement({ head: false });
  2715. quasis.push(quasi);
  2716. }
  2717. return node.finishTemplateLiteral(quasis, expressions);
  2718. }
  2719. // ECMA-262 12.2.10 The Grouping Operator
  2720. function parseGroupExpression() {
  2721. var expr, expressions, startToken, i, params = [];
  2722. expect('(');
  2723. if (match(')')) {
  2724. lex();
  2725. if (!match('=>')) {
  2726. expect('=>');
  2727. }
  2728. return {
  2729. type: PlaceHolders.ArrowParameterPlaceHolder,
  2730. params: [],
  2731. rawParams: []
  2732. };
  2733. }
  2734. startToken = lookahead;
  2735. if (match('...')) {
  2736. expr = parseRestElement(params);
  2737. expect(')');
  2738. if (!match('=>')) {
  2739. expect('=>');
  2740. }
  2741. return {
  2742. type: PlaceHolders.ArrowParameterPlaceHolder,
  2743. params: [expr]
  2744. };
  2745. }
  2746. isBindingElement = true;
  2747. expr = inheritCoverGrammar(parseAssignmentExpression);
  2748. if (match(',')) {
  2749. isAssignmentTarget = false;
  2750. expressions = [expr];
  2751. while (startIndex < length) {
  2752. if (!match(',')) {
  2753. break;
  2754. }
  2755. lex();
  2756. if (match('...')) {
  2757. if (!isBindingElement) {
  2758. throwUnexpectedToken(lookahead);
  2759. }
  2760. expressions.push(parseRestElement(params));
  2761. expect(')');
  2762. if (!match('=>')) {
  2763. expect('=>');
  2764. }
  2765. isBindingElement = false;
  2766. for (i = 0; i < expressions.length; i++) {
  2767. reinterpretExpressionAsPattern(expressions[i]);
  2768. }
  2769. return {
  2770. type: PlaceHolders.ArrowParameterPlaceHolder,
  2771. params: expressions
  2772. };
  2773. }
  2774. expressions.push(inheritCoverGrammar(parseAssignmentExpression));
  2775. }
  2776. expr = new WrappingNode(startToken).finishSequenceExpression(expressions);
  2777. }
  2778. expect(')');
  2779. if (match('=>')) {
  2780. if (expr.type === Syntax.Identifier && expr.name === 'yield') {
  2781. return {
  2782. type: PlaceHolders.ArrowParameterPlaceHolder,
  2783. params: [expr]
  2784. };
  2785. }
  2786. if (!isBindingElement) {
  2787. throwUnexpectedToken(lookahead);
  2788. }
  2789. if (expr.type === Syntax.SequenceExpression) {
  2790. for (i = 0; i < expr.expressions.length; i++) {
  2791. reinterpretExpressionAsPattern(expr.expressions[i]);
  2792. }
  2793. } else {
  2794. reinterpretExpressionAsPattern(expr);
  2795. }
  2796. expr = {
  2797. type: PlaceHolders.ArrowParameterPlaceHolder,
  2798. params: expr.type === Syntax.SequenceExpression ? expr.expressions : [expr]
  2799. };
  2800. }
  2801. isBindingElement = false;
  2802. return expr;
  2803. }
  2804. // ECMA-262 12.2 Primary Expressions
  2805. function parsePrimaryExpression() {
  2806. var type, token, expr, node;
  2807. if (match('(')) {
  2808. isBindingElement = false;
  2809. return inheritCoverGrammar(parseGroupExpression);
  2810. }
  2811. if (match('[')) {
  2812. return inheritCoverGrammar(parseArrayInitializer);
  2813. }
  2814. if (match('{')) {
  2815. return inheritCoverGrammar(parseObjectInitializer);
  2816. }
  2817. type = lookahead.type;
  2818. node = new Node();
  2819. if (type === Token.Identifier) {
  2820. if (state.sourceType === 'module' && lookahead.value === 'await') {
  2821. tolerateUnexpectedToken(lookahead);
  2822. }
  2823. expr = node.finishIdentifier(lex().value);
  2824. } else if (type === Token.StringLiteral || type === Token.NumericLiteral) {
  2825. isAssignmentTarget = isBindingElement = false;
  2826. if (strict && lookahead.octal) {
  2827. tolerateUnexpectedToken(lookahead, Messages.StrictOctalLiteral);
  2828. }
  2829. expr = node.finishLiteral(lex());
  2830. } else if (type === Token.Keyword) {
  2831. if (!strict && state.allowYield && matchKeyword('yield')) {
  2832. return parseNonComputedProperty();
  2833. }
  2834. if (!strict && matchKeyword('let')) {
  2835. return node.finishIdentifier(lex().value);
  2836. }
  2837. isAssignmentTarget = isBindingElement = false;
  2838. if (matchKeyword('function')) {
  2839. return parseFunctionExpression();
  2840. }
  2841. if (matchKeyword('this')) {
  2842. lex();
  2843. return node.finishThisExpression();
  2844. }
  2845. if (matchKeyword('class')) {
  2846. return parseClassExpression();
  2847. }
  2848. throwUnexpectedToken(lex());
  2849. } else if (type === Token.BooleanLiteral) {
  2850. isAssignmentTarget = isBindingElement = false;
  2851. token = lex();
  2852. token.value = (token.value === 'true');
  2853. expr = node.finishLiteral(token);
  2854. } else if (type === Token.NullLiteral) {
  2855. isAssignmentTarget = isBindingElement = false;
  2856. token = lex();
  2857. token.value = null;
  2858. expr = node.finishLiteral(token);
  2859. } else if (match('/') || match('/=')) {
  2860. isAssignmentTarget = isBindingElement = false;
  2861. index = startIndex;
  2862. if (typeof extra.tokens !== 'undefined') {
  2863. token = collectRegex();
  2864. } else {
  2865. token = scanRegExp();
  2866. }
  2867. lex();
  2868. expr = node.finishLiteral(token);
  2869. } else if (type === Token.Template) {
  2870. expr = parseTemplateLiteral();
  2871. } else {
  2872. throwUnexpectedToken(lex());
  2873. }
  2874. return expr;
  2875. }
  2876. // ECMA-262 12.3 Left-Hand-Side Expressions
  2877. function parseArguments() {
  2878. var args = [], expr;
  2879. expect('(');
  2880. if (!match(')')) {
  2881. while (startIndex < length) {
  2882. if (match('...')) {
  2883. expr = new Node();
  2884. lex();
  2885. expr.finishSpreadElement(isolateCoverGrammar(parseAssignmentExpression));
  2886. } else {
  2887. expr = isolateCoverGrammar(parseAssignmentExpression);
  2888. }
  2889. args.push(expr);
  2890. if (match(')')) {
  2891. break;
  2892. }
  2893. expectCommaSeparator();
  2894. }
  2895. }
  2896. expect(')');
  2897. return args;
  2898. }
  2899. function parseNonComputedProperty() {
  2900. var token, node = new Node();
  2901. token = lex();
  2902. if (!isIdentifierName(token)) {
  2903. throwUnexpectedToken(token);
  2904. }
  2905. return node.finishIdentifier(token.value);
  2906. }
  2907. function parseNonComputedMember() {
  2908. expect('.');
  2909. return parseNonComputedProperty();
  2910. }
  2911. function parseComputedMember() {
  2912. var expr;
  2913. expect('[');
  2914. expr = isolateCoverGrammar(parseExpression);
  2915. expect(']');
  2916. return expr;
  2917. }
  2918. // ECMA-262 12.3.3 The new Operator
  2919. function parseNewExpression() {
  2920. var callee, args, node = new Node();
  2921. expectKeyword('new');
  2922. if (match('.')) {
  2923. lex();
  2924. if (lookahead.type === Token.Identifier && lookahead.value === 'target') {
  2925. if (state.inFunctionBody) {
  2926. lex();
  2927. return node.finishMetaProperty('new', 'target');
  2928. }
  2929. }
  2930. throwUnexpectedToken(lookahead);
  2931. }
  2932. callee = isolateCoverGrammar(parseLeftHandSideExpression);
  2933. args = match('(') ? parseArguments() : [];
  2934. isAssignmentTarget = isBindingElement = false;
  2935. return node.finishNewExpression(callee, args);
  2936. }
  2937. // ECMA-262 12.3.4 Function Calls
  2938. function parseLeftHandSideExpressionAllowCall() {
  2939. var quasi, expr, args, property, startToken, previousAllowIn = state.allowIn;
  2940. startToken = lookahead;
  2941. state.allowIn = true;
  2942. if (matchKeyword('super') && state.inFunctionBody) {
  2943. expr = new Node();
  2944. lex();
  2945. expr = expr.finishSuper();
  2946. if (!match('(') && !match('.') && !match('[')) {
  2947. throwUnexpectedToken(lookahead);
  2948. }
  2949. } else {
  2950. expr = inheritCoverGrammar(matchKeyword('new') ? parseNewExpression : parsePrimaryExpression);
  2951. }
  2952. for (;;) {
  2953. if (match('.')) {
  2954. isBindingElement = false;
  2955. isAssignmentTarget = true;
  2956. property = parseNonComputedMember();
  2957. expr = new WrappingNode(startToken).finishMemberExpression('.', expr, property);
  2958. } else if (match('(')) {
  2959. isBindingElement = false;
  2960. isAssignmentTarget = false;
  2961. args = parseArguments();
  2962. expr = new WrappingNode(startToken).finishCallExpression(expr, args);
  2963. } else if (match('[')) {
  2964. isBindingElement = false;
  2965. isAssignmentTarget = true;
  2966. property = parseComputedMember();
  2967. expr = new WrappingNode(startToken).finishMemberExpression('[', expr, property);
  2968. } else if (lookahead.type === Token.Template && lookahead.head) {
  2969. quasi = parseTemplateLiteral();
  2970. expr = new WrappingNode(startToken).finishTaggedTemplateExpression(expr, quasi);
  2971. } else {
  2972. break;
  2973. }
  2974. }
  2975. state.allowIn = previousAllowIn;
  2976. return expr;
  2977. }
  2978. // ECMA-262 12.3 Left-Hand-Side Expressions
  2979. function parseLeftHandSideExpression() {
  2980. var quasi, expr, property, startToken;
  2981. assert(state.allowIn, 'callee of new expression always allow in keyword.');
  2982. startToken = lookahead;
  2983. if (matchKeyword('super') && state.inFunctionBody) {
  2984. expr = new Node();
  2985. lex();
  2986. expr = expr.finishSuper();
  2987. if (!match('[') && !match('.')) {
  2988. throwUnexpectedToken(lookahead);
  2989. }
  2990. } else {
  2991. expr = inheritCoverGrammar(matchKeyword('new') ? parseNewExpression : parsePrimaryExpression);
  2992. }
  2993. for (;;) {
  2994. if (match('[')) {
  2995. isBindingElement = false;
  2996. isAssignmentTarget = true;
  2997. property = parseComputedMember();
  2998. expr = new WrappingNode(startToken).finishMemberExpression('[', expr, property);
  2999. } else if (match('.')) {
  3000. isBindingElement = false;
  3001. isAssignmentTarget = true;
  3002. property = parseNonComputedMember();
  3003. expr = new WrappingNode(startToken).finishMemberExpression('.', expr, property);
  3004. } else if (lookahead.type === Token.Template && lookahead.head) {
  3005. quasi = parseTemplateLiteral();
  3006. expr = new WrappingNode(startToken).finishTaggedTemplateExpression(expr, quasi);
  3007. } else {
  3008. break;
  3009. }
  3010. }
  3011. return expr;
  3012. }
  3013. // ECMA-262 12.4 Postfix Expressions
  3014. function parsePostfixExpression() {
  3015. var expr, token, startToken = lookahead;
  3016. expr = inheritCoverGrammar(parseLeftHandSideExpressionAllowCall);
  3017. if (!hasLineTerminator && lookahead.type === Token.Punctuator) {
  3018. if (match('++') || match('--')) {
  3019. // ECMA-262 11.3.1, 11.3.2
  3020. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  3021. tolerateError(Messages.StrictLHSPostfix);
  3022. }
  3023. if (!isAssignmentTarget) {
  3024. tolerateError(Messages.InvalidLHSInAssignment);
  3025. }
  3026. isAssignmentTarget = isBindingElement = false;
  3027. token = lex();
  3028. expr = new WrappingNode(startToken).finishPostfixExpression(token.value, expr);
  3029. }
  3030. }
  3031. return expr;
  3032. }
  3033. // ECMA-262 12.5 Unary Operators
  3034. function parseUnaryExpression() {
  3035. var token, expr, startToken;
  3036. if (lookahead.type !== Token.Punctuator && lookahead.type !== Token.Keyword) {
  3037. expr = parsePostfixExpression();
  3038. } else if (match('++') || match('--')) {
  3039. startToken = lookahead;
  3040. token = lex();
  3041. expr = inheritCoverGrammar(parseUnaryExpression);
  3042. // ECMA-262 11.4.4, 11.4.5
  3043. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  3044. tolerateError(Messages.StrictLHSPrefix);
  3045. }
  3046. if (!isAssignmentTarget) {
  3047. tolerateError(Messages.InvalidLHSInAssignment);
  3048. }
  3049. expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr);
  3050. isAssignmentTarget = isBindingElement = false;
  3051. } else if (match('+') || match('-') || match('~') || match('!')) {
  3052. startToken = lookahead;
  3053. token = lex();
  3054. expr = inheritCoverGrammar(parseUnaryExpression);
  3055. expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr);
  3056. isAssignmentTarget = isBindingElement = false;
  3057. } else if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) {
  3058. startToken = lookahead;
  3059. token = lex();
  3060. expr = inheritCoverGrammar(parseUnaryExpression);
  3061. expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr);
  3062. if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) {
  3063. tolerateError(Messages.StrictDelete);
  3064. }
  3065. isAssignmentTarget = isBindingElement = false;
  3066. } else {
  3067. expr = parsePostfixExpression();
  3068. }
  3069. return expr;
  3070. }
  3071. function binaryPrecedence(token, allowIn) {
  3072. var prec = 0;
  3073. if (token.type !== Token.Punctuator && token.type !== Token.Keyword) {
  3074. return 0;
  3075. }
  3076. switch (token.value) {
  3077. case '||':
  3078. prec = 1;
  3079. break;
  3080. case '&&':
  3081. prec = 2;
  3082. break;
  3083. case '|':
  3084. prec = 3;
  3085. break;
  3086. case '^':
  3087. prec = 4;
  3088. break;
  3089. case '&':
  3090. prec = 5;
  3091. break;
  3092. case '==':
  3093. case '!=':
  3094. case '===':
  3095. case '!==':
  3096. prec = 6;
  3097. break;
  3098. case '<':
  3099. case '>':
  3100. case '<=':
  3101. case '>=':
  3102. case 'instanceof':
  3103. prec = 7;
  3104. break;
  3105. case 'in':
  3106. prec = allowIn ? 7 : 0;
  3107. break;
  3108. case '<<':
  3109. case '>>':
  3110. case '>>>':
  3111. prec = 8;
  3112. break;
  3113. case '+':
  3114. case '-':
  3115. prec = 9;
  3116. break;
  3117. case '*':
  3118. case '/':
  3119. case '%':
  3120. prec = 11;
  3121. break;
  3122. default:
  3123. break;
  3124. }
  3125. return prec;
  3126. }
  3127. // ECMA-262 12.6 Multiplicative Operators
  3128. // ECMA-262 12.7 Additive Operators
  3129. // ECMA-262 12.8 Bitwise Shift Operators
  3130. // ECMA-262 12.9 Relational Operators
  3131. // ECMA-262 12.10 Equality Operators
  3132. // ECMA-262 12.11 Binary Bitwise Operators
  3133. // ECMA-262 12.12 Binary Logical Operators
  3134. function parseBinaryExpression() {
  3135. var marker, markers, expr, token, prec, stack, right, operator, left, i;
  3136. marker = lookahead;
  3137. left = inheritCoverGrammar(parseUnaryExpression);
  3138. token = lookahead;
  3139. prec = binaryPrecedence(token, state.allowIn);
  3140. if (prec === 0) {
  3141. return left;
  3142. }
  3143. isAssignmentTarget = isBindingElement = false;
  3144. token.prec = prec;
  3145. lex();
  3146. markers = [marker, lookahead];
  3147. right = isolateCoverGrammar(parseUnaryExpression);
  3148. stack = [left, token, right];
  3149. while ((prec = binaryPrecedence(lookahead, state.allowIn)) > 0) {
  3150. // Reduce: make a binary expression from the three topmost entries.
  3151. while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) {
  3152. right = stack.pop();
  3153. operator = stack.pop().value;
  3154. left = stack.pop();
  3155. markers.pop();
  3156. expr = new WrappingNode(markers[markers.length - 1]).finishBinaryExpression(operator, left, right);
  3157. stack.push(expr);
  3158. }
  3159. // Shift.
  3160. token = lex();
  3161. token.prec = prec;
  3162. stack.push(token);
  3163. markers.push(lookahead);
  3164. expr = isolateCoverGrammar(parseUnaryExpression);
  3165. stack.push(expr);
  3166. }
  3167. // Final reduce to clean-up the stack.
  3168. i = stack.length - 1;
  3169. expr = stack[i];
  3170. markers.pop();
  3171. while (i > 1) {
  3172. expr = new WrappingNode(markers.pop()).finishBinaryExpression(stack[i - 1].value, stack[i - 2], expr);
  3173. i -= 2;
  3174. }
  3175. return expr;
  3176. }
  3177. // ECMA-262 12.13 Conditional Operator
  3178. function parseConditionalExpression() {
  3179. var expr, previousAllowIn, consequent, alternate, startToken;
  3180. startToken = lookahead;
  3181. expr = inheritCoverGrammar(parseBinaryExpression);
  3182. if (match('?')) {
  3183. lex();
  3184. previousAllowIn = state.allowIn;
  3185. state.allowIn = true;
  3186. consequent = isolateCoverGrammar(parseAssignmentExpression);
  3187. state.allowIn = previousAllowIn;
  3188. expect(':');
  3189. alternate = isolateCoverGrammar(parseAssignmentExpression);
  3190. expr = new WrappingNode(startToken).finishConditionalExpression(expr, consequent, alternate);
  3191. isAssignmentTarget = isBindingElement = false;
  3192. }
  3193. return expr;
  3194. }
  3195. // ECMA-262 14.2 Arrow Function Definitions
  3196. function parseConciseBody() {
  3197. if (match('{')) {
  3198. return parseFunctionSourceElements();
  3199. }
  3200. return isolateCoverGrammar(parseAssignmentExpression);
  3201. }
  3202. function checkPatternParam(options, param) {
  3203. var i;
  3204. switch (param.type) {
  3205. case Syntax.Identifier:
  3206. validateParam(options, param, param.name);
  3207. break;
  3208. case Syntax.RestElement:
  3209. checkPatternParam(options, param.argument);
  3210. break;
  3211. case Syntax.AssignmentPattern:
  3212. checkPatternParam(options, param.left);
  3213. break;
  3214. case Syntax.ArrayPattern:
  3215. for (i = 0; i < param.elements.length; i++) {
  3216. if (param.elements[i] !== null) {
  3217. checkPatternParam(options, param.elements[i]);
  3218. }
  3219. }
  3220. break;
  3221. case Syntax.YieldExpression:
  3222. break;
  3223. default:
  3224. assert(param.type === Syntax.ObjectPattern, 'Invalid type');
  3225. for (i = 0; i < param.properties.length; i++) {
  3226. checkPatternParam(options, param.properties[i].value);
  3227. }
  3228. break;
  3229. }
  3230. }
  3231. function reinterpretAsCoverFormalsList(expr) {
  3232. var i, len, param, params, defaults, defaultCount, options, token;
  3233. defaults = [];
  3234. defaultCount = 0;
  3235. params = [expr];
  3236. switch (expr.type) {
  3237. case Syntax.Identifier:
  3238. break;
  3239. case PlaceHolders.ArrowParameterPlaceHolder:
  3240. params = expr.params;
  3241. break;
  3242. default:
  3243. return null;
  3244. }
  3245. options = {
  3246. paramSet: {}
  3247. };
  3248. for (i = 0, len = params.length; i < len; i += 1) {
  3249. param = params[i];
  3250. switch (param.type) {
  3251. case Syntax.AssignmentPattern:
  3252. params[i] = param.left;
  3253. if (param.right.type === Syntax.YieldExpression) {
  3254. if (param.right.argument) {
  3255. throwUnexpectedToken(lookahead);
  3256. }
  3257. param.right.type = Syntax.Identifier;
  3258. param.right.name = 'yield';
  3259. delete param.right.argument;
  3260. delete param.right.delegate;
  3261. }
  3262. defaults.push(param.right);
  3263. ++defaultCount;
  3264. checkPatternParam(options, param.left);
  3265. break;
  3266. default:
  3267. checkPatternParam(options, param);
  3268. params[i] = param;
  3269. defaults.push(null);
  3270. break;
  3271. }
  3272. }
  3273. if (strict || !state.allowYield) {
  3274. for (i = 0, len = params.length; i < len; i += 1) {
  3275. param = params[i];
  3276. if (param.type === Syntax.YieldExpression) {
  3277. throwUnexpectedToken(lookahead);
  3278. }
  3279. }
  3280. }
  3281. if (options.message === Messages.StrictParamDupe) {
  3282. token = strict ? options.stricted : options.firstRestricted;
  3283. throwUnexpectedToken(token, options.message);
  3284. }
  3285. if (defaultCount === 0) {
  3286. defaults = [];
  3287. }
  3288. return {
  3289. params: params,
  3290. defaults: defaults,
  3291. stricted: options.stricted,
  3292. firstRestricted: options.firstRestricted,
  3293. message: options.message
  3294. };
  3295. }
  3296. function parseArrowFunctionExpression(options, node) {
  3297. var previousStrict, previousAllowYield, body;
  3298. if (hasLineTerminator) {
  3299. tolerateUnexpectedToken(lookahead);
  3300. }
  3301. expect('=>');
  3302. previousStrict = strict;
  3303. previousAllowYield = state.allowYield;
  3304. state.allowYield = true;
  3305. body = parseConciseBody();
  3306. if (strict && options.firstRestricted) {
  3307. throwUnexpectedToken(options.firstRestricted, options.message);
  3308. }
  3309. if (strict && options.stricted) {
  3310. tolerateUnexpectedToken(options.stricted, options.message);
  3311. }
  3312. strict = previousStrict;
  3313. state.allowYield = previousAllowYield;
  3314. return node.finishArrowFunctionExpression(options.params, options.defaults, body, body.type !== Syntax.BlockStatement);
  3315. }
  3316. // ECMA-262 14.4 Yield expression
  3317. function parseYieldExpression() {
  3318. var argument, expr, delegate, previousAllowYield;
  3319. argument = null;
  3320. expr = new Node();
  3321. delegate = false;
  3322. expectKeyword('yield');
  3323. if (!hasLineTerminator) {
  3324. previousAllowYield = state.allowYield;
  3325. state.allowYield = false;
  3326. delegate = match('*');
  3327. if (delegate) {
  3328. lex();
  3329. argument = parseAssignmentExpression();
  3330. } else {
  3331. if (!match(';') && !match('}') && !match(')') && lookahead.type !== Token.EOF) {
  3332. argument = parseAssignmentExpression();
  3333. }
  3334. }
  3335. state.allowYield = previousAllowYield;
  3336. }
  3337. return expr.finishYieldExpression(argument, delegate);
  3338. }
  3339. // ECMA-262 12.14 Assignment Operators
  3340. function parseAssignmentExpression() {
  3341. var token, expr, right, list, startToken;
  3342. startToken = lookahead;
  3343. token = lookahead;
  3344. if (!state.allowYield && matchKeyword('yield')) {
  3345. return parseYieldExpression();
  3346. }
  3347. expr = parseConditionalExpression();
  3348. if (expr.type === PlaceHolders.ArrowParameterPlaceHolder || match('=>')) {
  3349. isAssignmentTarget = isBindingElement = false;
  3350. list = reinterpretAsCoverFormalsList(expr);
  3351. if (list) {
  3352. firstCoverInitializedNameError = null;
  3353. return parseArrowFunctionExpression(list, new WrappingNode(startToken));
  3354. }
  3355. return expr;
  3356. }
  3357. if (matchAssign()) {
  3358. if (!isAssignmentTarget) {
  3359. tolerateError(Messages.InvalidLHSInAssignment);
  3360. }
  3361. // ECMA-262 12.1.1
  3362. if (strict && expr.type === Syntax.Identifier) {
  3363. if (isRestrictedWord(expr.name)) {
  3364. tolerateUnexpectedToken(token, Messages.StrictLHSAssignment);
  3365. }
  3366. if (isStrictModeReservedWord(expr.name)) {
  3367. tolerateUnexpectedToken(token, Messages.StrictReservedWord);
  3368. }
  3369. }
  3370. if (!match('=')) {
  3371. isAssignmentTarget = isBindingElement = false;
  3372. } else {
  3373. reinterpretExpressionAsPattern(expr);
  3374. }
  3375. token = lex();
  3376. right = isolateCoverGrammar(parseAssignmentExpression);
  3377. expr = new WrappingNode(startToken).finishAssignmentExpression(token.value, expr, right);
  3378. firstCoverInitializedNameError = null;
  3379. }
  3380. return expr;
  3381. }
  3382. // ECMA-262 12.15 Comma Operator
  3383. function parseExpression() {
  3384. var expr, startToken = lookahead, expressions;
  3385. expr = isolateCoverGrammar(parseAssignmentExpression);
  3386. if (match(',')) {
  3387. expressions = [expr];
  3388. while (startIndex < length) {
  3389. if (!match(',')) {
  3390. break;
  3391. }
  3392. lex();
  3393. expressions.push(isolateCoverGrammar(parseAssignmentExpression));
  3394. }
  3395. expr = new WrappingNode(startToken).finishSequenceExpression(expressions);
  3396. }
  3397. return expr;
  3398. }
  3399. // ECMA-262 13.2 Block
  3400. function parseStatementListItem() {
  3401. if (lookahead.type === Token.Keyword) {
  3402. switch (lookahead.value) {
  3403. case 'export':
  3404. if (state.sourceType !== 'module') {
  3405. tolerateUnexpectedToken(lookahead, Messages.IllegalExportDeclaration);
  3406. }
  3407. return parseExportDeclaration();
  3408. case 'import':
  3409. if (state.sourceType !== 'module') {
  3410. tolerateUnexpectedToken(lookahead, Messages.IllegalImportDeclaration);
  3411. }
  3412. return parseImportDeclaration();
  3413. case 'const':
  3414. return parseLexicalDeclaration({inFor: false});
  3415. case 'function':
  3416. return parseFunctionDeclaration(new Node());
  3417. case 'class':
  3418. return parseClassDeclaration();
  3419. }
  3420. }
  3421. if (matchKeyword('let') && isLexicalDeclaration()) {
  3422. return parseLexicalDeclaration({inFor: false});
  3423. }
  3424. return parseStatement();
  3425. }
  3426. function parseStatementList() {
  3427. var list = [];
  3428. while (startIndex < length) {
  3429. if (match('}')) {
  3430. break;
  3431. }
  3432. list.push(parseStatementListItem());
  3433. }
  3434. return list;
  3435. }
  3436. function parseBlock() {
  3437. var block, node = new Node();
  3438. expect('{');
  3439. block = parseStatementList();
  3440. expect('}');
  3441. return node.finishBlockStatement(block);
  3442. }
  3443. // ECMA-262 13.3.2 Variable Statement
  3444. function parseVariableIdentifier(kind) {
  3445. var token, node = new Node();
  3446. token = lex();
  3447. if (token.type === Token.Keyword && token.value === 'yield') {
  3448. if (strict) {
  3449. tolerateUnexpectedToken(token, Messages.StrictReservedWord);
  3450. } if (!state.allowYield) {
  3451. throwUnexpectedToken(token);
  3452. }
  3453. } else if (token.type !== Token.Identifier) {
  3454. if (strict && token.type === Token.Keyword && isStrictModeReservedWord(token.value)) {
  3455. tolerateUnexpectedToken(token, Messages.StrictReservedWord);
  3456. } else {
  3457. if (strict || token.value !== 'let' || kind !== 'var') {
  3458. throwUnexpectedToken(token);
  3459. }
  3460. }
  3461. } else if (state.sourceType === 'module' && token.type === Token.Identifier && token.value === 'await') {
  3462. tolerateUnexpectedToken(token);
  3463. }
  3464. return node.finishIdentifier(token.value);
  3465. }
  3466. function parseVariableDeclaration(options) {
  3467. var init = null, id, node = new Node(), params = [];
  3468. id = parsePattern(params, 'var');
  3469. // ECMA-262 12.2.1
  3470. if (strict && isRestrictedWord(id.name)) {
  3471. tolerateError(Messages.StrictVarName);
  3472. }
  3473. if (match('=')) {
  3474. lex();
  3475. init = isolateCoverGrammar(parseAssignmentExpression);
  3476. } else if (id.type !== Syntax.Identifier && !options.inFor) {
  3477. expect('=');
  3478. }
  3479. return node.finishVariableDeclarator(id, init);
  3480. }
  3481. function parseVariableDeclarationList(options) {
  3482. var opt, list;
  3483. opt = { inFor: options.inFor };
  3484. list = [parseVariableDeclaration(opt)];
  3485. while (match(',')) {
  3486. lex();
  3487. list.push(parseVariableDeclaration(opt));
  3488. }
  3489. return list;
  3490. }
  3491. function parseVariableStatement(node) {
  3492. var declarations;
  3493. expectKeyword('var');
  3494. declarations = parseVariableDeclarationList({ inFor: false });
  3495. consumeSemicolon();
  3496. return node.finishVariableDeclaration(declarations);
  3497. }
  3498. // ECMA-262 13.3.1 Let and Const Declarations
  3499. function parseLexicalBinding(kind, options) {
  3500. var init = null, id, node = new Node(), params = [];
  3501. id = parsePattern(params, kind);
  3502. // ECMA-262 12.2.1
  3503. if (strict && id.type === Syntax.Identifier && isRestrictedWord(id.name)) {
  3504. tolerateError(Messages.StrictVarName);
  3505. }
  3506. if (kind === 'const') {
  3507. if (!matchKeyword('in') && !matchContextualKeyword('of')) {
  3508. expect('=');
  3509. init = isolateCoverGrammar(parseAssignmentExpression);
  3510. }
  3511. } else if ((!options.inFor && id.type !== Syntax.Identifier) || match('=')) {
  3512. expect('=');
  3513. init = isolateCoverGrammar(parseAssignmentExpression);
  3514. }
  3515. return node.finishVariableDeclarator(id, init);
  3516. }
  3517. function parseBindingList(kind, options) {
  3518. var list = [parseLexicalBinding(kind, options)];
  3519. while (match(',')) {
  3520. lex();
  3521. list.push(parseLexicalBinding(kind, options));
  3522. }
  3523. return list;
  3524. }
  3525. function tokenizerState() {
  3526. return {
  3527. index: index,
  3528. lineNumber: lineNumber,
  3529. lineStart: lineStart,
  3530. hasLineTerminator: hasLineTerminator,
  3531. lastIndex: lastIndex,
  3532. lastLineNumber: lastLineNumber,
  3533. lastLineStart: lastLineStart,
  3534. startIndex: startIndex,
  3535. startLineNumber: startLineNumber,
  3536. startLineStart: startLineStart,
  3537. lookahead: lookahead,
  3538. tokenCount: extra.tokens ? extra.tokens.length : 0
  3539. };
  3540. }
  3541. function resetTokenizerState(ts) {
  3542. index = ts.index;
  3543. lineNumber = ts.lineNumber;
  3544. lineStart = ts.lineStart;
  3545. hasLineTerminator = ts.hasLineTerminator;
  3546. lastIndex = ts.lastIndex;
  3547. lastLineNumber = ts.lastLineNumber;
  3548. lastLineStart = ts.lastLineStart;
  3549. startIndex = ts.startIndex;
  3550. startLineNumber = ts.startLineNumber;
  3551. startLineStart = ts.startLineStart;
  3552. lookahead = ts.lookahead;
  3553. if (extra.tokens) {
  3554. extra.tokens.splice(ts.tokenCount, extra.tokens.length);
  3555. }
  3556. }
  3557. function isLexicalDeclaration() {
  3558. var lexical, ts;
  3559. ts = tokenizerState();
  3560. lex();
  3561. lexical = (lookahead.type === Token.Identifier) || match('[') || match('{') ||
  3562. matchKeyword('let') || matchKeyword('yield');
  3563. resetTokenizerState(ts);
  3564. return lexical;
  3565. }
  3566. function parseLexicalDeclaration(options) {
  3567. var kind, declarations, node = new Node();
  3568. kind = lex().value;
  3569. assert(kind === 'let' || kind === 'const', 'Lexical declaration must be either let or const');
  3570. declarations = parseBindingList(kind, options);
  3571. consumeSemicolon();
  3572. return node.finishLexicalDeclaration(declarations, kind);
  3573. }
  3574. function parseRestElement(params) {
  3575. var param, node = new Node();
  3576. lex();
  3577. if (match('{')) {
  3578. throwError(Messages.ObjectPatternAsRestParameter);
  3579. }
  3580. params.push(lookahead);
  3581. param = parseVariableIdentifier();
  3582. if (match('=')) {
  3583. throwError(Messages.DefaultRestParameter);
  3584. }
  3585. if (!match(')')) {
  3586. throwError(Messages.ParameterAfterRestParameter);
  3587. }
  3588. return node.finishRestElement(param);
  3589. }
  3590. // ECMA-262 13.4 Empty Statement
  3591. function parseEmptyStatement(node) {
  3592. expect(';');
  3593. return node.finishEmptyStatement();
  3594. }
  3595. // ECMA-262 12.4 Expression Statement
  3596. function parseExpressionStatement(node) {
  3597. var expr = parseExpression();
  3598. consumeSemicolon();
  3599. return node.finishExpressionStatement(expr);
  3600. }
  3601. // ECMA-262 13.6 If statement
  3602. function parseIfStatement(node) {
  3603. var test, consequent, alternate;
  3604. expectKeyword('if');
  3605. expect('(');
  3606. test = parseExpression();
  3607. expect(')');
  3608. consequent = parseStatement();
  3609. if (matchKeyword('else')) {
  3610. lex();
  3611. alternate = parseStatement();
  3612. } else {
  3613. alternate = null;
  3614. }
  3615. return node.finishIfStatement(test, consequent, alternate);
  3616. }
  3617. // ECMA-262 13.7 Iteration Statements
  3618. function parseDoWhileStatement(node) {
  3619. var body, test, oldInIteration;
  3620. expectKeyword('do');
  3621. oldInIteration = state.inIteration;
  3622. state.inIteration = true;
  3623. body = parseStatement();
  3624. state.inIteration = oldInIteration;
  3625. expectKeyword('while');
  3626. expect('(');
  3627. test = parseExpression();
  3628. expect(')');
  3629. if (match(';')) {
  3630. lex();
  3631. }
  3632. return node.finishDoWhileStatement(body, test);
  3633. }
  3634. function parseWhileStatement(node) {
  3635. var test, body, oldInIteration;
  3636. expectKeyword('while');
  3637. expect('(');
  3638. test = parseExpression();
  3639. expect(')');
  3640. oldInIteration = state.inIteration;
  3641. state.inIteration = true;
  3642. body = parseStatement();
  3643. state.inIteration = oldInIteration;
  3644. return node.finishWhileStatement(test, body);
  3645. }
  3646. function parseForStatement(node) {
  3647. var init, forIn, initSeq, initStartToken, test, update, left, right, kind, declarations,
  3648. body, oldInIteration, previousAllowIn = state.allowIn;
  3649. init = test = update = null;
  3650. forIn = true;
  3651. expectKeyword('for');
  3652. expect('(');
  3653. if (match(';')) {
  3654. lex();
  3655. } else {
  3656. if (matchKeyword('var')) {
  3657. init = new Node();
  3658. lex();
  3659. state.allowIn = false;
  3660. declarations = parseVariableDeclarationList({ inFor: true });
  3661. state.allowIn = previousAllowIn;
  3662. if (declarations.length === 1 && matchKeyword('in')) {
  3663. init = init.finishVariableDeclaration(declarations);
  3664. lex();
  3665. left = init;
  3666. right = parseExpression();
  3667. init = null;
  3668. } else if (declarations.length === 1 && declarations[0].init === null && matchContextualKeyword('of')) {
  3669. init = init.finishVariableDeclaration(declarations);
  3670. lex();
  3671. left = init;
  3672. right = parseAssignmentExpression();
  3673. init = null;
  3674. forIn = false;
  3675. } else {
  3676. init = init.finishVariableDeclaration(declarations);
  3677. expect(';');
  3678. }
  3679. } else if (matchKeyword('const') || matchKeyword('let')) {
  3680. init = new Node();
  3681. kind = lex().value;
  3682. if (!strict && lookahead.value === 'in') {
  3683. init = init.finishIdentifier(kind);
  3684. lex();
  3685. left = init;
  3686. right = parseExpression();
  3687. init = null;
  3688. } else {
  3689. state.allowIn = false;
  3690. declarations = parseBindingList(kind, {inFor: true});
  3691. state.allowIn = previousAllowIn;
  3692. if (declarations.length === 1 && declarations[0].init === null && matchKeyword('in')) {
  3693. init = init.finishLexicalDeclaration(declarations, kind);
  3694. lex();
  3695. left = init;
  3696. right = parseExpression();
  3697. init = null;
  3698. } else if (declarations.length === 1 && declarations[0].init === null && matchContextualKeyword('of')) {
  3699. init = init.finishLexicalDeclaration(declarations, kind);
  3700. lex();
  3701. left = init;
  3702. right = parseAssignmentExpression();
  3703. init = null;
  3704. forIn = false;
  3705. } else {
  3706. consumeSemicolon();
  3707. init = init.finishLexicalDeclaration(declarations, kind);
  3708. }
  3709. }
  3710. } else {
  3711. initStartToken = lookahead;
  3712. state.allowIn = false;
  3713. init = inheritCoverGrammar(parseAssignmentExpression);
  3714. state.allowIn = previousAllowIn;
  3715. if (matchKeyword('in')) {
  3716. if (!isAssignmentTarget) {
  3717. tolerateError(Messages.InvalidLHSInForIn);
  3718. }
  3719. lex();
  3720. reinterpretExpressionAsPattern(init);
  3721. left = init;
  3722. right = parseExpression();
  3723. init = null;
  3724. } else if (matchContextualKeyword('of')) {
  3725. if (!isAssignmentTarget) {
  3726. tolerateError(Messages.InvalidLHSInForLoop);
  3727. }
  3728. lex();
  3729. reinterpretExpressionAsPattern(init);
  3730. left = init;
  3731. right = parseAssignmentExpression();
  3732. init = null;
  3733. forIn = false;
  3734. } else {
  3735. if (match(',')) {
  3736. initSeq = [init];
  3737. while (match(',')) {
  3738. lex();
  3739. initSeq.push(isolateCoverGrammar(parseAssignmentExpression));
  3740. }
  3741. init = new WrappingNode(initStartToken).finishSequenceExpression(initSeq);
  3742. }
  3743. expect(';');
  3744. }
  3745. }
  3746. }
  3747. if (typeof left === 'undefined') {
  3748. if (!match(';')) {
  3749. test = parseExpression();
  3750. }
  3751. expect(';');
  3752. if (!match(')')) {
  3753. update = parseExpression();
  3754. }
  3755. }
  3756. expect(')');
  3757. oldInIteration = state.inIteration;
  3758. state.inIteration = true;
  3759. body = isolateCoverGrammar(parseStatement);
  3760. state.inIteration = oldInIteration;
  3761. return (typeof left === 'undefined') ?
  3762. node.finishForStatement(init, test, update, body) :
  3763. forIn ? node.finishForInStatement(left, right, body) :
  3764. node.finishForOfStatement(left, right, body);
  3765. }
  3766. // ECMA-262 13.8 The continue statement
  3767. function parseContinueStatement(node) {
  3768. var label = null, key;
  3769. expectKeyword('continue');
  3770. // Optimize the most common form: 'continue;'.
  3771. if (source.charCodeAt(startIndex) === 0x3B) {
  3772. lex();
  3773. if (!state.inIteration) {
  3774. throwError(Messages.IllegalContinue);
  3775. }
  3776. return node.finishContinueStatement(null);
  3777. }
  3778. if (hasLineTerminator) {
  3779. if (!state.inIteration) {
  3780. throwError(Messages.IllegalContinue);
  3781. }
  3782. return node.finishContinueStatement(null);
  3783. }
  3784. if (lookahead.type === Token.Identifier) {
  3785. label = parseVariableIdentifier();
  3786. key = '$' + label.name;
  3787. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  3788. throwError(Messages.UnknownLabel, label.name);
  3789. }
  3790. }
  3791. consumeSemicolon();
  3792. if (label === null && !state.inIteration) {
  3793. throwError(Messages.IllegalContinue);
  3794. }
  3795. return node.finishContinueStatement(label);
  3796. }
  3797. // ECMA-262 13.9 The break statement
  3798. function parseBreakStatement(node) {
  3799. var label = null, key;
  3800. expectKeyword('break');
  3801. // Catch the very common case first: immediately a semicolon (U+003B).
  3802. if (source.charCodeAt(lastIndex) === 0x3B) {
  3803. lex();
  3804. if (!(state.inIteration || state.inSwitch)) {
  3805. throwError(Messages.IllegalBreak);
  3806. }
  3807. return node.finishBreakStatement(null);
  3808. }
  3809. if (hasLineTerminator) {
  3810. if (!(state.inIteration || state.inSwitch)) {
  3811. throwError(Messages.IllegalBreak);
  3812. }
  3813. } else if (lookahead.type === Token.Identifier) {
  3814. label = parseVariableIdentifier();
  3815. key = '$' + label.name;
  3816. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  3817. throwError(Messages.UnknownLabel, label.name);
  3818. }
  3819. }
  3820. consumeSemicolon();
  3821. if (label === null && !(state.inIteration || state.inSwitch)) {
  3822. throwError(Messages.IllegalBreak);
  3823. }
  3824. return node.finishBreakStatement(label);
  3825. }
  3826. // ECMA-262 13.10 The return statement
  3827. function parseReturnStatement(node) {
  3828. var argument = null;
  3829. expectKeyword('return');
  3830. if (!state.inFunctionBody) {
  3831. tolerateError(Messages.IllegalReturn);
  3832. }
  3833. // 'return' followed by a space and an identifier is very common.
  3834. if (source.charCodeAt(lastIndex) === 0x20) {
  3835. if (isIdentifierStart(source.charCodeAt(lastIndex + 1))) {
  3836. argument = parseExpression();
  3837. consumeSemicolon();
  3838. return node.finishReturnStatement(argument);
  3839. }
  3840. }
  3841. if (hasLineTerminator) {
  3842. // HACK
  3843. return node.finishReturnStatement(null);
  3844. }
  3845. if (!match(';')) {
  3846. if (!match('}') && lookahead.type !== Token.EOF) {
  3847. argument = parseExpression();
  3848. }
  3849. }
  3850. consumeSemicolon();
  3851. return node.finishReturnStatement(argument);
  3852. }
  3853. // ECMA-262 13.11 The with statement
  3854. function parseWithStatement(node) {
  3855. var object, body;
  3856. if (strict) {
  3857. tolerateError(Messages.StrictModeWith);
  3858. }
  3859. expectKeyword('with');
  3860. expect('(');
  3861. object = parseExpression();
  3862. expect(')');
  3863. body = parseStatement();
  3864. return node.finishWithStatement(object, body);
  3865. }
  3866. // ECMA-262 13.12 The switch statement
  3867. function parseSwitchCase() {
  3868. var test, consequent = [], statement, node = new Node();
  3869. if (matchKeyword('default')) {
  3870. lex();
  3871. test = null;
  3872. } else {
  3873. expectKeyword('case');
  3874. test = parseExpression();
  3875. }
  3876. expect(':');
  3877. while (startIndex < length) {
  3878. if (match('}') || matchKeyword('default') || matchKeyword('case')) {
  3879. break;
  3880. }
  3881. statement = parseStatementListItem();
  3882. consequent.push(statement);
  3883. }
  3884. return node.finishSwitchCase(test, consequent);
  3885. }
  3886. function parseSwitchStatement(node) {
  3887. var discriminant, cases, clause, oldInSwitch, defaultFound;
  3888. expectKeyword('switch');
  3889. expect('(');
  3890. discriminant = parseExpression();
  3891. expect(')');
  3892. expect('{');
  3893. cases = [];
  3894. if (match('}')) {
  3895. lex();
  3896. return node.finishSwitchStatement(discriminant, cases);
  3897. }
  3898. oldInSwitch = state.inSwitch;
  3899. state.inSwitch = true;
  3900. defaultFound = false;
  3901. while (startIndex < length) {
  3902. if (match('}')) {
  3903. break;
  3904. }
  3905. clause = parseSwitchCase();
  3906. if (clause.test === null) {
  3907. if (defaultFound) {
  3908. throwError(Messages.MultipleDefaultsInSwitch);
  3909. }
  3910. defaultFound = true;
  3911. }
  3912. cases.push(clause);
  3913. }
  3914. state.inSwitch = oldInSwitch;
  3915. expect('}');
  3916. return node.finishSwitchStatement(discriminant, cases);
  3917. }
  3918. // ECMA-262 13.14 The throw statement
  3919. function parseThrowStatement(node) {
  3920. var argument;
  3921. expectKeyword('throw');
  3922. if (hasLineTerminator) {
  3923. throwError(Messages.NewlineAfterThrow);
  3924. }
  3925. argument = parseExpression();
  3926. consumeSemicolon();
  3927. return node.finishThrowStatement(argument);
  3928. }
  3929. // ECMA-262 13.15 The try statement
  3930. function parseCatchClause() {
  3931. var param, params = [], paramMap = {}, key, i, body, node = new Node();
  3932. expectKeyword('catch');
  3933. expect('(');
  3934. if (match(')')) {
  3935. throwUnexpectedToken(lookahead);
  3936. }
  3937. param = parsePattern(params);
  3938. for (i = 0; i < params.length; i++) {
  3939. key = '$' + params[i].value;
  3940. if (Object.prototype.hasOwnProperty.call(paramMap, key)) {
  3941. tolerateError(Messages.DuplicateBinding, params[i].value);
  3942. }
  3943. paramMap[key] = true;
  3944. }
  3945. // ECMA-262 12.14.1
  3946. if (strict && isRestrictedWord(param.name)) {
  3947. tolerateError(Messages.StrictCatchVariable);
  3948. }
  3949. expect(')');
  3950. body = parseBlock();
  3951. return node.finishCatchClause(param, body);
  3952. }
  3953. function parseTryStatement(node) {
  3954. var block, handler = null, finalizer = null;
  3955. expectKeyword('try');
  3956. block = parseBlock();
  3957. if (matchKeyword('catch')) {
  3958. handler = parseCatchClause();
  3959. }
  3960. if (matchKeyword('finally')) {
  3961. lex();
  3962. finalizer = parseBlock();
  3963. }
  3964. if (!handler && !finalizer) {
  3965. throwError(Messages.NoCatchOrFinally);
  3966. }
  3967. return node.finishTryStatement(block, handler, finalizer);
  3968. }
  3969. // ECMA-262 13.16 The debugger statement
  3970. function parseDebuggerStatement(node) {
  3971. expectKeyword('debugger');
  3972. consumeSemicolon();
  3973. return node.finishDebuggerStatement();
  3974. }
  3975. // 13 Statements
  3976. function parseStatement() {
  3977. var type = lookahead.type,
  3978. expr,
  3979. labeledBody,
  3980. key,
  3981. node;
  3982. if (type === Token.EOF) {
  3983. throwUnexpectedToken(lookahead);
  3984. }
  3985. if (type === Token.Punctuator && lookahead.value === '{') {
  3986. return parseBlock();
  3987. }
  3988. isAssignmentTarget = isBindingElement = true;
  3989. node = new Node();
  3990. if (type === Token.Punctuator) {
  3991. switch (lookahead.value) {
  3992. case ';':
  3993. return parseEmptyStatement(node);
  3994. case '(':
  3995. return parseExpressionStatement(node);
  3996. default:
  3997. break;
  3998. }
  3999. } else if (type === Token.Keyword) {
  4000. switch (lookahead.value) {
  4001. case 'break':
  4002. return parseBreakStatement(node);
  4003. case 'continue':
  4004. return parseContinueStatement(node);
  4005. case 'debugger':
  4006. return parseDebuggerStatement(node);
  4007. case 'do':
  4008. return parseDoWhileStatement(node);
  4009. case 'for':
  4010. return parseForStatement(node);
  4011. case 'function':
  4012. return parseFunctionDeclaration(node);
  4013. case 'if':
  4014. return parseIfStatement(node);
  4015. case 'return':
  4016. return parseReturnStatement(node);
  4017. case 'switch':
  4018. return parseSwitchStatement(node);
  4019. case 'throw':
  4020. return parseThrowStatement(node);
  4021. case 'try':
  4022. return parseTryStatement(node);
  4023. case 'var':
  4024. return parseVariableStatement(node);
  4025. case 'while':
  4026. return parseWhileStatement(node);
  4027. case 'with':
  4028. return parseWithStatement(node);
  4029. default:
  4030. break;
  4031. }
  4032. }
  4033. expr = parseExpression();
  4034. // ECMA-262 12.12 Labelled Statements
  4035. if ((expr.type === Syntax.Identifier) && match(':')) {
  4036. lex();
  4037. key = '$' + expr.name;
  4038. if (Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  4039. throwError(Messages.Redeclaration, 'Label', expr.name);
  4040. }
  4041. state.labelSet[key] = true;
  4042. labeledBody = parseStatement();
  4043. delete state.labelSet[key];
  4044. return node.finishLabeledStatement(expr, labeledBody);
  4045. }
  4046. consumeSemicolon();
  4047. return node.finishExpressionStatement(expr);
  4048. }
  4049. // ECMA-262 14.1 Function Definition
  4050. function parseFunctionSourceElements() {
  4051. var statement, body = [], token, directive, firstRestricted,
  4052. oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody,
  4053. node = new Node();
  4054. expect('{');
  4055. while (startIndex < length) {
  4056. if (lookahead.type !== Token.StringLiteral) {
  4057. break;
  4058. }
  4059. token = lookahead;
  4060. statement = parseStatementListItem();
  4061. body.push(statement);
  4062. if (statement.expression.type !== Syntax.Literal) {
  4063. // this is not directive
  4064. break;
  4065. }
  4066. directive = source.slice(token.start + 1, token.end - 1);
  4067. if (directive === 'use strict') {
  4068. strict = true;
  4069. if (firstRestricted) {
  4070. tolerateUnexpectedToken(firstRestricted, Messages.StrictOctalLiteral);
  4071. }
  4072. } else {
  4073. if (!firstRestricted && token.octal) {
  4074. firstRestricted = token;
  4075. }
  4076. }
  4077. }
  4078. oldLabelSet = state.labelSet;
  4079. oldInIteration = state.inIteration;
  4080. oldInSwitch = state.inSwitch;
  4081. oldInFunctionBody = state.inFunctionBody;
  4082. state.labelSet = {};
  4083. state.inIteration = false;
  4084. state.inSwitch = false;
  4085. state.inFunctionBody = true;
  4086. while (startIndex < length) {
  4087. if (match('}')) {
  4088. break;
  4089. }
  4090. body.push(parseStatementListItem());
  4091. }
  4092. expect('}');
  4093. state.labelSet = oldLabelSet;
  4094. state.inIteration = oldInIteration;
  4095. state.inSwitch = oldInSwitch;
  4096. state.inFunctionBody = oldInFunctionBody;
  4097. return node.finishBlockStatement(body);
  4098. }
  4099. function validateParam(options, param, name) {
  4100. var key = '$' + name;
  4101. if (strict) {
  4102. if (isRestrictedWord(name)) {
  4103. options.stricted = param;
  4104. options.message = Messages.StrictParamName;
  4105. }
  4106. if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) {
  4107. options.stricted = param;
  4108. options.message = Messages.StrictParamDupe;
  4109. }
  4110. } else if (!options.firstRestricted) {
  4111. if (isRestrictedWord(name)) {
  4112. options.firstRestricted = param;
  4113. options.message = Messages.StrictParamName;
  4114. } else if (isStrictModeReservedWord(name)) {
  4115. options.firstRestricted = param;
  4116. options.message = Messages.StrictReservedWord;
  4117. } else if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) {
  4118. options.stricted = param;
  4119. options.message = Messages.StrictParamDupe;
  4120. }
  4121. }
  4122. options.paramSet[key] = true;
  4123. }
  4124. function parseParam(options) {
  4125. var token, param, params = [], i, def;
  4126. token = lookahead;
  4127. if (token.value === '...') {
  4128. param = parseRestElement(params);
  4129. validateParam(options, param.argument, param.argument.name);
  4130. options.params.push(param);
  4131. options.defaults.push(null);
  4132. return false;
  4133. }
  4134. param = parsePatternWithDefault(params);
  4135. for (i = 0; i < params.length; i++) {
  4136. validateParam(options, params[i], params[i].value);
  4137. }
  4138. if (param.type === Syntax.AssignmentPattern) {
  4139. def = param.right;
  4140. param = param.left;
  4141. ++options.defaultCount;
  4142. }
  4143. options.params.push(param);
  4144. options.defaults.push(def);
  4145. return !match(')');
  4146. }
  4147. function parseParams(firstRestricted) {
  4148. var options;
  4149. options = {
  4150. params: [],
  4151. defaultCount: 0,
  4152. defaults: [],
  4153. firstRestricted: firstRestricted
  4154. };
  4155. expect('(');
  4156. if (!match(')')) {
  4157. options.paramSet = {};
  4158. while (startIndex < length) {
  4159. if (!parseParam(options)) {
  4160. break;
  4161. }
  4162. expect(',');
  4163. }
  4164. }
  4165. expect(')');
  4166. if (options.defaultCount === 0) {
  4167. options.defaults = [];
  4168. }
  4169. return {
  4170. params: options.params,
  4171. defaults: options.defaults,
  4172. stricted: options.stricted,
  4173. firstRestricted: options.firstRestricted,
  4174. message: options.message
  4175. };
  4176. }
  4177. function parseFunctionDeclaration(node, identifierIsOptional) {
  4178. var id = null, params = [], defaults = [], body, token, stricted, tmp, firstRestricted, message, previousStrict,
  4179. isGenerator, previousAllowYield;
  4180. previousAllowYield = state.allowYield;
  4181. expectKeyword('function');
  4182. isGenerator = match('*');
  4183. if (isGenerator) {
  4184. lex();
  4185. }
  4186. if (!identifierIsOptional || !match('(')) {
  4187. token = lookahead;
  4188. id = parseVariableIdentifier();
  4189. if (strict) {
  4190. if (isRestrictedWord(token.value)) {
  4191. tolerateUnexpectedToken(token, Messages.StrictFunctionName);
  4192. }
  4193. } else {
  4194. if (isRestrictedWord(token.value)) {
  4195. firstRestricted = token;
  4196. message = Messages.StrictFunctionName;
  4197. } else if (isStrictModeReservedWord(token.value)) {
  4198. firstRestricted = token;
  4199. message = Messages.StrictReservedWord;
  4200. }
  4201. }
  4202. }
  4203. state.allowYield = !isGenerator;
  4204. tmp = parseParams(firstRestricted);
  4205. params = tmp.params;
  4206. defaults = tmp.defaults;
  4207. stricted = tmp.stricted;
  4208. firstRestricted = tmp.firstRestricted;
  4209. if (tmp.message) {
  4210. message = tmp.message;
  4211. }
  4212. previousStrict = strict;
  4213. body = parseFunctionSourceElements();
  4214. if (strict && firstRestricted) {
  4215. throwUnexpectedToken(firstRestricted, message);
  4216. }
  4217. if (strict && stricted) {
  4218. tolerateUnexpectedToken(stricted, message);
  4219. }
  4220. strict = previousStrict;
  4221. state.allowYield = previousAllowYield;
  4222. return node.finishFunctionDeclaration(id, params, defaults, body, isGenerator);
  4223. }
  4224. function parseFunctionExpression() {
  4225. var token, id = null, stricted, firstRestricted, message, tmp,
  4226. params = [], defaults = [], body, previousStrict, node = new Node(),
  4227. isGenerator, previousAllowYield;
  4228. previousAllowYield = state.allowYield;
  4229. expectKeyword('function');
  4230. isGenerator = match('*');
  4231. if (isGenerator) {
  4232. lex();
  4233. }
  4234. state.allowYield = !isGenerator;
  4235. if (!match('(')) {
  4236. token = lookahead;
  4237. id = (!strict && !isGenerator && matchKeyword('yield')) ? parseNonComputedProperty() : parseVariableIdentifier();
  4238. if (strict) {
  4239. if (isRestrictedWord(token.value)) {
  4240. tolerateUnexpectedToken(token, Messages.StrictFunctionName);
  4241. }
  4242. } else {
  4243. if (isRestrictedWord(token.value)) {
  4244. firstRestricted = token;
  4245. message = Messages.StrictFunctionName;
  4246. } else if (isStrictModeReservedWord(token.value)) {
  4247. firstRestricted = token;
  4248. message = Messages.StrictReservedWord;
  4249. }
  4250. }
  4251. }
  4252. tmp = parseParams(firstRestricted);
  4253. params = tmp.params;
  4254. defaults = tmp.defaults;
  4255. stricted = tmp.stricted;
  4256. firstRestricted = tmp.firstRestricted;
  4257. if (tmp.message) {
  4258. message = tmp.message;
  4259. }
  4260. previousStrict = strict;
  4261. body = parseFunctionSourceElements();
  4262. if (strict && firstRestricted) {
  4263. throwUnexpectedToken(firstRestricted, message);
  4264. }
  4265. if (strict && stricted) {
  4266. tolerateUnexpectedToken(stricted, message);
  4267. }
  4268. strict = previousStrict;
  4269. state.allowYield = previousAllowYield;
  4270. return node.finishFunctionExpression(id, params, defaults, body, isGenerator);
  4271. }
  4272. // ECMA-262 14.5 Class Definitions
  4273. function parseClassBody() {
  4274. var classBody, token, isStatic, hasConstructor = false, body, method, computed, key;
  4275. classBody = new Node();
  4276. expect('{');
  4277. body = [];
  4278. while (!match('}')) {
  4279. if (match(';')) {
  4280. lex();
  4281. } else {
  4282. method = new Node();
  4283. token = lookahead;
  4284. isStatic = false;
  4285. computed = match('[');
  4286. if (match('*')) {
  4287. lex();
  4288. } else {
  4289. key = parseObjectPropertyKey();
  4290. if (key.name === 'static' && (lookaheadPropertyName() || match('*'))) {
  4291. token = lookahead;
  4292. isStatic = true;
  4293. computed = match('[');
  4294. if (match('*')) {
  4295. lex();
  4296. } else {
  4297. key = parseObjectPropertyKey();
  4298. }
  4299. }
  4300. }
  4301. method = tryParseMethodDefinition(token, key, computed, method);
  4302. if (method) {
  4303. method['static'] = isStatic; // jscs:ignore requireDotNotation
  4304. if (method.kind === 'init') {
  4305. method.kind = 'method';
  4306. }
  4307. if (!isStatic) {
  4308. if (!method.computed && (method.key.name || method.key.value.toString()) === 'constructor') {
  4309. if (method.kind !== 'method' || !method.method || method.value.generator) {
  4310. throwUnexpectedToken(token, Messages.ConstructorSpecialMethod);
  4311. }
  4312. if (hasConstructor) {
  4313. throwUnexpectedToken(token, Messages.DuplicateConstructor);
  4314. } else {
  4315. hasConstructor = true;
  4316. }
  4317. method.kind = 'constructor';
  4318. }
  4319. } else {
  4320. if (!method.computed && (method.key.name || method.key.value.toString()) === 'prototype') {
  4321. throwUnexpectedToken(token, Messages.StaticPrototype);
  4322. }
  4323. }
  4324. method.type = Syntax.MethodDefinition;
  4325. delete method.method;
  4326. delete method.shorthand;
  4327. body.push(method);
  4328. } else {
  4329. throwUnexpectedToken(lookahead);
  4330. }
  4331. }
  4332. }
  4333. lex();
  4334. return classBody.finishClassBody(body);
  4335. }
  4336. function parseClassDeclaration(identifierIsOptional) {
  4337. var id = null, superClass = null, classNode = new Node(), classBody, previousStrict = strict;
  4338. strict = true;
  4339. expectKeyword('class');
  4340. if (!identifierIsOptional || lookahead.type === Token.Identifier) {
  4341. id = parseVariableIdentifier();
  4342. }
  4343. if (matchKeyword('extends')) {
  4344. lex();
  4345. superClass = isolateCoverGrammar(parseLeftHandSideExpressionAllowCall);
  4346. }
  4347. classBody = parseClassBody();
  4348. strict = previousStrict;
  4349. return classNode.finishClassDeclaration(id, superClass, classBody);
  4350. }
  4351. function parseClassExpression() {
  4352. var id = null, superClass = null, classNode = new Node(), classBody, previousStrict = strict;
  4353. strict = true;
  4354. expectKeyword('class');
  4355. if (lookahead.type === Token.Identifier) {
  4356. id = parseVariableIdentifier();
  4357. }
  4358. if (matchKeyword('extends')) {
  4359. lex();
  4360. superClass = isolateCoverGrammar(parseLeftHandSideExpressionAllowCall);
  4361. }
  4362. classBody = parseClassBody();
  4363. strict = previousStrict;
  4364. return classNode.finishClassExpression(id, superClass, classBody);
  4365. }
  4366. // ECMA-262 15.2 Modules
  4367. function parseModuleSpecifier() {
  4368. var node = new Node();
  4369. if (lookahead.type !== Token.StringLiteral) {
  4370. throwError(Messages.InvalidModuleSpecifier);
  4371. }
  4372. return node.finishLiteral(lex());
  4373. }
  4374. // ECMA-262 15.2.3 Exports
  4375. function parseExportSpecifier() {
  4376. var exported, local, node = new Node(), def;
  4377. if (matchKeyword('default')) {
  4378. // export {default} from 'something';
  4379. def = new Node();
  4380. lex();
  4381. local = def.finishIdentifier('default');
  4382. } else {
  4383. local = parseVariableIdentifier();
  4384. }
  4385. if (matchContextualKeyword('as')) {
  4386. lex();
  4387. exported = parseNonComputedProperty();
  4388. }
  4389. return node.finishExportSpecifier(local, exported);
  4390. }
  4391. function parseExportNamedDeclaration(node) {
  4392. var declaration = null,
  4393. isExportFromIdentifier,
  4394. src = null, specifiers = [];
  4395. // non-default export
  4396. if (lookahead.type === Token.Keyword) {
  4397. // covers:
  4398. // export var f = 1;
  4399. switch (lookahead.value) {
  4400. case 'let':
  4401. case 'const':
  4402. declaration = parseLexicalDeclaration({inFor: false});
  4403. return node.finishExportNamedDeclaration(declaration, specifiers, null);
  4404. case 'var':
  4405. case 'class':
  4406. case 'function':
  4407. declaration = parseStatementListItem();
  4408. return node.finishExportNamedDeclaration(declaration, specifiers, null);
  4409. }
  4410. }
  4411. expect('{');
  4412. while (!match('}')) {
  4413. isExportFromIdentifier = isExportFromIdentifier || matchKeyword('default');
  4414. specifiers.push(parseExportSpecifier());
  4415. if (!match('}')) {
  4416. expect(',');
  4417. if (match('}')) {
  4418. break;
  4419. }
  4420. }
  4421. }
  4422. expect('}');
  4423. if (matchContextualKeyword('from')) {
  4424. // covering:
  4425. // export {default} from 'foo';
  4426. // export {foo} from 'foo';
  4427. lex();
  4428. src = parseModuleSpecifier();
  4429. consumeSemicolon();
  4430. } else if (isExportFromIdentifier) {
  4431. // covering:
  4432. // export {default}; // missing fromClause
  4433. throwError(lookahead.value ?
  4434. Messages.UnexpectedToken : Messages.MissingFromClause, lookahead.value);
  4435. } else {
  4436. // cover
  4437. // export {foo};
  4438. consumeSemicolon();
  4439. }
  4440. return node.finishExportNamedDeclaration(declaration, specifiers, src);
  4441. }
  4442. function parseExportDefaultDeclaration(node) {
  4443. var declaration = null,
  4444. expression = null;
  4445. // covers:
  4446. // export default ...
  4447. expectKeyword('default');
  4448. if (matchKeyword('function')) {
  4449. // covers:
  4450. // export default function foo () {}
  4451. // export default function () {}
  4452. declaration = parseFunctionDeclaration(new Node(), true);
  4453. return node.finishExportDefaultDeclaration(declaration);
  4454. }
  4455. if (matchKeyword('class')) {
  4456. declaration = parseClassDeclaration(true);
  4457. return node.finishExportDefaultDeclaration(declaration);
  4458. }
  4459. if (matchContextualKeyword('from')) {
  4460. throwError(Messages.UnexpectedToken, lookahead.value);
  4461. }
  4462. // covers:
  4463. // export default {};
  4464. // export default [];
  4465. // export default (1 + 2);
  4466. if (match('{')) {
  4467. expression = parseObjectInitializer();
  4468. } else if (match('[')) {
  4469. expression = parseArrayInitializer();
  4470. } else {
  4471. expression = parseAssignmentExpression();
  4472. }
  4473. consumeSemicolon();
  4474. return node.finishExportDefaultDeclaration(expression);
  4475. }
  4476. function parseExportAllDeclaration(node) {
  4477. var src;
  4478. // covers:
  4479. // export * from 'foo';
  4480. expect('*');
  4481. if (!matchContextualKeyword('from')) {
  4482. throwError(lookahead.value ?
  4483. Messages.UnexpectedToken : Messages.MissingFromClause, lookahead.value);
  4484. }
  4485. lex();
  4486. src = parseModuleSpecifier();
  4487. consumeSemicolon();
  4488. return node.finishExportAllDeclaration(src);
  4489. }
  4490. function parseExportDeclaration() {
  4491. var node = new Node();
  4492. if (state.inFunctionBody) {
  4493. throwError(Messages.IllegalExportDeclaration);
  4494. }
  4495. expectKeyword('export');
  4496. if (matchKeyword('default')) {
  4497. return parseExportDefaultDeclaration(node);
  4498. }
  4499. if (match('*')) {
  4500. return parseExportAllDeclaration(node);
  4501. }
  4502. return parseExportNamedDeclaration(node);
  4503. }
  4504. // ECMA-262 15.2.2 Imports
  4505. function parseImportSpecifier() {
  4506. // import {<foo as bar>} ...;
  4507. var local, imported, node = new Node();
  4508. imported = parseNonComputedProperty();
  4509. if (matchContextualKeyword('as')) {
  4510. lex();
  4511. local = parseVariableIdentifier();
  4512. }
  4513. return node.finishImportSpecifier(local, imported);
  4514. }
  4515. function parseNamedImports() {
  4516. var specifiers = [];
  4517. // {foo, bar as bas}
  4518. expect('{');
  4519. while (!match('}')) {
  4520. specifiers.push(parseImportSpecifier());
  4521. if (!match('}')) {
  4522. expect(',');
  4523. if (match('}')) {
  4524. break;
  4525. }
  4526. }
  4527. }
  4528. expect('}');
  4529. return specifiers;
  4530. }
  4531. function parseImportDefaultSpecifier() {
  4532. // import <foo> ...;
  4533. var local, node = new Node();
  4534. local = parseNonComputedProperty();
  4535. return node.finishImportDefaultSpecifier(local);
  4536. }
  4537. function parseImportNamespaceSpecifier() {
  4538. // import <* as foo> ...;
  4539. var local, node = new Node();
  4540. expect('*');
  4541. if (!matchContextualKeyword('as')) {
  4542. throwError(Messages.NoAsAfterImportNamespace);
  4543. }
  4544. lex();
  4545. local = parseNonComputedProperty();
  4546. return node.finishImportNamespaceSpecifier(local);
  4547. }
  4548. function parseImportDeclaration() {
  4549. var specifiers = [], src, node = new Node();
  4550. if (state.inFunctionBody) {
  4551. throwError(Messages.IllegalImportDeclaration);
  4552. }
  4553. expectKeyword('import');
  4554. if (lookahead.type === Token.StringLiteral) {
  4555. // import 'foo';
  4556. src = parseModuleSpecifier();
  4557. } else {
  4558. if (match('{')) {
  4559. // import {bar}
  4560. specifiers = specifiers.concat(parseNamedImports());
  4561. } else if (match('*')) {
  4562. // import * as foo
  4563. specifiers.push(parseImportNamespaceSpecifier());
  4564. } else if (isIdentifierName(lookahead) && !matchKeyword('default')) {
  4565. // import foo
  4566. specifiers.push(parseImportDefaultSpecifier());
  4567. if (match(',')) {
  4568. lex();
  4569. if (match('*')) {
  4570. // import foo, * as foo
  4571. specifiers.push(parseImportNamespaceSpecifier());
  4572. } else if (match('{')) {
  4573. // import foo, {bar}
  4574. specifiers = specifiers.concat(parseNamedImports());
  4575. } else {
  4576. throwUnexpectedToken(lookahead);
  4577. }
  4578. }
  4579. } else {
  4580. throwUnexpectedToken(lex());
  4581. }
  4582. if (!matchContextualKeyword('from')) {
  4583. throwError(lookahead.value ?
  4584. Messages.UnexpectedToken : Messages.MissingFromClause, lookahead.value);
  4585. }
  4586. lex();
  4587. src = parseModuleSpecifier();
  4588. }
  4589. consumeSemicolon();
  4590. return node.finishImportDeclaration(specifiers, src);
  4591. }
  4592. // ECMA-262 15.1 Scripts
  4593. function parseScriptBody() {
  4594. var statement, body = [], token, directive, firstRestricted;
  4595. while (startIndex < length) {
  4596. token = lookahead;
  4597. if (token.type !== Token.StringLiteral) {
  4598. break;
  4599. }
  4600. statement = parseStatementListItem();
  4601. body.push(statement);
  4602. if (statement.expression.type !== Syntax.Literal) {
  4603. // this is not directive
  4604. break;
  4605. }
  4606. directive = source.slice(token.start + 1, token.end - 1);
  4607. if (directive === 'use strict') {
  4608. strict = true;
  4609. if (firstRestricted) {
  4610. tolerateUnexpectedToken(firstRestricted, Messages.StrictOctalLiteral);
  4611. }
  4612. } else {
  4613. if (!firstRestricted && token.octal) {
  4614. firstRestricted = token;
  4615. }
  4616. }
  4617. }
  4618. while (startIndex < length) {
  4619. statement = parseStatementListItem();
  4620. /* istanbul ignore if */
  4621. if (typeof statement === 'undefined') {
  4622. break;
  4623. }
  4624. body.push(statement);
  4625. }
  4626. return body;
  4627. }
  4628. function parseProgram() {
  4629. var body, node;
  4630. peek();
  4631. node = new Node();
  4632. body = parseScriptBody();
  4633. return node.finishProgram(body, state.sourceType);
  4634. }
  4635. function filterTokenLocation() {
  4636. var i, entry, token, tokens = [];
  4637. for (i = 0; i < extra.tokens.length; ++i) {
  4638. entry = extra.tokens[i];
  4639. token = {
  4640. type: entry.type,
  4641. value: entry.value
  4642. };
  4643. if (entry.regex) {
  4644. token.regex = {
  4645. pattern: entry.regex.pattern,
  4646. flags: entry.regex.flags
  4647. };
  4648. }
  4649. if (extra.range) {
  4650. token.range = entry.range;
  4651. }
  4652. if (extra.loc) {
  4653. token.loc = entry.loc;
  4654. }
  4655. tokens.push(token);
  4656. }
  4657. extra.tokens = tokens;
  4658. }
  4659. function tokenize(code, options, delegate) {
  4660. var toString,
  4661. tokens;
  4662. toString = String;
  4663. if (typeof code !== 'string' && !(code instanceof String)) {
  4664. code = toString(code);
  4665. }
  4666. source = code;
  4667. index = 0;
  4668. lineNumber = (source.length > 0) ? 1 : 0;
  4669. lineStart = 0;
  4670. startIndex = index;
  4671. startLineNumber = lineNumber;
  4672. startLineStart = lineStart;
  4673. length = source.length;
  4674. lookahead = null;
  4675. state = {
  4676. allowIn: true,
  4677. allowYield: true,
  4678. labelSet: {},
  4679. inFunctionBody: false,
  4680. inIteration: false,
  4681. inSwitch: false,
  4682. lastCommentStart: -1,
  4683. curlyStack: []
  4684. };
  4685. extra = {};
  4686. // Options matching.
  4687. options = options || {};
  4688. // Of course we collect tokens here.
  4689. options.tokens = true;
  4690. extra.tokens = [];
  4691. extra.tokenValues = [];
  4692. extra.tokenize = true;
  4693. extra.delegate = delegate;
  4694. // The following two fields are necessary to compute the Regex tokens.
  4695. extra.openParenToken = -1;
  4696. extra.openCurlyToken = -1;
  4697. extra.range = (typeof options.range === 'boolean') && options.range;
  4698. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  4699. if (typeof options.comment === 'boolean' && options.comment) {
  4700. extra.comments = [];
  4701. }
  4702. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  4703. extra.errors = [];
  4704. }
  4705. try {
  4706. peek();
  4707. if (lookahead.type === Token.EOF) {
  4708. return extra.tokens;
  4709. }
  4710. lex();
  4711. while (lookahead.type !== Token.EOF) {
  4712. try {
  4713. lex();
  4714. } catch (lexError) {
  4715. if (extra.errors) {
  4716. recordError(lexError);
  4717. // We have to break on the first error
  4718. // to avoid infinite loops.
  4719. break;
  4720. } else {
  4721. throw lexError;
  4722. }
  4723. }
  4724. }
  4725. tokens = extra.tokens;
  4726. if (typeof extra.errors !== 'undefined') {
  4727. tokens.errors = extra.errors;
  4728. }
  4729. } catch (e) {
  4730. throw e;
  4731. } finally {
  4732. extra = {};
  4733. }
  4734. return tokens;
  4735. }
  4736. function parse(code, options) {
  4737. var program, toString;
  4738. toString = String;
  4739. if (typeof code !== 'string' && !(code instanceof String)) {
  4740. code = toString(code);
  4741. }
  4742. source = code;
  4743. index = 0;
  4744. lineNumber = (source.length > 0) ? 1 : 0;
  4745. lineStart = 0;
  4746. startIndex = index;
  4747. startLineNumber = lineNumber;
  4748. startLineStart = lineStart;
  4749. length = source.length;
  4750. lookahead = null;
  4751. state = {
  4752. allowIn: true,
  4753. allowYield: true,
  4754. labelSet: {},
  4755. inFunctionBody: false,
  4756. inIteration: false,
  4757. inSwitch: false,
  4758. lastCommentStart: -1,
  4759. curlyStack: [],
  4760. sourceType: 'script'
  4761. };
  4762. strict = false;
  4763. extra = {};
  4764. if (typeof options !== 'undefined') {
  4765. extra.range = (typeof options.range === 'boolean') && options.range;
  4766. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  4767. extra.attachComment = (typeof options.attachComment === 'boolean') && options.attachComment;
  4768. if (extra.loc && options.source !== null && options.source !== undefined) {
  4769. extra.source = toString(options.source);
  4770. }
  4771. if (typeof options.tokens === 'boolean' && options.tokens) {
  4772. extra.tokens = [];
  4773. }
  4774. if (typeof options.comment === 'boolean' && options.comment) {
  4775. extra.comments = [];
  4776. }
  4777. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  4778. extra.errors = [];
  4779. }
  4780. if (extra.attachComment) {
  4781. extra.range = true;
  4782. extra.comments = [];
  4783. extra.bottomRightStack = [];
  4784. extra.trailingComments = [];
  4785. extra.leadingComments = [];
  4786. }
  4787. if (options.sourceType === 'module') {
  4788. // very restrictive condition for now
  4789. state.sourceType = options.sourceType;
  4790. strict = true;
  4791. }
  4792. }
  4793. try {
  4794. program = parseProgram();
  4795. if (typeof extra.comments !== 'undefined') {
  4796. program.comments = extra.comments;
  4797. }
  4798. if (typeof extra.tokens !== 'undefined') {
  4799. filterTokenLocation();
  4800. program.tokens = extra.tokens;
  4801. }
  4802. if (typeof extra.errors !== 'undefined') {
  4803. program.errors = extra.errors;
  4804. }
  4805. } catch (e) {
  4806. throw e;
  4807. } finally {
  4808. extra = {};
  4809. }
  4810. return program;
  4811. }
  4812. // Sync with *.json manifests.
  4813. exports.version = '2.7.3';
  4814. exports.tokenize = tokenize;
  4815. exports.parse = parse;
  4816. // Deep copy.
  4817. /* istanbul ignore next */
  4818. exports.Syntax = (function () {
  4819. var name, types = {};
  4820. if (typeof Object.create === 'function') {
  4821. types = Object.create(null);
  4822. }
  4823. for (name in Syntax) {
  4824. if (Syntax.hasOwnProperty(name)) {
  4825. types[name] = Syntax[name];
  4826. }
  4827. }
  4828. if (typeof Object.freeze === 'function') {
  4829. Object.freeze(types);
  4830. }
  4831. return types;
  4832. }());
  4833. }));
  4834. /* vim: set sw=4 ts=4 et tw=80 : */