esprima.js 202KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307
  1. /*
  2. Copyright (C) 2013 Ariya Hidayat <ariya.hidayat@gmail.com>
  3. Copyright (C) 2013 Thaddee Tyl <thaddee.tyl@gmail.com>
  4. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  5. Copyright (C) 2012 Mathias Bynens <mathias@qiwi.be>
  6. Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
  7. Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
  8. Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
  9. Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
  10. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*jslint bitwise:true plusplus:true */
  30. /*global esprima:true, define:true, exports:true, window: true,
  31. throwError: true, generateStatement: true, peek: true,
  32. parseAssignmentExpression: true, parseBlock: true,
  33. parseClassExpression: true, parseClassDeclaration: true, parseExpression: true,
  34. parseForStatement: true,
  35. parseFunctionDeclaration: true, parseFunctionExpression: true,
  36. parseFunctionSourceElements: true, parseVariableIdentifier: true,
  37. parseImportSpecifier: true,
  38. parseLeftHandSideExpression: true, parseParams: true, validateParam: true,
  39. parseSpreadOrAssignmentExpression: true,
  40. parseStatement: true, parseSourceElement: true, parseModuleBlock: true, parseConciseBody: true,
  41. advanceXJSChild: true, isXJSIdentifierStart: true, isXJSIdentifierPart: true,
  42. scanXJSStringLiteral: true, scanXJSIdentifier: true,
  43. parseXJSAttributeValue: true, parseXJSChild: true, parseXJSElement: true, parseXJSExpressionContainer: true, parseXJSEmptyExpression: true,
  44. parseTypeAnnotation: true, parseTypeAnnotatableIdentifier: true,
  45. parseYieldExpression: true
  46. */
  47. (function (root, factory) {
  48. 'use strict';
  49. // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
  50. // Rhino, and plain browser loading.
  51. if (typeof define === 'function' && define.amd) {
  52. define(['exports'], factory);
  53. } else if (typeof exports !== 'undefined') {
  54. factory(exports);
  55. } else {
  56. factory((root.esprima = {}));
  57. }
  58. }(this, function (exports) {
  59. 'use strict';
  60. var Token,
  61. TokenName,
  62. FnExprTokens,
  63. Syntax,
  64. PropertyKind,
  65. Messages,
  66. Regex,
  67. SyntaxTreeDelegate,
  68. XHTMLEntities,
  69. ClassPropertyType,
  70. source,
  71. strict,
  72. index,
  73. lineNumber,
  74. lineStart,
  75. length,
  76. delegate,
  77. lookahead,
  78. state,
  79. extra;
  80. Token = {
  81. BooleanLiteral: 1,
  82. EOF: 2,
  83. Identifier: 3,
  84. Keyword: 4,
  85. NullLiteral: 5,
  86. NumericLiteral: 6,
  87. Punctuator: 7,
  88. StringLiteral: 8,
  89. RegularExpression: 9,
  90. Template: 10,
  91. XJSIdentifier: 11,
  92. XJSText: 12
  93. };
  94. TokenName = {};
  95. TokenName[Token.BooleanLiteral] = 'Boolean';
  96. TokenName[Token.EOF] = '<end>';
  97. TokenName[Token.Identifier] = 'Identifier';
  98. TokenName[Token.Keyword] = 'Keyword';
  99. TokenName[Token.NullLiteral] = 'Null';
  100. TokenName[Token.NumericLiteral] = 'Numeric';
  101. TokenName[Token.Punctuator] = 'Punctuator';
  102. TokenName[Token.StringLiteral] = 'String';
  103. TokenName[Token.XJSIdentifier] = 'XJSIdentifier';
  104. TokenName[Token.XJSText] = 'XJSText';
  105. TokenName[Token.RegularExpression] = 'RegularExpression';
  106. // A function following one of those tokens is an expression.
  107. FnExprTokens = ['(', '{', '[', 'in', 'typeof', 'instanceof', 'new',
  108. 'return', 'case', 'delete', 'throw', 'void',
  109. // assignment operators
  110. '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=',
  111. '&=', '|=', '^=', ',',
  112. // binary/unary operators
  113. '+', '-', '*', '/', '%', '++', '--', '<<', '>>', '>>>', '&',
  114. '|', '^', '!', '~', '&&', '||', '?', ':', '===', '==', '>=',
  115. '<=', '<', '>', '!=', '!=='];
  116. Syntax = {
  117. ArrayExpression: 'ArrayExpression',
  118. ArrayPattern: 'ArrayPattern',
  119. ArrowFunctionExpression: 'ArrowFunctionExpression',
  120. AssignmentExpression: 'AssignmentExpression',
  121. BinaryExpression: 'BinaryExpression',
  122. BlockStatement: 'BlockStatement',
  123. BreakStatement: 'BreakStatement',
  124. CallExpression: 'CallExpression',
  125. CatchClause: 'CatchClause',
  126. ClassBody: 'ClassBody',
  127. ClassDeclaration: 'ClassDeclaration',
  128. ClassExpression: 'ClassExpression',
  129. ClassHeritage: 'ClassHeritage',
  130. ComprehensionBlock: 'ComprehensionBlock',
  131. ComprehensionExpression: 'ComprehensionExpression',
  132. ConditionalExpression: 'ConditionalExpression',
  133. ContinueStatement: 'ContinueStatement',
  134. DebuggerStatement: 'DebuggerStatement',
  135. DoWhileStatement: 'DoWhileStatement',
  136. EmptyStatement: 'EmptyStatement',
  137. ExportDeclaration: 'ExportDeclaration',
  138. ExportBatchSpecifier: 'ExportBatchSpecifier',
  139. ExportSpecifier: 'ExportSpecifier',
  140. ExpressionStatement: 'ExpressionStatement',
  141. ForInStatement: 'ForInStatement',
  142. ForOfStatement: 'ForOfStatement',
  143. ForStatement: 'ForStatement',
  144. FunctionDeclaration: 'FunctionDeclaration',
  145. FunctionExpression: 'FunctionExpression',
  146. Identifier: 'Identifier',
  147. IfStatement: 'IfStatement',
  148. ImportDeclaration: 'ImportDeclaration',
  149. ImportSpecifier: 'ImportSpecifier',
  150. LabeledStatement: 'LabeledStatement',
  151. Literal: 'Literal',
  152. LogicalExpression: 'LogicalExpression',
  153. MemberExpression: 'MemberExpression',
  154. MethodDefinition: 'MethodDefinition',
  155. ModuleDeclaration: 'ModuleDeclaration',
  156. NewExpression: 'NewExpression',
  157. ObjectExpression: 'ObjectExpression',
  158. ObjectPattern: 'ObjectPattern',
  159. Program: 'Program',
  160. Property: 'Property',
  161. ReturnStatement: 'ReturnStatement',
  162. SequenceExpression: 'SequenceExpression',
  163. SpreadElement: 'SpreadElement',
  164. SwitchCase: 'SwitchCase',
  165. SwitchStatement: 'SwitchStatement',
  166. TaggedTemplateExpression: 'TaggedTemplateExpression',
  167. TemplateElement: 'TemplateElement',
  168. TemplateLiteral: 'TemplateLiteral',
  169. ThisExpression: 'ThisExpression',
  170. ThrowStatement: 'ThrowStatement',
  171. TryStatement: 'TryStatement',
  172. TypeAnnotatedIdentifier: 'TypeAnnotatedIdentifier',
  173. TypeAnnotation: 'TypeAnnotation',
  174. UnaryExpression: 'UnaryExpression',
  175. UpdateExpression: 'UpdateExpression',
  176. VariableDeclaration: 'VariableDeclaration',
  177. VariableDeclarator: 'VariableDeclarator',
  178. WhileStatement: 'WhileStatement',
  179. WithStatement: 'WithStatement',
  180. XJSIdentifier: 'XJSIdentifier',
  181. XJSEmptyExpression: 'XJSEmptyExpression',
  182. XJSExpressionContainer: 'XJSExpressionContainer',
  183. XJSElement: 'XJSElement',
  184. XJSClosingElement: 'XJSClosingElement',
  185. XJSOpeningElement: 'XJSOpeningElement',
  186. XJSAttribute: 'XJSAttribute',
  187. XJSText: 'XJSText',
  188. YieldExpression: 'YieldExpression'
  189. };
  190. PropertyKind = {
  191. Data: 1,
  192. Get: 2,
  193. Set: 4
  194. };
  195. ClassPropertyType = {
  196. 'static': 'static',
  197. prototype: 'prototype'
  198. };
  199. // Error messages should be identical to V8.
  200. Messages = {
  201. UnexpectedToken: 'Unexpected token %0',
  202. UnexpectedNumber: 'Unexpected number',
  203. UnexpectedString: 'Unexpected string',
  204. UnexpectedIdentifier: 'Unexpected identifier',
  205. UnexpectedReserved: 'Unexpected reserved word',
  206. UnexpectedTemplate: 'Unexpected quasi %0',
  207. UnexpectedEOS: 'Unexpected end of input',
  208. NewlineAfterThrow: 'Illegal newline after throw',
  209. InvalidRegExp: 'Invalid regular expression',
  210. UnterminatedRegExp: 'Invalid regular expression: missing /',
  211. InvalidLHSInAssignment: 'Invalid left-hand side in assignment',
  212. InvalidLHSInFormalsList: 'Invalid left-hand side in formals list',
  213. InvalidLHSInForIn: 'Invalid left-hand side in for-in',
  214. MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
  215. NoCatchOrFinally: 'Missing catch or finally after try',
  216. UnknownLabel: 'Undefined label \'%0\'',
  217. Redeclaration: '%0 \'%1\' has already been declared',
  218. IllegalContinue: 'Illegal continue statement',
  219. IllegalBreak: 'Illegal break statement',
  220. IllegalDuplicateClassProperty: 'Illegal duplicate property in class definition',
  221. IllegalReturn: 'Illegal return statement',
  222. IllegalYield: 'Illegal yield expression',
  223. IllegalSpread: 'Illegal spread element',
  224. StrictModeWith: 'Strict mode code may not include a with statement',
  225. StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
  226. StrictVarName: 'Variable name may not be eval or arguments in strict mode',
  227. StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
  228. StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
  229. ParameterAfterRestParameter: 'Rest parameter must be final parameter of an argument list',
  230. DefaultRestParameter: 'Rest parameter can not have a default value',
  231. ElementAfterSpreadElement: 'Spread must be the final element of an element list',
  232. ObjectPatternAsRestParameter: 'Invalid rest parameter',
  233. ObjectPatternAsSpread: 'Invalid spread argument',
  234. StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
  235. StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
  236. StrictDelete: 'Delete of an unqualified identifier in strict mode.',
  237. StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',
  238. AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',
  239. AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',
  240. StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
  241. StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
  242. StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
  243. StrictReservedWord: 'Use of future reserved word in strict mode',
  244. NewlineAfterModule: 'Illegal newline after module',
  245. NoFromAfterImport: 'Missing from after import',
  246. InvalidModuleSpecifier: 'Invalid module specifier',
  247. NestedModule: 'Module declaration can not be nested',
  248. NoYieldInGenerator: 'Missing yield in generator',
  249. NoUnintializedConst: 'Const must be initialized',
  250. ComprehensionRequiresBlock: 'Comprehension must have at least one block',
  251. ComprehensionError: 'Comprehension Error',
  252. EachNotAllowed: 'Each is not supported',
  253. InvalidXJSTagName: 'XJS tag name can not be empty',
  254. InvalidXJSAttributeValue: 'XJS value should be either an expression or a quoted XJS text',
  255. ExpectedXJSClosingTag: 'Expected corresponding XJS closing tag for %0'
  256. };
  257. // See also tools/generate-unicode-regex.py.
  258. Regex = {
  259. NonAsciiIdentifierStart: new RegExp('[\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\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\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\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-\u0c33\u0c35-\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-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\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\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\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\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\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-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\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]'),
  260. NonAsciiIdentifierPart: new RegExp('[\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0300-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u0483-\u0487\u048a-\u0527\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\u08a2-\u08ac\u08e4-\u08fe\u0900-\u0963\u0966-\u096f\u0971-\u0977\u0979-\u097f\u0981-\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\u0c01-\u0c03\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c58\u0c59\u0c60-\u0c63\u0c66-\u0c6f\u0c82\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\u0d02\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\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\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\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-\u191c\u1920-\u192b\u1930-\u193b\u1946-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u19d0-\u19d9\u1a00-\u1a1b\u1a20-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1aa7\u1b00-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1bf3\u1c00-\u1c37\u1c40-\u1c49\u1c4d-\u1c7d\u1cd0-\u1cd2\u1cd4-\u1cf6\u1d00-\u1de6\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\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\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\u2e2f\u3005-\u3007\u3021-\u302f\u3031-\u3035\u3038-\u303c\u3041-\u3096\u3099\u309a\u309d-\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-\ua697\ua69f-\ua6f1\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua827\ua840-\ua873\ua880-\ua8c4\ua8d0-\ua8d9\ua8e0-\ua8f7\ua8fb\ua900-\ua92d\ua930-\ua953\ua960-\ua97c\ua980-\ua9c0\ua9cf-\ua9d9\uaa00-\uaa36\uaa40-\uaa4d\uaa50-\uaa59\uaa60-\uaa76\uaa7a\uaa7b\uaa80-\uaac2\uaadb-\uaadd\uaae0-\uaaef\uaaf2-\uaaf6\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\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-\ufe26\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]')
  261. };
  262. // Ensure the condition is true, otherwise throw an error.
  263. // This is only to have a better contract semantic, i.e. another safety net
  264. // to catch a logic error. The condition shall be fulfilled in normal case.
  265. // Do NOT use this to enforce a certain condition on any user input.
  266. function assert(condition, message) {
  267. if (!condition) {
  268. throw new Error('ASSERT: ' + message);
  269. }
  270. }
  271. function isDecimalDigit(ch) {
  272. return (ch >= 48 && ch <= 57); // 0..9
  273. }
  274. function isHexDigit(ch) {
  275. return '0123456789abcdefABCDEF'.indexOf(ch) >= 0;
  276. }
  277. function isOctalDigit(ch) {
  278. return '01234567'.indexOf(ch) >= 0;
  279. }
  280. // 7.2 White Space
  281. function isWhiteSpace(ch) {
  282. return (ch === 32) || // space
  283. (ch === 9) || // tab
  284. (ch === 0xB) ||
  285. (ch === 0xC) ||
  286. (ch === 0xA0) ||
  287. (ch >= 0x1680 && '\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\uFEFF'.indexOf(String.fromCharCode(ch)) > 0);
  288. }
  289. // 7.3 Line Terminators
  290. function isLineTerminator(ch) {
  291. return (ch === 10) || (ch === 13) || (ch === 0x2028) || (ch === 0x2029);
  292. }
  293. // 7.6 Identifier Names and Identifiers
  294. function isIdentifierStart(ch) {
  295. return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
  296. (ch >= 65 && ch <= 90) || // A..Z
  297. (ch >= 97 && ch <= 122) || // a..z
  298. (ch === 92) || // \ (backslash)
  299. ((ch >= 0x80) && Regex.NonAsciiIdentifierStart.test(String.fromCharCode(ch)));
  300. }
  301. function isIdentifierPart(ch) {
  302. return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
  303. (ch >= 65 && ch <= 90) || // A..Z
  304. (ch >= 97 && ch <= 122) || // a..z
  305. (ch >= 48 && ch <= 57) || // 0..9
  306. (ch === 92) || // \ (backslash)
  307. ((ch >= 0x80) && Regex.NonAsciiIdentifierPart.test(String.fromCharCode(ch)));
  308. }
  309. // 7.6.1.2 Future Reserved Words
  310. function isFutureReservedWord(id) {
  311. switch (id) {
  312. case 'class':
  313. case 'enum':
  314. case 'export':
  315. case 'extends':
  316. case 'import':
  317. case 'super':
  318. return true;
  319. default:
  320. return false;
  321. }
  322. }
  323. function isStrictModeReservedWord(id) {
  324. switch (id) {
  325. case 'implements':
  326. case 'interface':
  327. case 'package':
  328. case 'private':
  329. case 'protected':
  330. case 'public':
  331. case 'static':
  332. case 'yield':
  333. case 'let':
  334. return true;
  335. default:
  336. return false;
  337. }
  338. }
  339. function isRestrictedWord(id) {
  340. return id === 'eval' || id === 'arguments';
  341. }
  342. // 7.6.1.1 Keywords
  343. function isKeyword(id) {
  344. if (strict && isStrictModeReservedWord(id)) {
  345. return true;
  346. }
  347. // 'const' is specialized as Keyword in V8.
  348. // 'yield' and 'let' are for compatiblity with SpiderMonkey and ES.next.
  349. // Some others are from future reserved words.
  350. switch (id.length) {
  351. case 2:
  352. return (id === 'if') || (id === 'in') || (id === 'do');
  353. case 3:
  354. return (id === 'var') || (id === 'for') || (id === 'new') ||
  355. (id === 'try') || (id === 'let');
  356. case 4:
  357. return (id === 'this') || (id === 'else') || (id === 'case') ||
  358. (id === 'void') || (id === 'with') || (id === 'enum');
  359. case 5:
  360. return (id === 'while') || (id === 'break') || (id === 'catch') ||
  361. (id === 'throw') || (id === 'const') || (id === 'yield') ||
  362. (id === 'class') || (id === 'super');
  363. case 6:
  364. return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
  365. (id === 'switch') || (id === 'export') || (id === 'import');
  366. case 7:
  367. return (id === 'default') || (id === 'finally') || (id === 'extends');
  368. case 8:
  369. return (id === 'function') || (id === 'continue') || (id === 'debugger');
  370. case 10:
  371. return (id === 'instanceof');
  372. default:
  373. return false;
  374. }
  375. }
  376. // 7.4 Comments
  377. function skipComment() {
  378. var ch, blockComment, lineComment;
  379. blockComment = false;
  380. lineComment = false;
  381. while (index < length) {
  382. ch = source.charCodeAt(index);
  383. if (lineComment) {
  384. ++index;
  385. if (isLineTerminator(ch)) {
  386. lineComment = false;
  387. if (ch === 13 && source.charCodeAt(index) === 10) {
  388. ++index;
  389. }
  390. ++lineNumber;
  391. lineStart = index;
  392. }
  393. } else if (blockComment) {
  394. if (isLineTerminator(ch)) {
  395. if (ch === 13 && source.charCodeAt(index + 1) === 10) {
  396. ++index;
  397. }
  398. ++lineNumber;
  399. ++index;
  400. lineStart = index;
  401. if (index >= length) {
  402. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  403. }
  404. } else {
  405. ch = source.charCodeAt(index++);
  406. if (index >= length) {
  407. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  408. }
  409. // Block comment ends with '*/' (char #42, char #47).
  410. if (ch === 42) {
  411. ch = source.charCodeAt(index);
  412. if (ch === 47) {
  413. ++index;
  414. blockComment = false;
  415. }
  416. }
  417. }
  418. } else if (ch === 47) {
  419. ch = source.charCodeAt(index + 1);
  420. // Line comment starts with '//' (char #47, char #47).
  421. if (ch === 47) {
  422. index += 2;
  423. lineComment = true;
  424. } else if (ch === 42) {
  425. // Block comment starts with '/*' (char #47, char #42).
  426. index += 2;
  427. blockComment = true;
  428. if (index >= length) {
  429. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  430. }
  431. } else {
  432. break;
  433. }
  434. } else if (isWhiteSpace(ch)) {
  435. ++index;
  436. } else if (isLineTerminator(ch)) {
  437. ++index;
  438. if (ch === 13 && source.charCodeAt(index) === 10) {
  439. ++index;
  440. }
  441. ++lineNumber;
  442. lineStart = index;
  443. } else {
  444. break;
  445. }
  446. }
  447. }
  448. function scanHexEscape(prefix) {
  449. var i, len, ch, code = 0;
  450. len = (prefix === 'u') ? 4 : 2;
  451. for (i = 0; i < len; ++i) {
  452. if (index < length && isHexDigit(source[index])) {
  453. ch = source[index++];
  454. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  455. } else {
  456. return '';
  457. }
  458. }
  459. return String.fromCharCode(code);
  460. }
  461. function scanUnicodeCodePointEscape() {
  462. var ch, code, cu1, cu2;
  463. ch = source[index];
  464. code = 0;
  465. // At least, one hex digit is required.
  466. if (ch === '}') {
  467. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  468. }
  469. while (index < length) {
  470. ch = source[index++];
  471. if (!isHexDigit(ch)) {
  472. break;
  473. }
  474. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  475. }
  476. if (code > 0x10FFFF || ch !== '}') {
  477. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  478. }
  479. // UTF-16 Encoding
  480. if (code <= 0xFFFF) {
  481. return String.fromCharCode(code);
  482. }
  483. cu1 = ((code - 0x10000) >> 10) + 0xD800;
  484. cu2 = ((code - 0x10000) & 1023) + 0xDC00;
  485. return String.fromCharCode(cu1, cu2);
  486. }
  487. function getEscapedIdentifier() {
  488. var ch, id;
  489. ch = source.charCodeAt(index++);
  490. id = String.fromCharCode(ch);
  491. // '\u' (char #92, char #117) denotes an escaped character.
  492. if (ch === 92) {
  493. if (source.charCodeAt(index) !== 117) {
  494. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  495. }
  496. ++index;
  497. ch = scanHexEscape('u');
  498. if (!ch || ch === '\\' || !isIdentifierStart(ch.charCodeAt(0))) {
  499. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  500. }
  501. id = ch;
  502. }
  503. while (index < length) {
  504. ch = source.charCodeAt(index);
  505. if (!isIdentifierPart(ch)) {
  506. break;
  507. }
  508. ++index;
  509. id += String.fromCharCode(ch);
  510. // '\u' (char #92, char #117) denotes an escaped character.
  511. if (ch === 92) {
  512. id = id.substr(0, id.length - 1);
  513. if (source.charCodeAt(index) !== 117) {
  514. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  515. }
  516. ++index;
  517. ch = scanHexEscape('u');
  518. if (!ch || ch === '\\' || !isIdentifierPart(ch.charCodeAt(0))) {
  519. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  520. }
  521. id += ch;
  522. }
  523. }
  524. return id;
  525. }
  526. function getIdentifier() {
  527. var start, ch;
  528. start = index++;
  529. while (index < length) {
  530. ch = source.charCodeAt(index);
  531. if (ch === 92) {
  532. // Blackslash (char #92) marks Unicode escape sequence.
  533. index = start;
  534. return getEscapedIdentifier();
  535. }
  536. if (isIdentifierPart(ch)) {
  537. ++index;
  538. } else {
  539. break;
  540. }
  541. }
  542. return source.slice(start, index);
  543. }
  544. function scanIdentifier() {
  545. var start, id, type;
  546. start = index;
  547. // Backslash (char #92) starts an escaped character.
  548. id = (source.charCodeAt(index) === 92) ? getEscapedIdentifier() : getIdentifier();
  549. // There is no keyword or literal with only one character.
  550. // Thus, it must be an identifier.
  551. if (id.length === 1) {
  552. type = Token.Identifier;
  553. } else if (isKeyword(id)) {
  554. type = Token.Keyword;
  555. } else if (id === 'null') {
  556. type = Token.NullLiteral;
  557. } else if (id === 'true' || id === 'false') {
  558. type = Token.BooleanLiteral;
  559. } else {
  560. type = Token.Identifier;
  561. }
  562. return {
  563. type: type,
  564. value: id,
  565. lineNumber: lineNumber,
  566. lineStart: lineStart,
  567. range: [start, index]
  568. };
  569. }
  570. // 7.7 Punctuators
  571. function scanPunctuator() {
  572. var start = index,
  573. code = source.charCodeAt(index),
  574. code2,
  575. ch1 = source[index],
  576. ch2,
  577. ch3,
  578. ch4;
  579. switch (code) {
  580. // Check for most common single-character punctuators.
  581. case 40: // ( open bracket
  582. case 41: // ) close bracket
  583. case 59: // ; semicolon
  584. case 44: // , comma
  585. case 123: // { open curly brace
  586. case 125: // } close curly brace
  587. case 91: // [
  588. case 93: // ]
  589. case 58: // :
  590. case 63: // ?
  591. case 126: // ~
  592. ++index;
  593. if (extra.tokenize) {
  594. if (code === 40) {
  595. extra.openParenToken = extra.tokens.length;
  596. } else if (code === 123) {
  597. extra.openCurlyToken = extra.tokens.length;
  598. }
  599. }
  600. return {
  601. type: Token.Punctuator,
  602. value: String.fromCharCode(code),
  603. lineNumber: lineNumber,
  604. lineStart: lineStart,
  605. range: [start, index]
  606. };
  607. default:
  608. code2 = source.charCodeAt(index + 1);
  609. // '=' (char #61) marks an assignment or comparison operator.
  610. if (code2 === 61) {
  611. switch (code) {
  612. case 37: // %
  613. case 38: // &
  614. case 42: // *:
  615. case 43: // +
  616. case 45: // -
  617. case 47: // /
  618. case 60: // <
  619. case 62: // >
  620. case 94: // ^
  621. case 124: // |
  622. index += 2;
  623. return {
  624. type: Token.Punctuator,
  625. value: String.fromCharCode(code) + String.fromCharCode(code2),
  626. lineNumber: lineNumber,
  627. lineStart: lineStart,
  628. range: [start, index]
  629. };
  630. case 33: // !
  631. case 61: // =
  632. index += 2;
  633. // !== and ===
  634. if (source.charCodeAt(index) === 61) {
  635. ++index;
  636. }
  637. return {
  638. type: Token.Punctuator,
  639. value: source.slice(start, index),
  640. lineNumber: lineNumber,
  641. lineStart: lineStart,
  642. range: [start, index]
  643. };
  644. default:
  645. break;
  646. }
  647. }
  648. break;
  649. }
  650. // Peek more characters.
  651. ch2 = source[index + 1];
  652. ch3 = source[index + 2];
  653. ch4 = source[index + 3];
  654. // 4-character punctuator: >>>=
  655. if (ch1 === '>' && ch2 === '>' && ch3 === '>') {
  656. if (ch4 === '=') {
  657. index += 4;
  658. return {
  659. type: Token.Punctuator,
  660. value: '>>>=',
  661. lineNumber: lineNumber,
  662. lineStart: lineStart,
  663. range: [start, index]
  664. };
  665. }
  666. }
  667. // 3-character punctuators: === !== >>> <<= >>=
  668. if (ch1 === '>' && ch2 === '>' && ch3 === '>') {
  669. index += 3;
  670. return {
  671. type: Token.Punctuator,
  672. value: '>>>',
  673. lineNumber: lineNumber,
  674. lineStart: lineStart,
  675. range: [start, index]
  676. };
  677. }
  678. if (ch1 === '<' && ch2 === '<' && ch3 === '=') {
  679. index += 3;
  680. return {
  681. type: Token.Punctuator,
  682. value: '<<=',
  683. lineNumber: lineNumber,
  684. lineStart: lineStart,
  685. range: [start, index]
  686. };
  687. }
  688. if (ch1 === '>' && ch2 === '>' && ch3 === '=') {
  689. index += 3;
  690. return {
  691. type: Token.Punctuator,
  692. value: '>>=',
  693. lineNumber: lineNumber,
  694. lineStart: lineStart,
  695. range: [start, index]
  696. };
  697. }
  698. if (ch1 === '.' && ch2 === '.' && ch3 === '.') {
  699. index += 3;
  700. return {
  701. type: Token.Punctuator,
  702. value: '...',
  703. lineNumber: lineNumber,
  704. lineStart: lineStart,
  705. range: [start, index]
  706. };
  707. }
  708. // Other 2-character punctuators: ++ -- << >> && ||
  709. if (ch1 === ch2 && ('+-<>&|'.indexOf(ch1) >= 0)) {
  710. index += 2;
  711. return {
  712. type: Token.Punctuator,
  713. value: ch1 + ch2,
  714. lineNumber: lineNumber,
  715. lineStart: lineStart,
  716. range: [start, index]
  717. };
  718. }
  719. if (ch1 === '=' && ch2 === '>') {
  720. index += 2;
  721. return {
  722. type: Token.Punctuator,
  723. value: '=>',
  724. lineNumber: lineNumber,
  725. lineStart: lineStart,
  726. range: [start, index]
  727. };
  728. }
  729. if ('<>=!+-*%&|^/'.indexOf(ch1) >= 0) {
  730. ++index;
  731. return {
  732. type: Token.Punctuator,
  733. value: ch1,
  734. lineNumber: lineNumber,
  735. lineStart: lineStart,
  736. range: [start, index]
  737. };
  738. }
  739. if (ch1 === '.') {
  740. ++index;
  741. return {
  742. type: Token.Punctuator,
  743. value: ch1,
  744. lineNumber: lineNumber,
  745. lineStart: lineStart,
  746. range: [start, index]
  747. };
  748. }
  749. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  750. }
  751. // 7.8.3 Numeric Literals
  752. function scanHexLiteral(start) {
  753. var number = '';
  754. while (index < length) {
  755. if (!isHexDigit(source[index])) {
  756. break;
  757. }
  758. number += source[index++];
  759. }
  760. if (number.length === 0) {
  761. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  762. }
  763. if (isIdentifierStart(source.charCodeAt(index))) {
  764. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  765. }
  766. return {
  767. type: Token.NumericLiteral,
  768. value: parseInt('0x' + number, 16),
  769. lineNumber: lineNumber,
  770. lineStart: lineStart,
  771. range: [start, index]
  772. };
  773. }
  774. function scanOctalLiteral(prefix, start) {
  775. var number, octal;
  776. if (isOctalDigit(prefix)) {
  777. octal = true;
  778. number = '0' + source[index++];
  779. } else {
  780. octal = false;
  781. ++index;
  782. number = '';
  783. }
  784. while (index < length) {
  785. if (!isOctalDigit(source[index])) {
  786. break;
  787. }
  788. number += source[index++];
  789. }
  790. if (!octal && number.length === 0) {
  791. // only 0o or 0O
  792. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  793. }
  794. if (isIdentifierStart(source.charCodeAt(index)) || isDecimalDigit(source.charCodeAt(index))) {
  795. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  796. }
  797. return {
  798. type: Token.NumericLiteral,
  799. value: parseInt(number, 8),
  800. octal: octal,
  801. lineNumber: lineNumber,
  802. lineStart: lineStart,
  803. range: [start, index]
  804. };
  805. }
  806. function scanNumericLiteral() {
  807. var number, start, ch, octal;
  808. ch = source[index];
  809. assert(isDecimalDigit(ch.charCodeAt(0)) || (ch === '.'),
  810. 'Numeric literal must start with a decimal digit or a decimal point');
  811. start = index;
  812. number = '';
  813. if (ch !== '.') {
  814. number = source[index++];
  815. ch = source[index];
  816. // Hex number starts with '0x'.
  817. // Octal number starts with '0'.
  818. // Octal number in ES6 starts with '0o'.
  819. // Binary number in ES6 starts with '0b'.
  820. if (number === '0') {
  821. if (ch === 'x' || ch === 'X') {
  822. ++index;
  823. return scanHexLiteral(start);
  824. }
  825. if (ch === 'b' || ch === 'B') {
  826. ++index;
  827. number = '';
  828. while (index < length) {
  829. ch = source[index];
  830. if (ch !== '0' && ch !== '1') {
  831. break;
  832. }
  833. number += source[index++];
  834. }
  835. if (number.length === 0) {
  836. // only 0b or 0B
  837. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  838. }
  839. if (index < length) {
  840. ch = source.charCodeAt(index);
  841. if (isIdentifierStart(ch) || isDecimalDigit(ch)) {
  842. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  843. }
  844. }
  845. return {
  846. type: Token.NumericLiteral,
  847. value: parseInt(number, 2),
  848. lineNumber: lineNumber,
  849. lineStart: lineStart,
  850. range: [start, index]
  851. };
  852. }
  853. if (ch === 'o' || ch === 'O' || isOctalDigit(ch)) {
  854. return scanOctalLiteral(ch, start);
  855. }
  856. // decimal number starts with '0' such as '09' is illegal.
  857. if (ch && isDecimalDigit(ch.charCodeAt(0))) {
  858. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  859. }
  860. }
  861. while (isDecimalDigit(source.charCodeAt(index))) {
  862. number += source[index++];
  863. }
  864. ch = source[index];
  865. }
  866. if (ch === '.') {
  867. number += source[index++];
  868. while (isDecimalDigit(source.charCodeAt(index))) {
  869. number += source[index++];
  870. }
  871. ch = source[index];
  872. }
  873. if (ch === 'e' || ch === 'E') {
  874. number += source[index++];
  875. ch = source[index];
  876. if (ch === '+' || ch === '-') {
  877. number += source[index++];
  878. }
  879. if (isDecimalDigit(source.charCodeAt(index))) {
  880. while (isDecimalDigit(source.charCodeAt(index))) {
  881. number += source[index++];
  882. }
  883. } else {
  884. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  885. }
  886. }
  887. if (isIdentifierStart(source.charCodeAt(index))) {
  888. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  889. }
  890. return {
  891. type: Token.NumericLiteral,
  892. value: parseFloat(number),
  893. lineNumber: lineNumber,
  894. lineStart: lineStart,
  895. range: [start, index]
  896. };
  897. }
  898. // 7.8.4 String Literals
  899. function scanStringLiteral() {
  900. var str = '', quote, start, ch, code, unescaped, restore, octal = false;
  901. quote = source[index];
  902. assert((quote === '\'' || quote === '"'),
  903. 'String literal must starts with a quote');
  904. start = index;
  905. ++index;
  906. while (index < length) {
  907. ch = source[index++];
  908. if (ch === quote) {
  909. quote = '';
  910. break;
  911. } else if (ch === '\\') {
  912. ch = source[index++];
  913. if (!ch || !isLineTerminator(ch.charCodeAt(0))) {
  914. switch (ch) {
  915. case 'n':
  916. str += '\n';
  917. break;
  918. case 'r':
  919. str += '\r';
  920. break;
  921. case 't':
  922. str += '\t';
  923. break;
  924. case 'u':
  925. case 'x':
  926. if (source[index] === '{') {
  927. ++index;
  928. str += scanUnicodeCodePointEscape();
  929. } else {
  930. restore = index;
  931. unescaped = scanHexEscape(ch);
  932. if (unescaped) {
  933. str += unescaped;
  934. } else {
  935. index = restore;
  936. str += ch;
  937. }
  938. }
  939. break;
  940. case 'b':
  941. str += '\b';
  942. break;
  943. case 'f':
  944. str += '\f';
  945. break;
  946. case 'v':
  947. str += '\x0B';
  948. break;
  949. default:
  950. if (isOctalDigit(ch)) {
  951. code = '01234567'.indexOf(ch);
  952. // \0 is not octal escape sequence
  953. if (code !== 0) {
  954. octal = true;
  955. }
  956. if (index < length && isOctalDigit(source[index])) {
  957. octal = true;
  958. code = code * 8 + '01234567'.indexOf(source[index++]);
  959. // 3 digits are only allowed when string starts
  960. // with 0, 1, 2, 3
  961. if ('0123'.indexOf(ch) >= 0 &&
  962. index < length &&
  963. isOctalDigit(source[index])) {
  964. code = code * 8 + '01234567'.indexOf(source[index++]);
  965. }
  966. }
  967. str += String.fromCharCode(code);
  968. } else {
  969. str += ch;
  970. }
  971. break;
  972. }
  973. } else {
  974. ++lineNumber;
  975. if (ch === '\r' && source[index] === '\n') {
  976. ++index;
  977. }
  978. }
  979. } else if (isLineTerminator(ch.charCodeAt(0))) {
  980. break;
  981. } else {
  982. str += ch;
  983. }
  984. }
  985. if (quote !== '') {
  986. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  987. }
  988. return {
  989. type: Token.StringLiteral,
  990. value: str,
  991. octal: octal,
  992. lineNumber: lineNumber,
  993. lineStart: lineStart,
  994. range: [start, index]
  995. };
  996. }
  997. function scanTemplate() {
  998. var cooked = '', ch, start, terminated, tail, restore, unescaped, code, octal;
  999. terminated = false;
  1000. tail = false;
  1001. start = index;
  1002. ++index;
  1003. while (index < length) {
  1004. ch = source[index++];
  1005. if (ch === '`') {
  1006. tail = true;
  1007. terminated = true;
  1008. break;
  1009. } else if (ch === '$') {
  1010. if (source[index] === '{') {
  1011. ++index;
  1012. terminated = true;
  1013. break;
  1014. }
  1015. cooked += ch;
  1016. } else if (ch === '\\') {
  1017. ch = source[index++];
  1018. if (!isLineTerminator(ch.charCodeAt(0))) {
  1019. switch (ch) {
  1020. case 'n':
  1021. cooked += '\n';
  1022. break;
  1023. case 'r':
  1024. cooked += '\r';
  1025. break;
  1026. case 't':
  1027. cooked += '\t';
  1028. break;
  1029. case 'u':
  1030. case 'x':
  1031. if (source[index] === '{') {
  1032. ++index;
  1033. cooked += scanUnicodeCodePointEscape();
  1034. } else {
  1035. restore = index;
  1036. unescaped = scanHexEscape(ch);
  1037. if (unescaped) {
  1038. cooked += unescaped;
  1039. } else {
  1040. index = restore;
  1041. cooked += ch;
  1042. }
  1043. }
  1044. break;
  1045. case 'b':
  1046. cooked += '\b';
  1047. break;
  1048. case 'f':
  1049. cooked += '\f';
  1050. break;
  1051. case 'v':
  1052. cooked += '\v';
  1053. break;
  1054. default:
  1055. if (isOctalDigit(ch)) {
  1056. code = '01234567'.indexOf(ch);
  1057. // \0 is not octal escape sequence
  1058. if (code !== 0) {
  1059. octal = true;
  1060. }
  1061. if (index < length && isOctalDigit(source[index])) {
  1062. octal = true;
  1063. code = code * 8 + '01234567'.indexOf(source[index++]);
  1064. // 3 digits are only allowed when string starts
  1065. // with 0, 1, 2, 3
  1066. if ('0123'.indexOf(ch) >= 0 &&
  1067. index < length &&
  1068. isOctalDigit(source[index])) {
  1069. code = code * 8 + '01234567'.indexOf(source[index++]);
  1070. }
  1071. }
  1072. cooked += String.fromCharCode(code);
  1073. } else {
  1074. cooked += ch;
  1075. }
  1076. break;
  1077. }
  1078. } else {
  1079. ++lineNumber;
  1080. if (ch === '\r' && source[index] === '\n') {
  1081. ++index;
  1082. }
  1083. }
  1084. } else if (isLineTerminator(ch.charCodeAt(0))) {
  1085. ++lineNumber;
  1086. if (ch === '\r' && source[index] === '\n') {
  1087. ++index;
  1088. }
  1089. cooked += '\n';
  1090. } else {
  1091. cooked += ch;
  1092. }
  1093. }
  1094. if (!terminated) {
  1095. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  1096. }
  1097. return {
  1098. type: Token.Template,
  1099. value: {
  1100. cooked: cooked,
  1101. raw: source.slice(start + 1, index - ((tail) ? 1 : 2))
  1102. },
  1103. tail: tail,
  1104. octal: octal,
  1105. lineNumber: lineNumber,
  1106. lineStart: lineStart,
  1107. range: [start, index]
  1108. };
  1109. }
  1110. function scanTemplateElement(option) {
  1111. var startsWith, template;
  1112. lookahead = null;
  1113. skipComment();
  1114. startsWith = (option.head) ? '`' : '}';
  1115. if (source[index] !== startsWith) {
  1116. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  1117. }
  1118. template = scanTemplate();
  1119. peek();
  1120. return template;
  1121. }
  1122. function scanRegExp() {
  1123. var str, ch, start, pattern, flags, value, classMarker = false, restore, terminated = false;
  1124. lookahead = null;
  1125. skipComment();
  1126. start = index;
  1127. ch = source[index];
  1128. assert(ch === '/', 'Regular expression literal must start with a slash');
  1129. str = source[index++];
  1130. while (index < length) {
  1131. ch = source[index++];
  1132. str += ch;
  1133. if (classMarker) {
  1134. if (ch === ']') {
  1135. classMarker = false;
  1136. }
  1137. } else {
  1138. if (ch === '\\') {
  1139. ch = source[index++];
  1140. // ECMA-262 7.8.5
  1141. if (isLineTerminator(ch.charCodeAt(0))) {
  1142. throwError({}, Messages.UnterminatedRegExp);
  1143. }
  1144. str += ch;
  1145. } else if (ch === '/') {
  1146. terminated = true;
  1147. break;
  1148. } else if (ch === '[') {
  1149. classMarker = true;
  1150. } else if (isLineTerminator(ch.charCodeAt(0))) {
  1151. throwError({}, Messages.UnterminatedRegExp);
  1152. }
  1153. }
  1154. }
  1155. if (!terminated) {
  1156. throwError({}, Messages.UnterminatedRegExp);
  1157. }
  1158. // Exclude leading and trailing slash.
  1159. pattern = str.substr(1, str.length - 2);
  1160. flags = '';
  1161. while (index < length) {
  1162. ch = source[index];
  1163. if (!isIdentifierPart(ch.charCodeAt(0))) {
  1164. break;
  1165. }
  1166. ++index;
  1167. if (ch === '\\' && index < length) {
  1168. ch = source[index];
  1169. if (ch === 'u') {
  1170. ++index;
  1171. restore = index;
  1172. ch = scanHexEscape('u');
  1173. if (ch) {
  1174. flags += ch;
  1175. for (str += '\\u'; restore < index; ++restore) {
  1176. str += source[restore];
  1177. }
  1178. } else {
  1179. index = restore;
  1180. flags += 'u';
  1181. str += '\\u';
  1182. }
  1183. } else {
  1184. str += '\\';
  1185. }
  1186. } else {
  1187. flags += ch;
  1188. str += ch;
  1189. }
  1190. }
  1191. try {
  1192. value = new RegExp(pattern, flags);
  1193. } catch (e) {
  1194. throwError({}, Messages.InvalidRegExp);
  1195. }
  1196. peek();
  1197. if (extra.tokenize) {
  1198. return {
  1199. type: Token.RegularExpression,
  1200. value: value,
  1201. lineNumber: lineNumber,
  1202. lineStart: lineStart,
  1203. range: [start, index]
  1204. };
  1205. }
  1206. return {
  1207. literal: str,
  1208. value: value,
  1209. range: [start, index]
  1210. };
  1211. }
  1212. function isIdentifierName(token) {
  1213. return token.type === Token.Identifier ||
  1214. token.type === Token.Keyword ||
  1215. token.type === Token.BooleanLiteral ||
  1216. token.type === Token.NullLiteral;
  1217. }
  1218. function advanceSlash() {
  1219. var prevToken,
  1220. checkToken;
  1221. // Using the following algorithm:
  1222. // https://github.com/mozilla/sweet.js/wiki/design
  1223. prevToken = extra.tokens[extra.tokens.length - 1];
  1224. if (!prevToken) {
  1225. // Nothing before that: it cannot be a division.
  1226. return scanRegExp();
  1227. }
  1228. if (prevToken.type === 'Punctuator') {
  1229. if (prevToken.value === ')') {
  1230. checkToken = extra.tokens[extra.openParenToken - 1];
  1231. if (checkToken &&
  1232. checkToken.type === 'Keyword' &&
  1233. (checkToken.value === 'if' ||
  1234. checkToken.value === 'while' ||
  1235. checkToken.value === 'for' ||
  1236. checkToken.value === 'with')) {
  1237. return scanRegExp();
  1238. }
  1239. return scanPunctuator();
  1240. }
  1241. if (prevToken.value === '}') {
  1242. // Dividing a function by anything makes little sense,
  1243. // but we have to check for that.
  1244. if (extra.tokens[extra.openCurlyToken - 3] &&
  1245. extra.tokens[extra.openCurlyToken - 3].type === 'Keyword') {
  1246. // Anonymous function.
  1247. checkToken = extra.tokens[extra.openCurlyToken - 4];
  1248. if (!checkToken) {
  1249. return scanPunctuator();
  1250. }
  1251. } else if (extra.tokens[extra.openCurlyToken - 4] &&
  1252. extra.tokens[extra.openCurlyToken - 4].type === 'Keyword') {
  1253. // Named function.
  1254. checkToken = extra.tokens[extra.openCurlyToken - 5];
  1255. if (!checkToken) {
  1256. return scanRegExp();
  1257. }
  1258. } else {
  1259. return scanPunctuator();
  1260. }
  1261. // checkToken determines whether the function is
  1262. // a declaration or an expression.
  1263. if (FnExprTokens.indexOf(checkToken.value) >= 0) {
  1264. // It is an expression.
  1265. return scanPunctuator();
  1266. }
  1267. // It is a declaration.
  1268. return scanRegExp();
  1269. }
  1270. return scanRegExp();
  1271. }
  1272. if (prevToken.type === 'Keyword') {
  1273. return scanRegExp();
  1274. }
  1275. return scanPunctuator();
  1276. }
  1277. function advance() {
  1278. var ch;
  1279. if (!state.inXJSChild) {
  1280. skipComment();
  1281. }
  1282. if (index >= length) {
  1283. return {
  1284. type: Token.EOF,
  1285. lineNumber: lineNumber,
  1286. lineStart: lineStart,
  1287. range: [index, index]
  1288. };
  1289. }
  1290. if (state.inXJSChild) {
  1291. return advanceXJSChild();
  1292. }
  1293. ch = source.charCodeAt(index);
  1294. // Very common: ( and ) and ;
  1295. if (ch === 40 || ch === 41 || ch === 58) {
  1296. return scanPunctuator();
  1297. }
  1298. // String literal starts with single quote (#39) or double quote (#34).
  1299. if (ch === 39 || ch === 34) {
  1300. if (state.inXJSTag) {
  1301. return scanXJSStringLiteral();
  1302. }
  1303. return scanStringLiteral();
  1304. }
  1305. if (state.inXJSTag && isXJSIdentifierStart(ch)) {
  1306. return scanXJSIdentifier();
  1307. }
  1308. if (ch === 96) {
  1309. return scanTemplate();
  1310. }
  1311. if (isIdentifierStart(ch)) {
  1312. return scanIdentifier();
  1313. }
  1314. // Dot (.) char #46 can also start a floating-point number, hence the need
  1315. // to check the next character.
  1316. if (ch === 46) {
  1317. if (isDecimalDigit(source.charCodeAt(index + 1))) {
  1318. return scanNumericLiteral();
  1319. }
  1320. return scanPunctuator();
  1321. }
  1322. if (isDecimalDigit(ch)) {
  1323. return scanNumericLiteral();
  1324. }
  1325. // Slash (/) char #47 can also start a regex.
  1326. if (extra.tokenize && ch === 47) {
  1327. return advanceSlash();
  1328. }
  1329. return scanPunctuator();
  1330. }
  1331. function lex() {
  1332. var token;
  1333. token = lookahead;
  1334. index = token.range[1];
  1335. lineNumber = token.lineNumber;
  1336. lineStart = token.lineStart;
  1337. lookahead = advance();
  1338. index = token.range[1];
  1339. lineNumber = token.lineNumber;
  1340. lineStart = token.lineStart;
  1341. return token;
  1342. }
  1343. function peek() {
  1344. var pos, line, start;
  1345. pos = index;
  1346. line = lineNumber;
  1347. start = lineStart;
  1348. lookahead = advance();
  1349. index = pos;
  1350. lineNumber = line;
  1351. lineStart = start;
  1352. }
  1353. function lookahead2() {
  1354. var adv, pos, line, start, result;
  1355. // If we are collecting the tokens, don't grab the next one yet.
  1356. adv = (typeof extra.advance === 'function') ? extra.advance : advance;
  1357. pos = index;
  1358. line = lineNumber;
  1359. start = lineStart;
  1360. // Scan for the next immediate token.
  1361. if (lookahead === null) {
  1362. lookahead = adv();
  1363. }
  1364. index = lookahead.range[1];
  1365. lineNumber = lookahead.lineNumber;
  1366. lineStart = lookahead.lineStart;
  1367. // Grab the token right after.
  1368. result = adv();
  1369. index = pos;
  1370. lineNumber = line;
  1371. lineStart = start;
  1372. return result;
  1373. }
  1374. SyntaxTreeDelegate = {
  1375. name: 'SyntaxTree',
  1376. postProcess: function (node) {
  1377. return node;
  1378. },
  1379. createArrayExpression: function (elements) {
  1380. return {
  1381. type: Syntax.ArrayExpression,
  1382. elements: elements
  1383. };
  1384. },
  1385. createAssignmentExpression: function (operator, left, right) {
  1386. return {
  1387. type: Syntax.AssignmentExpression,
  1388. operator: operator,
  1389. left: left,
  1390. right: right
  1391. };
  1392. },
  1393. createBinaryExpression: function (operator, left, right) {
  1394. var type = (operator === '||' || operator === '&&') ? Syntax.LogicalExpression :
  1395. Syntax.BinaryExpression;
  1396. return {
  1397. type: type,
  1398. operator: operator,
  1399. left: left,
  1400. right: right
  1401. };
  1402. },
  1403. createBlockStatement: function (body) {
  1404. return {
  1405. type: Syntax.BlockStatement,
  1406. body: body
  1407. };
  1408. },
  1409. createBreakStatement: function (label) {
  1410. return {
  1411. type: Syntax.BreakStatement,
  1412. label: label
  1413. };
  1414. },
  1415. createCallExpression: function (callee, args) {
  1416. return {
  1417. type: Syntax.CallExpression,
  1418. callee: callee,
  1419. 'arguments': args
  1420. };
  1421. },
  1422. createCatchClause: function (param, body) {
  1423. return {
  1424. type: Syntax.CatchClause,
  1425. param: param,
  1426. body: body
  1427. };
  1428. },
  1429. createConditionalExpression: function (test, consequent, alternate) {
  1430. return {
  1431. type: Syntax.ConditionalExpression,
  1432. test: test,
  1433. consequent: consequent,
  1434. alternate: alternate
  1435. };
  1436. },
  1437. createContinueStatement: function (label) {
  1438. return {
  1439. type: Syntax.ContinueStatement,
  1440. label: label
  1441. };
  1442. },
  1443. createDebuggerStatement: function () {
  1444. return {
  1445. type: Syntax.DebuggerStatement
  1446. };
  1447. },
  1448. createDoWhileStatement: function (body, test) {
  1449. return {
  1450. type: Syntax.DoWhileStatement,
  1451. body: body,
  1452. test: test
  1453. };
  1454. },
  1455. createEmptyStatement: function () {
  1456. return {
  1457. type: Syntax.EmptyStatement
  1458. };
  1459. },
  1460. createExpressionStatement: function (expression) {
  1461. return {
  1462. type: Syntax.ExpressionStatement,
  1463. expression: expression
  1464. };
  1465. },
  1466. createForStatement: function (init, test, update, body) {
  1467. return {
  1468. type: Syntax.ForStatement,
  1469. init: init,
  1470. test: test,
  1471. update: update,
  1472. body: body
  1473. };
  1474. },
  1475. createForInStatement: function (left, right, body) {
  1476. return {
  1477. type: Syntax.ForInStatement,
  1478. left: left,
  1479. right: right,
  1480. body: body,
  1481. each: false
  1482. };
  1483. },
  1484. createForOfStatement: function (left, right, body) {
  1485. return {
  1486. type: Syntax.ForOfStatement,
  1487. left: left,
  1488. right: right,
  1489. body: body
  1490. };
  1491. },
  1492. createFunctionDeclaration: function (id, params, defaults, body, rest, generator, expression,
  1493. returnType) {
  1494. return {
  1495. type: Syntax.FunctionDeclaration,
  1496. id: id,
  1497. params: params,
  1498. defaults: defaults,
  1499. body: body,
  1500. rest: rest,
  1501. generator: generator,
  1502. expression: expression,
  1503. returnType: returnType
  1504. };
  1505. },
  1506. createFunctionExpression: function (id, params, defaults, body, rest, generator, expression,
  1507. returnType) {
  1508. return {
  1509. type: Syntax.FunctionExpression,
  1510. id: id,
  1511. params: params,
  1512. defaults: defaults,
  1513. body: body,
  1514. rest: rest,
  1515. generator: generator,
  1516. expression: expression,
  1517. returnType: returnType
  1518. };
  1519. },
  1520. createIdentifier: function (name) {
  1521. return {
  1522. type: Syntax.Identifier,
  1523. name: name,
  1524. // Only here to initialize the shape of the object to ensure
  1525. // that the 'typeAnnotation' key is ordered before others that
  1526. // are added later (like 'loc' and 'range'). This just helps
  1527. // keep the shape of Identifier nodes consistent with everything
  1528. // else.
  1529. typeAnnotation: undefined
  1530. };
  1531. },
  1532. createTypeAnnotation: function (typeIdentifier, paramTypes, returnType, nullable) {
  1533. return {
  1534. type: Syntax.TypeAnnotation,
  1535. id: typeIdentifier,
  1536. paramTypes: paramTypes,
  1537. returnType: returnType,
  1538. nullable: nullable
  1539. };
  1540. },
  1541. createTypeAnnotatedIdentifier: function (identifier, annotation) {
  1542. return {
  1543. type: Syntax.TypeAnnotatedIdentifier,
  1544. id: identifier,
  1545. annotation: annotation
  1546. };
  1547. },
  1548. createXJSAttribute: function (name, value) {
  1549. return {
  1550. type: Syntax.XJSAttribute,
  1551. name: name,
  1552. value: value
  1553. };
  1554. },
  1555. createXJSIdentifier: function (name, namespace) {
  1556. return {
  1557. type: Syntax.XJSIdentifier,
  1558. name: name,
  1559. namespace: namespace
  1560. };
  1561. },
  1562. createXJSElement: function (openingElement, closingElement, children) {
  1563. return {
  1564. type: Syntax.XJSElement,
  1565. openingElement: openingElement,
  1566. closingElement: closingElement,
  1567. children: children
  1568. };
  1569. },
  1570. createXJSEmptyExpression: function () {
  1571. return {
  1572. type: Syntax.XJSEmptyExpression
  1573. };
  1574. },
  1575. createXJSExpressionContainer: function (expression) {
  1576. return {
  1577. type: Syntax.XJSExpressionContainer,
  1578. expression: expression
  1579. };
  1580. },
  1581. createXJSOpeningElement: function (name, attributes, selfClosing) {
  1582. return {
  1583. type: Syntax.XJSOpeningElement,
  1584. name: name,
  1585. selfClosing: selfClosing,
  1586. attributes: attributes
  1587. };
  1588. },
  1589. createXJSClosingElement: function (name) {
  1590. return {
  1591. type: Syntax.XJSClosingElement,
  1592. name: name
  1593. };
  1594. },
  1595. createIfStatement: function (test, consequent, alternate) {
  1596. return {
  1597. type: Syntax.IfStatement,
  1598. test: test,
  1599. consequent: consequent,
  1600. alternate: alternate
  1601. };
  1602. },
  1603. createLabeledStatement: function (label, body) {
  1604. return {
  1605. type: Syntax.LabeledStatement,
  1606. label: label,
  1607. body: body
  1608. };
  1609. },
  1610. createLiteral: function (token) {
  1611. return {
  1612. type: Syntax.Literal,
  1613. value: token.value,
  1614. raw: source.slice(token.range[0], token.range[1])
  1615. };
  1616. },
  1617. createMemberExpression: function (accessor, object, property) {
  1618. return {
  1619. type: Syntax.MemberExpression,
  1620. computed: accessor === '[',
  1621. object: object,
  1622. property: property
  1623. };
  1624. },
  1625. createNewExpression: function (callee, args) {
  1626. return {
  1627. type: Syntax.NewExpression,
  1628. callee: callee,
  1629. 'arguments': args
  1630. };
  1631. },
  1632. createObjectExpression: function (properties) {
  1633. return {
  1634. type: Syntax.ObjectExpression,
  1635. properties: properties
  1636. };
  1637. },
  1638. createPostfixExpression: function (operator, argument) {
  1639. return {
  1640. type: Syntax.UpdateExpression,
  1641. operator: operator,
  1642. argument: argument,
  1643. prefix: false
  1644. };
  1645. },
  1646. createProgram: function (body) {
  1647. return {
  1648. type: Syntax.Program,
  1649. body: body
  1650. };
  1651. },
  1652. createProperty: function (kind, key, value, method, shorthand) {
  1653. return {
  1654. type: Syntax.Property,
  1655. key: key,
  1656. value: value,
  1657. kind: kind,
  1658. method: method,
  1659. shorthand: shorthand
  1660. };
  1661. },
  1662. createReturnStatement: function (argument) {
  1663. return {
  1664. type: Syntax.ReturnStatement,
  1665. argument: argument
  1666. };
  1667. },
  1668. createSequenceExpression: function (expressions) {
  1669. return {
  1670. type: Syntax.SequenceExpression,
  1671. expressions: expressions
  1672. };
  1673. },
  1674. createSwitchCase: function (test, consequent) {
  1675. return {
  1676. type: Syntax.SwitchCase,
  1677. test: test,
  1678. consequent: consequent
  1679. };
  1680. },
  1681. createSwitchStatement: function (discriminant, cases) {
  1682. return {
  1683. type: Syntax.SwitchStatement,
  1684. discriminant: discriminant,
  1685. cases: cases
  1686. };
  1687. },
  1688. createThisExpression: function () {
  1689. return {
  1690. type: Syntax.ThisExpression
  1691. };
  1692. },
  1693. createThrowStatement: function (argument) {
  1694. return {
  1695. type: Syntax.ThrowStatement,
  1696. argument: argument
  1697. };
  1698. },
  1699. createTryStatement: function (block, guardedHandlers, handlers, finalizer) {
  1700. return {
  1701. type: Syntax.TryStatement,
  1702. block: block,
  1703. guardedHandlers: guardedHandlers,
  1704. handlers: handlers,
  1705. finalizer: finalizer
  1706. };
  1707. },
  1708. createUnaryExpression: function (operator, argument) {
  1709. if (operator === '++' || operator === '--') {
  1710. return {
  1711. type: Syntax.UpdateExpression,
  1712. operator: operator,
  1713. argument: argument,
  1714. prefix: true
  1715. };
  1716. }
  1717. return {
  1718. type: Syntax.UnaryExpression,
  1719. operator: operator,
  1720. argument: argument
  1721. };
  1722. },
  1723. createVariableDeclaration: function (declarations, kind) {
  1724. return {
  1725. type: Syntax.VariableDeclaration,
  1726. declarations: declarations,
  1727. kind: kind
  1728. };
  1729. },
  1730. createVariableDeclarator: function (id, init) {
  1731. return {
  1732. type: Syntax.VariableDeclarator,
  1733. id: id,
  1734. init: init
  1735. };
  1736. },
  1737. createWhileStatement: function (test, body) {
  1738. return {
  1739. type: Syntax.WhileStatement,
  1740. test: test,
  1741. body: body
  1742. };
  1743. },
  1744. createWithStatement: function (object, body) {
  1745. return {
  1746. type: Syntax.WithStatement,
  1747. object: object,
  1748. body: body
  1749. };
  1750. },
  1751. createTemplateElement: function (value, tail) {
  1752. return {
  1753. type: Syntax.TemplateElement,
  1754. value: value,
  1755. tail: tail
  1756. };
  1757. },
  1758. createTemplateLiteral: function (quasis, expressions) {
  1759. return {
  1760. type: Syntax.TemplateLiteral,
  1761. quasis: quasis,
  1762. expressions: expressions
  1763. };
  1764. },
  1765. createSpreadElement: function (argument) {
  1766. return {
  1767. type: Syntax.SpreadElement,
  1768. argument: argument
  1769. };
  1770. },
  1771. createTaggedTemplateExpression: function (tag, quasi) {
  1772. return {
  1773. type: Syntax.TaggedTemplateExpression,
  1774. tag: tag,
  1775. quasi: quasi
  1776. };
  1777. },
  1778. createArrowFunctionExpression: function (params, defaults, body, rest, expression) {
  1779. return {
  1780. type: Syntax.ArrowFunctionExpression,
  1781. id: null,
  1782. params: params,
  1783. defaults: defaults,
  1784. body: body,
  1785. rest: rest,
  1786. generator: false,
  1787. expression: expression
  1788. };
  1789. },
  1790. createMethodDefinition: function (propertyType, kind, key, value) {
  1791. return {
  1792. type: Syntax.MethodDefinition,
  1793. key: key,
  1794. value: value,
  1795. kind: kind,
  1796. 'static': propertyType === ClassPropertyType.static
  1797. };
  1798. },
  1799. createClassBody: function (body) {
  1800. return {
  1801. type: Syntax.ClassBody,
  1802. body: body
  1803. };
  1804. },
  1805. createClassExpression: function (id, superClass, body) {
  1806. return {
  1807. type: Syntax.ClassExpression,
  1808. id: id,
  1809. superClass: superClass,
  1810. body: body
  1811. };
  1812. },
  1813. createClassDeclaration: function (id, superClass, body) {
  1814. return {
  1815. type: Syntax.ClassDeclaration,
  1816. id: id,
  1817. superClass: superClass,
  1818. body: body
  1819. };
  1820. },
  1821. createExportSpecifier: function (id, name) {
  1822. return {
  1823. type: Syntax.ExportSpecifier,
  1824. id: id,
  1825. name: name
  1826. };
  1827. },
  1828. createExportBatchSpecifier: function () {
  1829. return {
  1830. type: Syntax.ExportBatchSpecifier
  1831. };
  1832. },
  1833. createExportDeclaration: function (declaration, specifiers, source) {
  1834. return {
  1835. type: Syntax.ExportDeclaration,
  1836. declaration: declaration,
  1837. specifiers: specifiers,
  1838. source: source
  1839. };
  1840. },
  1841. createImportSpecifier: function (id, name) {
  1842. return {
  1843. type: Syntax.ImportSpecifier,
  1844. id: id,
  1845. name: name
  1846. };
  1847. },
  1848. createImportDeclaration: function (specifiers, kind, source) {
  1849. return {
  1850. type: Syntax.ImportDeclaration,
  1851. specifiers: specifiers,
  1852. kind: kind,
  1853. source: source
  1854. };
  1855. },
  1856. createYieldExpression: function (argument, delegate) {
  1857. return {
  1858. type: Syntax.YieldExpression,
  1859. argument: argument,
  1860. delegate: delegate
  1861. };
  1862. },
  1863. createModuleDeclaration: function (id, source, body) {
  1864. return {
  1865. type: Syntax.ModuleDeclaration,
  1866. id: id,
  1867. source: source,
  1868. body: body
  1869. };
  1870. }
  1871. };
  1872. // Return true if there is a line terminator before the next token.
  1873. function peekLineTerminator() {
  1874. var pos, line, start, found;
  1875. pos = index;
  1876. line = lineNumber;
  1877. start = lineStart;
  1878. skipComment();
  1879. found = lineNumber !== line;
  1880. index = pos;
  1881. lineNumber = line;
  1882. lineStart = start;
  1883. return found;
  1884. }
  1885. // Throw an exception
  1886. function throwError(token, messageFormat) {
  1887. var error,
  1888. args = Array.prototype.slice.call(arguments, 2),
  1889. msg = messageFormat.replace(
  1890. /%(\d)/g,
  1891. function (whole, index) {
  1892. assert(index < args.length, 'Message reference must be in range');
  1893. return args[index];
  1894. }
  1895. );
  1896. if (typeof token.lineNumber === 'number') {
  1897. error = new Error('Line ' + token.lineNumber + ': ' + msg);
  1898. error.index = token.range[0];
  1899. error.lineNumber = token.lineNumber;
  1900. error.column = token.range[0] - lineStart + 1;
  1901. } else {
  1902. error = new Error('Line ' + lineNumber + ': ' + msg);
  1903. error.index = index;
  1904. error.lineNumber = lineNumber;
  1905. error.column = index - lineStart + 1;
  1906. }
  1907. error.description = msg;
  1908. throw error;
  1909. }
  1910. function throwErrorTolerant() {
  1911. try {
  1912. throwError.apply(null, arguments);
  1913. } catch (e) {
  1914. if (extra.errors) {
  1915. extra.errors.push(e);
  1916. } else {
  1917. throw e;
  1918. }
  1919. }
  1920. }
  1921. // Throw an exception because of the token.
  1922. function throwUnexpected(token) {
  1923. if (token.type === Token.EOF) {
  1924. throwError(token, Messages.UnexpectedEOS);
  1925. }
  1926. if (token.type === Token.NumericLiteral) {
  1927. throwError(token, Messages.UnexpectedNumber);
  1928. }
  1929. if (token.type === Token.StringLiteral || token.type === Token.XJSText) {
  1930. throwError(token, Messages.UnexpectedString);
  1931. }
  1932. if (token.type === Token.Identifier) {
  1933. throwError(token, Messages.UnexpectedIdentifier);
  1934. }
  1935. if (token.type === Token.Keyword) {
  1936. if (isFutureReservedWord(token.value)) {
  1937. throwError(token, Messages.UnexpectedReserved);
  1938. } else if (strict && isStrictModeReservedWord(token.value)) {
  1939. throwErrorTolerant(token, Messages.StrictReservedWord);
  1940. return;
  1941. }
  1942. throwError(token, Messages.UnexpectedToken, token.value);
  1943. }
  1944. if (token.type === Token.Template) {
  1945. throwError(token, Messages.UnexpectedTemplate, token.value.raw);
  1946. }
  1947. // BooleanLiteral, NullLiteral, or Punctuator.
  1948. throwError(token, Messages.UnexpectedToken, token.value);
  1949. }
  1950. // Expect the next token to match the specified punctuator.
  1951. // If not, an exception will be thrown.
  1952. function expect(value) {
  1953. var token = lex();
  1954. if (token.type !== Token.Punctuator || token.value !== value) {
  1955. throwUnexpected(token);
  1956. }
  1957. }
  1958. // Expect the next token to match the specified keyword.
  1959. // If not, an exception will be thrown.
  1960. function expectKeyword(keyword) {
  1961. var token = lex();
  1962. if (token.type !== Token.Keyword || token.value !== keyword) {
  1963. throwUnexpected(token);
  1964. }
  1965. }
  1966. // Return true if the next token matches the specified punctuator.
  1967. function match(value) {
  1968. return lookahead.type === Token.Punctuator && lookahead.value === value;
  1969. }
  1970. // Return true if the next token matches the specified keyword
  1971. function matchKeyword(keyword) {
  1972. return lookahead.type === Token.Keyword && lookahead.value === keyword;
  1973. }
  1974. // Return true if the next token matches the specified contextual keyword
  1975. function matchContextualKeyword(keyword) {
  1976. return lookahead.type === Token.Identifier && lookahead.value === keyword;
  1977. }
  1978. // Return true if the next token is an assignment operator
  1979. function matchAssign() {
  1980. var op;
  1981. if (lookahead.type !== Token.Punctuator) {
  1982. return false;
  1983. }
  1984. op = lookahead.value;
  1985. return op === '=' ||
  1986. op === '*=' ||
  1987. op === '/=' ||
  1988. op === '%=' ||
  1989. op === '+=' ||
  1990. op === '-=' ||
  1991. op === '<<=' ||
  1992. op === '>>=' ||
  1993. op === '>>>=' ||
  1994. op === '&=' ||
  1995. op === '^=' ||
  1996. op === '|=';
  1997. }
  1998. function consumeSemicolon() {
  1999. var line;
  2000. // Catch the very common case first: immediately a semicolon (char #59).
  2001. if (source.charCodeAt(index) === 59) {
  2002. lex();
  2003. return;
  2004. }
  2005. line = lineNumber;
  2006. skipComment();
  2007. if (lineNumber !== line) {
  2008. return;
  2009. }
  2010. if (match(';')) {
  2011. lex();
  2012. return;
  2013. }
  2014. if (lookahead.type !== Token.EOF && !match('}')) {
  2015. throwUnexpected(lookahead);
  2016. }
  2017. }
  2018. // Return true if provided expression is LeftHandSideExpression
  2019. function isLeftHandSide(expr) {
  2020. return expr.type === Syntax.Identifier || expr.type === Syntax.MemberExpression;
  2021. }
  2022. function isAssignableLeftHandSide(expr) {
  2023. return isLeftHandSide(expr) || expr.type === Syntax.ObjectPattern || expr.type === Syntax.ArrayPattern;
  2024. }
  2025. // 11.1.4 Array Initialiser
  2026. function parseArrayInitialiser() {
  2027. var elements = [], blocks = [], filter = null, tmp, possiblecomprehension = true, body;
  2028. expect('[');
  2029. while (!match(']')) {
  2030. if (lookahead.value === 'for' &&
  2031. lookahead.type === Token.Keyword) {
  2032. if (!possiblecomprehension) {
  2033. throwError({}, Messages.ComprehensionError);
  2034. }
  2035. matchKeyword('for');
  2036. tmp = parseForStatement({ignoreBody: true});
  2037. tmp.of = tmp.type === Syntax.ForOfStatement;
  2038. tmp.type = Syntax.ComprehensionBlock;
  2039. if (tmp.left.kind) { // can't be let or const
  2040. throwError({}, Messages.ComprehensionError);
  2041. }
  2042. blocks.push(tmp);
  2043. } else if (lookahead.value === 'if' &&
  2044. lookahead.type === Token.Keyword) {
  2045. if (!possiblecomprehension) {
  2046. throwError({}, Messages.ComprehensionError);
  2047. }
  2048. expectKeyword('if');
  2049. expect('(');
  2050. filter = parseExpression();
  2051. expect(')');
  2052. } else if (lookahead.value === ',' &&
  2053. lookahead.type === Token.Punctuator) {
  2054. possiblecomprehension = false; // no longer allowed.
  2055. lex();
  2056. elements.push(null);
  2057. } else {
  2058. tmp = parseSpreadOrAssignmentExpression();
  2059. elements.push(tmp);
  2060. if (tmp && tmp.type === Syntax.SpreadElement) {
  2061. if (!match(']')) {
  2062. throwError({}, Messages.ElementAfterSpreadElement);
  2063. }
  2064. } else if (!(match(']') || matchKeyword('for') || matchKeyword('if'))) {
  2065. expect(','); // this lexes.
  2066. possiblecomprehension = false;
  2067. }
  2068. }
  2069. }
  2070. expect(']');
  2071. if (filter && !blocks.length) {
  2072. throwError({}, Messages.ComprehensionRequiresBlock);
  2073. }
  2074. if (blocks.length) {
  2075. if (elements.length !== 1) {
  2076. throwError({}, Messages.ComprehensionError);
  2077. }
  2078. return {
  2079. type: Syntax.ComprehensionExpression,
  2080. filter: filter,
  2081. blocks: blocks,
  2082. body: elements[0]
  2083. };
  2084. }
  2085. return delegate.createArrayExpression(elements);
  2086. }
  2087. // 11.1.5 Object Initialiser
  2088. function parsePropertyFunction(options) {
  2089. var previousStrict, previousYieldAllowed, params, defaults, body;
  2090. previousStrict = strict;
  2091. previousYieldAllowed = state.yieldAllowed;
  2092. state.yieldAllowed = options.generator;
  2093. params = options.params || [];
  2094. defaults = options.defaults || [];
  2095. body = parseConciseBody();
  2096. if (options.name && strict && isRestrictedWord(params[0].name)) {
  2097. throwErrorTolerant(options.name, Messages.StrictParamName);
  2098. }
  2099. if (state.yieldAllowed && !state.yieldFound) {
  2100. throwErrorTolerant({}, Messages.NoYieldInGenerator);
  2101. }
  2102. strict = previousStrict;
  2103. state.yieldAllowed = previousYieldAllowed;
  2104. return delegate.createFunctionExpression(null, params, defaults, body, options.rest || null, options.generator, body.type !== Syntax.BlockStatement,
  2105. options.returnTypeAnnotation);
  2106. }
  2107. function parsePropertyMethodFunction(options) {
  2108. var previousStrict, tmp, method;
  2109. previousStrict = strict;
  2110. strict = true;
  2111. tmp = parseParams();
  2112. if (tmp.stricted) {
  2113. throwErrorTolerant(tmp.stricted, tmp.message);
  2114. }
  2115. method = parsePropertyFunction({
  2116. params: tmp.params,
  2117. defaults: tmp.defaults,
  2118. rest: tmp.rest,
  2119. generator: options.generator,
  2120. returnTypeAnnotation: tmp.returnTypeAnnotation
  2121. });
  2122. strict = previousStrict;
  2123. return method;
  2124. }
  2125. function parseObjectPropertyKey() {
  2126. var token = lex();
  2127. // Note: This function is called only from parseObjectProperty(), where
  2128. // EOF and Punctuator tokens are already filtered out.
  2129. if (token.type === Token.StringLiteral || token.type === Token.NumericLiteral) {
  2130. if (strict && token.octal) {
  2131. throwErrorTolerant(token, Messages.StrictOctalLiteral);
  2132. }
  2133. return delegate.createLiteral(token);
  2134. }
  2135. return delegate.createIdentifier(token.value);
  2136. }
  2137. function parseObjectProperty() {
  2138. var token, key, id, value, param;
  2139. token = lookahead;
  2140. if (token.type === Token.Identifier) {
  2141. id = parseObjectPropertyKey();
  2142. // Property Assignment: Getter and Setter.
  2143. if (token.value === 'get' && !(match(':') || match('('))) {
  2144. key = parseObjectPropertyKey();
  2145. expect('(');
  2146. expect(')');
  2147. return delegate.createProperty('get', key, parsePropertyFunction({ generator: false }), false, false);
  2148. }
  2149. if (token.value === 'set' && !(match(':') || match('('))) {
  2150. key = parseObjectPropertyKey();
  2151. expect('(');
  2152. token = lookahead;
  2153. param = [ parseTypeAnnotatableIdentifier() ];
  2154. expect(')');
  2155. return delegate.createProperty('set', key, parsePropertyFunction({ params: param, generator: false, name: token }), false, false);
  2156. }
  2157. if (match(':')) {
  2158. lex();
  2159. return delegate.createProperty('init', id, parseAssignmentExpression(), false, false);
  2160. }
  2161. if (match('(')) {
  2162. return delegate.createProperty('init', id, parsePropertyMethodFunction({ generator: false }), true, false);
  2163. }
  2164. return delegate.createProperty('init', id, id, false, true);
  2165. }
  2166. if (token.type === Token.EOF || token.type === Token.Punctuator) {
  2167. if (!match('*')) {
  2168. throwUnexpected(token);
  2169. }
  2170. lex();
  2171. id = parseObjectPropertyKey();
  2172. if (!match('(')) {
  2173. throwUnexpected(lex());
  2174. }
  2175. return delegate.createProperty('init', id, parsePropertyMethodFunction({ generator: true }), true, false);
  2176. }
  2177. key = parseObjectPropertyKey();
  2178. if (match(':')) {
  2179. lex();
  2180. return delegate.createProperty('init', key, parseAssignmentExpression(), false, false);
  2181. }
  2182. if (match('(')) {
  2183. return delegate.createProperty('init', key, parsePropertyMethodFunction({ generator: false }), true, false);
  2184. }
  2185. throwUnexpected(lex());
  2186. }
  2187. function parseObjectInitialiser() {
  2188. var properties = [], property, name, key, kind, map = {}, toString = String;
  2189. expect('{');
  2190. while (!match('}')) {
  2191. property = parseObjectProperty();
  2192. if (property.key.type === Syntax.Identifier) {
  2193. name = property.key.name;
  2194. } else {
  2195. name = toString(property.key.value);
  2196. }
  2197. kind = (property.kind === 'init') ? PropertyKind.Data : (property.kind === 'get') ? PropertyKind.Get : PropertyKind.Set;
  2198. key = '$' + name;
  2199. if (Object.prototype.hasOwnProperty.call(map, key)) {
  2200. if (map[key] === PropertyKind.Data) {
  2201. if (strict && kind === PropertyKind.Data) {
  2202. throwErrorTolerant({}, Messages.StrictDuplicateProperty);
  2203. } else if (kind !== PropertyKind.Data) {
  2204. throwErrorTolerant({}, Messages.AccessorDataProperty);
  2205. }
  2206. } else {
  2207. if (kind === PropertyKind.Data) {
  2208. throwErrorTolerant({}, Messages.AccessorDataProperty);
  2209. } else if (map[key] & kind) {
  2210. throwErrorTolerant({}, Messages.AccessorGetSet);
  2211. }
  2212. }
  2213. map[key] |= kind;
  2214. } else {
  2215. map[key] = kind;
  2216. }
  2217. properties.push(property);
  2218. if (!match('}')) {
  2219. expect(',');
  2220. }
  2221. }
  2222. expect('}');
  2223. return delegate.createObjectExpression(properties);
  2224. }
  2225. function parseTemplateElement(option) {
  2226. var token = scanTemplateElement(option);
  2227. if (strict && token.octal) {
  2228. throwError(token, Messages.StrictOctalLiteral);
  2229. }
  2230. return delegate.createTemplateElement({ raw: token.value.raw, cooked: token.value.cooked }, token.tail);
  2231. }
  2232. function parseTemplateLiteral() {
  2233. var quasi, quasis, expressions;
  2234. quasi = parseTemplateElement({ head: true });
  2235. quasis = [ quasi ];
  2236. expressions = [];
  2237. while (!quasi.tail) {
  2238. expressions.push(parseExpression());
  2239. quasi = parseTemplateElement({ head: false });
  2240. quasis.push(quasi);
  2241. }
  2242. return delegate.createTemplateLiteral(quasis, expressions);
  2243. }
  2244. // 11.1.6 The Grouping Operator
  2245. function parseGroupExpression() {
  2246. var expr;
  2247. expect('(');
  2248. ++state.parenthesizedCount;
  2249. expr = parseExpression();
  2250. expect(')');
  2251. return expr;
  2252. }
  2253. // 11.1 Primary Expressions
  2254. function parsePrimaryExpression() {
  2255. var type, token;
  2256. token = lookahead;
  2257. type = lookahead.type;
  2258. if (type === Token.Identifier) {
  2259. lex();
  2260. return delegate.createIdentifier(token.value);
  2261. }
  2262. if (type === Token.StringLiteral || type === Token.NumericLiteral) {
  2263. if (strict && lookahead.octal) {
  2264. throwErrorTolerant(lookahead, Messages.StrictOctalLiteral);
  2265. }
  2266. return delegate.createLiteral(lex());
  2267. }
  2268. if (type === Token.Keyword) {
  2269. if (matchKeyword('this')) {
  2270. lex();
  2271. return delegate.createThisExpression();
  2272. }
  2273. if (matchKeyword('function')) {
  2274. return parseFunctionExpression();
  2275. }
  2276. if (matchKeyword('class')) {
  2277. return parseClassExpression();
  2278. }
  2279. if (matchKeyword('super')) {
  2280. lex();
  2281. return delegate.createIdentifier('super');
  2282. }
  2283. }
  2284. if (type === Token.BooleanLiteral) {
  2285. token = lex();
  2286. token.value = (token.value === 'true');
  2287. return delegate.createLiteral(token);
  2288. }
  2289. if (type === Token.NullLiteral) {
  2290. token = lex();
  2291. token.value = null;
  2292. return delegate.createLiteral(token);
  2293. }
  2294. if (match('[')) {
  2295. return parseArrayInitialiser();
  2296. }
  2297. if (match('{')) {
  2298. return parseObjectInitialiser();
  2299. }
  2300. if (match('(')) {
  2301. return parseGroupExpression();
  2302. }
  2303. if (match('/') || match('/=')) {
  2304. return delegate.createLiteral(scanRegExp());
  2305. }
  2306. if (type === Token.Template) {
  2307. return parseTemplateLiteral();
  2308. }
  2309. if (match('<')) {
  2310. return parseXJSElement();
  2311. }
  2312. return throwUnexpected(lex());
  2313. }
  2314. // 11.2 Left-Hand-Side Expressions
  2315. function parseArguments() {
  2316. var args = [], arg;
  2317. expect('(');
  2318. if (!match(')')) {
  2319. while (index < length) {
  2320. arg = parseSpreadOrAssignmentExpression();
  2321. args.push(arg);
  2322. if (match(')')) {
  2323. break;
  2324. } else if (arg.type === Syntax.SpreadElement) {
  2325. throwError({}, Messages.ElementAfterSpreadElement);
  2326. }
  2327. expect(',');
  2328. }
  2329. }
  2330. expect(')');
  2331. return args;
  2332. }
  2333. function parseSpreadOrAssignmentExpression() {
  2334. if (match('...')) {
  2335. lex();
  2336. return delegate.createSpreadElement(parseAssignmentExpression());
  2337. }
  2338. return parseAssignmentExpression();
  2339. }
  2340. function parseNonComputedProperty() {
  2341. var token = lex();
  2342. if (!isIdentifierName(token)) {
  2343. throwUnexpected(token);
  2344. }
  2345. return delegate.createIdentifier(token.value);
  2346. }
  2347. function parseNonComputedMember() {
  2348. expect('.');
  2349. return parseNonComputedProperty();
  2350. }
  2351. function parseComputedMember() {
  2352. var expr;
  2353. expect('[');
  2354. expr = parseExpression();
  2355. expect(']');
  2356. return expr;
  2357. }
  2358. function parseNewExpression() {
  2359. var callee, args;
  2360. expectKeyword('new');
  2361. callee = parseLeftHandSideExpression();
  2362. args = match('(') ? parseArguments() : [];
  2363. return delegate.createNewExpression(callee, args);
  2364. }
  2365. function parseLeftHandSideExpressionAllowCall() {
  2366. var expr, args, property;
  2367. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  2368. while (match('.') || match('[') || match('(') || lookahead.type === Token.Template) {
  2369. if (match('(')) {
  2370. args = parseArguments();
  2371. expr = delegate.createCallExpression(expr, args);
  2372. } else if (match('[')) {
  2373. expr = delegate.createMemberExpression('[', expr, parseComputedMember());
  2374. } else if (match('.')) {
  2375. expr = delegate.createMemberExpression('.', expr, parseNonComputedMember());
  2376. } else {
  2377. expr = delegate.createTaggedTemplateExpression(expr, parseTemplateLiteral());
  2378. }
  2379. }
  2380. return expr;
  2381. }
  2382. function parseLeftHandSideExpression() {
  2383. var expr, property;
  2384. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  2385. while (match('.') || match('[') || lookahead.type === Token.Template) {
  2386. if (match('[')) {
  2387. expr = delegate.createMemberExpression('[', expr, parseComputedMember());
  2388. } else if (match('.')) {
  2389. expr = delegate.createMemberExpression('.', expr, parseNonComputedMember());
  2390. } else {
  2391. expr = delegate.createTaggedTemplateExpression(expr, parseTemplateLiteral());
  2392. }
  2393. }
  2394. return expr;
  2395. }
  2396. // 11.3 Postfix Expressions
  2397. function parsePostfixExpression() {
  2398. var expr = parseLeftHandSideExpressionAllowCall(),
  2399. token = lookahead;
  2400. if (lookahead.type !== Token.Punctuator) {
  2401. return expr;
  2402. }
  2403. if ((match('++') || match('--')) && !peekLineTerminator()) {
  2404. // 11.3.1, 11.3.2
  2405. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2406. throwErrorTolerant({}, Messages.StrictLHSPostfix);
  2407. }
  2408. if (!isLeftHandSide(expr)) {
  2409. throwError({}, Messages.InvalidLHSInAssignment);
  2410. }
  2411. token = lex();
  2412. expr = delegate.createPostfixExpression(token.value, expr);
  2413. }
  2414. return expr;
  2415. }
  2416. // 11.4 Unary Operators
  2417. function parseUnaryExpression() {
  2418. var token, expr;
  2419. if (lookahead.type !== Token.Punctuator && lookahead.type !== Token.Keyword) {
  2420. return parsePostfixExpression();
  2421. }
  2422. if (match('++') || match('--')) {
  2423. token = lex();
  2424. expr = parseUnaryExpression();
  2425. // 11.4.4, 11.4.5
  2426. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2427. throwErrorTolerant({}, Messages.StrictLHSPrefix);
  2428. }
  2429. if (!isLeftHandSide(expr)) {
  2430. throwError({}, Messages.InvalidLHSInAssignment);
  2431. }
  2432. return delegate.createUnaryExpression(token.value, expr);
  2433. }
  2434. if (match('+') || match('-') || match('~') || match('!')) {
  2435. token = lex();
  2436. expr = parseUnaryExpression();
  2437. return delegate.createUnaryExpression(token.value, expr);
  2438. }
  2439. if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) {
  2440. token = lex();
  2441. expr = parseUnaryExpression();
  2442. expr = delegate.createUnaryExpression(token.value, expr);
  2443. if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) {
  2444. throwErrorTolerant({}, Messages.StrictDelete);
  2445. }
  2446. return expr;
  2447. }
  2448. return parsePostfixExpression();
  2449. }
  2450. function binaryPrecedence(token, allowIn) {
  2451. var prec = 0;
  2452. if (token.type !== Token.Punctuator && token.type !== Token.Keyword) {
  2453. return 0;
  2454. }
  2455. switch (token.value) {
  2456. case '||':
  2457. prec = 1;
  2458. break;
  2459. case '&&':
  2460. prec = 2;
  2461. break;
  2462. case '|':
  2463. prec = 3;
  2464. break;
  2465. case '^':
  2466. prec = 4;
  2467. break;
  2468. case '&':
  2469. prec = 5;
  2470. break;
  2471. case '==':
  2472. case '!=':
  2473. case '===':
  2474. case '!==':
  2475. prec = 6;
  2476. break;
  2477. case '<':
  2478. case '>':
  2479. case '<=':
  2480. case '>=':
  2481. case 'instanceof':
  2482. prec = 7;
  2483. break;
  2484. case 'in':
  2485. prec = allowIn ? 7 : 0;
  2486. break;
  2487. case '<<':
  2488. case '>>':
  2489. case '>>>':
  2490. prec = 8;
  2491. break;
  2492. case '+':
  2493. case '-':
  2494. prec = 9;
  2495. break;
  2496. case '*':
  2497. case '/':
  2498. case '%':
  2499. prec = 11;
  2500. break;
  2501. default:
  2502. break;
  2503. }
  2504. return prec;
  2505. }
  2506. // 11.5 Multiplicative Operators
  2507. // 11.6 Additive Operators
  2508. // 11.7 Bitwise Shift Operators
  2509. // 11.8 Relational Operators
  2510. // 11.9 Equality Operators
  2511. // 11.10 Binary Bitwise Operators
  2512. // 11.11 Binary Logical Operators
  2513. function parseBinaryExpression() {
  2514. var expr, token, prec, previousAllowIn, stack, right, operator, left, i;
  2515. previousAllowIn = state.allowIn;
  2516. state.allowIn = true;
  2517. expr = parseUnaryExpression();
  2518. token = lookahead;
  2519. prec = binaryPrecedence(token, previousAllowIn);
  2520. if (prec === 0) {
  2521. return expr;
  2522. }
  2523. token.prec = prec;
  2524. lex();
  2525. stack = [expr, token, parseUnaryExpression()];
  2526. while ((prec = binaryPrecedence(lookahead, previousAllowIn)) > 0) {
  2527. // Reduce: make a binary expression from the three topmost entries.
  2528. while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) {
  2529. right = stack.pop();
  2530. operator = stack.pop().value;
  2531. left = stack.pop();
  2532. stack.push(delegate.createBinaryExpression(operator, left, right));
  2533. }
  2534. // Shift.
  2535. token = lex();
  2536. token.prec = prec;
  2537. stack.push(token);
  2538. stack.push(parseUnaryExpression());
  2539. }
  2540. state.allowIn = previousAllowIn;
  2541. // Final reduce to clean-up the stack.
  2542. i = stack.length - 1;
  2543. expr = stack[i];
  2544. while (i > 1) {
  2545. expr = delegate.createBinaryExpression(stack[i - 1].value, stack[i - 2], expr);
  2546. i -= 2;
  2547. }
  2548. return expr;
  2549. }
  2550. // 11.12 Conditional Operator
  2551. function parseConditionalExpression() {
  2552. var expr, previousAllowIn, consequent, alternate;
  2553. expr = parseBinaryExpression();
  2554. if (match('?')) {
  2555. lex();
  2556. previousAllowIn = state.allowIn;
  2557. state.allowIn = true;
  2558. consequent = parseAssignmentExpression();
  2559. state.allowIn = previousAllowIn;
  2560. expect(':');
  2561. alternate = parseAssignmentExpression();
  2562. expr = delegate.createConditionalExpression(expr, consequent, alternate);
  2563. }
  2564. return expr;
  2565. }
  2566. // 11.13 Assignment Operators
  2567. function reinterpretAsAssignmentBindingPattern(expr) {
  2568. var i, len, property, element;
  2569. if (expr.type === Syntax.ObjectExpression) {
  2570. expr.type = Syntax.ObjectPattern;
  2571. for (i = 0, len = expr.properties.length; i < len; i += 1) {
  2572. property = expr.properties[i];
  2573. if (property.kind !== 'init') {
  2574. throwError({}, Messages.InvalidLHSInAssignment);
  2575. }
  2576. reinterpretAsAssignmentBindingPattern(property.value);
  2577. }
  2578. } else if (expr.type === Syntax.ArrayExpression) {
  2579. expr.type = Syntax.ArrayPattern;
  2580. for (i = 0, len = expr.elements.length; i < len; i += 1) {
  2581. element = expr.elements[i];
  2582. if (element) {
  2583. reinterpretAsAssignmentBindingPattern(element);
  2584. }
  2585. }
  2586. } else if (expr.type === Syntax.Identifier) {
  2587. if (isRestrictedWord(expr.name)) {
  2588. throwError({}, Messages.InvalidLHSInAssignment);
  2589. }
  2590. } else if (expr.type === Syntax.SpreadElement) {
  2591. reinterpretAsAssignmentBindingPattern(expr.argument);
  2592. if (expr.argument.type === Syntax.ObjectPattern) {
  2593. throwError({}, Messages.ObjectPatternAsSpread);
  2594. }
  2595. } else {
  2596. if (expr.type !== Syntax.MemberExpression && expr.type !== Syntax.CallExpression && expr.type !== Syntax.NewExpression) {
  2597. throwError({}, Messages.InvalidLHSInAssignment);
  2598. }
  2599. }
  2600. }
  2601. function reinterpretAsDestructuredParameter(options, expr) {
  2602. var i, len, property, element;
  2603. if (expr.type === Syntax.ObjectExpression) {
  2604. expr.type = Syntax.ObjectPattern;
  2605. for (i = 0, len = expr.properties.length; i < len; i += 1) {
  2606. property = expr.properties[i];
  2607. if (property.kind !== 'init') {
  2608. throwError({}, Messages.InvalidLHSInFormalsList);
  2609. }
  2610. reinterpretAsDestructuredParameter(options, property.value);
  2611. }
  2612. } else if (expr.type === Syntax.ArrayExpression) {
  2613. expr.type = Syntax.ArrayPattern;
  2614. for (i = 0, len = expr.elements.length; i < len; i += 1) {
  2615. element = expr.elements[i];
  2616. if (element) {
  2617. reinterpretAsDestructuredParameter(options, element);
  2618. }
  2619. }
  2620. } else if (expr.type === Syntax.Identifier) {
  2621. validateParam(options, expr, expr.name);
  2622. } else {
  2623. if (expr.type !== Syntax.MemberExpression) {
  2624. throwError({}, Messages.InvalidLHSInFormalsList);
  2625. }
  2626. }
  2627. }
  2628. function reinterpretAsCoverFormalsList(expressions) {
  2629. var i, len, param, params, defaults, defaultCount, options, rest;
  2630. params = [];
  2631. defaults = [];
  2632. defaultCount = 0;
  2633. rest = null;
  2634. options = {
  2635. paramSet: {}
  2636. };
  2637. for (i = 0, len = expressions.length; i < len; i += 1) {
  2638. param = expressions[i];
  2639. if (param.type === Syntax.Identifier) {
  2640. params.push(param);
  2641. defaults.push(null);
  2642. validateParam(options, param, param.name);
  2643. } else if (param.type === Syntax.ObjectExpression || param.type === Syntax.ArrayExpression) {
  2644. reinterpretAsDestructuredParameter(options, param);
  2645. params.push(param);
  2646. defaults.push(null);
  2647. } else if (param.type === Syntax.SpreadElement) {
  2648. assert(i === len - 1, 'It is guaranteed that SpreadElement is last element by parseExpression');
  2649. reinterpretAsDestructuredParameter(options, param.argument);
  2650. rest = param.argument;
  2651. } else if (param.type === Syntax.AssignmentExpression) {
  2652. params.push(param.left);
  2653. defaults.push(param.right);
  2654. ++defaultCount;
  2655. validateParam(options, param.left, param.left.name);
  2656. } else {
  2657. return null;
  2658. }
  2659. }
  2660. if (options.message === Messages.StrictParamDupe) {
  2661. throwError(
  2662. strict ? options.stricted : options.firstRestricted,
  2663. options.message
  2664. );
  2665. }
  2666. if (defaultCount === 0) {
  2667. defaults = [];
  2668. }
  2669. return {
  2670. params: params,
  2671. defaults: defaults,
  2672. rest: rest,
  2673. stricted: options.stricted,
  2674. firstRestricted: options.firstRestricted,
  2675. message: options.message
  2676. };
  2677. }
  2678. function parseArrowFunctionExpression(options) {
  2679. var previousStrict, previousYieldAllowed, body;
  2680. expect('=>');
  2681. previousStrict = strict;
  2682. previousYieldAllowed = state.yieldAllowed;
  2683. state.yieldAllowed = false;
  2684. body = parseConciseBody();
  2685. if (strict && options.firstRestricted) {
  2686. throwError(options.firstRestricted, options.message);
  2687. }
  2688. if (strict && options.stricted) {
  2689. throwErrorTolerant(options.stricted, options.message);
  2690. }
  2691. strict = previousStrict;
  2692. state.yieldAllowed = previousYieldAllowed;
  2693. return delegate.createArrowFunctionExpression(options.params, options.defaults, body, options.rest, body.type !== Syntax.BlockStatement);
  2694. }
  2695. function parseAssignmentExpression() {
  2696. var expr, token, params, oldParenthesizedCount;
  2697. if (matchKeyword('yield')) {
  2698. return parseYieldExpression();
  2699. }
  2700. oldParenthesizedCount = state.parenthesizedCount;
  2701. if (match('(')) {
  2702. token = lookahead2();
  2703. if ((token.type === Token.Punctuator && token.value === ')') || token.value === '...') {
  2704. params = parseParams();
  2705. if (!match('=>')) {
  2706. throwUnexpected(lex());
  2707. }
  2708. return parseArrowFunctionExpression(params);
  2709. }
  2710. }
  2711. token = lookahead;
  2712. expr = parseConditionalExpression();
  2713. if (match('=>') &&
  2714. (state.parenthesizedCount === oldParenthesizedCount ||
  2715. state.parenthesizedCount === (oldParenthesizedCount + 1))) {
  2716. if (expr.type === Syntax.Identifier) {
  2717. params = reinterpretAsCoverFormalsList([ expr ]);
  2718. } else if (expr.type === Syntax.SequenceExpression) {
  2719. params = reinterpretAsCoverFormalsList(expr.expressions);
  2720. }
  2721. if (params) {
  2722. return parseArrowFunctionExpression(params);
  2723. }
  2724. }
  2725. if (matchAssign()) {
  2726. // 11.13.1
  2727. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2728. throwErrorTolerant(token, Messages.StrictLHSAssignment);
  2729. }
  2730. // ES.next draf 11.13 Runtime Semantics step 1
  2731. if (match('=') && (expr.type === Syntax.ObjectExpression || expr.type === Syntax.ArrayExpression)) {
  2732. reinterpretAsAssignmentBindingPattern(expr);
  2733. } else if (!isLeftHandSide(expr)) {
  2734. throwError({}, Messages.InvalidLHSInAssignment);
  2735. }
  2736. expr = delegate.createAssignmentExpression(lex().value, expr, parseAssignmentExpression());
  2737. }
  2738. return expr;
  2739. }
  2740. // 11.14 Comma Operator
  2741. function parseExpression() {
  2742. var expr, expressions, sequence, coverFormalsList, spreadFound, oldParenthesizedCount;
  2743. oldParenthesizedCount = state.parenthesizedCount;
  2744. expr = parseAssignmentExpression();
  2745. expressions = [ expr ];
  2746. if (match(',')) {
  2747. while (index < length) {
  2748. if (!match(',')) {
  2749. break;
  2750. }
  2751. lex();
  2752. expr = parseSpreadOrAssignmentExpression();
  2753. expressions.push(expr);
  2754. if (expr.type === Syntax.SpreadElement) {
  2755. spreadFound = true;
  2756. if (!match(')')) {
  2757. throwError({}, Messages.ElementAfterSpreadElement);
  2758. }
  2759. break;
  2760. }
  2761. }
  2762. sequence = delegate.createSequenceExpression(expressions);
  2763. }
  2764. if (match('=>')) {
  2765. // Do not allow nested parentheses on the LHS of the =>.
  2766. if (state.parenthesizedCount === oldParenthesizedCount || state.parenthesizedCount === (oldParenthesizedCount + 1)) {
  2767. expr = expr.type === Syntax.SequenceExpression ? expr.expressions : expressions;
  2768. coverFormalsList = reinterpretAsCoverFormalsList(expr);
  2769. if (coverFormalsList) {
  2770. return parseArrowFunctionExpression(coverFormalsList);
  2771. }
  2772. }
  2773. throwUnexpected(lex());
  2774. }
  2775. if (spreadFound && lookahead2().value !== '=>') {
  2776. throwError({}, Messages.IllegalSpread);
  2777. }
  2778. return sequence || expr;
  2779. }
  2780. // 12.1 Block
  2781. function parseStatementList() {
  2782. var list = [],
  2783. statement;
  2784. while (index < length) {
  2785. if (match('}')) {
  2786. break;
  2787. }
  2788. statement = parseSourceElement();
  2789. if (typeof statement === 'undefined') {
  2790. break;
  2791. }
  2792. list.push(statement);
  2793. }
  2794. return list;
  2795. }
  2796. function parseBlock() {
  2797. var block;
  2798. expect('{');
  2799. block = parseStatementList();
  2800. expect('}');
  2801. return delegate.createBlockStatement(block);
  2802. }
  2803. // 12.2 Variable Statement
  2804. function parseTypeAnnotation(dontExpectColon) {
  2805. var typeIdentifier = null, paramTypes = null, returnType = null,
  2806. nullable = false;
  2807. if (!dontExpectColon) {
  2808. expect(':');
  2809. }
  2810. if (match('?')) {
  2811. lex();
  2812. nullable = true;
  2813. }
  2814. if (lookahead.type === Token.Identifier) {
  2815. typeIdentifier = parseVariableIdentifier();
  2816. }
  2817. if (match('(')) {
  2818. lex();
  2819. paramTypes = [];
  2820. while (lookahead.type === Token.Identifier || match('?')) {
  2821. paramTypes.push(parseTypeAnnotation(true));
  2822. if (!match(')')) {
  2823. expect(',');
  2824. }
  2825. }
  2826. expect(')');
  2827. expect('=>');
  2828. if (matchKeyword('void')) {
  2829. lex();
  2830. } else {
  2831. returnType = parseTypeAnnotation(true);
  2832. }
  2833. }
  2834. return delegate.createTypeAnnotation(
  2835. typeIdentifier,
  2836. paramTypes,
  2837. returnType,
  2838. nullable
  2839. );
  2840. }
  2841. function parseVariableIdentifier() {
  2842. var token = lex();
  2843. if (token.type !== Token.Identifier) {
  2844. throwUnexpected(token);
  2845. }
  2846. return delegate.createIdentifier(token.value);
  2847. }
  2848. function parseTypeAnnotatableIdentifier() {
  2849. var ident = parseVariableIdentifier();
  2850. if (match(':')) {
  2851. return delegate.createTypeAnnotatedIdentifier(ident, parseTypeAnnotation());
  2852. }
  2853. return ident;
  2854. }
  2855. function parseVariableDeclaration(kind) {
  2856. var id,
  2857. init = null;
  2858. if (match('{')) {
  2859. id = parseObjectInitialiser();
  2860. reinterpretAsAssignmentBindingPattern(id);
  2861. } else if (match('[')) {
  2862. id = parseArrayInitialiser();
  2863. reinterpretAsAssignmentBindingPattern(id);
  2864. } else {
  2865. id = state.allowKeyword ? parseNonComputedProperty() : parseTypeAnnotatableIdentifier();
  2866. // 12.2.1
  2867. if (strict && isRestrictedWord(id.name)) {
  2868. throwErrorTolerant({}, Messages.StrictVarName);
  2869. }
  2870. }
  2871. if (kind === 'const') {
  2872. if (!match('=')) {
  2873. throwError({}, Messages.NoUnintializedConst);
  2874. }
  2875. expect('=');
  2876. init = parseAssignmentExpression();
  2877. } else if (match('=')) {
  2878. lex();
  2879. init = parseAssignmentExpression();
  2880. }
  2881. return delegate.createVariableDeclarator(id, init);
  2882. }
  2883. function parseVariableDeclarationList(kind) {
  2884. var list = [];
  2885. do {
  2886. list.push(parseVariableDeclaration(kind));
  2887. if (!match(',')) {
  2888. break;
  2889. }
  2890. lex();
  2891. } while (index < length);
  2892. return list;
  2893. }
  2894. function parseVariableStatement() {
  2895. var declarations;
  2896. expectKeyword('var');
  2897. declarations = parseVariableDeclarationList();
  2898. consumeSemicolon();
  2899. return delegate.createVariableDeclaration(declarations, 'var');
  2900. }
  2901. // kind may be `const` or `let`
  2902. // Both are experimental and not in the specification yet.
  2903. // see http://wiki.ecmascript.org/doku.php?id=harmony:const
  2904. // and http://wiki.ecmascript.org/doku.php?id=harmony:let
  2905. function parseConstLetDeclaration(kind) {
  2906. var declarations;
  2907. expectKeyword(kind);
  2908. declarations = parseVariableDeclarationList(kind);
  2909. consumeSemicolon();
  2910. return delegate.createVariableDeclaration(declarations, kind);
  2911. }
  2912. // http://wiki.ecmascript.org/doku.php?id=harmony:modules
  2913. function parseModuleDeclaration() {
  2914. var id, src, body;
  2915. lex(); // 'module'
  2916. if (peekLineTerminator()) {
  2917. throwError({}, Messages.NewlineAfterModule);
  2918. }
  2919. switch (lookahead.type) {
  2920. case Token.StringLiteral:
  2921. id = parsePrimaryExpression();
  2922. body = parseModuleBlock();
  2923. src = null;
  2924. break;
  2925. case Token.Identifier:
  2926. id = parseVariableIdentifier();
  2927. body = null;
  2928. if (!matchContextualKeyword('from')) {
  2929. throwUnexpected(lex());
  2930. }
  2931. lex();
  2932. src = parsePrimaryExpression();
  2933. if (src.type !== Syntax.Literal) {
  2934. throwError({}, Messages.InvalidModuleSpecifier);
  2935. }
  2936. break;
  2937. }
  2938. consumeSemicolon();
  2939. return delegate.createModuleDeclaration(id, src, body);
  2940. }
  2941. function parseExportBatchSpecifier() {
  2942. expect('*');
  2943. return delegate.createExportBatchSpecifier();
  2944. }
  2945. function parseExportSpecifier() {
  2946. var id, name = null;
  2947. id = parseVariableIdentifier();
  2948. if (matchContextualKeyword('as')) {
  2949. lex();
  2950. name = parseNonComputedProperty();
  2951. }
  2952. return delegate.createExportSpecifier(id, name);
  2953. }
  2954. function parseExportDeclaration() {
  2955. var previousAllowKeyword, decl, def, src, specifiers;
  2956. expectKeyword('export');
  2957. if (lookahead.type === Token.Keyword) {
  2958. switch (lookahead.value) {
  2959. case 'let':
  2960. case 'const':
  2961. case 'var':
  2962. case 'class':
  2963. case 'function':
  2964. return delegate.createExportDeclaration(parseSourceElement(), null, null);
  2965. }
  2966. }
  2967. if (isIdentifierName(lookahead)) {
  2968. previousAllowKeyword = state.allowKeyword;
  2969. state.allowKeyword = true;
  2970. decl = parseVariableDeclarationList('let');
  2971. state.allowKeyword = previousAllowKeyword;
  2972. return delegate.createExportDeclaration(decl, null, null);
  2973. }
  2974. specifiers = [];
  2975. src = null;
  2976. if (match('*')) {
  2977. specifiers.push(parseExportBatchSpecifier());
  2978. } else {
  2979. expect('{');
  2980. do {
  2981. specifiers.push(parseExportSpecifier());
  2982. } while (match(',') && lex());
  2983. expect('}');
  2984. }
  2985. if (matchContextualKeyword('from')) {
  2986. lex();
  2987. src = parsePrimaryExpression();
  2988. if (src.type !== Syntax.Literal) {
  2989. throwError({}, Messages.InvalidModuleSpecifier);
  2990. }
  2991. }
  2992. consumeSemicolon();
  2993. return delegate.createExportDeclaration(null, specifiers, src);
  2994. }
  2995. function parseImportDeclaration() {
  2996. var specifiers, kind, src;
  2997. expectKeyword('import');
  2998. specifiers = [];
  2999. if (isIdentifierName(lookahead)) {
  3000. kind = 'default';
  3001. specifiers.push(parseImportSpecifier());
  3002. if (!matchContextualKeyword('from')) {
  3003. throwError({}, Messages.NoFromAfterImport);
  3004. }
  3005. lex();
  3006. } else if (match('{')) {
  3007. kind = 'named';
  3008. lex();
  3009. do {
  3010. specifiers.push(parseImportSpecifier());
  3011. } while (match(',') && lex());
  3012. expect('}');
  3013. if (!matchContextualKeyword('from')) {
  3014. throwError({}, Messages.NoFromAfterImport);
  3015. }
  3016. lex();
  3017. }
  3018. src = parsePrimaryExpression();
  3019. if (src.type !== Syntax.Literal) {
  3020. throwError({}, Messages.InvalidModuleSpecifier);
  3021. }
  3022. consumeSemicolon();
  3023. return delegate.createImportDeclaration(specifiers, kind, src);
  3024. }
  3025. function parseImportSpecifier() {
  3026. var id, name = null;
  3027. id = parseNonComputedProperty();
  3028. if (matchContextualKeyword('as')) {
  3029. lex();
  3030. name = parseVariableIdentifier();
  3031. }
  3032. return delegate.createImportSpecifier(id, name);
  3033. }
  3034. // 12.3 Empty Statement
  3035. function parseEmptyStatement() {
  3036. expect(';');
  3037. return delegate.createEmptyStatement();
  3038. }
  3039. // 12.4 Expression Statement
  3040. function parseExpressionStatement() {
  3041. var expr = parseExpression();
  3042. consumeSemicolon();
  3043. return delegate.createExpressionStatement(expr);
  3044. }
  3045. // 12.5 If statement
  3046. function parseIfStatement() {
  3047. var test, consequent, alternate;
  3048. expectKeyword('if');
  3049. expect('(');
  3050. test = parseExpression();
  3051. expect(')');
  3052. consequent = parseStatement();
  3053. if (matchKeyword('else')) {
  3054. lex();
  3055. alternate = parseStatement();
  3056. } else {
  3057. alternate = null;
  3058. }
  3059. return delegate.createIfStatement(test, consequent, alternate);
  3060. }
  3061. // 12.6 Iteration Statements
  3062. function parseDoWhileStatement() {
  3063. var body, test, oldInIteration;
  3064. expectKeyword('do');
  3065. oldInIteration = state.inIteration;
  3066. state.inIteration = true;
  3067. body = parseStatement();
  3068. state.inIteration = oldInIteration;
  3069. expectKeyword('while');
  3070. expect('(');
  3071. test = parseExpression();
  3072. expect(')');
  3073. if (match(';')) {
  3074. lex();
  3075. }
  3076. return delegate.createDoWhileStatement(body, test);
  3077. }
  3078. function parseWhileStatement() {
  3079. var test, body, oldInIteration;
  3080. expectKeyword('while');
  3081. expect('(');
  3082. test = parseExpression();
  3083. expect(')');
  3084. oldInIteration = state.inIteration;
  3085. state.inIteration = true;
  3086. body = parseStatement();
  3087. state.inIteration = oldInIteration;
  3088. return delegate.createWhileStatement(test, body);
  3089. }
  3090. function parseForVariableDeclaration() {
  3091. var token = lex(),
  3092. declarations = parseVariableDeclarationList();
  3093. return delegate.createVariableDeclaration(declarations, token.value);
  3094. }
  3095. function parseForStatement(opts) {
  3096. var init, test, update, left, right, body, operator, oldInIteration;
  3097. init = test = update = null;
  3098. expectKeyword('for');
  3099. // http://wiki.ecmascript.org/doku.php?id=proposals:iterators_and_generators&s=each
  3100. if (matchContextualKeyword('each')) {
  3101. throwError({}, Messages.EachNotAllowed);
  3102. }
  3103. expect('(');
  3104. if (match(';')) {
  3105. lex();
  3106. } else {
  3107. if (matchKeyword('var') || matchKeyword('let') || matchKeyword('const')) {
  3108. state.allowIn = false;
  3109. init = parseForVariableDeclaration();
  3110. state.allowIn = true;
  3111. if (init.declarations.length === 1) {
  3112. if (matchKeyword('in') || matchContextualKeyword('of')) {
  3113. operator = lookahead;
  3114. if (!((operator.value === 'in' || init.kind !== 'var') && init.declarations[0].init)) {
  3115. lex();
  3116. left = init;
  3117. right = parseExpression();
  3118. init = null;
  3119. }
  3120. }
  3121. }
  3122. } else {
  3123. state.allowIn = false;
  3124. init = parseExpression();
  3125. state.allowIn = true;
  3126. if (matchContextualKeyword('of')) {
  3127. operator = lex();
  3128. left = init;
  3129. right = parseExpression();
  3130. init = null;
  3131. } else if (matchKeyword('in')) {
  3132. // LeftHandSideExpression
  3133. if (!isAssignableLeftHandSide(init)) {
  3134. throwError({}, Messages.InvalidLHSInForIn);
  3135. }
  3136. operator = lex();
  3137. left = init;
  3138. right = parseExpression();
  3139. init = null;
  3140. }
  3141. }
  3142. if (typeof left === 'undefined') {
  3143. expect(';');
  3144. }
  3145. }
  3146. if (typeof left === 'undefined') {
  3147. if (!match(';')) {
  3148. test = parseExpression();
  3149. }
  3150. expect(';');
  3151. if (!match(')')) {
  3152. update = parseExpression();
  3153. }
  3154. }
  3155. expect(')');
  3156. oldInIteration = state.inIteration;
  3157. state.inIteration = true;
  3158. if (!(opts !== undefined && opts.ignoreBody)) {
  3159. body = parseStatement();
  3160. }
  3161. state.inIteration = oldInIteration;
  3162. if (typeof left === 'undefined') {
  3163. return delegate.createForStatement(init, test, update, body);
  3164. }
  3165. if (operator.value === 'in') {
  3166. return delegate.createForInStatement(left, right, body);
  3167. }
  3168. return delegate.createForOfStatement(left, right, body);
  3169. }
  3170. // 12.7 The continue statement
  3171. function parseContinueStatement() {
  3172. var label = null, key;
  3173. expectKeyword('continue');
  3174. // Optimize the most common form: 'continue;'.
  3175. if (source.charCodeAt(index) === 59) {
  3176. lex();
  3177. if (!state.inIteration) {
  3178. throwError({}, Messages.IllegalContinue);
  3179. }
  3180. return delegate.createContinueStatement(null);
  3181. }
  3182. if (peekLineTerminator()) {
  3183. if (!state.inIteration) {
  3184. throwError({}, Messages.IllegalContinue);
  3185. }
  3186. return delegate.createContinueStatement(null);
  3187. }
  3188. if (lookahead.type === Token.Identifier) {
  3189. label = parseVariableIdentifier();
  3190. key = '$' + label.name;
  3191. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  3192. throwError({}, Messages.UnknownLabel, label.name);
  3193. }
  3194. }
  3195. consumeSemicolon();
  3196. if (label === null && !state.inIteration) {
  3197. throwError({}, Messages.IllegalContinue);
  3198. }
  3199. return delegate.createContinueStatement(label);
  3200. }
  3201. // 12.8 The break statement
  3202. function parseBreakStatement() {
  3203. var label = null, key;
  3204. expectKeyword('break');
  3205. // Catch the very common case first: immediately a semicolon (char #59).
  3206. if (source.charCodeAt(index) === 59) {
  3207. lex();
  3208. if (!(state.inIteration || state.inSwitch)) {
  3209. throwError({}, Messages.IllegalBreak);
  3210. }
  3211. return delegate.createBreakStatement(null);
  3212. }
  3213. if (peekLineTerminator()) {
  3214. if (!(state.inIteration || state.inSwitch)) {
  3215. throwError({}, Messages.IllegalBreak);
  3216. }
  3217. return delegate.createBreakStatement(null);
  3218. }
  3219. if (lookahead.type === Token.Identifier) {
  3220. label = parseVariableIdentifier();
  3221. key = '$' + label.name;
  3222. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  3223. throwError({}, Messages.UnknownLabel, label.name);
  3224. }
  3225. }
  3226. consumeSemicolon();
  3227. if (label === null && !(state.inIteration || state.inSwitch)) {
  3228. throwError({}, Messages.IllegalBreak);
  3229. }
  3230. return delegate.createBreakStatement(label);
  3231. }
  3232. // 12.9 The return statement
  3233. function parseReturnStatement() {
  3234. var argument = null;
  3235. expectKeyword('return');
  3236. if (!state.inFunctionBody) {
  3237. throwErrorTolerant({}, Messages.IllegalReturn);
  3238. }
  3239. // 'return' followed by a space and an identifier is very common.
  3240. if (source.charCodeAt(index) === 32) {
  3241. if (isIdentifierStart(source.charCodeAt(index + 1))) {
  3242. argument = parseExpression();
  3243. consumeSemicolon();
  3244. return delegate.createReturnStatement(argument);
  3245. }
  3246. }
  3247. if (peekLineTerminator()) {
  3248. return delegate.createReturnStatement(null);
  3249. }
  3250. if (!match(';')) {
  3251. if (!match('}') && lookahead.type !== Token.EOF) {
  3252. argument = parseExpression();
  3253. }
  3254. }
  3255. consumeSemicolon();
  3256. return delegate.createReturnStatement(argument);
  3257. }
  3258. // 12.10 The with statement
  3259. function parseWithStatement() {
  3260. var object, body;
  3261. if (strict) {
  3262. throwErrorTolerant({}, Messages.StrictModeWith);
  3263. }
  3264. expectKeyword('with');
  3265. expect('(');
  3266. object = parseExpression();
  3267. expect(')');
  3268. body = parseStatement();
  3269. return delegate.createWithStatement(object, body);
  3270. }
  3271. // 12.10 The swith statement
  3272. function parseSwitchCase() {
  3273. var test,
  3274. consequent = [],
  3275. sourceElement;
  3276. if (matchKeyword('default')) {
  3277. lex();
  3278. test = null;
  3279. } else {
  3280. expectKeyword('case');
  3281. test = parseExpression();
  3282. }
  3283. expect(':');
  3284. while (index < length) {
  3285. if (match('}') || matchKeyword('default') || matchKeyword('case')) {
  3286. break;
  3287. }
  3288. sourceElement = parseSourceElement();
  3289. if (typeof sourceElement === 'undefined') {
  3290. break;
  3291. }
  3292. consequent.push(sourceElement);
  3293. }
  3294. return delegate.createSwitchCase(test, consequent);
  3295. }
  3296. function parseSwitchStatement() {
  3297. var discriminant, cases, clause, oldInSwitch, defaultFound;
  3298. expectKeyword('switch');
  3299. expect('(');
  3300. discriminant = parseExpression();
  3301. expect(')');
  3302. expect('{');
  3303. cases = [];
  3304. if (match('}')) {
  3305. lex();
  3306. return delegate.createSwitchStatement(discriminant, cases);
  3307. }
  3308. oldInSwitch = state.inSwitch;
  3309. state.inSwitch = true;
  3310. defaultFound = false;
  3311. while (index < length) {
  3312. if (match('}')) {
  3313. break;
  3314. }
  3315. clause = parseSwitchCase();
  3316. if (clause.test === null) {
  3317. if (defaultFound) {
  3318. throwError({}, Messages.MultipleDefaultsInSwitch);
  3319. }
  3320. defaultFound = true;
  3321. }
  3322. cases.push(clause);
  3323. }
  3324. state.inSwitch = oldInSwitch;
  3325. expect('}');
  3326. return delegate.createSwitchStatement(discriminant, cases);
  3327. }
  3328. // 12.13 The throw statement
  3329. function parseThrowStatement() {
  3330. var argument;
  3331. expectKeyword('throw');
  3332. if (peekLineTerminator()) {
  3333. throwError({}, Messages.NewlineAfterThrow);
  3334. }
  3335. argument = parseExpression();
  3336. consumeSemicolon();
  3337. return delegate.createThrowStatement(argument);
  3338. }
  3339. // 12.14 The try statement
  3340. function parseCatchClause() {
  3341. var param, body;
  3342. expectKeyword('catch');
  3343. expect('(');
  3344. if (match(')')) {
  3345. throwUnexpected(lookahead);
  3346. }
  3347. param = parseExpression();
  3348. // 12.14.1
  3349. if (strict && param.type === Syntax.Identifier && isRestrictedWord(param.name)) {
  3350. throwErrorTolerant({}, Messages.StrictCatchVariable);
  3351. }
  3352. expect(')');
  3353. body = parseBlock();
  3354. return delegate.createCatchClause(param, body);
  3355. }
  3356. function parseTryStatement() {
  3357. var block, handlers = [], finalizer = null;
  3358. expectKeyword('try');
  3359. block = parseBlock();
  3360. if (matchKeyword('catch')) {
  3361. handlers.push(parseCatchClause());
  3362. }
  3363. if (matchKeyword('finally')) {
  3364. lex();
  3365. finalizer = parseBlock();
  3366. }
  3367. if (handlers.length === 0 && !finalizer) {
  3368. throwError({}, Messages.NoCatchOrFinally);
  3369. }
  3370. return delegate.createTryStatement(block, [], handlers, finalizer);
  3371. }
  3372. // 12.15 The debugger statement
  3373. function parseDebuggerStatement() {
  3374. expectKeyword('debugger');
  3375. consumeSemicolon();
  3376. return delegate.createDebuggerStatement();
  3377. }
  3378. // 12 Statements
  3379. function parseStatement() {
  3380. var type = lookahead.type,
  3381. expr,
  3382. labeledBody,
  3383. key;
  3384. if (type === Token.EOF) {
  3385. throwUnexpected(lookahead);
  3386. }
  3387. if (type === Token.Punctuator) {
  3388. switch (lookahead.value) {
  3389. case ';':
  3390. return parseEmptyStatement();
  3391. case '{':
  3392. return parseBlock();
  3393. case '(':
  3394. return parseExpressionStatement();
  3395. default:
  3396. break;
  3397. }
  3398. }
  3399. if (type === Token.Keyword) {
  3400. switch (lookahead.value) {
  3401. case 'break':
  3402. return parseBreakStatement();
  3403. case 'continue':
  3404. return parseContinueStatement();
  3405. case 'debugger':
  3406. return parseDebuggerStatement();
  3407. case 'do':
  3408. return parseDoWhileStatement();
  3409. case 'for':
  3410. return parseForStatement();
  3411. case 'function':
  3412. return parseFunctionDeclaration();
  3413. case 'class':
  3414. return parseClassDeclaration();
  3415. case 'if':
  3416. return parseIfStatement();
  3417. case 'return':
  3418. return parseReturnStatement();
  3419. case 'switch':
  3420. return parseSwitchStatement();
  3421. case 'throw':
  3422. return parseThrowStatement();
  3423. case 'try':
  3424. return parseTryStatement();
  3425. case 'var':
  3426. return parseVariableStatement();
  3427. case 'while':
  3428. return parseWhileStatement();
  3429. case 'with':
  3430. return parseWithStatement();
  3431. default:
  3432. break;
  3433. }
  3434. }
  3435. expr = parseExpression();
  3436. // 12.12 Labelled Statements
  3437. if ((expr.type === Syntax.Identifier) && match(':')) {
  3438. lex();
  3439. key = '$' + expr.name;
  3440. if (Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  3441. throwError({}, Messages.Redeclaration, 'Label', expr.name);
  3442. }
  3443. state.labelSet[key] = true;
  3444. labeledBody = parseStatement();
  3445. delete state.labelSet[key];
  3446. return delegate.createLabeledStatement(expr, labeledBody);
  3447. }
  3448. consumeSemicolon();
  3449. return delegate.createExpressionStatement(expr);
  3450. }
  3451. // 13 Function Definition
  3452. function parseConciseBody() {
  3453. if (match('{')) {
  3454. return parseFunctionSourceElements();
  3455. }
  3456. return parseAssignmentExpression();
  3457. }
  3458. function parseFunctionSourceElements() {
  3459. var sourceElement, sourceElements = [], token, directive, firstRestricted,
  3460. oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody, oldParenthesizedCount;
  3461. expect('{');
  3462. while (index < length) {
  3463. if (lookahead.type !== Token.StringLiteral) {
  3464. break;
  3465. }
  3466. token = lookahead;
  3467. sourceElement = parseSourceElement();
  3468. sourceElements.push(sourceElement);
  3469. if (sourceElement.expression.type !== Syntax.Literal) {
  3470. // this is not directive
  3471. break;
  3472. }
  3473. directive = source.slice(token.range[0] + 1, token.range[1] - 1);
  3474. if (directive === 'use strict') {
  3475. strict = true;
  3476. if (firstRestricted) {
  3477. throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  3478. }
  3479. } else {
  3480. if (!firstRestricted && token.octal) {
  3481. firstRestricted = token;
  3482. }
  3483. }
  3484. }
  3485. oldLabelSet = state.labelSet;
  3486. oldInIteration = state.inIteration;
  3487. oldInSwitch = state.inSwitch;
  3488. oldInFunctionBody = state.inFunctionBody;
  3489. oldParenthesizedCount = state.parenthesizedCount;
  3490. state.labelSet = {};
  3491. state.inIteration = false;
  3492. state.inSwitch = false;
  3493. state.inFunctionBody = true;
  3494. state.parenthesizedCount = 0;
  3495. while (index < length) {
  3496. if (match('}')) {
  3497. break;
  3498. }
  3499. sourceElement = parseSourceElement();
  3500. if (typeof sourceElement === 'undefined') {
  3501. break;
  3502. }
  3503. sourceElements.push(sourceElement);
  3504. }
  3505. expect('}');
  3506. state.labelSet = oldLabelSet;
  3507. state.inIteration = oldInIteration;
  3508. state.inSwitch = oldInSwitch;
  3509. state.inFunctionBody = oldInFunctionBody;
  3510. state.parenthesizedCount = oldParenthesizedCount;
  3511. return delegate.createBlockStatement(sourceElements);
  3512. }
  3513. function validateParam(options, param, name) {
  3514. var key = '$' + name;
  3515. if (strict) {
  3516. if (isRestrictedWord(name)) {
  3517. options.stricted = param;
  3518. options.message = Messages.StrictParamName;
  3519. }
  3520. if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) {
  3521. options.stricted = param;
  3522. options.message = Messages.StrictParamDupe;
  3523. }
  3524. } else if (!options.firstRestricted) {
  3525. if (isRestrictedWord(name)) {
  3526. options.firstRestricted = param;
  3527. options.message = Messages.StrictParamName;
  3528. } else if (isStrictModeReservedWord(name)) {
  3529. options.firstRestricted = param;
  3530. options.message = Messages.StrictReservedWord;
  3531. } else if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) {
  3532. options.firstRestricted = param;
  3533. options.message = Messages.StrictParamDupe;
  3534. }
  3535. }
  3536. options.paramSet[key] = true;
  3537. }
  3538. function parseParam(options) {
  3539. var token, rest, param, def;
  3540. token = lookahead;
  3541. if (token.value === '...') {
  3542. token = lex();
  3543. rest = true;
  3544. }
  3545. if (match('[')) {
  3546. param = parseArrayInitialiser();
  3547. reinterpretAsDestructuredParameter(options, param);
  3548. } else if (match('{')) {
  3549. if (rest) {
  3550. throwError({}, Messages.ObjectPatternAsRestParameter);
  3551. }
  3552. param = parseObjectInitialiser();
  3553. reinterpretAsDestructuredParameter(options, param);
  3554. } else {
  3555. // Typing rest params is awkward, so punting on that for now
  3556. param = rest
  3557. ? parseVariableIdentifier()
  3558. : parseTypeAnnotatableIdentifier();
  3559. validateParam(options, token, token.value);
  3560. if (match('=')) {
  3561. if (rest) {
  3562. throwErrorTolerant(lookahead, Messages.DefaultRestParameter);
  3563. }
  3564. lex();
  3565. def = parseAssignmentExpression();
  3566. ++options.defaultCount;
  3567. }
  3568. }
  3569. if (rest) {
  3570. if (!match(')')) {
  3571. throwError({}, Messages.ParameterAfterRestParameter);
  3572. }
  3573. options.rest = param;
  3574. return false;
  3575. }
  3576. options.params.push(param);
  3577. options.defaults.push(def);
  3578. return !match(')');
  3579. }
  3580. function parseParams(firstRestricted) {
  3581. var options;
  3582. options = {
  3583. params: [],
  3584. defaultCount: 0,
  3585. defaults: [],
  3586. rest: null,
  3587. firstRestricted: firstRestricted
  3588. };
  3589. expect('(');
  3590. if (!match(')')) {
  3591. options.paramSet = {};
  3592. while (index < length) {
  3593. if (!parseParam(options)) {
  3594. break;
  3595. }
  3596. expect(',');
  3597. }
  3598. }
  3599. expect(')');
  3600. if (options.defaultCount === 0) {
  3601. options.defaults = [];
  3602. }
  3603. if (match(':')) {
  3604. options.returnTypeAnnotation = parseTypeAnnotation();
  3605. }
  3606. return options;
  3607. }
  3608. function parseFunctionDeclaration() {
  3609. var id, body, token, tmp, firstRestricted, message, previousStrict, previousYieldAllowed, generator;
  3610. expectKeyword('function');
  3611. generator = false;
  3612. if (match('*')) {
  3613. lex();
  3614. generator = true;
  3615. }
  3616. token = lookahead;
  3617. id = parseVariableIdentifier();
  3618. if (strict) {
  3619. if (isRestrictedWord(token.value)) {
  3620. throwErrorTolerant(token, Messages.StrictFunctionName);
  3621. }
  3622. } else {
  3623. if (isRestrictedWord(token.value)) {
  3624. firstRestricted = token;
  3625. message = Messages.StrictFunctionName;
  3626. } else if (isStrictModeReservedWord(token.value)) {
  3627. firstRestricted = token;
  3628. message = Messages.StrictReservedWord;
  3629. }
  3630. }
  3631. tmp = parseParams(firstRestricted);
  3632. firstRestricted = tmp.firstRestricted;
  3633. if (tmp.message) {
  3634. message = tmp.message;
  3635. }
  3636. previousStrict = strict;
  3637. previousYieldAllowed = state.yieldAllowed;
  3638. state.yieldAllowed = generator;
  3639. body = parseFunctionSourceElements();
  3640. if (strict && firstRestricted) {
  3641. throwError(firstRestricted, message);
  3642. }
  3643. if (strict && tmp.stricted) {
  3644. throwErrorTolerant(tmp.stricted, message);
  3645. }
  3646. if (state.yieldAllowed && !state.yieldFound) {
  3647. throwErrorTolerant({}, Messages.NoYieldInGenerator);
  3648. }
  3649. strict = previousStrict;
  3650. state.yieldAllowed = previousYieldAllowed;
  3651. return delegate.createFunctionDeclaration(id, tmp.params, tmp.defaults, body, tmp.rest, generator, false,
  3652. tmp.returnTypeAnnotation);
  3653. }
  3654. function parseFunctionExpression() {
  3655. var token, id = null, firstRestricted, message, tmp, body, previousStrict, previousYieldAllowed, generator;
  3656. expectKeyword('function');
  3657. generator = false;
  3658. if (match('*')) {
  3659. lex();
  3660. generator = true;
  3661. }
  3662. if (!match('(')) {
  3663. token = lookahead;
  3664. id = parseVariableIdentifier();
  3665. if (strict) {
  3666. if (isRestrictedWord(token.value)) {
  3667. throwErrorTolerant(token, Messages.StrictFunctionName);
  3668. }
  3669. } else {
  3670. if (isRestrictedWord(token.value)) {
  3671. firstRestricted = token;
  3672. message = Messages.StrictFunctionName;
  3673. } else if (isStrictModeReservedWord(token.value)) {
  3674. firstRestricted = token;
  3675. message = Messages.StrictReservedWord;
  3676. }
  3677. }
  3678. }
  3679. tmp = parseParams(firstRestricted);
  3680. firstRestricted = tmp.firstRestricted;
  3681. if (tmp.message) {
  3682. message = tmp.message;
  3683. }
  3684. previousStrict = strict;
  3685. previousYieldAllowed = state.yieldAllowed;
  3686. state.yieldAllowed = generator;
  3687. body = parseFunctionSourceElements();
  3688. if (strict && firstRestricted) {
  3689. throwError(firstRestricted, message);
  3690. }
  3691. if (strict && tmp.stricted) {
  3692. throwErrorTolerant(tmp.stricted, message);
  3693. }
  3694. if (state.yieldAllowed && !state.yieldFound) {
  3695. throwErrorTolerant({}, Messages.NoYieldInGenerator);
  3696. }
  3697. strict = previousStrict;
  3698. state.yieldAllowed = previousYieldAllowed;
  3699. return delegate.createFunctionExpression(id, tmp.params, tmp.defaults, body, tmp.rest, generator, false,
  3700. tmp.returnTypeAnnotation);
  3701. }
  3702. function parseYieldExpression() {
  3703. var delegateFlag, expr;
  3704. expectKeyword('yield');
  3705. if (!state.yieldAllowed) {
  3706. throwErrorTolerant({}, Messages.IllegalYield);
  3707. }
  3708. delegateFlag = false;
  3709. if (match('*')) {
  3710. lex();
  3711. delegateFlag = true;
  3712. }
  3713. expr = parseAssignmentExpression();
  3714. state.yieldFound = true;
  3715. return delegate.createYieldExpression(expr, delegateFlag);
  3716. }
  3717. // 14 Classes
  3718. function parseMethodDefinition(existingPropNames) {
  3719. var token, key, param, propType, isValidDuplicateProp = false;
  3720. if (lookahead.value === 'static') {
  3721. propType = ClassPropertyType.static;
  3722. lex();
  3723. } else {
  3724. propType = ClassPropertyType.prototype;
  3725. }
  3726. if (match('*')) {
  3727. lex();
  3728. return delegate.createMethodDefinition(
  3729. propType,
  3730. '',
  3731. parseObjectPropertyKey(),
  3732. parsePropertyMethodFunction({ generator: true })
  3733. );
  3734. }
  3735. token = lookahead;
  3736. key = parseObjectPropertyKey();
  3737. if (token.value === 'get' && !match('(')) {
  3738. key = parseObjectPropertyKey();
  3739. // It is a syntax error if any other properties have a name
  3740. // duplicating this one unless they are a setter
  3741. if (existingPropNames[propType].hasOwnProperty(key.name)) {
  3742. isValidDuplicateProp =
  3743. // There isn't already a getter for this prop
  3744. existingPropNames[propType][key.name].get === undefined
  3745. // There isn't already a data prop by this name
  3746. && existingPropNames[propType][key.name].data === undefined
  3747. // The only existing prop by this name is a setter
  3748. && existingPropNames[propType][key.name].set !== undefined;
  3749. if (!isValidDuplicateProp) {
  3750. throwError(key, Messages.IllegalDuplicateClassProperty);
  3751. }
  3752. } else {
  3753. existingPropNames[propType][key.name] = {};
  3754. }
  3755. existingPropNames[propType][key.name].get = true;
  3756. expect('(');
  3757. expect(')');
  3758. return delegate.createMethodDefinition(
  3759. propType,
  3760. 'get',
  3761. key,
  3762. parsePropertyFunction({ generator: false })
  3763. );
  3764. }
  3765. if (token.value === 'set' && !match('(')) {
  3766. key = parseObjectPropertyKey();
  3767. // It is a syntax error if any other properties have a name
  3768. // duplicating this one unless they are a getter
  3769. if (existingPropNames[propType].hasOwnProperty(key.name)) {
  3770. isValidDuplicateProp =
  3771. // There isn't already a setter for this prop
  3772. existingPropNames[propType][key.name].set === undefined
  3773. // There isn't already a data prop by this name
  3774. && existingPropNames[propType][key.name].data === undefined
  3775. // The only existing prop by this name is a getter
  3776. && existingPropNames[propType][key.name].get !== undefined;
  3777. if (!isValidDuplicateProp) {
  3778. throwError(key, Messages.IllegalDuplicateClassProperty);
  3779. }
  3780. } else {
  3781. existingPropNames[propType][key.name] = {};
  3782. }
  3783. existingPropNames[propType][key.name].set = true;
  3784. expect('(');
  3785. token = lookahead;
  3786. param = [ parseTypeAnnotatableIdentifier() ];
  3787. expect(')');
  3788. return delegate.createMethodDefinition(
  3789. propType,
  3790. 'set',
  3791. key,
  3792. parsePropertyFunction({ params: param, generator: false, name: token })
  3793. );
  3794. }
  3795. // It is a syntax error if any other properties have the same name as a
  3796. // non-getter, non-setter method
  3797. if (existingPropNames[propType].hasOwnProperty(key.name)) {
  3798. throwError(key, Messages.IllegalDuplicateClassProperty);
  3799. } else {
  3800. existingPropNames[propType][key.name] = {};
  3801. }
  3802. existingPropNames[propType][key.name].data = true;
  3803. return delegate.createMethodDefinition(
  3804. propType,
  3805. '',
  3806. key,
  3807. parsePropertyMethodFunction({ generator: false })
  3808. );
  3809. }
  3810. function parseClassElement(existingProps) {
  3811. if (match(';')) {
  3812. lex();
  3813. return;
  3814. }
  3815. return parseMethodDefinition(existingProps);
  3816. }
  3817. function parseClassBody() {
  3818. var classElement, classElements = [], existingProps = {};
  3819. existingProps[ClassPropertyType.static] = {};
  3820. existingProps[ClassPropertyType.prototype] = {};
  3821. expect('{');
  3822. while (index < length) {
  3823. if (match('}')) {
  3824. break;
  3825. }
  3826. classElement = parseClassElement(existingProps);
  3827. if (typeof classElement !== 'undefined') {
  3828. classElements.push(classElement);
  3829. }
  3830. }
  3831. expect('}');
  3832. return delegate.createClassBody(classElements);
  3833. }
  3834. function parseClassExpression() {
  3835. var id, previousYieldAllowed, superClass = null;
  3836. expectKeyword('class');
  3837. if (!matchKeyword('extends') && !match('{')) {
  3838. id = parseVariableIdentifier();
  3839. }
  3840. if (matchKeyword('extends')) {
  3841. expectKeyword('extends');
  3842. previousYieldAllowed = state.yieldAllowed;
  3843. state.yieldAllowed = false;
  3844. superClass = parseAssignmentExpression();
  3845. state.yieldAllowed = previousYieldAllowed;
  3846. }
  3847. return delegate.createClassExpression(id, superClass, parseClassBody());
  3848. }
  3849. function parseClassDeclaration() {
  3850. var id, previousYieldAllowed, superClass = null;
  3851. expectKeyword('class');
  3852. id = parseVariableIdentifier();
  3853. if (matchKeyword('extends')) {
  3854. expectKeyword('extends');
  3855. previousYieldAllowed = state.yieldAllowed;
  3856. state.yieldAllowed = false;
  3857. superClass = parseAssignmentExpression();
  3858. state.yieldAllowed = previousYieldAllowed;
  3859. }
  3860. return delegate.createClassDeclaration(id, superClass, parseClassBody());
  3861. }
  3862. // 15 Program
  3863. function matchModuleDeclaration() {
  3864. var id;
  3865. if (matchContextualKeyword('module')) {
  3866. id = lookahead2();
  3867. return id.type === Token.StringLiteral || id.type === Token.Identifier;
  3868. }
  3869. return false;
  3870. }
  3871. function parseSourceElement() {
  3872. if (lookahead.type === Token.Keyword) {
  3873. switch (lookahead.value) {
  3874. case 'const':
  3875. case 'let':
  3876. return parseConstLetDeclaration(lookahead.value);
  3877. case 'function':
  3878. return parseFunctionDeclaration();
  3879. case 'export':
  3880. return parseExportDeclaration();
  3881. case 'import':
  3882. return parseImportDeclaration();
  3883. default:
  3884. return parseStatement();
  3885. }
  3886. }
  3887. if (matchModuleDeclaration()) {
  3888. throwError({}, Messages.NestedModule);
  3889. }
  3890. if (lookahead.type !== Token.EOF) {
  3891. return parseStatement();
  3892. }
  3893. }
  3894. function parseProgramElement() {
  3895. if (lookahead.type === Token.Keyword) {
  3896. switch (lookahead.value) {
  3897. case 'export':
  3898. return parseExportDeclaration();
  3899. case 'import':
  3900. return parseImportDeclaration();
  3901. }
  3902. }
  3903. if (matchModuleDeclaration()) {
  3904. return parseModuleDeclaration();
  3905. }
  3906. return parseSourceElement();
  3907. }
  3908. function parseProgramElements() {
  3909. var sourceElement, sourceElements = [], token, directive, firstRestricted;
  3910. while (index < length) {
  3911. token = lookahead;
  3912. if (token.type !== Token.StringLiteral) {
  3913. break;
  3914. }
  3915. sourceElement = parseProgramElement();
  3916. sourceElements.push(sourceElement);
  3917. if (sourceElement.expression.type !== Syntax.Literal) {
  3918. // this is not directive
  3919. break;
  3920. }
  3921. directive = source.slice(token.range[0] + 1, token.range[1] - 1);
  3922. if (directive === 'use strict') {
  3923. strict = true;
  3924. if (firstRestricted) {
  3925. throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  3926. }
  3927. } else {
  3928. if (!firstRestricted && token.octal) {
  3929. firstRestricted = token;
  3930. }
  3931. }
  3932. }
  3933. while (index < length) {
  3934. sourceElement = parseProgramElement();
  3935. if (typeof sourceElement === 'undefined') {
  3936. break;
  3937. }
  3938. sourceElements.push(sourceElement);
  3939. }
  3940. return sourceElements;
  3941. }
  3942. function parseModuleElement() {
  3943. return parseSourceElement();
  3944. }
  3945. function parseModuleElements() {
  3946. var list = [],
  3947. statement;
  3948. while (index < length) {
  3949. if (match('}')) {
  3950. break;
  3951. }
  3952. statement = parseModuleElement();
  3953. if (typeof statement === 'undefined') {
  3954. break;
  3955. }
  3956. list.push(statement);
  3957. }
  3958. return list;
  3959. }
  3960. function parseModuleBlock() {
  3961. var block;
  3962. expect('{');
  3963. block = parseModuleElements();
  3964. expect('}');
  3965. return delegate.createBlockStatement(block);
  3966. }
  3967. function parseProgram() {
  3968. var body;
  3969. strict = false;
  3970. peek();
  3971. body = parseProgramElements();
  3972. return delegate.createProgram(body);
  3973. }
  3974. // The following functions are needed only when the option to preserve
  3975. // the comments is active.
  3976. function addComment(type, value, start, end, loc) {
  3977. assert(typeof start === 'number', 'Comment must have valid position');
  3978. // Because the way the actual token is scanned, often the comments
  3979. // (if any) are skipped twice during the lexical analysis.
  3980. // Thus, we need to skip adding a comment if the comment array already
  3981. // handled it.
  3982. if (extra.comments.length > 0) {
  3983. if (extra.comments[extra.comments.length - 1].range[1] > start) {
  3984. return;
  3985. }
  3986. }
  3987. extra.comments.push({
  3988. type: type,
  3989. value: value,
  3990. range: [start, end],
  3991. loc: loc
  3992. });
  3993. }
  3994. function scanComment() {
  3995. var comment, ch, loc, start, blockComment, lineComment;
  3996. comment = '';
  3997. blockComment = false;
  3998. lineComment = false;
  3999. while (index < length) {
  4000. ch = source[index];
  4001. if (lineComment) {
  4002. ch = source[index++];
  4003. if (isLineTerminator(ch.charCodeAt(0))) {
  4004. loc.end = {
  4005. line: lineNumber,
  4006. column: index - lineStart - 1
  4007. };
  4008. lineComment = false;
  4009. addComment('Line', comment, start, index - 1, loc);
  4010. if (ch === '\r' && source[index] === '\n') {
  4011. ++index;
  4012. }
  4013. ++lineNumber;
  4014. lineStart = index;
  4015. comment = '';
  4016. } else if (index >= length) {
  4017. lineComment = false;
  4018. comment += ch;
  4019. loc.end = {
  4020. line: lineNumber,
  4021. column: length - lineStart
  4022. };
  4023. addComment('Line', comment, start, length, loc);
  4024. } else {
  4025. comment += ch;
  4026. }
  4027. } else if (blockComment) {
  4028. if (isLineTerminator(ch.charCodeAt(0))) {
  4029. if (ch === '\r' && source[index + 1] === '\n') {
  4030. ++index;
  4031. comment += '\r\n';
  4032. } else {
  4033. comment += ch;
  4034. }
  4035. ++lineNumber;
  4036. ++index;
  4037. lineStart = index;
  4038. if (index >= length) {
  4039. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  4040. }
  4041. } else {
  4042. ch = source[index++];
  4043. if (index >= length) {
  4044. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  4045. }
  4046. comment += ch;
  4047. if (ch === '*') {
  4048. ch = source[index];
  4049. if (ch === '/') {
  4050. comment = comment.substr(0, comment.length - 1);
  4051. blockComment = false;
  4052. ++index;
  4053. loc.end = {
  4054. line: lineNumber,
  4055. column: index - lineStart
  4056. };
  4057. addComment('Block', comment, start, index, loc);
  4058. comment = '';
  4059. }
  4060. }
  4061. }
  4062. } else if (ch === '/') {
  4063. ch = source[index + 1];
  4064. if (ch === '/') {
  4065. loc = {
  4066. start: {
  4067. line: lineNumber,
  4068. column: index - lineStart
  4069. }
  4070. };
  4071. start = index;
  4072. index += 2;
  4073. lineComment = true;
  4074. if (index >= length) {
  4075. loc.end = {
  4076. line: lineNumber,
  4077. column: index - lineStart
  4078. };
  4079. lineComment = false;
  4080. addComment('Line', comment, start, index, loc);
  4081. }
  4082. } else if (ch === '*') {
  4083. start = index;
  4084. index += 2;
  4085. blockComment = true;
  4086. loc = {
  4087. start: {
  4088. line: lineNumber,
  4089. column: index - lineStart - 2
  4090. }
  4091. };
  4092. if (index >= length) {
  4093. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  4094. }
  4095. } else {
  4096. break;
  4097. }
  4098. } else if (isWhiteSpace(ch.charCodeAt(0))) {
  4099. ++index;
  4100. } else if (isLineTerminator(ch.charCodeAt(0))) {
  4101. ++index;
  4102. if (ch === '\r' && source[index] === '\n') {
  4103. ++index;
  4104. }
  4105. ++lineNumber;
  4106. lineStart = index;
  4107. } else {
  4108. break;
  4109. }
  4110. }
  4111. }
  4112. function filterCommentLocation() {
  4113. var i, entry, comment, comments = [];
  4114. for (i = 0; i < extra.comments.length; ++i) {
  4115. entry = extra.comments[i];
  4116. comment = {
  4117. type: entry.type,
  4118. value: entry.value
  4119. };
  4120. if (extra.range) {
  4121. comment.range = entry.range;
  4122. }
  4123. if (extra.loc) {
  4124. comment.loc = entry.loc;
  4125. }
  4126. comments.push(comment);
  4127. }
  4128. extra.comments = comments;
  4129. }
  4130. // 16 XJS
  4131. XHTMLEntities = {
  4132. quot: '\u0022',
  4133. amp: '&',
  4134. apos: "\u0027",
  4135. lt: "<",
  4136. gt: ">",
  4137. nbsp: "\u00A0",
  4138. iexcl: "\u00A1",
  4139. cent: "\u00A2",
  4140. pound: "\u00A3",
  4141. curren: "\u00A4",
  4142. yen: "\u00A5",
  4143. brvbar: "\u00A6",
  4144. sect: "\u00A7",
  4145. uml: "\u00A8",
  4146. copy: "\u00A9",
  4147. ordf: "\u00AA",
  4148. laquo: "\u00AB",
  4149. not: "\u00AC",
  4150. shy: "\u00AD",
  4151. reg: "\u00AE",
  4152. macr: "\u00AF",
  4153. deg: "\u00B0",
  4154. plusmn: "\u00B1",
  4155. sup2: "\u00B2",
  4156. sup3: "\u00B3",
  4157. acute: "\u00B4",
  4158. micro: "\u00B5",
  4159. para: "\u00B6",
  4160. middot: "\u00B7",
  4161. cedil: "\u00B8",
  4162. sup1: "\u00B9",
  4163. ordm: "\u00BA",
  4164. raquo: "\u00BB",
  4165. frac14: "\u00BC",
  4166. frac12: "\u00BD",
  4167. frac34: "\u00BE",
  4168. iquest: "\u00BF",
  4169. Agrave: "\u00C0",
  4170. Aacute: "\u00C1",
  4171. Acirc: "\u00C2",
  4172. Atilde: "\u00C3",
  4173. Auml: "\u00C4",
  4174. Aring: "\u00C5",
  4175. AElig: "\u00C6",
  4176. Ccedil: "\u00C7",
  4177. Egrave: "\u00C8",
  4178. Eacute: "\u00C9",
  4179. Ecirc: "\u00CA",
  4180. Euml: "\u00CB",
  4181. Igrave: "\u00CC",
  4182. Iacute: "\u00CD",
  4183. Icirc: "\u00CE",
  4184. Iuml: "\u00CF",
  4185. ETH: "\u00D0",
  4186. Ntilde: "\u00D1",
  4187. Ograve: "\u00D2",
  4188. Oacute: "\u00D3",
  4189. Ocirc: "\u00D4",
  4190. Otilde: "\u00D5",
  4191. Ouml: "\u00D6",
  4192. times: "\u00D7",
  4193. Oslash: "\u00D8",
  4194. Ugrave: "\u00D9",
  4195. Uacute: "\u00DA",
  4196. Ucirc: "\u00DB",
  4197. Uuml: "\u00DC",
  4198. Yacute: "\u00DD",
  4199. THORN: "\u00DE",
  4200. szlig: "\u00DF",
  4201. agrave: "\u00E0",
  4202. aacute: "\u00E1",
  4203. acirc: "\u00E2",
  4204. atilde: "\u00E3",
  4205. auml: "\u00E4",
  4206. aring: "\u00E5",
  4207. aelig: "\u00E6",
  4208. ccedil: "\u00E7",
  4209. egrave: "\u00E8",
  4210. eacute: "\u00E9",
  4211. ecirc: "\u00EA",
  4212. euml: "\u00EB",
  4213. igrave: "\u00EC",
  4214. iacute: "\u00ED",
  4215. icirc: "\u00EE",
  4216. iuml: "\u00EF",
  4217. eth: "\u00F0",
  4218. ntilde: "\u00F1",
  4219. ograve: "\u00F2",
  4220. oacute: "\u00F3",
  4221. ocirc: "\u00F4",
  4222. otilde: "\u00F5",
  4223. ouml: "\u00F6",
  4224. divide: "\u00F7",
  4225. oslash: "\u00F8",
  4226. ugrave: "\u00F9",
  4227. uacute: "\u00FA",
  4228. ucirc: "\u00FB",
  4229. uuml: "\u00FC",
  4230. yacute: "\u00FD",
  4231. thorn: "\u00FE",
  4232. yuml: "\u00FF",
  4233. OElig: "\u0152",
  4234. oelig: "\u0153",
  4235. Scaron: "\u0160",
  4236. scaron: "\u0161",
  4237. Yuml: "\u0178",
  4238. fnof: "\u0192",
  4239. circ: "\u02C6",
  4240. tilde: "\u02DC",
  4241. Alpha: "\u0391",
  4242. Beta: "\u0392",
  4243. Gamma: "\u0393",
  4244. Delta: "\u0394",
  4245. Epsilon: "\u0395",
  4246. Zeta: "\u0396",
  4247. Eta: "\u0397",
  4248. Theta: "\u0398",
  4249. Iota: "\u0399",
  4250. Kappa: "\u039A",
  4251. Lambda: "\u039B",
  4252. Mu: "\u039C",
  4253. Nu: "\u039D",
  4254. Xi: "\u039E",
  4255. Omicron: "\u039F",
  4256. Pi: "\u03A0",
  4257. Rho: "\u03A1",
  4258. Sigma: "\u03A3",
  4259. Tau: "\u03A4",
  4260. Upsilon: "\u03A5",
  4261. Phi: "\u03A6",
  4262. Chi: "\u03A7",
  4263. Psi: "\u03A8",
  4264. Omega: "\u03A9",
  4265. alpha: "\u03B1",
  4266. beta: "\u03B2",
  4267. gamma: "\u03B3",
  4268. delta: "\u03B4",
  4269. epsilon: "\u03B5",
  4270. zeta: "\u03B6",
  4271. eta: "\u03B7",
  4272. theta: "\u03B8",
  4273. iota: "\u03B9",
  4274. kappa: "\u03BA",
  4275. lambda: "\u03BB",
  4276. mu: "\u03BC",
  4277. nu: "\u03BD",
  4278. xi: "\u03BE",
  4279. omicron: "\u03BF",
  4280. pi: "\u03C0",
  4281. rho: "\u03C1",
  4282. sigmaf: "\u03C2",
  4283. sigma: "\u03C3",
  4284. tau: "\u03C4",
  4285. upsilon: "\u03C5",
  4286. phi: "\u03C6",
  4287. chi: "\u03C7",
  4288. psi: "\u03C8",
  4289. omega: "\u03C9",
  4290. thetasym: "\u03D1",
  4291. upsih: "\u03D2",
  4292. piv: "\u03D6",
  4293. ensp: "\u2002",
  4294. emsp: "\u2003",
  4295. thinsp: "\u2009",
  4296. zwnj: "\u200C",
  4297. zwj: "\u200D",
  4298. lrm: "\u200E",
  4299. rlm: "\u200F",
  4300. ndash: "\u2013",
  4301. mdash: "\u2014",
  4302. lsquo: "\u2018",
  4303. rsquo: "\u2019",
  4304. sbquo: "\u201A",
  4305. ldquo: "\u201C",
  4306. rdquo: "\u201D",
  4307. bdquo: "\u201E",
  4308. dagger: "\u2020",
  4309. Dagger: "\u2021",
  4310. bull: "\u2022",
  4311. hellip: "\u2026",
  4312. permil: "\u2030",
  4313. prime: "\u2032",
  4314. Prime: "\u2033",
  4315. lsaquo: "\u2039",
  4316. rsaquo: "\u203A",
  4317. oline: "\u203E",
  4318. frasl: "\u2044",
  4319. euro: "\u20AC",
  4320. image: "\u2111",
  4321. weierp: "\u2118",
  4322. real: "\u211C",
  4323. trade: "\u2122",
  4324. alefsym: "\u2135",
  4325. larr: "\u2190",
  4326. uarr: "\u2191",
  4327. rarr: "\u2192",
  4328. darr: "\u2193",
  4329. harr: "\u2194",
  4330. crarr: "\u21B5",
  4331. lArr: "\u21D0",
  4332. uArr: "\u21D1",
  4333. rArr: "\u21D2",
  4334. dArr: "\u21D3",
  4335. hArr: "\u21D4",
  4336. forall: "\u2200",
  4337. part: "\u2202",
  4338. exist: "\u2203",
  4339. empty: "\u2205",
  4340. nabla: "\u2207",
  4341. isin: "\u2208",
  4342. notin: "\u2209",
  4343. ni: "\u220B",
  4344. prod: "\u220F",
  4345. sum: "\u2211",
  4346. minus: "\u2212",
  4347. lowast: "\u2217",
  4348. radic: "\u221A",
  4349. prop: "\u221D",
  4350. infin: "\u221E",
  4351. ang: "\u2220",
  4352. and: "\u2227",
  4353. or: "\u2228",
  4354. cap: "\u2229",
  4355. cup: "\u222A",
  4356. "int": "\u222B",
  4357. there4: "\u2234",
  4358. sim: "\u223C",
  4359. cong: "\u2245",
  4360. asymp: "\u2248",
  4361. ne: "\u2260",
  4362. equiv: "\u2261",
  4363. le: "\u2264",
  4364. ge: "\u2265",
  4365. sub: "\u2282",
  4366. sup: "\u2283",
  4367. nsub: "\u2284",
  4368. sube: "\u2286",
  4369. supe: "\u2287",
  4370. oplus: "\u2295",
  4371. otimes: "\u2297",
  4372. perp: "\u22A5",
  4373. sdot: "\u22C5",
  4374. lceil: "\u2308",
  4375. rceil: "\u2309",
  4376. lfloor: "\u230A",
  4377. rfloor: "\u230B",
  4378. lang: "\u2329",
  4379. rang: "\u232A",
  4380. loz: "\u25CA",
  4381. spades: "\u2660",
  4382. clubs: "\u2663",
  4383. hearts: "\u2665",
  4384. diams: "\u2666"
  4385. };
  4386. function isXJSIdentifierStart(ch) {
  4387. // exclude backslash (\)
  4388. return (ch !== 92) && isIdentifierStart(ch);
  4389. }
  4390. function isXJSIdentifierPart(ch) {
  4391. // exclude backslash (\) and add hyphen (-)
  4392. return (ch !== 92) && (ch === 45 || isIdentifierPart(ch));
  4393. }
  4394. function scanXJSIdentifier() {
  4395. var ch, start, id = '', namespace;
  4396. start = index;
  4397. while (index < length) {
  4398. ch = source.charCodeAt(index);
  4399. if (!isXJSIdentifierPart(ch)) {
  4400. break;
  4401. }
  4402. id += source[index++];
  4403. }
  4404. if (ch === 58) { // :
  4405. ++index;
  4406. namespace = id;
  4407. id = '';
  4408. while (index < length) {
  4409. ch = source.charCodeAt(index);
  4410. if (!isXJSIdentifierPart(ch)) {
  4411. break;
  4412. }
  4413. id += source[index++];
  4414. }
  4415. }
  4416. if (!id) {
  4417. throwError({}, Messages.InvalidXJSTagName);
  4418. }
  4419. return {
  4420. type: Token.XJSIdentifier,
  4421. value: id,
  4422. namespace: namespace,
  4423. lineNumber: lineNumber,
  4424. lineStart: lineStart,
  4425. range: [start, index]
  4426. };
  4427. }
  4428. function scanXJSEntity() {
  4429. var ch, str = '', count = 0, entity;
  4430. ch = source[index];
  4431. assert(ch === '&', 'Entity must start with an ampersand');
  4432. index++;
  4433. while (index < length && count++ < 10) {
  4434. ch = source[index++];
  4435. if (ch === ';') {
  4436. break;
  4437. }
  4438. str += ch;
  4439. }
  4440. if (str[0] === '#' && str[1] === 'x') {
  4441. entity = String.fromCharCode(parseInt(str.substr(2), 16));
  4442. } else if (str[0] === '#') {
  4443. entity = String.fromCharCode(parseInt(str.substr(1), 10));
  4444. } else {
  4445. entity = XHTMLEntities[str];
  4446. }
  4447. return entity;
  4448. }
  4449. function scanXJSText(stopChars) {
  4450. var ch, str = '', start;
  4451. start = index;
  4452. while (index < length) {
  4453. ch = source[index];
  4454. if (stopChars.indexOf(ch) !== -1) {
  4455. break;
  4456. }
  4457. if (ch === '&') {
  4458. str += scanXJSEntity();
  4459. } else {
  4460. ch = source[index++];
  4461. if (isLineTerminator(ch.charCodeAt(0))) {
  4462. ++lineNumber;
  4463. lineStart = index;
  4464. }
  4465. str += ch;
  4466. }
  4467. }
  4468. return {
  4469. type: Token.XJSText,
  4470. value: str,
  4471. lineNumber: lineNumber,
  4472. lineStart: lineStart,
  4473. range: [start, index]
  4474. };
  4475. }
  4476. function scanXJSStringLiteral() {
  4477. var innerToken, quote, start;
  4478. quote = source[index];
  4479. assert((quote === '\'' || quote === '"'),
  4480. 'String literal must starts with a quote');
  4481. start = index;
  4482. ++index;
  4483. innerToken = scanXJSText([quote]);
  4484. if (quote !== source[index]) {
  4485. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  4486. }
  4487. ++index;
  4488. innerToken.range = [start, index];
  4489. return innerToken;
  4490. }
  4491. /**
  4492. * Between XJS opening and closing tags (e.g. <foo>HERE</foo>), anything that
  4493. * is not another XJS tag and is not an expression wrapped by {} is text.
  4494. */
  4495. function advanceXJSChild() {
  4496. var ch = source.charCodeAt(index);
  4497. // { (123) and < (60)
  4498. if (ch !== 123 && ch !== 60) {
  4499. return scanXJSText(['<', '{']);
  4500. }
  4501. return scanPunctuator();
  4502. }
  4503. function parseXJSIdentifier() {
  4504. var token;
  4505. if (lookahead.type !== Token.XJSIdentifier) {
  4506. throwUnexpected(lookahead);
  4507. }
  4508. token = lex();
  4509. return delegate.createXJSIdentifier(token.value, token.namespace);
  4510. }
  4511. function parseXJSAttributeValue() {
  4512. var value;
  4513. if (match('{')) {
  4514. value = parseXJSExpressionContainer();
  4515. if (value.expression.type === Syntax.XJSEmptyExpression) {
  4516. throwError(
  4517. value,
  4518. 'XJS attributes must only be assigned a non-empty ' +
  4519. 'expression'
  4520. );
  4521. }
  4522. } else if (match('<')) {
  4523. value = parseXJSElement();
  4524. } else if (lookahead.type === Token.XJSText) {
  4525. value = delegate.createLiteral(lex());
  4526. } else {
  4527. throwError({}, Messages.InvalidXJSAttributeValue);
  4528. }
  4529. return value;
  4530. }
  4531. function parseXJSEmptyExpression() {
  4532. while (source.charAt(index) !== '}') {
  4533. index++;
  4534. }
  4535. return delegate.createXJSEmptyExpression();
  4536. }
  4537. function parseXJSExpressionContainer() {
  4538. var expression, origInXJSChild, origInXJSTag;
  4539. origInXJSChild = state.inXJSChild;
  4540. origInXJSTag = state.inXJSTag;
  4541. state.inXJSChild = false;
  4542. state.inXJSTag = false;
  4543. expect('{');
  4544. if (match('}')) {
  4545. expression = parseXJSEmptyExpression();
  4546. } else {
  4547. expression = parseExpression();
  4548. }
  4549. state.inXJSChild = origInXJSChild;
  4550. state.inXJSTag = origInXJSTag;
  4551. expect('}');
  4552. return delegate.createXJSExpressionContainer(expression);
  4553. }
  4554. function parseXJSAttribute() {
  4555. var token, name, value;
  4556. name = parseXJSIdentifier();
  4557. // HTML empty attribute
  4558. if (match('=')) {
  4559. lex();
  4560. return delegate.createXJSAttribute(name, parseXJSAttributeValue());
  4561. }
  4562. return delegate.createXJSAttribute(name);
  4563. }
  4564. function parseXJSChild() {
  4565. var token;
  4566. if (match('{')) {
  4567. token = parseXJSExpressionContainer();
  4568. } else if (lookahead.type === Token.XJSText) {
  4569. token = delegate.createLiteral(lex());
  4570. } else {
  4571. token = parseXJSElement();
  4572. }
  4573. return token;
  4574. }
  4575. function parseXJSClosingElement() {
  4576. var name, origInXJSChild, origInXJSTag;
  4577. origInXJSChild = state.inXJSChild;
  4578. origInXJSTag = state.inXJSTag;
  4579. state.inXJSChild = false;
  4580. state.inXJSTag = true;
  4581. expect('<');
  4582. expect('/');
  4583. name = parseXJSIdentifier();
  4584. // Because advance() (called by lex() called by expect()) expects there
  4585. // to be a valid token after >, it needs to know whether to look for a
  4586. // standard JS token or an XJS text node
  4587. state.inXJSChild = origInXJSChild;
  4588. state.inXJSTag = origInXJSTag;
  4589. expect('>');
  4590. return delegate.createXJSClosingElement(name);
  4591. }
  4592. function parseXJSOpeningElement() {
  4593. var name, attribute, attributes = [], selfClosing = false, origInXJSChild, origInXJSTag;
  4594. origInXJSChild = state.inXJSChild;
  4595. origInXJSTag = state.inXJSTag;
  4596. state.inXJSChild = false;
  4597. state.inXJSTag = true;
  4598. expect('<');
  4599. name = parseXJSIdentifier();
  4600. while (index < length &&
  4601. lookahead.value !== '/' &&
  4602. lookahead.value !== '>') {
  4603. attributes.push(parseXJSAttribute());
  4604. }
  4605. state.inXJSTag = origInXJSTag;
  4606. if (lookahead.value === '/') {
  4607. expect('/');
  4608. // Because advance() (called by lex() called by expect()) expects
  4609. // there to be a valid token after >, it needs to know whether to
  4610. // look for a standard JS token or an XJS text node
  4611. state.inXJSChild = origInXJSChild;
  4612. expect('>');
  4613. selfClosing = true;
  4614. } else {
  4615. state.inXJSChild = true;
  4616. expect('>');
  4617. }
  4618. return delegate.createXJSOpeningElement(name, attributes, selfClosing);
  4619. }
  4620. function parseXJSElement() {
  4621. var openingElement, closingElement, children = [], origInXJSChild, origInXJSTag;
  4622. origInXJSChild = state.inXJSChild;
  4623. origInXJSTag = state.inXJSTag;
  4624. openingElement = parseXJSOpeningElement();
  4625. if (!openingElement.selfClosing) {
  4626. while (index < length) {
  4627. state.inXJSChild = false; // Call lookahead2() with inXJSChild = false because </ should not be considered in the child
  4628. if (lookahead.value === '<' && lookahead2().value === '/') {
  4629. break;
  4630. }
  4631. state.inXJSChild = true;
  4632. peek(); // reset lookahead token
  4633. children.push(parseXJSChild());
  4634. }
  4635. state.inXJSChild = origInXJSChild;
  4636. state.inXJSTag = origInXJSTag;
  4637. closingElement = parseXJSClosingElement();
  4638. if (closingElement.name.namespace !== openingElement.name.namespace || closingElement.name.name !== openingElement.name.name) {
  4639. throwError({}, Messages.ExpectedXJSClosingTag, openingElement.name.namespace ? openingElement.name.namespace + ':' + openingElement.name.name : openingElement.name.name);
  4640. }
  4641. }
  4642. return delegate.createXJSElement(openingElement, closingElement, children);
  4643. }
  4644. function collectToken() {
  4645. var start, loc, token, range, value;
  4646. skipComment();
  4647. start = index;
  4648. loc = {
  4649. start: {
  4650. line: lineNumber,
  4651. column: index - lineStart
  4652. }
  4653. };
  4654. token = extra.advance();
  4655. loc.end = {
  4656. line: lineNumber,
  4657. column: index - lineStart
  4658. };
  4659. if (token.type !== Token.EOF) {
  4660. range = [token.range[0], token.range[1]];
  4661. value = source.slice(token.range[0], token.range[1]);
  4662. extra.tokens.push({
  4663. type: TokenName[token.type],
  4664. value: value,
  4665. range: range,
  4666. loc: loc
  4667. });
  4668. }
  4669. return token;
  4670. }
  4671. function collectRegex() {
  4672. var pos, loc, regex, token;
  4673. skipComment();
  4674. pos = index;
  4675. loc = {
  4676. start: {
  4677. line: lineNumber,
  4678. column: index - lineStart
  4679. }
  4680. };
  4681. regex = extra.scanRegExp();
  4682. loc.end = {
  4683. line: lineNumber,
  4684. column: index - lineStart
  4685. };
  4686. if (!extra.tokenize) {
  4687. // Pop the previous token, which is likely '/' or '/='
  4688. if (extra.tokens.length > 0) {
  4689. token = extra.tokens[extra.tokens.length - 1];
  4690. if (token.range[0] === pos && token.type === 'Punctuator') {
  4691. if (token.value === '/' || token.value === '/=') {
  4692. extra.tokens.pop();
  4693. }
  4694. }
  4695. }
  4696. extra.tokens.push({
  4697. type: 'RegularExpression',
  4698. value: regex.literal,
  4699. range: [pos, index],
  4700. loc: loc
  4701. });
  4702. }
  4703. return regex;
  4704. }
  4705. function filterTokenLocation() {
  4706. var i, entry, token, tokens = [];
  4707. for (i = 0; i < extra.tokens.length; ++i) {
  4708. entry = extra.tokens[i];
  4709. token = {
  4710. type: entry.type,
  4711. value: entry.value
  4712. };
  4713. if (extra.range) {
  4714. token.range = entry.range;
  4715. }
  4716. if (extra.loc) {
  4717. token.loc = entry.loc;
  4718. }
  4719. tokens.push(token);
  4720. }
  4721. extra.tokens = tokens;
  4722. }
  4723. function LocationMarker() {
  4724. this.range = [index, index];
  4725. this.loc = {
  4726. start: {
  4727. line: lineNumber,
  4728. column: index - lineStart
  4729. },
  4730. end: {
  4731. line: lineNumber,
  4732. column: index - lineStart
  4733. }
  4734. };
  4735. }
  4736. LocationMarker.prototype = {
  4737. constructor: LocationMarker,
  4738. end: function () {
  4739. this.range[1] = index;
  4740. this.loc.end.line = lineNumber;
  4741. this.loc.end.column = index - lineStart;
  4742. },
  4743. applyGroup: function (node) {
  4744. if (extra.range) {
  4745. node.groupRange = [this.range[0], this.range[1]];
  4746. }
  4747. if (extra.loc) {
  4748. node.groupLoc = {
  4749. start: {
  4750. line: this.loc.start.line,
  4751. column: this.loc.start.column
  4752. },
  4753. end: {
  4754. line: this.loc.end.line,
  4755. column: this.loc.end.column
  4756. }
  4757. };
  4758. node = delegate.postProcess(node);
  4759. }
  4760. },
  4761. apply: function (node) {
  4762. var nodeType = typeof node;
  4763. assert(nodeType === 'object',
  4764. 'Applying location marker to an unexpected node type: ' +
  4765. nodeType);
  4766. if (extra.range) {
  4767. node.range = [this.range[0], this.range[1]];
  4768. }
  4769. if (extra.loc) {
  4770. node.loc = {
  4771. start: {
  4772. line: this.loc.start.line,
  4773. column: this.loc.start.column
  4774. },
  4775. end: {
  4776. line: this.loc.end.line,
  4777. column: this.loc.end.column
  4778. }
  4779. };
  4780. node = delegate.postProcess(node);
  4781. }
  4782. }
  4783. };
  4784. function createLocationMarker() {
  4785. return new LocationMarker();
  4786. }
  4787. function trackGroupExpression() {
  4788. var marker, expr;
  4789. skipComment();
  4790. marker = createLocationMarker();
  4791. expect('(');
  4792. ++state.parenthesizedCount;
  4793. expr = parseExpression();
  4794. expect(')');
  4795. marker.end();
  4796. marker.applyGroup(expr);
  4797. return expr;
  4798. }
  4799. function trackLeftHandSideExpression() {
  4800. var marker, expr;
  4801. skipComment();
  4802. marker = createLocationMarker();
  4803. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  4804. while (match('.') || match('[') || lookahead.type === Token.Template) {
  4805. if (match('[')) {
  4806. expr = delegate.createMemberExpression('[', expr, parseComputedMember());
  4807. marker.end();
  4808. marker.apply(expr);
  4809. } else if (match('.')) {
  4810. expr = delegate.createMemberExpression('.', expr, parseNonComputedMember());
  4811. marker.end();
  4812. marker.apply(expr);
  4813. } else {
  4814. expr = delegate.createTaggedTemplateExpression(expr, parseTemplateLiteral());
  4815. marker.end();
  4816. marker.apply(expr);
  4817. }
  4818. }
  4819. return expr;
  4820. }
  4821. function trackLeftHandSideExpressionAllowCall() {
  4822. var marker, expr, args;
  4823. skipComment();
  4824. marker = createLocationMarker();
  4825. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  4826. while (match('.') || match('[') || match('(') || lookahead.type === Token.Template) {
  4827. if (match('(')) {
  4828. args = parseArguments();
  4829. expr = delegate.createCallExpression(expr, args);
  4830. marker.end();
  4831. marker.apply(expr);
  4832. } else if (match('[')) {
  4833. expr = delegate.createMemberExpression('[', expr, parseComputedMember());
  4834. marker.end();
  4835. marker.apply(expr);
  4836. } else if (match('.')) {
  4837. expr = delegate.createMemberExpression('.', expr, parseNonComputedMember());
  4838. marker.end();
  4839. marker.apply(expr);
  4840. } else {
  4841. expr = delegate.createTaggedTemplateExpression(expr, parseTemplateLiteral());
  4842. marker.end();
  4843. marker.apply(expr);
  4844. }
  4845. }
  4846. return expr;
  4847. }
  4848. function filterGroup(node) {
  4849. var n, i, entry;
  4850. n = (Object.prototype.toString.apply(node) === '[object Array]') ? [] : {};
  4851. for (i in node) {
  4852. if (node.hasOwnProperty(i) && i !== 'groupRange' && i !== 'groupLoc') {
  4853. entry = node[i];
  4854. if (entry === null || typeof entry !== 'object' || entry instanceof RegExp) {
  4855. n[i] = entry;
  4856. } else {
  4857. n[i] = filterGroup(entry);
  4858. }
  4859. }
  4860. }
  4861. return n;
  4862. }
  4863. function wrapTrackingFunction(range, loc, preserveWhitespace) {
  4864. return function (parseFunction) {
  4865. function isBinary(node) {
  4866. return node.type === Syntax.LogicalExpression ||
  4867. node.type === Syntax.BinaryExpression;
  4868. }
  4869. function visit(node) {
  4870. var start, end;
  4871. if (isBinary(node.left)) {
  4872. visit(node.left);
  4873. }
  4874. if (isBinary(node.right)) {
  4875. visit(node.right);
  4876. }
  4877. if (range) {
  4878. if (node.left.groupRange || node.right.groupRange) {
  4879. start = node.left.groupRange ? node.left.groupRange[0] : node.left.range[0];
  4880. end = node.right.groupRange ? node.right.groupRange[1] : node.right.range[1];
  4881. node.range = [start, end];
  4882. } else if (typeof node.range === 'undefined') {
  4883. start = node.left.range[0];
  4884. end = node.right.range[1];
  4885. node.range = [start, end];
  4886. }
  4887. }
  4888. if (loc) {
  4889. if (node.left.groupLoc || node.right.groupLoc) {
  4890. start = node.left.groupLoc ? node.left.groupLoc.start : node.left.loc.start;
  4891. end = node.right.groupLoc ? node.right.groupLoc.end : node.right.loc.end;
  4892. node.loc = {
  4893. start: start,
  4894. end: end
  4895. };
  4896. node = delegate.postProcess(node);
  4897. } else if (typeof node.loc === 'undefined') {
  4898. node.loc = {
  4899. start: node.left.loc.start,
  4900. end: node.right.loc.end
  4901. };
  4902. node = delegate.postProcess(node);
  4903. }
  4904. }
  4905. }
  4906. return function () {
  4907. var marker, node;
  4908. if (!preserveWhitespace) {
  4909. skipComment();
  4910. }
  4911. marker = createLocationMarker();
  4912. node = parseFunction.apply(null, arguments);
  4913. marker.end();
  4914. if (range && typeof node.range === 'undefined') {
  4915. marker.apply(node);
  4916. }
  4917. if (loc && typeof node.loc === 'undefined') {
  4918. marker.apply(node);
  4919. }
  4920. if (isBinary(node)) {
  4921. visit(node);
  4922. }
  4923. return node;
  4924. };
  4925. };
  4926. }
  4927. function patch() {
  4928. var wrapTracking, wrapTrackingPreserveWhitespace;
  4929. if (extra.comments) {
  4930. extra.skipComment = skipComment;
  4931. skipComment = scanComment;
  4932. }
  4933. if (extra.range || extra.loc) {
  4934. extra.parseGroupExpression = parseGroupExpression;
  4935. extra.parseLeftHandSideExpression = parseLeftHandSideExpression;
  4936. extra.parseLeftHandSideExpressionAllowCall = parseLeftHandSideExpressionAllowCall;
  4937. parseGroupExpression = trackGroupExpression;
  4938. parseLeftHandSideExpression = trackLeftHandSideExpression;
  4939. parseLeftHandSideExpressionAllowCall = trackLeftHandSideExpressionAllowCall;
  4940. wrapTracking = wrapTrackingFunction(extra.range, extra.loc);
  4941. wrapTrackingPreserveWhitespace =
  4942. wrapTrackingFunction(extra.range, extra.loc, true);
  4943. extra.parseArrayInitialiser = parseArrayInitialiser;
  4944. extra.parseAssignmentExpression = parseAssignmentExpression;
  4945. extra.parseBinaryExpression = parseBinaryExpression;
  4946. extra.parseBlock = parseBlock;
  4947. extra.parseFunctionSourceElements = parseFunctionSourceElements;
  4948. extra.parseCatchClause = parseCatchClause;
  4949. extra.parseComputedMember = parseComputedMember;
  4950. extra.parseConditionalExpression = parseConditionalExpression;
  4951. extra.parseConstLetDeclaration = parseConstLetDeclaration;
  4952. extra.parseExportBatchSpecifier = parseExportBatchSpecifier;
  4953. extra.parseExportDeclaration = parseExportDeclaration;
  4954. extra.parseExportSpecifier = parseExportSpecifier;
  4955. extra.parseExpression = parseExpression;
  4956. extra.parseForVariableDeclaration = parseForVariableDeclaration;
  4957. extra.parseFunctionDeclaration = parseFunctionDeclaration;
  4958. extra.parseFunctionExpression = parseFunctionExpression;
  4959. extra.parseParams = parseParams;
  4960. extra.parseImportDeclaration = parseImportDeclaration;
  4961. extra.parseImportSpecifier = parseImportSpecifier;
  4962. extra.parseModuleDeclaration = parseModuleDeclaration;
  4963. extra.parseModuleBlock = parseModuleBlock;
  4964. extra.parseNewExpression = parseNewExpression;
  4965. extra.parseNonComputedProperty = parseNonComputedProperty;
  4966. extra.parseObjectInitialiser = parseObjectInitialiser;
  4967. extra.parseObjectProperty = parseObjectProperty;
  4968. extra.parseObjectPropertyKey = parseObjectPropertyKey;
  4969. extra.parsePostfixExpression = parsePostfixExpression;
  4970. extra.parsePrimaryExpression = parsePrimaryExpression;
  4971. extra.parseProgram = parseProgram;
  4972. extra.parsePropertyFunction = parsePropertyFunction;
  4973. extra.parseSpreadOrAssignmentExpression = parseSpreadOrAssignmentExpression;
  4974. extra.parseTemplateElement = parseTemplateElement;
  4975. extra.parseTemplateLiteral = parseTemplateLiteral;
  4976. extra.parseTypeAnnotatableIdentifier = parseTypeAnnotatableIdentifier;
  4977. extra.parseTypeAnnotation = parseTypeAnnotation;
  4978. extra.parseStatement = parseStatement;
  4979. extra.parseSwitchCase = parseSwitchCase;
  4980. extra.parseUnaryExpression = parseUnaryExpression;
  4981. extra.parseVariableDeclaration = parseVariableDeclaration;
  4982. extra.parseVariableIdentifier = parseVariableIdentifier;
  4983. extra.parseMethodDefinition = parseMethodDefinition;
  4984. extra.parseClassDeclaration = parseClassDeclaration;
  4985. extra.parseClassExpression = parseClassExpression;
  4986. extra.parseClassBody = parseClassBody;
  4987. extra.parseXJSIdentifier = parseXJSIdentifier;
  4988. extra.parseXJSChild = parseXJSChild;
  4989. extra.parseXJSAttribute = parseXJSAttribute;
  4990. extra.parseXJSAttributeValue = parseXJSAttributeValue;
  4991. extra.parseXJSExpressionContainer = parseXJSExpressionContainer;
  4992. extra.parseXJSEmptyExpression = parseXJSEmptyExpression;
  4993. extra.parseXJSElement = parseXJSElement;
  4994. extra.parseXJSClosingElement = parseXJSClosingElement;
  4995. extra.parseXJSOpeningElement = parseXJSOpeningElement;
  4996. parseArrayInitialiser = wrapTracking(extra.parseArrayInitialiser);
  4997. parseAssignmentExpression = wrapTracking(extra.parseAssignmentExpression);
  4998. parseBinaryExpression = wrapTracking(extra.parseBinaryExpression);
  4999. parseBlock = wrapTracking(extra.parseBlock);
  5000. parseFunctionSourceElements = wrapTracking(extra.parseFunctionSourceElements);
  5001. parseCatchClause = wrapTracking(extra.parseCatchClause);
  5002. parseComputedMember = wrapTracking(extra.parseComputedMember);
  5003. parseConditionalExpression = wrapTracking(extra.parseConditionalExpression);
  5004. parseConstLetDeclaration = wrapTracking(extra.parseConstLetDeclaration);
  5005. parseExportBatchSpecifier = wrapTracking(parseExportBatchSpecifier);
  5006. parseExportDeclaration = wrapTracking(parseExportDeclaration);
  5007. parseExportSpecifier = wrapTracking(parseExportSpecifier);
  5008. parseExpression = wrapTracking(extra.parseExpression);
  5009. parseForVariableDeclaration = wrapTracking(extra.parseForVariableDeclaration);
  5010. parseFunctionDeclaration = wrapTracking(extra.parseFunctionDeclaration);
  5011. parseFunctionExpression = wrapTracking(extra.parseFunctionExpression);
  5012. parseParams = wrapTracking(extra.parseParams);
  5013. parseImportDeclaration = wrapTracking(extra.parseImportDeclaration);
  5014. parseImportSpecifier = wrapTracking(extra.parseImportSpecifier);
  5015. parseModuleDeclaration = wrapTracking(extra.parseModuleDeclaration);
  5016. parseModuleBlock = wrapTracking(extra.parseModuleBlock);
  5017. parseLeftHandSideExpression = wrapTracking(parseLeftHandSideExpression);
  5018. parseNewExpression = wrapTracking(extra.parseNewExpression);
  5019. parseNonComputedProperty = wrapTracking(extra.parseNonComputedProperty);
  5020. parseObjectInitialiser = wrapTracking(extra.parseObjectInitialiser);
  5021. parseObjectProperty = wrapTracking(extra.parseObjectProperty);
  5022. parseObjectPropertyKey = wrapTracking(extra.parseObjectPropertyKey);
  5023. parsePostfixExpression = wrapTracking(extra.parsePostfixExpression);
  5024. parsePrimaryExpression = wrapTracking(extra.parsePrimaryExpression);
  5025. parseProgram = wrapTracking(extra.parseProgram);
  5026. parsePropertyFunction = wrapTracking(extra.parsePropertyFunction);
  5027. parseTemplateElement = wrapTracking(extra.parseTemplateElement);
  5028. parseTemplateLiteral = wrapTracking(extra.parseTemplateLiteral);
  5029. parseTypeAnnotatableIdentifier = wrapTracking(extra.parseTypeAnnotatableIdentifier);
  5030. parseTypeAnnotation = wrapTracking(extra.parseTypeAnnotation);
  5031. parseSpreadOrAssignmentExpression = wrapTracking(extra.parseSpreadOrAssignmentExpression);
  5032. parseStatement = wrapTracking(extra.parseStatement);
  5033. parseSwitchCase = wrapTracking(extra.parseSwitchCase);
  5034. parseUnaryExpression = wrapTracking(extra.parseUnaryExpression);
  5035. parseVariableDeclaration = wrapTracking(extra.parseVariableDeclaration);
  5036. parseVariableIdentifier = wrapTracking(extra.parseVariableIdentifier);
  5037. parseMethodDefinition = wrapTracking(extra.parseMethodDefinition);
  5038. parseClassDeclaration = wrapTracking(extra.parseClassDeclaration);
  5039. parseClassExpression = wrapTracking(extra.parseClassExpression);
  5040. parseClassBody = wrapTracking(extra.parseClassBody);
  5041. parseXJSIdentifier = wrapTracking(extra.parseXJSIdentifier);
  5042. parseXJSChild = wrapTrackingPreserveWhitespace(extra.parseXJSChild);
  5043. parseXJSAttribute = wrapTracking(extra.parseXJSAttribute);
  5044. parseXJSAttributeValue = wrapTracking(extra.parseXJSAttributeValue);
  5045. parseXJSExpressionContainer = wrapTracking(extra.parseXJSExpressionContainer);
  5046. parseXJSEmptyExpression = wrapTrackingPreserveWhitespace(extra.parseXJSEmptyExpression);
  5047. parseXJSElement = wrapTracking(extra.parseXJSElement);
  5048. parseXJSClosingElement = wrapTracking(extra.parseXJSClosingElement);
  5049. parseXJSOpeningElement = wrapTracking(extra.parseXJSOpeningElement);
  5050. }
  5051. if (typeof extra.tokens !== 'undefined') {
  5052. extra.advance = advance;
  5053. extra.scanRegExp = scanRegExp;
  5054. advance = collectToken;
  5055. scanRegExp = collectRegex;
  5056. }
  5057. }
  5058. function unpatch() {
  5059. if (typeof extra.skipComment === 'function') {
  5060. skipComment = extra.skipComment;
  5061. }
  5062. if (extra.range || extra.loc) {
  5063. parseArrayInitialiser = extra.parseArrayInitialiser;
  5064. parseAssignmentExpression = extra.parseAssignmentExpression;
  5065. parseBinaryExpression = extra.parseBinaryExpression;
  5066. parseBlock = extra.parseBlock;
  5067. parseFunctionSourceElements = extra.parseFunctionSourceElements;
  5068. parseCatchClause = extra.parseCatchClause;
  5069. parseComputedMember = extra.parseComputedMember;
  5070. parseConditionalExpression = extra.parseConditionalExpression;
  5071. parseConstLetDeclaration = extra.parseConstLetDeclaration;
  5072. parseExportBatchSpecifier = extra.parseExportBatchSpecifier;
  5073. parseExportDeclaration = extra.parseExportDeclaration;
  5074. parseExportSpecifier = extra.parseExportSpecifier;
  5075. parseExpression = extra.parseExpression;
  5076. parseForVariableDeclaration = extra.parseForVariableDeclaration;
  5077. parseFunctionDeclaration = extra.parseFunctionDeclaration;
  5078. parseFunctionExpression = extra.parseFunctionExpression;
  5079. parseImportDeclaration = extra.parseImportDeclaration;
  5080. parseImportSpecifier = extra.parseImportSpecifier;
  5081. parseGroupExpression = extra.parseGroupExpression;
  5082. parseLeftHandSideExpression = extra.parseLeftHandSideExpression;
  5083. parseLeftHandSideExpressionAllowCall = extra.parseLeftHandSideExpressionAllowCall;
  5084. parseModuleDeclaration = extra.parseModuleDeclaration;
  5085. parseModuleBlock = extra.parseModuleBlock;
  5086. parseNewExpression = extra.parseNewExpression;
  5087. parseNonComputedProperty = extra.parseNonComputedProperty;
  5088. parseObjectInitialiser = extra.parseObjectInitialiser;
  5089. parseObjectProperty = extra.parseObjectProperty;
  5090. parseObjectPropertyKey = extra.parseObjectPropertyKey;
  5091. parsePostfixExpression = extra.parsePostfixExpression;
  5092. parsePrimaryExpression = extra.parsePrimaryExpression;
  5093. parseProgram = extra.parseProgram;
  5094. parsePropertyFunction = extra.parsePropertyFunction;
  5095. parseTemplateElement = extra.parseTemplateElement;
  5096. parseTemplateLiteral = extra.parseTemplateLiteral;
  5097. parseTypeAnnotatableIdentifier = extra.parseTypeAnnotatableIdentifier;
  5098. parseTypeAnnotation = extra.parseTypeAnnotation;
  5099. parseSpreadOrAssignmentExpression = extra.parseSpreadOrAssignmentExpression;
  5100. parseStatement = extra.parseStatement;
  5101. parseSwitchCase = extra.parseSwitchCase;
  5102. parseUnaryExpression = extra.parseUnaryExpression;
  5103. parseVariableDeclaration = extra.parseVariableDeclaration;
  5104. parseVariableIdentifier = extra.parseVariableIdentifier;
  5105. parseMethodDefinition = extra.parseMethodDefinition;
  5106. parseClassDeclaration = extra.parseClassDeclaration;
  5107. parseClassExpression = extra.parseClassExpression;
  5108. parseClassBody = extra.parseClassBody;
  5109. parseXJSIdentifier = extra.parseXJSIdentifier;
  5110. parseXJSChild = extra.parseXJSChild;
  5111. parseXJSAttribute = extra.parseXJSAttribute;
  5112. parseXJSAttributeValue = extra.parseXJSAttributeValue;
  5113. parseXJSExpressionContainer = extra.parseXJSExpressionContainer;
  5114. parseXJSEmptyExpression = extra.parseXJSEmptyExpression;
  5115. parseXJSElement = extra.parseXJSElement;
  5116. parseXJSClosingElement = extra.parseXJSClosingElement;
  5117. parseXJSOpeningElement = extra.parseXJSOpeningElement;
  5118. }
  5119. if (typeof extra.scanRegExp === 'function') {
  5120. advance = extra.advance;
  5121. scanRegExp = extra.scanRegExp;
  5122. }
  5123. }
  5124. // This is used to modify the delegate.
  5125. function extend(object, properties) {
  5126. var entry, result = {};
  5127. for (entry in object) {
  5128. if (object.hasOwnProperty(entry)) {
  5129. result[entry] = object[entry];
  5130. }
  5131. }
  5132. for (entry in properties) {
  5133. if (properties.hasOwnProperty(entry)) {
  5134. result[entry] = properties[entry];
  5135. }
  5136. }
  5137. return result;
  5138. }
  5139. function tokenize(code, options) {
  5140. var toString,
  5141. token,
  5142. tokens;
  5143. toString = String;
  5144. if (typeof code !== 'string' && !(code instanceof String)) {
  5145. code = toString(code);
  5146. }
  5147. delegate = SyntaxTreeDelegate;
  5148. source = code;
  5149. index = 0;
  5150. lineNumber = (source.length > 0) ? 1 : 0;
  5151. lineStart = 0;
  5152. length = source.length;
  5153. lookahead = null;
  5154. state = {
  5155. allowKeyword: true,
  5156. allowIn: true,
  5157. labelSet: {},
  5158. inFunctionBody: false,
  5159. inIteration: false,
  5160. inSwitch: false
  5161. };
  5162. extra = {};
  5163. // Options matching.
  5164. options = options || {};
  5165. // Of course we collect tokens here.
  5166. options.tokens = true;
  5167. extra.tokens = [];
  5168. extra.tokenize = true;
  5169. // The following two fields are necessary to compute the Regex tokens.
  5170. extra.openParenToken = -1;
  5171. extra.openCurlyToken = -1;
  5172. extra.range = (typeof options.range === 'boolean') && options.range;
  5173. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  5174. if (typeof options.comment === 'boolean' && options.comment) {
  5175. extra.comments = [];
  5176. }
  5177. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  5178. extra.errors = [];
  5179. }
  5180. if (length > 0) {
  5181. if (typeof source[0] === 'undefined') {
  5182. // Try first to convert to a string. This is good as fast path
  5183. // for old IE which understands string indexing for string
  5184. // literals only and not for string object.
  5185. if (code instanceof String) {
  5186. source = code.valueOf();
  5187. }
  5188. }
  5189. }
  5190. patch();
  5191. try {
  5192. peek();
  5193. if (lookahead.type === Token.EOF) {
  5194. return extra.tokens;
  5195. }
  5196. token = lex();
  5197. while (lookahead.type !== Token.EOF) {
  5198. try {
  5199. token = lex();
  5200. } catch (lexError) {
  5201. token = lookahead;
  5202. if (extra.errors) {
  5203. extra.errors.push(lexError);
  5204. // We have to break on the first error
  5205. // to avoid infinite loops.
  5206. break;
  5207. } else {
  5208. throw lexError;
  5209. }
  5210. }
  5211. }
  5212. filterTokenLocation();
  5213. tokens = extra.tokens;
  5214. if (typeof extra.comments !== 'undefined') {
  5215. filterCommentLocation();
  5216. tokens.comments = extra.comments;
  5217. }
  5218. if (typeof extra.errors !== 'undefined') {
  5219. tokens.errors = extra.errors;
  5220. }
  5221. } catch (e) {
  5222. throw e;
  5223. } finally {
  5224. unpatch();
  5225. extra = {};
  5226. }
  5227. return tokens;
  5228. }
  5229. function parse(code, options) {
  5230. var program, toString;
  5231. toString = String;
  5232. if (typeof code !== 'string' && !(code instanceof String)) {
  5233. code = toString(code);
  5234. }
  5235. delegate = SyntaxTreeDelegate;
  5236. source = code;
  5237. index = 0;
  5238. lineNumber = (source.length > 0) ? 1 : 0;
  5239. lineStart = 0;
  5240. length = source.length;
  5241. lookahead = null;
  5242. state = {
  5243. allowKeyword: false,
  5244. allowIn: true,
  5245. labelSet: {},
  5246. parenthesizedCount: 0,
  5247. inFunctionBody: false,
  5248. inIteration: false,
  5249. inSwitch: false,
  5250. inXJSChild: false,
  5251. inXJSTag: false,
  5252. yieldAllowed: false,
  5253. yieldFound: false
  5254. };
  5255. extra = {};
  5256. if (typeof options !== 'undefined') {
  5257. extra.range = (typeof options.range === 'boolean') && options.range;
  5258. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  5259. if (extra.loc && options.source !== null && options.source !== undefined) {
  5260. delegate = extend(delegate, {
  5261. 'postProcess': function (node) {
  5262. node.loc.source = toString(options.source);
  5263. return node;
  5264. }
  5265. });
  5266. }
  5267. if (typeof options.tokens === 'boolean' && options.tokens) {
  5268. extra.tokens = [];
  5269. }
  5270. if (typeof options.comment === 'boolean' && options.comment) {
  5271. extra.comments = [];
  5272. }
  5273. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  5274. extra.errors = [];
  5275. }
  5276. }
  5277. if (length > 0) {
  5278. if (typeof source[0] === 'undefined') {
  5279. // Try first to convert to a string. This is good as fast path
  5280. // for old IE which understands string indexing for string
  5281. // literals only and not for string object.
  5282. if (code instanceof String) {
  5283. source = code.valueOf();
  5284. }
  5285. }
  5286. }
  5287. patch();
  5288. try {
  5289. program = parseProgram();
  5290. if (typeof extra.comments !== 'undefined') {
  5291. filterCommentLocation();
  5292. program.comments = extra.comments;
  5293. }
  5294. if (typeof extra.tokens !== 'undefined') {
  5295. filterTokenLocation();
  5296. program.tokens = extra.tokens;
  5297. }
  5298. if (typeof extra.errors !== 'undefined') {
  5299. program.errors = extra.errors;
  5300. }
  5301. if (extra.range || extra.loc) {
  5302. program.body = filterGroup(program.body);
  5303. }
  5304. } catch (e) {
  5305. throw e;
  5306. } finally {
  5307. unpatch();
  5308. extra = {};
  5309. }
  5310. return program;
  5311. }
  5312. // Sync with *.json manifests.
  5313. exports.version = '1.1.0-dev-harmony';
  5314. exports.tokenize = tokenize;
  5315. exports.parse = parse;
  5316. // Deep copy.
  5317. exports.Syntax = (function () {
  5318. var name, types = {};
  5319. if (typeof Object.create === 'function') {
  5320. types = Object.create(null);
  5321. }
  5322. for (name in Syntax) {
  5323. if (Syntax.hasOwnProperty(name)) {
  5324. types[name] = Syntax[name];
  5325. }
  5326. }
  5327. if (typeof Object.freeze === 'function') {
  5328. Object.freeze(types);
  5329. }
  5330. return types;
  5331. }());
  5332. }));
  5333. /* vim: set sw=4 ts=4 et tw=80 : */