jsonpath.js 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842
  1. /*! jsonpath 1.1.1 */
  2. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsonpath = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"./aesprim":[function(require,module,exports){
  3. /*
  4. Copyright (C) 2013 Ariya Hidayat <ariya.hidayat@gmail.com>
  5. Copyright (C) 2013 Thaddee Tyl <thaddee.tyl@gmail.com>
  6. Copyright (C) 2013 Mathias Bynens <mathias@qiwi.be>
  7. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  8. Copyright (C) 2012 Mathias Bynens <mathias@qiwi.be>
  9. Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
  10. Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
  11. Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
  12. Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
  13. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  14. Redistribution and use in source and binary forms, with or without
  15. modification, are permitted provided that the following conditions are met:
  16. * Redistributions of source code must retain the above copyright
  17. notice, this list of conditions and the following disclaimer.
  18. * Redistributions in binary form must reproduce the above copyright
  19. notice, this list of conditions and the following disclaimer in the
  20. documentation and/or other materials provided with the distribution.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  25. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*jslint bitwise:true plusplus:true */
  33. /*global esprima:true, define:true, exports:true, window: true,
  34. throwErrorTolerant: true,
  35. throwError: true, generateStatement: true, peek: true,
  36. parseAssignmentExpression: true, parseBlock: true, parseExpression: true,
  37. parseFunctionDeclaration: true, parseFunctionExpression: true,
  38. parseFunctionSourceElements: true, parseVariableIdentifier: true,
  39. parseLeftHandSideExpression: true,
  40. parseUnaryExpression: true,
  41. parseStatement: true, parseSourceElement: true */
  42. (function (root, factory) {
  43. 'use strict';
  44. // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
  45. // Rhino, and plain browser loading.
  46. /* istanbul ignore next */
  47. if (typeof define === 'function' && define.amd) {
  48. define(['exports'], factory);
  49. } else if (typeof exports !== 'undefined') {
  50. factory(exports);
  51. } else {
  52. factory((root.esprima = {}));
  53. }
  54. }(this, function (exports) {
  55. 'use strict';
  56. var Token,
  57. TokenName,
  58. FnExprTokens,
  59. Syntax,
  60. PropertyKind,
  61. Messages,
  62. Regex,
  63. SyntaxTreeDelegate,
  64. source,
  65. strict,
  66. index,
  67. lineNumber,
  68. lineStart,
  69. length,
  70. delegate,
  71. lookahead,
  72. state,
  73. extra;
  74. Token = {
  75. BooleanLiteral: 1,
  76. EOF: 2,
  77. Identifier: 3,
  78. Keyword: 4,
  79. NullLiteral: 5,
  80. NumericLiteral: 6,
  81. Punctuator: 7,
  82. StringLiteral: 8,
  83. RegularExpression: 9
  84. };
  85. TokenName = {};
  86. TokenName[Token.BooleanLiteral] = 'Boolean';
  87. TokenName[Token.EOF] = '<end>';
  88. TokenName[Token.Identifier] = 'Identifier';
  89. TokenName[Token.Keyword] = 'Keyword';
  90. TokenName[Token.NullLiteral] = 'Null';
  91. TokenName[Token.NumericLiteral] = 'Numeric';
  92. TokenName[Token.Punctuator] = 'Punctuator';
  93. TokenName[Token.StringLiteral] = 'String';
  94. TokenName[Token.RegularExpression] = 'RegularExpression';
  95. // A function following one of those tokens is an expression.
  96. FnExprTokens = ['(', '{', '[', 'in', 'typeof', 'instanceof', 'new',
  97. 'return', 'case', 'delete', 'throw', 'void',
  98. // assignment operators
  99. '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=',
  100. '&=', '|=', '^=', ',',
  101. // binary/unary operators
  102. '+', '-', '*', '/', '%', '++', '--', '<<', '>>', '>>>', '&',
  103. '|', '^', '!', '~', '&&', '||', '?', ':', '===', '==', '>=',
  104. '<=', '<', '>', '!=', '!=='];
  105. Syntax = {
  106. AssignmentExpression: 'AssignmentExpression',
  107. ArrayExpression: 'ArrayExpression',
  108. BlockStatement: 'BlockStatement',
  109. BinaryExpression: 'BinaryExpression',
  110. BreakStatement: 'BreakStatement',
  111. CallExpression: 'CallExpression',
  112. CatchClause: 'CatchClause',
  113. ConditionalExpression: 'ConditionalExpression',
  114. ContinueStatement: 'ContinueStatement',
  115. DoWhileStatement: 'DoWhileStatement',
  116. DebuggerStatement: 'DebuggerStatement',
  117. EmptyStatement: 'EmptyStatement',
  118. ExpressionStatement: 'ExpressionStatement',
  119. ForStatement: 'ForStatement',
  120. ForInStatement: 'ForInStatement',
  121. FunctionDeclaration: 'FunctionDeclaration',
  122. FunctionExpression: 'FunctionExpression',
  123. Identifier: 'Identifier',
  124. IfStatement: 'IfStatement',
  125. Literal: 'Literal',
  126. LabeledStatement: 'LabeledStatement',
  127. LogicalExpression: 'LogicalExpression',
  128. MemberExpression: 'MemberExpression',
  129. NewExpression: 'NewExpression',
  130. ObjectExpression: 'ObjectExpression',
  131. Program: 'Program',
  132. Property: 'Property',
  133. ReturnStatement: 'ReturnStatement',
  134. SequenceExpression: 'SequenceExpression',
  135. SwitchStatement: 'SwitchStatement',
  136. SwitchCase: 'SwitchCase',
  137. ThisExpression: 'ThisExpression',
  138. ThrowStatement: 'ThrowStatement',
  139. TryStatement: 'TryStatement',
  140. UnaryExpression: 'UnaryExpression',
  141. UpdateExpression: 'UpdateExpression',
  142. VariableDeclaration: 'VariableDeclaration',
  143. VariableDeclarator: 'VariableDeclarator',
  144. WhileStatement: 'WhileStatement',
  145. WithStatement: 'WithStatement'
  146. };
  147. PropertyKind = {
  148. Data: 1,
  149. Get: 2,
  150. Set: 4
  151. };
  152. // Error messages should be identical to V8.
  153. Messages = {
  154. UnexpectedToken: 'Unexpected token %0',
  155. UnexpectedNumber: 'Unexpected number',
  156. UnexpectedString: 'Unexpected string',
  157. UnexpectedIdentifier: 'Unexpected identifier',
  158. UnexpectedReserved: 'Unexpected reserved word',
  159. UnexpectedEOS: 'Unexpected end of input',
  160. NewlineAfterThrow: 'Illegal newline after throw',
  161. InvalidRegExp: 'Invalid regular expression',
  162. UnterminatedRegExp: 'Invalid regular expression: missing /',
  163. InvalidLHSInAssignment: 'Invalid left-hand side in assignment',
  164. InvalidLHSInForIn: 'Invalid left-hand side in for-in',
  165. MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
  166. NoCatchOrFinally: 'Missing catch or finally after try',
  167. UnknownLabel: 'Undefined label \'%0\'',
  168. Redeclaration: '%0 \'%1\' has already been declared',
  169. IllegalContinue: 'Illegal continue statement',
  170. IllegalBreak: 'Illegal break statement',
  171. IllegalReturn: 'Illegal return statement',
  172. StrictModeWith: 'Strict mode code may not include a with statement',
  173. StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
  174. StrictVarName: 'Variable name may not be eval or arguments in strict mode',
  175. StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
  176. StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
  177. StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
  178. StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
  179. StrictDelete: 'Delete of an unqualified identifier in strict mode.',
  180. StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',
  181. AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',
  182. AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',
  183. StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
  184. StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
  185. StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
  186. StrictReservedWord: 'Use of future reserved word in strict mode'
  187. };
  188. // See also tools/generate-unicode-regex.py.
  189. Regex = {
  190. 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]'),
  191. 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]')
  192. };
  193. // Ensure the condition is true, otherwise throw an error.
  194. // This is only to have a better contract semantic, i.e. another safety net
  195. // to catch a logic error. The condition shall be fulfilled in normal case.
  196. // Do NOT use this to enforce a certain condition on any user input.
  197. function assert(condition, message) {
  198. /* istanbul ignore if */
  199. if (!condition) {
  200. throw new Error('ASSERT: ' + message);
  201. }
  202. }
  203. function isDecimalDigit(ch) {
  204. return (ch >= 48 && ch <= 57); // 0..9
  205. }
  206. function isHexDigit(ch) {
  207. return '0123456789abcdefABCDEF'.indexOf(ch) >= 0;
  208. }
  209. function isOctalDigit(ch) {
  210. return '01234567'.indexOf(ch) >= 0;
  211. }
  212. // 7.2 White Space
  213. function isWhiteSpace(ch) {
  214. return (ch === 0x20) || (ch === 0x09) || (ch === 0x0B) || (ch === 0x0C) || (ch === 0xA0) ||
  215. (ch >= 0x1680 && [0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x202F, 0x205F, 0x3000, 0xFEFF].indexOf(ch) >= 0);
  216. }
  217. // 7.3 Line Terminators
  218. function isLineTerminator(ch) {
  219. return (ch === 0x0A) || (ch === 0x0D) || (ch === 0x2028) || (ch === 0x2029);
  220. }
  221. // 7.6 Identifier Names and Identifiers
  222. function isIdentifierStart(ch) {
  223. return (ch == 0x40) || (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore)
  224. (ch >= 0x41 && ch <= 0x5A) || // A..Z
  225. (ch >= 0x61 && ch <= 0x7A) || // a..z
  226. (ch === 0x5C) || // \ (backslash)
  227. ((ch >= 0x80) && Regex.NonAsciiIdentifierStart.test(String.fromCharCode(ch)));
  228. }
  229. function isIdentifierPart(ch) {
  230. return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore)
  231. (ch >= 0x41 && ch <= 0x5A) || // A..Z
  232. (ch >= 0x61 && ch <= 0x7A) || // a..z
  233. (ch >= 0x30 && ch <= 0x39) || // 0..9
  234. (ch === 0x5C) || // \ (backslash)
  235. ((ch >= 0x80) && Regex.NonAsciiIdentifierPart.test(String.fromCharCode(ch)));
  236. }
  237. // 7.6.1.2 Future Reserved Words
  238. function isFutureReservedWord(id) {
  239. switch (id) {
  240. case 'class':
  241. case 'enum':
  242. case 'export':
  243. case 'extends':
  244. case 'import':
  245. case 'super':
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. function isStrictModeReservedWord(id) {
  252. switch (id) {
  253. case 'implements':
  254. case 'interface':
  255. case 'package':
  256. case 'private':
  257. case 'protected':
  258. case 'public':
  259. case 'static':
  260. case 'yield':
  261. case 'let':
  262. return true;
  263. default:
  264. return false;
  265. }
  266. }
  267. function isRestrictedWord(id) {
  268. return id === 'eval' || id === 'arguments';
  269. }
  270. // 7.6.1.1 Keywords
  271. function isKeyword(id) {
  272. if (strict && isStrictModeReservedWord(id)) {
  273. return true;
  274. }
  275. // 'const' is specialized as Keyword in V8.
  276. // 'yield' and 'let' are for compatiblity with SpiderMonkey and ES.next.
  277. // Some others are from future reserved words.
  278. switch (id.length) {
  279. case 2:
  280. return (id === 'if') || (id === 'in') || (id === 'do');
  281. case 3:
  282. return (id === 'var') || (id === 'for') || (id === 'new') ||
  283. (id === 'try') || (id === 'let');
  284. case 4:
  285. return (id === 'this') || (id === 'else') || (id === 'case') ||
  286. (id === 'void') || (id === 'with') || (id === 'enum');
  287. case 5:
  288. return (id === 'while') || (id === 'break') || (id === 'catch') ||
  289. (id === 'throw') || (id === 'const') || (id === 'yield') ||
  290. (id === 'class') || (id === 'super');
  291. case 6:
  292. return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
  293. (id === 'switch') || (id === 'export') || (id === 'import');
  294. case 7:
  295. return (id === 'default') || (id === 'finally') || (id === 'extends');
  296. case 8:
  297. return (id === 'function') || (id === 'continue') || (id === 'debugger');
  298. case 10:
  299. return (id === 'instanceof');
  300. default:
  301. return false;
  302. }
  303. }
  304. // 7.4 Comments
  305. function addComment(type, value, start, end, loc) {
  306. var comment, attacher;
  307. assert(typeof start === 'number', 'Comment must have valid position');
  308. // Because the way the actual token is scanned, often the comments
  309. // (if any) are skipped twice during the lexical analysis.
  310. // Thus, we need to skip adding a comment if the comment array already
  311. // handled it.
  312. if (state.lastCommentStart >= start) {
  313. return;
  314. }
  315. state.lastCommentStart = start;
  316. comment = {
  317. type: type,
  318. value: value
  319. };
  320. if (extra.range) {
  321. comment.range = [start, end];
  322. }
  323. if (extra.loc) {
  324. comment.loc = loc;
  325. }
  326. extra.comments.push(comment);
  327. if (extra.attachComment) {
  328. extra.leadingComments.push(comment);
  329. extra.trailingComments.push(comment);
  330. }
  331. }
  332. function skipSingleLineComment(offset) {
  333. var start, loc, ch, comment;
  334. start = index - offset;
  335. loc = {
  336. start: {
  337. line: lineNumber,
  338. column: index - lineStart - offset
  339. }
  340. };
  341. while (index < length) {
  342. ch = source.charCodeAt(index);
  343. ++index;
  344. if (isLineTerminator(ch)) {
  345. if (extra.comments) {
  346. comment = source.slice(start + offset, index - 1);
  347. loc.end = {
  348. line: lineNumber,
  349. column: index - lineStart - 1
  350. };
  351. addComment('Line', comment, start, index - 1, loc);
  352. }
  353. if (ch === 13 && source.charCodeAt(index) === 10) {
  354. ++index;
  355. }
  356. ++lineNumber;
  357. lineStart = index;
  358. return;
  359. }
  360. }
  361. if (extra.comments) {
  362. comment = source.slice(start + offset, index);
  363. loc.end = {
  364. line: lineNumber,
  365. column: index - lineStart
  366. };
  367. addComment('Line', comment, start, index, loc);
  368. }
  369. }
  370. function skipMultiLineComment() {
  371. var start, loc, ch, comment;
  372. if (extra.comments) {
  373. start = index - 2;
  374. loc = {
  375. start: {
  376. line: lineNumber,
  377. column: index - lineStart - 2
  378. }
  379. };
  380. }
  381. while (index < length) {
  382. ch = source.charCodeAt(index);
  383. if (isLineTerminator(ch)) {
  384. if (ch === 0x0D && source.charCodeAt(index + 1) === 0x0A) {
  385. ++index;
  386. }
  387. ++lineNumber;
  388. ++index;
  389. lineStart = index;
  390. if (index >= length) {
  391. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  392. }
  393. } else if (ch === 0x2A) {
  394. // Block comment ends with '*/'.
  395. if (source.charCodeAt(index + 1) === 0x2F) {
  396. ++index;
  397. ++index;
  398. if (extra.comments) {
  399. comment = source.slice(start + 2, index - 2);
  400. loc.end = {
  401. line: lineNumber,
  402. column: index - lineStart
  403. };
  404. addComment('Block', comment, start, index, loc);
  405. }
  406. return;
  407. }
  408. ++index;
  409. } else {
  410. ++index;
  411. }
  412. }
  413. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  414. }
  415. function skipComment() {
  416. var ch, start;
  417. start = (index === 0);
  418. while (index < length) {
  419. ch = source.charCodeAt(index);
  420. if (isWhiteSpace(ch)) {
  421. ++index;
  422. } else if (isLineTerminator(ch)) {
  423. ++index;
  424. if (ch === 0x0D && source.charCodeAt(index) === 0x0A) {
  425. ++index;
  426. }
  427. ++lineNumber;
  428. lineStart = index;
  429. start = true;
  430. } else if (ch === 0x2F) { // U+002F is '/'
  431. ch = source.charCodeAt(index + 1);
  432. if (ch === 0x2F) {
  433. ++index;
  434. ++index;
  435. skipSingleLineComment(2);
  436. start = true;
  437. } else if (ch === 0x2A) { // U+002A is '*'
  438. ++index;
  439. ++index;
  440. skipMultiLineComment();
  441. } else {
  442. break;
  443. }
  444. } else if (start && ch === 0x2D) { // U+002D is '-'
  445. // U+003E is '>'
  446. if ((source.charCodeAt(index + 1) === 0x2D) && (source.charCodeAt(index + 2) === 0x3E)) {
  447. // '-->' is a single-line comment
  448. index += 3;
  449. skipSingleLineComment(3);
  450. } else {
  451. break;
  452. }
  453. } else if (ch === 0x3C) { // U+003C is '<'
  454. if (source.slice(index + 1, index + 4) === '!--') {
  455. ++index; // `<`
  456. ++index; // `!`
  457. ++index; // `-`
  458. ++index; // `-`
  459. skipSingleLineComment(4);
  460. } else {
  461. break;
  462. }
  463. } else {
  464. break;
  465. }
  466. }
  467. }
  468. function scanHexEscape(prefix) {
  469. var i, len, ch, code = 0;
  470. len = (prefix === 'u') ? 4 : 2;
  471. for (i = 0; i < len; ++i) {
  472. if (index < length && isHexDigit(source[index])) {
  473. ch = source[index++];
  474. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  475. } else {
  476. return '';
  477. }
  478. }
  479. return String.fromCharCode(code);
  480. }
  481. function getEscapedIdentifier() {
  482. var ch, id;
  483. ch = source.charCodeAt(index++);
  484. id = String.fromCharCode(ch);
  485. // '\u' (U+005C, U+0075) denotes an escaped character.
  486. if (ch === 0x5C) {
  487. if (source.charCodeAt(index) !== 0x75) {
  488. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  489. }
  490. ++index;
  491. ch = scanHexEscape('u');
  492. if (!ch || ch === '\\' || !isIdentifierStart(ch.charCodeAt(0))) {
  493. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  494. }
  495. id = ch;
  496. }
  497. while (index < length) {
  498. ch = source.charCodeAt(index);
  499. if (!isIdentifierPart(ch)) {
  500. break;
  501. }
  502. ++index;
  503. id += String.fromCharCode(ch);
  504. // '\u' (U+005C, U+0075) denotes an escaped character.
  505. if (ch === 0x5C) {
  506. id = id.substr(0, id.length - 1);
  507. if (source.charCodeAt(index) !== 0x75) {
  508. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  509. }
  510. ++index;
  511. ch = scanHexEscape('u');
  512. if (!ch || ch === '\\' || !isIdentifierPart(ch.charCodeAt(0))) {
  513. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  514. }
  515. id += ch;
  516. }
  517. }
  518. return id;
  519. }
  520. function getIdentifier() {
  521. var start, ch;
  522. start = index++;
  523. while (index < length) {
  524. ch = source.charCodeAt(index);
  525. if (ch === 0x5C) {
  526. // Blackslash (U+005C) marks Unicode escape sequence.
  527. index = start;
  528. return getEscapedIdentifier();
  529. }
  530. if (isIdentifierPart(ch)) {
  531. ++index;
  532. } else {
  533. break;
  534. }
  535. }
  536. return source.slice(start, index);
  537. }
  538. function scanIdentifier() {
  539. var start, id, type;
  540. start = index;
  541. // Backslash (U+005C) starts an escaped character.
  542. id = (source.charCodeAt(index) === 0x5C) ? getEscapedIdentifier() : getIdentifier();
  543. // There is no keyword or literal with only one character.
  544. // Thus, it must be an identifier.
  545. if (id.length === 1) {
  546. type = Token.Identifier;
  547. } else if (isKeyword(id)) {
  548. type = Token.Keyword;
  549. } else if (id === 'null') {
  550. type = Token.NullLiteral;
  551. } else if (id === 'true' || id === 'false') {
  552. type = Token.BooleanLiteral;
  553. } else {
  554. type = Token.Identifier;
  555. }
  556. return {
  557. type: type,
  558. value: id,
  559. lineNumber: lineNumber,
  560. lineStart: lineStart,
  561. start: start,
  562. end: index
  563. };
  564. }
  565. // 7.7 Punctuators
  566. function scanPunctuator() {
  567. var start = index,
  568. code = source.charCodeAt(index),
  569. code2,
  570. ch1 = source[index],
  571. ch2,
  572. ch3,
  573. ch4;
  574. switch (code) {
  575. // Check for most common single-character punctuators.
  576. case 0x2E: // . dot
  577. case 0x28: // ( open bracket
  578. case 0x29: // ) close bracket
  579. case 0x3B: // ; semicolon
  580. case 0x2C: // , comma
  581. case 0x7B: // { open curly brace
  582. case 0x7D: // } close curly brace
  583. case 0x5B: // [
  584. case 0x5D: // ]
  585. case 0x3A: // :
  586. case 0x3F: // ?
  587. case 0x7E: // ~
  588. ++index;
  589. if (extra.tokenize) {
  590. if (code === 0x28) {
  591. extra.openParenToken = extra.tokens.length;
  592. } else if (code === 0x7B) {
  593. extra.openCurlyToken = extra.tokens.length;
  594. }
  595. }
  596. return {
  597. type: Token.Punctuator,
  598. value: String.fromCharCode(code),
  599. lineNumber: lineNumber,
  600. lineStart: lineStart,
  601. start: start,
  602. end: index
  603. };
  604. default:
  605. code2 = source.charCodeAt(index + 1);
  606. // '=' (U+003D) marks an assignment or comparison operator.
  607. if (code2 === 0x3D) {
  608. switch (code) {
  609. case 0x2B: // +
  610. case 0x2D: // -
  611. case 0x2F: // /
  612. case 0x3C: // <
  613. case 0x3E: // >
  614. case 0x5E: // ^
  615. case 0x7C: // |
  616. case 0x25: // %
  617. case 0x26: // &
  618. case 0x2A: // *
  619. index += 2;
  620. return {
  621. type: Token.Punctuator,
  622. value: String.fromCharCode(code) + String.fromCharCode(code2),
  623. lineNumber: lineNumber,
  624. lineStart: lineStart,
  625. start: start,
  626. end: index
  627. };
  628. case 0x21: // !
  629. case 0x3D: // =
  630. index += 2;
  631. // !== and ===
  632. if (source.charCodeAt(index) === 0x3D) {
  633. ++index;
  634. }
  635. return {
  636. type: Token.Punctuator,
  637. value: source.slice(start, index),
  638. lineNumber: lineNumber,
  639. lineStart: lineStart,
  640. start: start,
  641. end: index
  642. };
  643. }
  644. }
  645. }
  646. // 4-character punctuator: >>>=
  647. ch4 = source.substr(index, 4);
  648. if (ch4 === '>>>=') {
  649. index += 4;
  650. return {
  651. type: Token.Punctuator,
  652. value: ch4,
  653. lineNumber: lineNumber,
  654. lineStart: lineStart,
  655. start: start,
  656. end: index
  657. };
  658. }
  659. // 3-character punctuators: === !== >>> <<= >>=
  660. ch3 = ch4.substr(0, 3);
  661. if (ch3 === '>>>' || ch3 === '<<=' || ch3 === '>>=') {
  662. index += 3;
  663. return {
  664. type: Token.Punctuator,
  665. value: ch3,
  666. lineNumber: lineNumber,
  667. lineStart: lineStart,
  668. start: start,
  669. end: index
  670. };
  671. }
  672. // Other 2-character punctuators: ++ -- << >> && ||
  673. ch2 = ch3.substr(0, 2);
  674. if ((ch1 === ch2[1] && ('+-<>&|'.indexOf(ch1) >= 0)) || ch2 === '=>') {
  675. index += 2;
  676. return {
  677. type: Token.Punctuator,
  678. value: ch2,
  679. lineNumber: lineNumber,
  680. lineStart: lineStart,
  681. start: start,
  682. end: index
  683. };
  684. }
  685. // 1-character punctuators: < > = ! + - * % & | ^ /
  686. if ('<>=!+-*%&|^/'.indexOf(ch1) >= 0) {
  687. ++index;
  688. return {
  689. type: Token.Punctuator,
  690. value: ch1,
  691. lineNumber: lineNumber,
  692. lineStart: lineStart,
  693. start: start,
  694. end: index
  695. };
  696. }
  697. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  698. }
  699. // 7.8.3 Numeric Literals
  700. function scanHexLiteral(start) {
  701. var number = '';
  702. while (index < length) {
  703. if (!isHexDigit(source[index])) {
  704. break;
  705. }
  706. number += source[index++];
  707. }
  708. if (number.length === 0) {
  709. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  710. }
  711. if (isIdentifierStart(source.charCodeAt(index))) {
  712. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  713. }
  714. return {
  715. type: Token.NumericLiteral,
  716. value: parseInt('0x' + number, 16),
  717. lineNumber: lineNumber,
  718. lineStart: lineStart,
  719. start: start,
  720. end: index
  721. };
  722. }
  723. function scanOctalLiteral(start) {
  724. var number = '0' + source[index++];
  725. while (index < length) {
  726. if (!isOctalDigit(source[index])) {
  727. break;
  728. }
  729. number += source[index++];
  730. }
  731. if (isIdentifierStart(source.charCodeAt(index)) || isDecimalDigit(source.charCodeAt(index))) {
  732. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  733. }
  734. return {
  735. type: Token.NumericLiteral,
  736. value: parseInt(number, 8),
  737. octal: true,
  738. lineNumber: lineNumber,
  739. lineStart: lineStart,
  740. start: start,
  741. end: index
  742. };
  743. }
  744. function scanNumericLiteral() {
  745. var number, start, ch;
  746. ch = source[index];
  747. assert(isDecimalDigit(ch.charCodeAt(0)) || (ch === '.'),
  748. 'Numeric literal must start with a decimal digit or a decimal point');
  749. start = index;
  750. number = '';
  751. if (ch !== '.') {
  752. number = source[index++];
  753. ch = source[index];
  754. // Hex number starts with '0x'.
  755. // Octal number starts with '0'.
  756. if (number === '0') {
  757. if (ch === 'x' || ch === 'X') {
  758. ++index;
  759. return scanHexLiteral(start);
  760. }
  761. if (isOctalDigit(ch)) {
  762. return scanOctalLiteral(start);
  763. }
  764. // decimal number starts with '0' such as '09' is illegal.
  765. if (ch && isDecimalDigit(ch.charCodeAt(0))) {
  766. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  767. }
  768. }
  769. while (isDecimalDigit(source.charCodeAt(index))) {
  770. number += source[index++];
  771. }
  772. ch = source[index];
  773. }
  774. if (ch === '.') {
  775. number += source[index++];
  776. while (isDecimalDigit(source.charCodeAt(index))) {
  777. number += source[index++];
  778. }
  779. ch = source[index];
  780. }
  781. if (ch === 'e' || ch === 'E') {
  782. number += source[index++];
  783. ch = source[index];
  784. if (ch === '+' || ch === '-') {
  785. number += source[index++];
  786. }
  787. if (isDecimalDigit(source.charCodeAt(index))) {
  788. while (isDecimalDigit(source.charCodeAt(index))) {
  789. number += source[index++];
  790. }
  791. } else {
  792. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  793. }
  794. }
  795. if (isIdentifierStart(source.charCodeAt(index))) {
  796. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  797. }
  798. return {
  799. type: Token.NumericLiteral,
  800. value: parseFloat(number),
  801. lineNumber: lineNumber,
  802. lineStart: lineStart,
  803. start: start,
  804. end: index
  805. };
  806. }
  807. // 7.8.4 String Literals
  808. function scanStringLiteral() {
  809. var str = '', quote, start, ch, code, unescaped, restore, octal = false, startLineNumber, startLineStart;
  810. startLineNumber = lineNumber;
  811. startLineStart = lineStart;
  812. quote = source[index];
  813. assert((quote === '\'' || quote === '"'),
  814. 'String literal must starts with a quote');
  815. start = index;
  816. ++index;
  817. while (index < length) {
  818. ch = source[index++];
  819. if (ch === quote) {
  820. quote = '';
  821. break;
  822. } else if (ch === '\\') {
  823. ch = source[index++];
  824. if (!ch || !isLineTerminator(ch.charCodeAt(0))) {
  825. switch (ch) {
  826. case 'u':
  827. case 'x':
  828. restore = index;
  829. unescaped = scanHexEscape(ch);
  830. if (unescaped) {
  831. str += unescaped;
  832. } else {
  833. index = restore;
  834. str += ch;
  835. }
  836. break;
  837. case 'n':
  838. str += '\n';
  839. break;
  840. case 'r':
  841. str += '\r';
  842. break;
  843. case 't':
  844. str += '\t';
  845. break;
  846. case 'b':
  847. str += '\b';
  848. break;
  849. case 'f':
  850. str += '\f';
  851. break;
  852. case 'v':
  853. str += '\x0B';
  854. break;
  855. default:
  856. if (isOctalDigit(ch)) {
  857. code = '01234567'.indexOf(ch);
  858. // \0 is not octal escape sequence
  859. if (code !== 0) {
  860. octal = true;
  861. }
  862. if (index < length && isOctalDigit(source[index])) {
  863. octal = true;
  864. code = code * 8 + '01234567'.indexOf(source[index++]);
  865. // 3 digits are only allowed when string starts
  866. // with 0, 1, 2, 3
  867. if ('0123'.indexOf(ch) >= 0 &&
  868. index < length &&
  869. isOctalDigit(source[index])) {
  870. code = code * 8 + '01234567'.indexOf(source[index++]);
  871. }
  872. }
  873. str += String.fromCharCode(code);
  874. } else {
  875. str += ch;
  876. }
  877. break;
  878. }
  879. } else {
  880. ++lineNumber;
  881. if (ch === '\r' && source[index] === '\n') {
  882. ++index;
  883. }
  884. lineStart = index;
  885. }
  886. } else if (isLineTerminator(ch.charCodeAt(0))) {
  887. break;
  888. } else {
  889. str += ch;
  890. }
  891. }
  892. if (quote !== '') {
  893. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  894. }
  895. return {
  896. type: Token.StringLiteral,
  897. value: str,
  898. octal: octal,
  899. startLineNumber: startLineNumber,
  900. startLineStart: startLineStart,
  901. lineNumber: lineNumber,
  902. lineStart: lineStart,
  903. start: start,
  904. end: index
  905. };
  906. }
  907. function testRegExp(pattern, flags) {
  908. var value;
  909. try {
  910. value = new RegExp(pattern, flags);
  911. } catch (e) {
  912. throwError({}, Messages.InvalidRegExp);
  913. }
  914. return value;
  915. }
  916. function scanRegExpBody() {
  917. var ch, str, classMarker, terminated, body;
  918. ch = source[index];
  919. assert(ch === '/', 'Regular expression literal must start with a slash');
  920. str = source[index++];
  921. classMarker = false;
  922. terminated = false;
  923. while (index < length) {
  924. ch = source[index++];
  925. str += ch;
  926. if (ch === '\\') {
  927. ch = source[index++];
  928. // ECMA-262 7.8.5
  929. if (isLineTerminator(ch.charCodeAt(0))) {
  930. throwError({}, Messages.UnterminatedRegExp);
  931. }
  932. str += ch;
  933. } else if (isLineTerminator(ch.charCodeAt(0))) {
  934. throwError({}, Messages.UnterminatedRegExp);
  935. } else if (classMarker) {
  936. if (ch === ']') {
  937. classMarker = false;
  938. }
  939. } else {
  940. if (ch === '/') {
  941. terminated = true;
  942. break;
  943. } else if (ch === '[') {
  944. classMarker = true;
  945. }
  946. }
  947. }
  948. if (!terminated) {
  949. throwError({}, Messages.UnterminatedRegExp);
  950. }
  951. // Exclude leading and trailing slash.
  952. body = str.substr(1, str.length - 2);
  953. return {
  954. value: body,
  955. literal: str
  956. };
  957. }
  958. function scanRegExpFlags() {
  959. var ch, str, flags, restore;
  960. str = '';
  961. flags = '';
  962. while (index < length) {
  963. ch = source[index];
  964. if (!isIdentifierPart(ch.charCodeAt(0))) {
  965. break;
  966. }
  967. ++index;
  968. if (ch === '\\' && index < length) {
  969. ch = source[index];
  970. if (ch === 'u') {
  971. ++index;
  972. restore = index;
  973. ch = scanHexEscape('u');
  974. if (ch) {
  975. flags += ch;
  976. for (str += '\\u'; restore < index; ++restore) {
  977. str += source[restore];
  978. }
  979. } else {
  980. index = restore;
  981. flags += 'u';
  982. str += '\\u';
  983. }
  984. throwErrorTolerant({}, Messages.UnexpectedToken, 'ILLEGAL');
  985. } else {
  986. str += '\\';
  987. throwErrorTolerant({}, Messages.UnexpectedToken, 'ILLEGAL');
  988. }
  989. } else {
  990. flags += ch;
  991. str += ch;
  992. }
  993. }
  994. return {
  995. value: flags,
  996. literal: str
  997. };
  998. }
  999. function scanRegExp() {
  1000. var start, body, flags, pattern, value;
  1001. lookahead = null;
  1002. skipComment();
  1003. start = index;
  1004. body = scanRegExpBody();
  1005. flags = scanRegExpFlags();
  1006. value = testRegExp(body.value, flags.value);
  1007. if (extra.tokenize) {
  1008. return {
  1009. type: Token.RegularExpression,
  1010. value: value,
  1011. lineNumber: lineNumber,
  1012. lineStart: lineStart,
  1013. start: start,
  1014. end: index
  1015. };
  1016. }
  1017. return {
  1018. literal: body.literal + flags.literal,
  1019. value: value,
  1020. start: start,
  1021. end: index
  1022. };
  1023. }
  1024. function collectRegex() {
  1025. var pos, loc, regex, token;
  1026. skipComment();
  1027. pos = index;
  1028. loc = {
  1029. start: {
  1030. line: lineNumber,
  1031. column: index - lineStart
  1032. }
  1033. };
  1034. regex = scanRegExp();
  1035. loc.end = {
  1036. line: lineNumber,
  1037. column: index - lineStart
  1038. };
  1039. /* istanbul ignore next */
  1040. if (!extra.tokenize) {
  1041. // Pop the previous token, which is likely '/' or '/='
  1042. if (extra.tokens.length > 0) {
  1043. token = extra.tokens[extra.tokens.length - 1];
  1044. if (token.range[0] === pos && token.type === 'Punctuator') {
  1045. if (token.value === '/' || token.value === '/=') {
  1046. extra.tokens.pop();
  1047. }
  1048. }
  1049. }
  1050. extra.tokens.push({
  1051. type: 'RegularExpression',
  1052. value: regex.literal,
  1053. range: [pos, index],
  1054. loc: loc
  1055. });
  1056. }
  1057. return regex;
  1058. }
  1059. function isIdentifierName(token) {
  1060. return token.type === Token.Identifier ||
  1061. token.type === Token.Keyword ||
  1062. token.type === Token.BooleanLiteral ||
  1063. token.type === Token.NullLiteral;
  1064. }
  1065. function advanceSlash() {
  1066. var prevToken,
  1067. checkToken;
  1068. // Using the following algorithm:
  1069. // https://github.com/mozilla/sweet.js/wiki/design
  1070. prevToken = extra.tokens[extra.tokens.length - 1];
  1071. if (!prevToken) {
  1072. // Nothing before that: it cannot be a division.
  1073. return collectRegex();
  1074. }
  1075. if (prevToken.type === 'Punctuator') {
  1076. if (prevToken.value === ']') {
  1077. return scanPunctuator();
  1078. }
  1079. if (prevToken.value === ')') {
  1080. checkToken = extra.tokens[extra.openParenToken - 1];
  1081. if (checkToken &&
  1082. checkToken.type === 'Keyword' &&
  1083. (checkToken.value === 'if' ||
  1084. checkToken.value === 'while' ||
  1085. checkToken.value === 'for' ||
  1086. checkToken.value === 'with')) {
  1087. return collectRegex();
  1088. }
  1089. return scanPunctuator();
  1090. }
  1091. if (prevToken.value === '}') {
  1092. // Dividing a function by anything makes little sense,
  1093. // but we have to check for that.
  1094. if (extra.tokens[extra.openCurlyToken - 3] &&
  1095. extra.tokens[extra.openCurlyToken - 3].type === 'Keyword') {
  1096. // Anonymous function.
  1097. checkToken = extra.tokens[extra.openCurlyToken - 4];
  1098. if (!checkToken) {
  1099. return scanPunctuator();
  1100. }
  1101. } else if (extra.tokens[extra.openCurlyToken - 4] &&
  1102. extra.tokens[extra.openCurlyToken - 4].type === 'Keyword') {
  1103. // Named function.
  1104. checkToken = extra.tokens[extra.openCurlyToken - 5];
  1105. if (!checkToken) {
  1106. return collectRegex();
  1107. }
  1108. } else {
  1109. return scanPunctuator();
  1110. }
  1111. // checkToken determines whether the function is
  1112. // a declaration or an expression.
  1113. if (FnExprTokens.indexOf(checkToken.value) >= 0) {
  1114. // It is an expression.
  1115. return scanPunctuator();
  1116. }
  1117. // It is a declaration.
  1118. return collectRegex();
  1119. }
  1120. return collectRegex();
  1121. }
  1122. if (prevToken.type === 'Keyword') {
  1123. return collectRegex();
  1124. }
  1125. return scanPunctuator();
  1126. }
  1127. function advance() {
  1128. var ch;
  1129. skipComment();
  1130. if (index >= length) {
  1131. return {
  1132. type: Token.EOF,
  1133. lineNumber: lineNumber,
  1134. lineStart: lineStart,
  1135. start: index,
  1136. end: index
  1137. };
  1138. }
  1139. ch = source.charCodeAt(index);
  1140. if (isIdentifierStart(ch)) {
  1141. return scanIdentifier();
  1142. }
  1143. // Very common: ( and ) and ;
  1144. if (ch === 0x28 || ch === 0x29 || ch === 0x3B) {
  1145. return scanPunctuator();
  1146. }
  1147. // String literal starts with single quote (U+0027) or double quote (U+0022).
  1148. if (ch === 0x27 || ch === 0x22) {
  1149. return scanStringLiteral();
  1150. }
  1151. // Dot (.) U+002E can also start a floating-point number, hence the need
  1152. // to check the next character.
  1153. if (ch === 0x2E) {
  1154. if (isDecimalDigit(source.charCodeAt(index + 1))) {
  1155. return scanNumericLiteral();
  1156. }
  1157. return scanPunctuator();
  1158. }
  1159. if (isDecimalDigit(ch)) {
  1160. return scanNumericLiteral();
  1161. }
  1162. // Slash (/) U+002F can also start a regex.
  1163. if (extra.tokenize && ch === 0x2F) {
  1164. return advanceSlash();
  1165. }
  1166. return scanPunctuator();
  1167. }
  1168. function collectToken() {
  1169. var loc, token, range, value;
  1170. skipComment();
  1171. loc = {
  1172. start: {
  1173. line: lineNumber,
  1174. column: index - lineStart
  1175. }
  1176. };
  1177. token = advance();
  1178. loc.end = {
  1179. line: lineNumber,
  1180. column: index - lineStart
  1181. };
  1182. if (token.type !== Token.EOF) {
  1183. value = source.slice(token.start, token.end);
  1184. extra.tokens.push({
  1185. type: TokenName[token.type],
  1186. value: value,
  1187. range: [token.start, token.end],
  1188. loc: loc
  1189. });
  1190. }
  1191. return token;
  1192. }
  1193. function lex() {
  1194. var token;
  1195. token = lookahead;
  1196. index = token.end;
  1197. lineNumber = token.lineNumber;
  1198. lineStart = token.lineStart;
  1199. lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance();
  1200. index = token.end;
  1201. lineNumber = token.lineNumber;
  1202. lineStart = token.lineStart;
  1203. return token;
  1204. }
  1205. function peek() {
  1206. var pos, line, start;
  1207. pos = index;
  1208. line = lineNumber;
  1209. start = lineStart;
  1210. lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance();
  1211. index = pos;
  1212. lineNumber = line;
  1213. lineStart = start;
  1214. }
  1215. function Position(line, column) {
  1216. this.line = line;
  1217. this.column = column;
  1218. }
  1219. function SourceLocation(startLine, startColumn, line, column) {
  1220. this.start = new Position(startLine, startColumn);
  1221. this.end = new Position(line, column);
  1222. }
  1223. SyntaxTreeDelegate = {
  1224. name: 'SyntaxTree',
  1225. processComment: function (node) {
  1226. var lastChild, trailingComments;
  1227. if (node.type === Syntax.Program) {
  1228. if (node.body.length > 0) {
  1229. return;
  1230. }
  1231. }
  1232. if (extra.trailingComments.length > 0) {
  1233. if (extra.trailingComments[0].range[0] >= node.range[1]) {
  1234. trailingComments = extra.trailingComments;
  1235. extra.trailingComments = [];
  1236. } else {
  1237. extra.trailingComments.length = 0;
  1238. }
  1239. } else {
  1240. if (extra.bottomRightStack.length > 0 &&
  1241. extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments &&
  1242. extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments[0].range[0] >= node.range[1]) {
  1243. trailingComments = extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments;
  1244. delete extra.bottomRightStack[extra.bottomRightStack.length - 1].trailingComments;
  1245. }
  1246. }
  1247. // Eating the stack.
  1248. while (extra.bottomRightStack.length > 0 && extra.bottomRightStack[extra.bottomRightStack.length - 1].range[0] >= node.range[0]) {
  1249. lastChild = extra.bottomRightStack.pop();
  1250. }
  1251. if (lastChild) {
  1252. if (lastChild.leadingComments && lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= node.range[0]) {
  1253. node.leadingComments = lastChild.leadingComments;
  1254. delete lastChild.leadingComments;
  1255. }
  1256. } else if (extra.leadingComments.length > 0 && extra.leadingComments[extra.leadingComments.length - 1].range[1] <= node.range[0]) {
  1257. node.leadingComments = extra.leadingComments;
  1258. extra.leadingComments = [];
  1259. }
  1260. if (trailingComments) {
  1261. node.trailingComments = trailingComments;
  1262. }
  1263. extra.bottomRightStack.push(node);
  1264. },
  1265. markEnd: function (node, startToken) {
  1266. if (extra.range) {
  1267. node.range = [startToken.start, index];
  1268. }
  1269. if (extra.loc) {
  1270. node.loc = new SourceLocation(
  1271. startToken.startLineNumber === undefined ? startToken.lineNumber : startToken.startLineNumber,
  1272. startToken.start - (startToken.startLineStart === undefined ? startToken.lineStart : startToken.startLineStart),
  1273. lineNumber,
  1274. index - lineStart
  1275. );
  1276. this.postProcess(node);
  1277. }
  1278. if (extra.attachComment) {
  1279. this.processComment(node);
  1280. }
  1281. return node;
  1282. },
  1283. postProcess: function (node) {
  1284. if (extra.source) {
  1285. node.loc.source = extra.source;
  1286. }
  1287. return node;
  1288. },
  1289. createArrayExpression: function (elements) {
  1290. return {
  1291. type: Syntax.ArrayExpression,
  1292. elements: elements
  1293. };
  1294. },
  1295. createAssignmentExpression: function (operator, left, right) {
  1296. return {
  1297. type: Syntax.AssignmentExpression,
  1298. operator: operator,
  1299. left: left,
  1300. right: right
  1301. };
  1302. },
  1303. createBinaryExpression: function (operator, left, right) {
  1304. var type = (operator === '||' || operator === '&&') ? Syntax.LogicalExpression :
  1305. Syntax.BinaryExpression;
  1306. return {
  1307. type: type,
  1308. operator: operator,
  1309. left: left,
  1310. right: right
  1311. };
  1312. },
  1313. createBlockStatement: function (body) {
  1314. return {
  1315. type: Syntax.BlockStatement,
  1316. body: body
  1317. };
  1318. },
  1319. createBreakStatement: function (label) {
  1320. return {
  1321. type: Syntax.BreakStatement,
  1322. label: label
  1323. };
  1324. },
  1325. createCallExpression: function (callee, args) {
  1326. return {
  1327. type: Syntax.CallExpression,
  1328. callee: callee,
  1329. 'arguments': args
  1330. };
  1331. },
  1332. createCatchClause: function (param, body) {
  1333. return {
  1334. type: Syntax.CatchClause,
  1335. param: param,
  1336. body: body
  1337. };
  1338. },
  1339. createConditionalExpression: function (test, consequent, alternate) {
  1340. return {
  1341. type: Syntax.ConditionalExpression,
  1342. test: test,
  1343. consequent: consequent,
  1344. alternate: alternate
  1345. };
  1346. },
  1347. createContinueStatement: function (label) {
  1348. return {
  1349. type: Syntax.ContinueStatement,
  1350. label: label
  1351. };
  1352. },
  1353. createDebuggerStatement: function () {
  1354. return {
  1355. type: Syntax.DebuggerStatement
  1356. };
  1357. },
  1358. createDoWhileStatement: function (body, test) {
  1359. return {
  1360. type: Syntax.DoWhileStatement,
  1361. body: body,
  1362. test: test
  1363. };
  1364. },
  1365. createEmptyStatement: function () {
  1366. return {
  1367. type: Syntax.EmptyStatement
  1368. };
  1369. },
  1370. createExpressionStatement: function (expression) {
  1371. return {
  1372. type: Syntax.ExpressionStatement,
  1373. expression: expression
  1374. };
  1375. },
  1376. createForStatement: function (init, test, update, body) {
  1377. return {
  1378. type: Syntax.ForStatement,
  1379. init: init,
  1380. test: test,
  1381. update: update,
  1382. body: body
  1383. };
  1384. },
  1385. createForInStatement: function (left, right, body) {
  1386. return {
  1387. type: Syntax.ForInStatement,
  1388. left: left,
  1389. right: right,
  1390. body: body,
  1391. each: false
  1392. };
  1393. },
  1394. createFunctionDeclaration: function (id, params, defaults, body) {
  1395. return {
  1396. type: Syntax.FunctionDeclaration,
  1397. id: id,
  1398. params: params,
  1399. defaults: defaults,
  1400. body: body,
  1401. rest: null,
  1402. generator: false,
  1403. expression: false
  1404. };
  1405. },
  1406. createFunctionExpression: function (id, params, defaults, body) {
  1407. return {
  1408. type: Syntax.FunctionExpression,
  1409. id: id,
  1410. params: params,
  1411. defaults: defaults,
  1412. body: body,
  1413. rest: null,
  1414. generator: false,
  1415. expression: false
  1416. };
  1417. },
  1418. createIdentifier: function (name) {
  1419. return {
  1420. type: Syntax.Identifier,
  1421. name: name
  1422. };
  1423. },
  1424. createIfStatement: function (test, consequent, alternate) {
  1425. return {
  1426. type: Syntax.IfStatement,
  1427. test: test,
  1428. consequent: consequent,
  1429. alternate: alternate
  1430. };
  1431. },
  1432. createLabeledStatement: function (label, body) {
  1433. return {
  1434. type: Syntax.LabeledStatement,
  1435. label: label,
  1436. body: body
  1437. };
  1438. },
  1439. createLiteral: function (token) {
  1440. return {
  1441. type: Syntax.Literal,
  1442. value: token.value,
  1443. raw: source.slice(token.start, token.end)
  1444. };
  1445. },
  1446. createMemberExpression: function (accessor, object, property) {
  1447. return {
  1448. type: Syntax.MemberExpression,
  1449. computed: accessor === '[',
  1450. object: object,
  1451. property: property
  1452. };
  1453. },
  1454. createNewExpression: function (callee, args) {
  1455. return {
  1456. type: Syntax.NewExpression,
  1457. callee: callee,
  1458. 'arguments': args
  1459. };
  1460. },
  1461. createObjectExpression: function (properties) {
  1462. return {
  1463. type: Syntax.ObjectExpression,
  1464. properties: properties
  1465. };
  1466. },
  1467. createPostfixExpression: function (operator, argument) {
  1468. return {
  1469. type: Syntax.UpdateExpression,
  1470. operator: operator,
  1471. argument: argument,
  1472. prefix: false
  1473. };
  1474. },
  1475. createProgram: function (body) {
  1476. return {
  1477. type: Syntax.Program,
  1478. body: body
  1479. };
  1480. },
  1481. createProperty: function (kind, key, value) {
  1482. return {
  1483. type: Syntax.Property,
  1484. key: key,
  1485. value: value,
  1486. kind: kind
  1487. };
  1488. },
  1489. createReturnStatement: function (argument) {
  1490. return {
  1491. type: Syntax.ReturnStatement,
  1492. argument: argument
  1493. };
  1494. },
  1495. createSequenceExpression: function (expressions) {
  1496. return {
  1497. type: Syntax.SequenceExpression,
  1498. expressions: expressions
  1499. };
  1500. },
  1501. createSwitchCase: function (test, consequent) {
  1502. return {
  1503. type: Syntax.SwitchCase,
  1504. test: test,
  1505. consequent: consequent
  1506. };
  1507. },
  1508. createSwitchStatement: function (discriminant, cases) {
  1509. return {
  1510. type: Syntax.SwitchStatement,
  1511. discriminant: discriminant,
  1512. cases: cases
  1513. };
  1514. },
  1515. createThisExpression: function () {
  1516. return {
  1517. type: Syntax.ThisExpression
  1518. };
  1519. },
  1520. createThrowStatement: function (argument) {
  1521. return {
  1522. type: Syntax.ThrowStatement,
  1523. argument: argument
  1524. };
  1525. },
  1526. createTryStatement: function (block, guardedHandlers, handlers, finalizer) {
  1527. return {
  1528. type: Syntax.TryStatement,
  1529. block: block,
  1530. guardedHandlers: guardedHandlers,
  1531. handlers: handlers,
  1532. finalizer: finalizer
  1533. };
  1534. },
  1535. createUnaryExpression: function (operator, argument) {
  1536. if (operator === '++' || operator === '--') {
  1537. return {
  1538. type: Syntax.UpdateExpression,
  1539. operator: operator,
  1540. argument: argument,
  1541. prefix: true
  1542. };
  1543. }
  1544. return {
  1545. type: Syntax.UnaryExpression,
  1546. operator: operator,
  1547. argument: argument,
  1548. prefix: true
  1549. };
  1550. },
  1551. createVariableDeclaration: function (declarations, kind) {
  1552. return {
  1553. type: Syntax.VariableDeclaration,
  1554. declarations: declarations,
  1555. kind: kind
  1556. };
  1557. },
  1558. createVariableDeclarator: function (id, init) {
  1559. return {
  1560. type: Syntax.VariableDeclarator,
  1561. id: id,
  1562. init: init
  1563. };
  1564. },
  1565. createWhileStatement: function (test, body) {
  1566. return {
  1567. type: Syntax.WhileStatement,
  1568. test: test,
  1569. body: body
  1570. };
  1571. },
  1572. createWithStatement: function (object, body) {
  1573. return {
  1574. type: Syntax.WithStatement,
  1575. object: object,
  1576. body: body
  1577. };
  1578. }
  1579. };
  1580. // Return true if there is a line terminator before the next token.
  1581. function peekLineTerminator() {
  1582. var pos, line, start, found;
  1583. pos = index;
  1584. line = lineNumber;
  1585. start = lineStart;
  1586. skipComment();
  1587. found = lineNumber !== line;
  1588. index = pos;
  1589. lineNumber = line;
  1590. lineStart = start;
  1591. return found;
  1592. }
  1593. // Throw an exception
  1594. function throwError(token, messageFormat) {
  1595. var error,
  1596. args = Array.prototype.slice.call(arguments, 2),
  1597. msg = messageFormat.replace(
  1598. /%(\d)/g,
  1599. function (whole, index) {
  1600. assert(index < args.length, 'Message reference must be in range');
  1601. return args[index];
  1602. }
  1603. );
  1604. if (typeof token.lineNumber === 'number') {
  1605. error = new Error('Line ' + token.lineNumber + ': ' + msg);
  1606. error.index = token.start;
  1607. error.lineNumber = token.lineNumber;
  1608. error.column = token.start - lineStart + 1;
  1609. } else {
  1610. error = new Error('Line ' + lineNumber + ': ' + msg);
  1611. error.index = index;
  1612. error.lineNumber = lineNumber;
  1613. error.column = index - lineStart + 1;
  1614. }
  1615. error.description = msg;
  1616. throw error;
  1617. }
  1618. function throwErrorTolerant() {
  1619. try {
  1620. throwError.apply(null, arguments);
  1621. } catch (e) {
  1622. if (extra.errors) {
  1623. extra.errors.push(e);
  1624. } else {
  1625. throw e;
  1626. }
  1627. }
  1628. }
  1629. // Throw an exception because of the token.
  1630. function throwUnexpected(token) {
  1631. if (token.type === Token.EOF) {
  1632. throwError(token, Messages.UnexpectedEOS);
  1633. }
  1634. if (token.type === Token.NumericLiteral) {
  1635. throwError(token, Messages.UnexpectedNumber);
  1636. }
  1637. if (token.type === Token.StringLiteral) {
  1638. throwError(token, Messages.UnexpectedString);
  1639. }
  1640. if (token.type === Token.Identifier) {
  1641. throwError(token, Messages.UnexpectedIdentifier);
  1642. }
  1643. if (token.type === Token.Keyword) {
  1644. if (isFutureReservedWord(token.value)) {
  1645. throwError(token, Messages.UnexpectedReserved);
  1646. } else if (strict && isStrictModeReservedWord(token.value)) {
  1647. throwErrorTolerant(token, Messages.StrictReservedWord);
  1648. return;
  1649. }
  1650. throwError(token, Messages.UnexpectedToken, token.value);
  1651. }
  1652. // BooleanLiteral, NullLiteral, or Punctuator.
  1653. throwError(token, Messages.UnexpectedToken, token.value);
  1654. }
  1655. // Expect the next token to match the specified punctuator.
  1656. // If not, an exception will be thrown.
  1657. function expect(value) {
  1658. var token = lex();
  1659. if (token.type !== Token.Punctuator || token.value !== value) {
  1660. throwUnexpected(token);
  1661. }
  1662. }
  1663. // Expect the next token to match the specified keyword.
  1664. // If not, an exception will be thrown.
  1665. function expectKeyword(keyword) {
  1666. var token = lex();
  1667. if (token.type !== Token.Keyword || token.value !== keyword) {
  1668. throwUnexpected(token);
  1669. }
  1670. }
  1671. // Return true if the next token matches the specified punctuator.
  1672. function match(value) {
  1673. return lookahead.type === Token.Punctuator && lookahead.value === value;
  1674. }
  1675. // Return true if the next token matches the specified keyword
  1676. function matchKeyword(keyword) {
  1677. return lookahead.type === Token.Keyword && lookahead.value === keyword;
  1678. }
  1679. // Return true if the next token is an assignment operator
  1680. function matchAssign() {
  1681. var op;
  1682. if (lookahead.type !== Token.Punctuator) {
  1683. return false;
  1684. }
  1685. op = lookahead.value;
  1686. return op === '=' ||
  1687. op === '*=' ||
  1688. op === '/=' ||
  1689. op === '%=' ||
  1690. op === '+=' ||
  1691. op === '-=' ||
  1692. op === '<<=' ||
  1693. op === '>>=' ||
  1694. op === '>>>=' ||
  1695. op === '&=' ||
  1696. op === '^=' ||
  1697. op === '|=';
  1698. }
  1699. function consumeSemicolon() {
  1700. var line;
  1701. // Catch the very common case first: immediately a semicolon (U+003B).
  1702. if (source.charCodeAt(index) === 0x3B || match(';')) {
  1703. lex();
  1704. return;
  1705. }
  1706. line = lineNumber;
  1707. skipComment();
  1708. if (lineNumber !== line) {
  1709. return;
  1710. }
  1711. if (lookahead.type !== Token.EOF && !match('}')) {
  1712. throwUnexpected(lookahead);
  1713. }
  1714. }
  1715. // Return true if provided expression is LeftHandSideExpression
  1716. function isLeftHandSide(expr) {
  1717. return expr.type === Syntax.Identifier || expr.type === Syntax.MemberExpression;
  1718. }
  1719. // 11.1.4 Array Initialiser
  1720. function parseArrayInitialiser() {
  1721. var elements = [], startToken;
  1722. startToken = lookahead;
  1723. expect('[');
  1724. while (!match(']')) {
  1725. if (match(',')) {
  1726. lex();
  1727. elements.push(null);
  1728. } else {
  1729. elements.push(parseAssignmentExpression());
  1730. if (!match(']')) {
  1731. expect(',');
  1732. }
  1733. }
  1734. }
  1735. lex();
  1736. return delegate.markEnd(delegate.createArrayExpression(elements), startToken);
  1737. }
  1738. // 11.1.5 Object Initialiser
  1739. function parsePropertyFunction(param, first) {
  1740. var previousStrict, body, startToken;
  1741. previousStrict = strict;
  1742. startToken = lookahead;
  1743. body = parseFunctionSourceElements();
  1744. if (first && strict && isRestrictedWord(param[0].name)) {
  1745. throwErrorTolerant(first, Messages.StrictParamName);
  1746. }
  1747. strict = previousStrict;
  1748. return delegate.markEnd(delegate.createFunctionExpression(null, param, [], body), startToken);
  1749. }
  1750. function parseObjectPropertyKey() {
  1751. var token, startToken;
  1752. startToken = lookahead;
  1753. token = lex();
  1754. // Note: This function is called only from parseObjectProperty(), where
  1755. // EOF and Punctuator tokens are already filtered out.
  1756. if (token.type === Token.StringLiteral || token.type === Token.NumericLiteral) {
  1757. if (strict && token.octal) {
  1758. throwErrorTolerant(token, Messages.StrictOctalLiteral);
  1759. }
  1760. return delegate.markEnd(delegate.createLiteral(token), startToken);
  1761. }
  1762. return delegate.markEnd(delegate.createIdentifier(token.value), startToken);
  1763. }
  1764. function parseObjectProperty() {
  1765. var token, key, id, value, param, startToken;
  1766. token = lookahead;
  1767. startToken = lookahead;
  1768. if (token.type === Token.Identifier) {
  1769. id = parseObjectPropertyKey();
  1770. // Property Assignment: Getter and Setter.
  1771. if (token.value === 'get' && !match(':')) {
  1772. key = parseObjectPropertyKey();
  1773. expect('(');
  1774. expect(')');
  1775. value = parsePropertyFunction([]);
  1776. return delegate.markEnd(delegate.createProperty('get', key, value), startToken);
  1777. }
  1778. if (token.value === 'set' && !match(':')) {
  1779. key = parseObjectPropertyKey();
  1780. expect('(');
  1781. token = lookahead;
  1782. if (token.type !== Token.Identifier) {
  1783. expect(')');
  1784. throwErrorTolerant(token, Messages.UnexpectedToken, token.value);
  1785. value = parsePropertyFunction([]);
  1786. } else {
  1787. param = [ parseVariableIdentifier() ];
  1788. expect(')');
  1789. value = parsePropertyFunction(param, token);
  1790. }
  1791. return delegate.markEnd(delegate.createProperty('set', key, value), startToken);
  1792. }
  1793. expect(':');
  1794. value = parseAssignmentExpression();
  1795. return delegate.markEnd(delegate.createProperty('init', id, value), startToken);
  1796. }
  1797. if (token.type === Token.EOF || token.type === Token.Punctuator) {
  1798. throwUnexpected(token);
  1799. } else {
  1800. key = parseObjectPropertyKey();
  1801. expect(':');
  1802. value = parseAssignmentExpression();
  1803. return delegate.markEnd(delegate.createProperty('init', key, value), startToken);
  1804. }
  1805. }
  1806. function parseObjectInitialiser() {
  1807. var properties = [], property, name, key, kind, map = {}, toString = String, startToken;
  1808. startToken = lookahead;
  1809. expect('{');
  1810. while (!match('}')) {
  1811. property = parseObjectProperty();
  1812. if (property.key.type === Syntax.Identifier) {
  1813. name = property.key.name;
  1814. } else {
  1815. name = toString(property.key.value);
  1816. }
  1817. kind = (property.kind === 'init') ? PropertyKind.Data : (property.kind === 'get') ? PropertyKind.Get : PropertyKind.Set;
  1818. key = '$' + name;
  1819. if (Object.prototype.hasOwnProperty.call(map, key)) {
  1820. if (map[key] === PropertyKind.Data) {
  1821. if (strict && kind === PropertyKind.Data) {
  1822. throwErrorTolerant({}, Messages.StrictDuplicateProperty);
  1823. } else if (kind !== PropertyKind.Data) {
  1824. throwErrorTolerant({}, Messages.AccessorDataProperty);
  1825. }
  1826. } else {
  1827. if (kind === PropertyKind.Data) {
  1828. throwErrorTolerant({}, Messages.AccessorDataProperty);
  1829. } else if (map[key] & kind) {
  1830. throwErrorTolerant({}, Messages.AccessorGetSet);
  1831. }
  1832. }
  1833. map[key] |= kind;
  1834. } else {
  1835. map[key] = kind;
  1836. }
  1837. properties.push(property);
  1838. if (!match('}')) {
  1839. expect(',');
  1840. }
  1841. }
  1842. expect('}');
  1843. return delegate.markEnd(delegate.createObjectExpression(properties), startToken);
  1844. }
  1845. // 11.1.6 The Grouping Operator
  1846. function parseGroupExpression() {
  1847. var expr;
  1848. expect('(');
  1849. expr = parseExpression();
  1850. expect(')');
  1851. return expr;
  1852. }
  1853. // 11.1 Primary Expressions
  1854. function parsePrimaryExpression() {
  1855. var type, token, expr, startToken;
  1856. if (match('(')) {
  1857. return parseGroupExpression();
  1858. }
  1859. if (match('[')) {
  1860. return parseArrayInitialiser();
  1861. }
  1862. if (match('{')) {
  1863. return parseObjectInitialiser();
  1864. }
  1865. type = lookahead.type;
  1866. startToken = lookahead;
  1867. if (type === Token.Identifier) {
  1868. expr = delegate.createIdentifier(lex().value);
  1869. } else if (type === Token.StringLiteral || type === Token.NumericLiteral) {
  1870. if (strict && lookahead.octal) {
  1871. throwErrorTolerant(lookahead, Messages.StrictOctalLiteral);
  1872. }
  1873. expr = delegate.createLiteral(lex());
  1874. } else if (type === Token.Keyword) {
  1875. if (matchKeyword('function')) {
  1876. return parseFunctionExpression();
  1877. }
  1878. if (matchKeyword('this')) {
  1879. lex();
  1880. expr = delegate.createThisExpression();
  1881. } else {
  1882. throwUnexpected(lex());
  1883. }
  1884. } else if (type === Token.BooleanLiteral) {
  1885. token = lex();
  1886. token.value = (token.value === 'true');
  1887. expr = delegate.createLiteral(token);
  1888. } else if (type === Token.NullLiteral) {
  1889. token = lex();
  1890. token.value = null;
  1891. expr = delegate.createLiteral(token);
  1892. } else if (match('/') || match('/=')) {
  1893. if (typeof extra.tokens !== 'undefined') {
  1894. expr = delegate.createLiteral(collectRegex());
  1895. } else {
  1896. expr = delegate.createLiteral(scanRegExp());
  1897. }
  1898. peek();
  1899. } else {
  1900. throwUnexpected(lex());
  1901. }
  1902. return delegate.markEnd(expr, startToken);
  1903. }
  1904. // 11.2 Left-Hand-Side Expressions
  1905. function parseArguments() {
  1906. var args = [];
  1907. expect('(');
  1908. if (!match(')')) {
  1909. while (index < length) {
  1910. args.push(parseAssignmentExpression());
  1911. if (match(')')) {
  1912. break;
  1913. }
  1914. expect(',');
  1915. }
  1916. }
  1917. expect(')');
  1918. return args;
  1919. }
  1920. function parseNonComputedProperty() {
  1921. var token, startToken;
  1922. startToken = lookahead;
  1923. token = lex();
  1924. if (!isIdentifierName(token)) {
  1925. throwUnexpected(token);
  1926. }
  1927. return delegate.markEnd(delegate.createIdentifier(token.value), startToken);
  1928. }
  1929. function parseNonComputedMember() {
  1930. expect('.');
  1931. return parseNonComputedProperty();
  1932. }
  1933. function parseComputedMember() {
  1934. var expr;
  1935. expect('[');
  1936. expr = parseExpression();
  1937. expect(']');
  1938. return expr;
  1939. }
  1940. function parseNewExpression() {
  1941. var callee, args, startToken;
  1942. startToken = lookahead;
  1943. expectKeyword('new');
  1944. callee = parseLeftHandSideExpression();
  1945. args = match('(') ? parseArguments() : [];
  1946. return delegate.markEnd(delegate.createNewExpression(callee, args), startToken);
  1947. }
  1948. function parseLeftHandSideExpressionAllowCall() {
  1949. var previousAllowIn, expr, args, property, startToken;
  1950. startToken = lookahead;
  1951. previousAllowIn = state.allowIn;
  1952. state.allowIn = true;
  1953. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  1954. state.allowIn = previousAllowIn;
  1955. for (;;) {
  1956. if (match('.')) {
  1957. property = parseNonComputedMember();
  1958. expr = delegate.createMemberExpression('.', expr, property);
  1959. } else if (match('(')) {
  1960. args = parseArguments();
  1961. expr = delegate.createCallExpression(expr, args);
  1962. } else if (match('[')) {
  1963. property = parseComputedMember();
  1964. expr = delegate.createMemberExpression('[', expr, property);
  1965. } else {
  1966. break;
  1967. }
  1968. delegate.markEnd(expr, startToken);
  1969. }
  1970. return expr;
  1971. }
  1972. function parseLeftHandSideExpression() {
  1973. var previousAllowIn, expr, property, startToken;
  1974. startToken = lookahead;
  1975. previousAllowIn = state.allowIn;
  1976. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  1977. state.allowIn = previousAllowIn;
  1978. while (match('.') || match('[')) {
  1979. if (match('[')) {
  1980. property = parseComputedMember();
  1981. expr = delegate.createMemberExpression('[', expr, property);
  1982. } else {
  1983. property = parseNonComputedMember();
  1984. expr = delegate.createMemberExpression('.', expr, property);
  1985. }
  1986. delegate.markEnd(expr, startToken);
  1987. }
  1988. return expr;
  1989. }
  1990. // 11.3 Postfix Expressions
  1991. function parsePostfixExpression() {
  1992. var expr, token, startToken = lookahead;
  1993. expr = parseLeftHandSideExpressionAllowCall();
  1994. if (lookahead.type === Token.Punctuator) {
  1995. if ((match('++') || match('--')) && !peekLineTerminator()) {
  1996. // 11.3.1, 11.3.2
  1997. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  1998. throwErrorTolerant({}, Messages.StrictLHSPostfix);
  1999. }
  2000. if (!isLeftHandSide(expr)) {
  2001. throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
  2002. }
  2003. token = lex();
  2004. expr = delegate.markEnd(delegate.createPostfixExpression(token.value, expr), startToken);
  2005. }
  2006. }
  2007. return expr;
  2008. }
  2009. // 11.4 Unary Operators
  2010. function parseUnaryExpression() {
  2011. var token, expr, startToken;
  2012. if (lookahead.type !== Token.Punctuator && lookahead.type !== Token.Keyword) {
  2013. expr = parsePostfixExpression();
  2014. } else if (match('++') || match('--')) {
  2015. startToken = lookahead;
  2016. token = lex();
  2017. expr = parseUnaryExpression();
  2018. // 11.4.4, 11.4.5
  2019. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2020. throwErrorTolerant({}, Messages.StrictLHSPrefix);
  2021. }
  2022. if (!isLeftHandSide(expr)) {
  2023. throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
  2024. }
  2025. expr = delegate.createUnaryExpression(token.value, expr);
  2026. expr = delegate.markEnd(expr, startToken);
  2027. } else if (match('+') || match('-') || match('~') || match('!')) {
  2028. startToken = lookahead;
  2029. token = lex();
  2030. expr = parseUnaryExpression();
  2031. expr = delegate.createUnaryExpression(token.value, expr);
  2032. expr = delegate.markEnd(expr, startToken);
  2033. } else if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) {
  2034. startToken = lookahead;
  2035. token = lex();
  2036. expr = parseUnaryExpression();
  2037. expr = delegate.createUnaryExpression(token.value, expr);
  2038. expr = delegate.markEnd(expr, startToken);
  2039. if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) {
  2040. throwErrorTolerant({}, Messages.StrictDelete);
  2041. }
  2042. } else {
  2043. expr = parsePostfixExpression();
  2044. }
  2045. return expr;
  2046. }
  2047. function binaryPrecedence(token, allowIn) {
  2048. var prec = 0;
  2049. if (token.type !== Token.Punctuator && token.type !== Token.Keyword) {
  2050. return 0;
  2051. }
  2052. switch (token.value) {
  2053. case '||':
  2054. prec = 1;
  2055. break;
  2056. case '&&':
  2057. prec = 2;
  2058. break;
  2059. case '|':
  2060. prec = 3;
  2061. break;
  2062. case '^':
  2063. prec = 4;
  2064. break;
  2065. case '&':
  2066. prec = 5;
  2067. break;
  2068. case '==':
  2069. case '!=':
  2070. case '===':
  2071. case '!==':
  2072. prec = 6;
  2073. break;
  2074. case '<':
  2075. case '>':
  2076. case '<=':
  2077. case '>=':
  2078. case 'instanceof':
  2079. prec = 7;
  2080. break;
  2081. case 'in':
  2082. prec = allowIn ? 7 : 0;
  2083. break;
  2084. case '<<':
  2085. case '>>':
  2086. case '>>>':
  2087. prec = 8;
  2088. break;
  2089. case '+':
  2090. case '-':
  2091. prec = 9;
  2092. break;
  2093. case '*':
  2094. case '/':
  2095. case '%':
  2096. prec = 11;
  2097. break;
  2098. default:
  2099. break;
  2100. }
  2101. return prec;
  2102. }
  2103. // 11.5 Multiplicative Operators
  2104. // 11.6 Additive Operators
  2105. // 11.7 Bitwise Shift Operators
  2106. // 11.8 Relational Operators
  2107. // 11.9 Equality Operators
  2108. // 11.10 Binary Bitwise Operators
  2109. // 11.11 Binary Logical Operators
  2110. function parseBinaryExpression() {
  2111. var marker, markers, expr, token, prec, stack, right, operator, left, i;
  2112. marker = lookahead;
  2113. left = parseUnaryExpression();
  2114. token = lookahead;
  2115. prec = binaryPrecedence(token, state.allowIn);
  2116. if (prec === 0) {
  2117. return left;
  2118. }
  2119. token.prec = prec;
  2120. lex();
  2121. markers = [marker, lookahead];
  2122. right = parseUnaryExpression();
  2123. stack = [left, token, right];
  2124. while ((prec = binaryPrecedence(lookahead, state.allowIn)) > 0) {
  2125. // Reduce: make a binary expression from the three topmost entries.
  2126. while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) {
  2127. right = stack.pop();
  2128. operator = stack.pop().value;
  2129. left = stack.pop();
  2130. expr = delegate.createBinaryExpression(operator, left, right);
  2131. markers.pop();
  2132. marker = markers[markers.length - 1];
  2133. delegate.markEnd(expr, marker);
  2134. stack.push(expr);
  2135. }
  2136. // Shift.
  2137. token = lex();
  2138. token.prec = prec;
  2139. stack.push(token);
  2140. markers.push(lookahead);
  2141. expr = parseUnaryExpression();
  2142. stack.push(expr);
  2143. }
  2144. // Final reduce to clean-up the stack.
  2145. i = stack.length - 1;
  2146. expr = stack[i];
  2147. markers.pop();
  2148. while (i > 1) {
  2149. expr = delegate.createBinaryExpression(stack[i - 1].value, stack[i - 2], expr);
  2150. i -= 2;
  2151. marker = markers.pop();
  2152. delegate.markEnd(expr, marker);
  2153. }
  2154. return expr;
  2155. }
  2156. // 11.12 Conditional Operator
  2157. function parseConditionalExpression() {
  2158. var expr, previousAllowIn, consequent, alternate, startToken;
  2159. startToken = lookahead;
  2160. expr = parseBinaryExpression();
  2161. if (match('?')) {
  2162. lex();
  2163. previousAllowIn = state.allowIn;
  2164. state.allowIn = true;
  2165. consequent = parseAssignmentExpression();
  2166. state.allowIn = previousAllowIn;
  2167. expect(':');
  2168. alternate = parseAssignmentExpression();
  2169. expr = delegate.createConditionalExpression(expr, consequent, alternate);
  2170. delegate.markEnd(expr, startToken);
  2171. }
  2172. return expr;
  2173. }
  2174. // 11.13 Assignment Operators
  2175. function parseAssignmentExpression() {
  2176. var token, left, right, node, startToken;
  2177. token = lookahead;
  2178. startToken = lookahead;
  2179. node = left = parseConditionalExpression();
  2180. if (matchAssign()) {
  2181. // LeftHandSideExpression
  2182. if (!isLeftHandSide(left)) {
  2183. throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
  2184. }
  2185. // 11.13.1
  2186. if (strict && left.type === Syntax.Identifier && isRestrictedWord(left.name)) {
  2187. throwErrorTolerant(token, Messages.StrictLHSAssignment);
  2188. }
  2189. token = lex();
  2190. right = parseAssignmentExpression();
  2191. node = delegate.markEnd(delegate.createAssignmentExpression(token.value, left, right), startToken);
  2192. }
  2193. return node;
  2194. }
  2195. // 11.14 Comma Operator
  2196. function parseExpression() {
  2197. var expr, startToken = lookahead;
  2198. expr = parseAssignmentExpression();
  2199. if (match(',')) {
  2200. expr = delegate.createSequenceExpression([ expr ]);
  2201. while (index < length) {
  2202. if (!match(',')) {
  2203. break;
  2204. }
  2205. lex();
  2206. expr.expressions.push(parseAssignmentExpression());
  2207. }
  2208. delegate.markEnd(expr, startToken);
  2209. }
  2210. return expr;
  2211. }
  2212. // 12.1 Block
  2213. function parseStatementList() {
  2214. var list = [],
  2215. statement;
  2216. while (index < length) {
  2217. if (match('}')) {
  2218. break;
  2219. }
  2220. statement = parseSourceElement();
  2221. if (typeof statement === 'undefined') {
  2222. break;
  2223. }
  2224. list.push(statement);
  2225. }
  2226. return list;
  2227. }
  2228. function parseBlock() {
  2229. var block, startToken;
  2230. startToken = lookahead;
  2231. expect('{');
  2232. block = parseStatementList();
  2233. expect('}');
  2234. return delegate.markEnd(delegate.createBlockStatement(block), startToken);
  2235. }
  2236. // 12.2 Variable Statement
  2237. function parseVariableIdentifier() {
  2238. var token, startToken;
  2239. startToken = lookahead;
  2240. token = lex();
  2241. if (token.type !== Token.Identifier) {
  2242. throwUnexpected(token);
  2243. }
  2244. return delegate.markEnd(delegate.createIdentifier(token.value), startToken);
  2245. }
  2246. function parseVariableDeclaration(kind) {
  2247. var init = null, id, startToken;
  2248. startToken = lookahead;
  2249. id = parseVariableIdentifier();
  2250. // 12.2.1
  2251. if (strict && isRestrictedWord(id.name)) {
  2252. throwErrorTolerant({}, Messages.StrictVarName);
  2253. }
  2254. if (kind === 'const') {
  2255. expect('=');
  2256. init = parseAssignmentExpression();
  2257. } else if (match('=')) {
  2258. lex();
  2259. init = parseAssignmentExpression();
  2260. }
  2261. return delegate.markEnd(delegate.createVariableDeclarator(id, init), startToken);
  2262. }
  2263. function parseVariableDeclarationList(kind) {
  2264. var list = [];
  2265. do {
  2266. list.push(parseVariableDeclaration(kind));
  2267. if (!match(',')) {
  2268. break;
  2269. }
  2270. lex();
  2271. } while (index < length);
  2272. return list;
  2273. }
  2274. function parseVariableStatement() {
  2275. var declarations;
  2276. expectKeyword('var');
  2277. declarations = parseVariableDeclarationList();
  2278. consumeSemicolon();
  2279. return delegate.createVariableDeclaration(declarations, 'var');
  2280. }
  2281. // kind may be `const` or `let`
  2282. // Both are experimental and not in the specification yet.
  2283. // see http://wiki.ecmascript.org/doku.php?id=harmony:const
  2284. // and http://wiki.ecmascript.org/doku.php?id=harmony:let
  2285. function parseConstLetDeclaration(kind) {
  2286. var declarations, startToken;
  2287. startToken = lookahead;
  2288. expectKeyword(kind);
  2289. declarations = parseVariableDeclarationList(kind);
  2290. consumeSemicolon();
  2291. return delegate.markEnd(delegate.createVariableDeclaration(declarations, kind), startToken);
  2292. }
  2293. // 12.3 Empty Statement
  2294. function parseEmptyStatement() {
  2295. expect(';');
  2296. return delegate.createEmptyStatement();
  2297. }
  2298. // 12.4 Expression Statement
  2299. function parseExpressionStatement() {
  2300. var expr = parseExpression();
  2301. consumeSemicolon();
  2302. return delegate.createExpressionStatement(expr);
  2303. }
  2304. // 12.5 If statement
  2305. function parseIfStatement() {
  2306. var test, consequent, alternate;
  2307. expectKeyword('if');
  2308. expect('(');
  2309. test = parseExpression();
  2310. expect(')');
  2311. consequent = parseStatement();
  2312. if (matchKeyword('else')) {
  2313. lex();
  2314. alternate = parseStatement();
  2315. } else {
  2316. alternate = null;
  2317. }
  2318. return delegate.createIfStatement(test, consequent, alternate);
  2319. }
  2320. // 12.6 Iteration Statements
  2321. function parseDoWhileStatement() {
  2322. var body, test, oldInIteration;
  2323. expectKeyword('do');
  2324. oldInIteration = state.inIteration;
  2325. state.inIteration = true;
  2326. body = parseStatement();
  2327. state.inIteration = oldInIteration;
  2328. expectKeyword('while');
  2329. expect('(');
  2330. test = parseExpression();
  2331. expect(')');
  2332. if (match(';')) {
  2333. lex();
  2334. }
  2335. return delegate.createDoWhileStatement(body, test);
  2336. }
  2337. function parseWhileStatement() {
  2338. var test, body, oldInIteration;
  2339. expectKeyword('while');
  2340. expect('(');
  2341. test = parseExpression();
  2342. expect(')');
  2343. oldInIteration = state.inIteration;
  2344. state.inIteration = true;
  2345. body = parseStatement();
  2346. state.inIteration = oldInIteration;
  2347. return delegate.createWhileStatement(test, body);
  2348. }
  2349. function parseForVariableDeclaration() {
  2350. var token, declarations, startToken;
  2351. startToken = lookahead;
  2352. token = lex();
  2353. declarations = parseVariableDeclarationList();
  2354. return delegate.markEnd(delegate.createVariableDeclaration(declarations, token.value), startToken);
  2355. }
  2356. function parseForStatement() {
  2357. var init, test, update, left, right, body, oldInIteration;
  2358. init = test = update = null;
  2359. expectKeyword('for');
  2360. expect('(');
  2361. if (match(';')) {
  2362. lex();
  2363. } else {
  2364. if (matchKeyword('var') || matchKeyword('let')) {
  2365. state.allowIn = false;
  2366. init = parseForVariableDeclaration();
  2367. state.allowIn = true;
  2368. if (init.declarations.length === 1 && matchKeyword('in')) {
  2369. lex();
  2370. left = init;
  2371. right = parseExpression();
  2372. init = null;
  2373. }
  2374. } else {
  2375. state.allowIn = false;
  2376. init = parseExpression();
  2377. state.allowIn = true;
  2378. if (matchKeyword('in')) {
  2379. // LeftHandSideExpression
  2380. if (!isLeftHandSide(init)) {
  2381. throwErrorTolerant({}, Messages.InvalidLHSInForIn);
  2382. }
  2383. lex();
  2384. left = init;
  2385. right = parseExpression();
  2386. init = null;
  2387. }
  2388. }
  2389. if (typeof left === 'undefined') {
  2390. expect(';');
  2391. }
  2392. }
  2393. if (typeof left === 'undefined') {
  2394. if (!match(';')) {
  2395. test = parseExpression();
  2396. }
  2397. expect(';');
  2398. if (!match(')')) {
  2399. update = parseExpression();
  2400. }
  2401. }
  2402. expect(')');
  2403. oldInIteration = state.inIteration;
  2404. state.inIteration = true;
  2405. body = parseStatement();
  2406. state.inIteration = oldInIteration;
  2407. return (typeof left === 'undefined') ?
  2408. delegate.createForStatement(init, test, update, body) :
  2409. delegate.createForInStatement(left, right, body);
  2410. }
  2411. // 12.7 The continue statement
  2412. function parseContinueStatement() {
  2413. var label = null, key;
  2414. expectKeyword('continue');
  2415. // Optimize the most common form: 'continue;'.
  2416. if (source.charCodeAt(index) === 0x3B) {
  2417. lex();
  2418. if (!state.inIteration) {
  2419. throwError({}, Messages.IllegalContinue);
  2420. }
  2421. return delegate.createContinueStatement(null);
  2422. }
  2423. if (peekLineTerminator()) {
  2424. if (!state.inIteration) {
  2425. throwError({}, Messages.IllegalContinue);
  2426. }
  2427. return delegate.createContinueStatement(null);
  2428. }
  2429. if (lookahead.type === Token.Identifier) {
  2430. label = parseVariableIdentifier();
  2431. key = '$' + label.name;
  2432. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  2433. throwError({}, Messages.UnknownLabel, label.name);
  2434. }
  2435. }
  2436. consumeSemicolon();
  2437. if (label === null && !state.inIteration) {
  2438. throwError({}, Messages.IllegalContinue);
  2439. }
  2440. return delegate.createContinueStatement(label);
  2441. }
  2442. // 12.8 The break statement
  2443. function parseBreakStatement() {
  2444. var label = null, key;
  2445. expectKeyword('break');
  2446. // Catch the very common case first: immediately a semicolon (U+003B).
  2447. if (source.charCodeAt(index) === 0x3B) {
  2448. lex();
  2449. if (!(state.inIteration || state.inSwitch)) {
  2450. throwError({}, Messages.IllegalBreak);
  2451. }
  2452. return delegate.createBreakStatement(null);
  2453. }
  2454. if (peekLineTerminator()) {
  2455. if (!(state.inIteration || state.inSwitch)) {
  2456. throwError({}, Messages.IllegalBreak);
  2457. }
  2458. return delegate.createBreakStatement(null);
  2459. }
  2460. if (lookahead.type === Token.Identifier) {
  2461. label = parseVariableIdentifier();
  2462. key = '$' + label.name;
  2463. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  2464. throwError({}, Messages.UnknownLabel, label.name);
  2465. }
  2466. }
  2467. consumeSemicolon();
  2468. if (label === null && !(state.inIteration || state.inSwitch)) {
  2469. throwError({}, Messages.IllegalBreak);
  2470. }
  2471. return delegate.createBreakStatement(label);
  2472. }
  2473. // 12.9 The return statement
  2474. function parseReturnStatement() {
  2475. var argument = null;
  2476. expectKeyword('return');
  2477. if (!state.inFunctionBody) {
  2478. throwErrorTolerant({}, Messages.IllegalReturn);
  2479. }
  2480. // 'return' followed by a space and an identifier is very common.
  2481. if (source.charCodeAt(index) === 0x20) {
  2482. if (isIdentifierStart(source.charCodeAt(index + 1))) {
  2483. argument = parseExpression();
  2484. consumeSemicolon();
  2485. return delegate.createReturnStatement(argument);
  2486. }
  2487. }
  2488. if (peekLineTerminator()) {
  2489. return delegate.createReturnStatement(null);
  2490. }
  2491. if (!match(';')) {
  2492. if (!match('}') && lookahead.type !== Token.EOF) {
  2493. argument = parseExpression();
  2494. }
  2495. }
  2496. consumeSemicolon();
  2497. return delegate.createReturnStatement(argument);
  2498. }
  2499. // 12.10 The with statement
  2500. function parseWithStatement() {
  2501. var object, body;
  2502. if (strict) {
  2503. // TODO(ikarienator): Should we update the test cases instead?
  2504. skipComment();
  2505. throwErrorTolerant({}, Messages.StrictModeWith);
  2506. }
  2507. expectKeyword('with');
  2508. expect('(');
  2509. object = parseExpression();
  2510. expect(')');
  2511. body = parseStatement();
  2512. return delegate.createWithStatement(object, body);
  2513. }
  2514. // 12.10 The swith statement
  2515. function parseSwitchCase() {
  2516. var test, consequent = [], statement, startToken;
  2517. startToken = lookahead;
  2518. if (matchKeyword('default')) {
  2519. lex();
  2520. test = null;
  2521. } else {
  2522. expectKeyword('case');
  2523. test = parseExpression();
  2524. }
  2525. expect(':');
  2526. while (index < length) {
  2527. if (match('}') || matchKeyword('default') || matchKeyword('case')) {
  2528. break;
  2529. }
  2530. statement = parseStatement();
  2531. consequent.push(statement);
  2532. }
  2533. return delegate.markEnd(delegate.createSwitchCase(test, consequent), startToken);
  2534. }
  2535. function parseSwitchStatement() {
  2536. var discriminant, cases, clause, oldInSwitch, defaultFound;
  2537. expectKeyword('switch');
  2538. expect('(');
  2539. discriminant = parseExpression();
  2540. expect(')');
  2541. expect('{');
  2542. cases = [];
  2543. if (match('}')) {
  2544. lex();
  2545. return delegate.createSwitchStatement(discriminant, cases);
  2546. }
  2547. oldInSwitch = state.inSwitch;
  2548. state.inSwitch = true;
  2549. defaultFound = false;
  2550. while (index < length) {
  2551. if (match('}')) {
  2552. break;
  2553. }
  2554. clause = parseSwitchCase();
  2555. if (clause.test === null) {
  2556. if (defaultFound) {
  2557. throwError({}, Messages.MultipleDefaultsInSwitch);
  2558. }
  2559. defaultFound = true;
  2560. }
  2561. cases.push(clause);
  2562. }
  2563. state.inSwitch = oldInSwitch;
  2564. expect('}');
  2565. return delegate.createSwitchStatement(discriminant, cases);
  2566. }
  2567. // 12.13 The throw statement
  2568. function parseThrowStatement() {
  2569. var argument;
  2570. expectKeyword('throw');
  2571. if (peekLineTerminator()) {
  2572. throwError({}, Messages.NewlineAfterThrow);
  2573. }
  2574. argument = parseExpression();
  2575. consumeSemicolon();
  2576. return delegate.createThrowStatement(argument);
  2577. }
  2578. // 12.14 The try statement
  2579. function parseCatchClause() {
  2580. var param, body, startToken;
  2581. startToken = lookahead;
  2582. expectKeyword('catch');
  2583. expect('(');
  2584. if (match(')')) {
  2585. throwUnexpected(lookahead);
  2586. }
  2587. param = parseVariableIdentifier();
  2588. // 12.14.1
  2589. if (strict && isRestrictedWord(param.name)) {
  2590. throwErrorTolerant({}, Messages.StrictCatchVariable);
  2591. }
  2592. expect(')');
  2593. body = parseBlock();
  2594. return delegate.markEnd(delegate.createCatchClause(param, body), startToken);
  2595. }
  2596. function parseTryStatement() {
  2597. var block, handlers = [], finalizer = null;
  2598. expectKeyword('try');
  2599. block = parseBlock();
  2600. if (matchKeyword('catch')) {
  2601. handlers.push(parseCatchClause());
  2602. }
  2603. if (matchKeyword('finally')) {
  2604. lex();
  2605. finalizer = parseBlock();
  2606. }
  2607. if (handlers.length === 0 && !finalizer) {
  2608. throwError({}, Messages.NoCatchOrFinally);
  2609. }
  2610. return delegate.createTryStatement(block, [], handlers, finalizer);
  2611. }
  2612. // 12.15 The debugger statement
  2613. function parseDebuggerStatement() {
  2614. expectKeyword('debugger');
  2615. consumeSemicolon();
  2616. return delegate.createDebuggerStatement();
  2617. }
  2618. // 12 Statements
  2619. function parseStatement() {
  2620. var type = lookahead.type,
  2621. expr,
  2622. labeledBody,
  2623. key,
  2624. startToken;
  2625. if (type === Token.EOF) {
  2626. throwUnexpected(lookahead);
  2627. }
  2628. if (type === Token.Punctuator && lookahead.value === '{') {
  2629. return parseBlock();
  2630. }
  2631. startToken = lookahead;
  2632. if (type === Token.Punctuator) {
  2633. switch (lookahead.value) {
  2634. case ';':
  2635. return delegate.markEnd(parseEmptyStatement(), startToken);
  2636. case '(':
  2637. return delegate.markEnd(parseExpressionStatement(), startToken);
  2638. default:
  2639. break;
  2640. }
  2641. }
  2642. if (type === Token.Keyword) {
  2643. switch (lookahead.value) {
  2644. case 'break':
  2645. return delegate.markEnd(parseBreakStatement(), startToken);
  2646. case 'continue':
  2647. return delegate.markEnd(parseContinueStatement(), startToken);
  2648. case 'debugger':
  2649. return delegate.markEnd(parseDebuggerStatement(), startToken);
  2650. case 'do':
  2651. return delegate.markEnd(parseDoWhileStatement(), startToken);
  2652. case 'for':
  2653. return delegate.markEnd(parseForStatement(), startToken);
  2654. case 'function':
  2655. return delegate.markEnd(parseFunctionDeclaration(), startToken);
  2656. case 'if':
  2657. return delegate.markEnd(parseIfStatement(), startToken);
  2658. case 'return':
  2659. return delegate.markEnd(parseReturnStatement(), startToken);
  2660. case 'switch':
  2661. return delegate.markEnd(parseSwitchStatement(), startToken);
  2662. case 'throw':
  2663. return delegate.markEnd(parseThrowStatement(), startToken);
  2664. case 'try':
  2665. return delegate.markEnd(parseTryStatement(), startToken);
  2666. case 'var':
  2667. return delegate.markEnd(parseVariableStatement(), startToken);
  2668. case 'while':
  2669. return delegate.markEnd(parseWhileStatement(), startToken);
  2670. case 'with':
  2671. return delegate.markEnd(parseWithStatement(), startToken);
  2672. default:
  2673. break;
  2674. }
  2675. }
  2676. expr = parseExpression();
  2677. // 12.12 Labelled Statements
  2678. if ((expr.type === Syntax.Identifier) && match(':')) {
  2679. lex();
  2680. key = '$' + expr.name;
  2681. if (Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  2682. throwError({}, Messages.Redeclaration, 'Label', expr.name);
  2683. }
  2684. state.labelSet[key] = true;
  2685. labeledBody = parseStatement();
  2686. delete state.labelSet[key];
  2687. return delegate.markEnd(delegate.createLabeledStatement(expr, labeledBody), startToken);
  2688. }
  2689. consumeSemicolon();
  2690. return delegate.markEnd(delegate.createExpressionStatement(expr), startToken);
  2691. }
  2692. // 13 Function Definition
  2693. function parseFunctionSourceElements() {
  2694. var sourceElement, sourceElements = [], token, directive, firstRestricted,
  2695. oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody, startToken;
  2696. startToken = lookahead;
  2697. expect('{');
  2698. while (index < length) {
  2699. if (lookahead.type !== Token.StringLiteral) {
  2700. break;
  2701. }
  2702. token = lookahead;
  2703. sourceElement = parseSourceElement();
  2704. sourceElements.push(sourceElement);
  2705. if (sourceElement.expression.type !== Syntax.Literal) {
  2706. // this is not directive
  2707. break;
  2708. }
  2709. directive = source.slice(token.start + 1, token.end - 1);
  2710. if (directive === 'use strict') {
  2711. strict = true;
  2712. if (firstRestricted) {
  2713. throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  2714. }
  2715. } else {
  2716. if (!firstRestricted && token.octal) {
  2717. firstRestricted = token;
  2718. }
  2719. }
  2720. }
  2721. oldLabelSet = state.labelSet;
  2722. oldInIteration = state.inIteration;
  2723. oldInSwitch = state.inSwitch;
  2724. oldInFunctionBody = state.inFunctionBody;
  2725. state.labelSet = {};
  2726. state.inIteration = false;
  2727. state.inSwitch = false;
  2728. state.inFunctionBody = true;
  2729. while (index < length) {
  2730. if (match('}')) {
  2731. break;
  2732. }
  2733. sourceElement = parseSourceElement();
  2734. if (typeof sourceElement === 'undefined') {
  2735. break;
  2736. }
  2737. sourceElements.push(sourceElement);
  2738. }
  2739. expect('}');
  2740. state.labelSet = oldLabelSet;
  2741. state.inIteration = oldInIteration;
  2742. state.inSwitch = oldInSwitch;
  2743. state.inFunctionBody = oldInFunctionBody;
  2744. return delegate.markEnd(delegate.createBlockStatement(sourceElements), startToken);
  2745. }
  2746. function parseParams(firstRestricted) {
  2747. var param, params = [], token, stricted, paramSet, key, message;
  2748. expect('(');
  2749. if (!match(')')) {
  2750. paramSet = {};
  2751. while (index < length) {
  2752. token = lookahead;
  2753. param = parseVariableIdentifier();
  2754. key = '$' + token.value;
  2755. if (strict) {
  2756. if (isRestrictedWord(token.value)) {
  2757. stricted = token;
  2758. message = Messages.StrictParamName;
  2759. }
  2760. if (Object.prototype.hasOwnProperty.call(paramSet, key)) {
  2761. stricted = token;
  2762. message = Messages.StrictParamDupe;
  2763. }
  2764. } else if (!firstRestricted) {
  2765. if (isRestrictedWord(token.value)) {
  2766. firstRestricted = token;
  2767. message = Messages.StrictParamName;
  2768. } else if (isStrictModeReservedWord(token.value)) {
  2769. firstRestricted = token;
  2770. message = Messages.StrictReservedWord;
  2771. } else if (Object.prototype.hasOwnProperty.call(paramSet, key)) {
  2772. firstRestricted = token;
  2773. message = Messages.StrictParamDupe;
  2774. }
  2775. }
  2776. params.push(param);
  2777. paramSet[key] = true;
  2778. if (match(')')) {
  2779. break;
  2780. }
  2781. expect(',');
  2782. }
  2783. }
  2784. expect(')');
  2785. return {
  2786. params: params,
  2787. stricted: stricted,
  2788. firstRestricted: firstRestricted,
  2789. message: message
  2790. };
  2791. }
  2792. function parseFunctionDeclaration() {
  2793. var id, params = [], body, token, stricted, tmp, firstRestricted, message, previousStrict, startToken;
  2794. startToken = lookahead;
  2795. expectKeyword('function');
  2796. token = lookahead;
  2797. id = parseVariableIdentifier();
  2798. if (strict) {
  2799. if (isRestrictedWord(token.value)) {
  2800. throwErrorTolerant(token, Messages.StrictFunctionName);
  2801. }
  2802. } else {
  2803. if (isRestrictedWord(token.value)) {
  2804. firstRestricted = token;
  2805. message = Messages.StrictFunctionName;
  2806. } else if (isStrictModeReservedWord(token.value)) {
  2807. firstRestricted = token;
  2808. message = Messages.StrictReservedWord;
  2809. }
  2810. }
  2811. tmp = parseParams(firstRestricted);
  2812. params = tmp.params;
  2813. stricted = tmp.stricted;
  2814. firstRestricted = tmp.firstRestricted;
  2815. if (tmp.message) {
  2816. message = tmp.message;
  2817. }
  2818. previousStrict = strict;
  2819. body = parseFunctionSourceElements();
  2820. if (strict && firstRestricted) {
  2821. throwError(firstRestricted, message);
  2822. }
  2823. if (strict && stricted) {
  2824. throwErrorTolerant(stricted, message);
  2825. }
  2826. strict = previousStrict;
  2827. return delegate.markEnd(delegate.createFunctionDeclaration(id, params, [], body), startToken);
  2828. }
  2829. function parseFunctionExpression() {
  2830. var token, id = null, stricted, firstRestricted, message, tmp, params = [], body, previousStrict, startToken;
  2831. startToken = lookahead;
  2832. expectKeyword('function');
  2833. if (!match('(')) {
  2834. token = lookahead;
  2835. id = parseVariableIdentifier();
  2836. if (strict) {
  2837. if (isRestrictedWord(token.value)) {
  2838. throwErrorTolerant(token, Messages.StrictFunctionName);
  2839. }
  2840. } else {
  2841. if (isRestrictedWord(token.value)) {
  2842. firstRestricted = token;
  2843. message = Messages.StrictFunctionName;
  2844. } else if (isStrictModeReservedWord(token.value)) {
  2845. firstRestricted = token;
  2846. message = Messages.StrictReservedWord;
  2847. }
  2848. }
  2849. }
  2850. tmp = parseParams(firstRestricted);
  2851. params = tmp.params;
  2852. stricted = tmp.stricted;
  2853. firstRestricted = tmp.firstRestricted;
  2854. if (tmp.message) {
  2855. message = tmp.message;
  2856. }
  2857. previousStrict = strict;
  2858. body = parseFunctionSourceElements();
  2859. if (strict && firstRestricted) {
  2860. throwError(firstRestricted, message);
  2861. }
  2862. if (strict && stricted) {
  2863. throwErrorTolerant(stricted, message);
  2864. }
  2865. strict = previousStrict;
  2866. return delegate.markEnd(delegate.createFunctionExpression(id, params, [], body), startToken);
  2867. }
  2868. // 14 Program
  2869. function parseSourceElement() {
  2870. if (lookahead.type === Token.Keyword) {
  2871. switch (lookahead.value) {
  2872. case 'const':
  2873. case 'let':
  2874. return parseConstLetDeclaration(lookahead.value);
  2875. case 'function':
  2876. return parseFunctionDeclaration();
  2877. default:
  2878. return parseStatement();
  2879. }
  2880. }
  2881. if (lookahead.type !== Token.EOF) {
  2882. return parseStatement();
  2883. }
  2884. }
  2885. function parseSourceElements() {
  2886. var sourceElement, sourceElements = [], token, directive, firstRestricted;
  2887. while (index < length) {
  2888. token = lookahead;
  2889. if (token.type !== Token.StringLiteral) {
  2890. break;
  2891. }
  2892. sourceElement = parseSourceElement();
  2893. sourceElements.push(sourceElement);
  2894. if (sourceElement.expression.type !== Syntax.Literal) {
  2895. // this is not directive
  2896. break;
  2897. }
  2898. directive = source.slice(token.start + 1, token.end - 1);
  2899. if (directive === 'use strict') {
  2900. strict = true;
  2901. if (firstRestricted) {
  2902. throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  2903. }
  2904. } else {
  2905. if (!firstRestricted && token.octal) {
  2906. firstRestricted = token;
  2907. }
  2908. }
  2909. }
  2910. while (index < length) {
  2911. sourceElement = parseSourceElement();
  2912. /* istanbul ignore if */
  2913. if (typeof sourceElement === 'undefined') {
  2914. break;
  2915. }
  2916. sourceElements.push(sourceElement);
  2917. }
  2918. return sourceElements;
  2919. }
  2920. function parseProgram() {
  2921. var body, startToken;
  2922. skipComment();
  2923. peek();
  2924. startToken = lookahead;
  2925. strict = false;
  2926. body = parseSourceElements();
  2927. return delegate.markEnd(delegate.createProgram(body), startToken);
  2928. }
  2929. function filterTokenLocation() {
  2930. var i, entry, token, tokens = [];
  2931. for (i = 0; i < extra.tokens.length; ++i) {
  2932. entry = extra.tokens[i];
  2933. token = {
  2934. type: entry.type,
  2935. value: entry.value
  2936. };
  2937. if (extra.range) {
  2938. token.range = entry.range;
  2939. }
  2940. if (extra.loc) {
  2941. token.loc = entry.loc;
  2942. }
  2943. tokens.push(token);
  2944. }
  2945. extra.tokens = tokens;
  2946. }
  2947. function tokenize(code, options) {
  2948. var toString,
  2949. token,
  2950. tokens;
  2951. toString = String;
  2952. if (typeof code !== 'string' && !(code instanceof String)) {
  2953. code = toString(code);
  2954. }
  2955. delegate = SyntaxTreeDelegate;
  2956. source = code;
  2957. index = 0;
  2958. lineNumber = (source.length > 0) ? 1 : 0;
  2959. lineStart = 0;
  2960. length = source.length;
  2961. lookahead = null;
  2962. state = {
  2963. allowIn: true,
  2964. labelSet: {},
  2965. inFunctionBody: false,
  2966. inIteration: false,
  2967. inSwitch: false,
  2968. lastCommentStart: -1
  2969. };
  2970. extra = {};
  2971. // Options matching.
  2972. options = options || {};
  2973. // Of course we collect tokens here.
  2974. options.tokens = true;
  2975. extra.tokens = [];
  2976. extra.tokenize = true;
  2977. // The following two fields are necessary to compute the Regex tokens.
  2978. extra.openParenToken = -1;
  2979. extra.openCurlyToken = -1;
  2980. extra.range = (typeof options.range === 'boolean') && options.range;
  2981. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  2982. if (typeof options.comment === 'boolean' && options.comment) {
  2983. extra.comments = [];
  2984. }
  2985. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  2986. extra.errors = [];
  2987. }
  2988. try {
  2989. peek();
  2990. if (lookahead.type === Token.EOF) {
  2991. return extra.tokens;
  2992. }
  2993. token = lex();
  2994. while (lookahead.type !== Token.EOF) {
  2995. try {
  2996. token = lex();
  2997. } catch (lexError) {
  2998. token = lookahead;
  2999. if (extra.errors) {
  3000. extra.errors.push(lexError);
  3001. // We have to break on the first error
  3002. // to avoid infinite loops.
  3003. break;
  3004. } else {
  3005. throw lexError;
  3006. }
  3007. }
  3008. }
  3009. filterTokenLocation();
  3010. tokens = extra.tokens;
  3011. if (typeof extra.comments !== 'undefined') {
  3012. tokens.comments = extra.comments;
  3013. }
  3014. if (typeof extra.errors !== 'undefined') {
  3015. tokens.errors = extra.errors;
  3016. }
  3017. } catch (e) {
  3018. throw e;
  3019. } finally {
  3020. extra = {};
  3021. }
  3022. return tokens;
  3023. }
  3024. function parse(code, options) {
  3025. var program, toString;
  3026. toString = String;
  3027. if (typeof code !== 'string' && !(code instanceof String)) {
  3028. code = toString(code);
  3029. }
  3030. delegate = SyntaxTreeDelegate;
  3031. source = code;
  3032. index = 0;
  3033. lineNumber = (source.length > 0) ? 1 : 0;
  3034. lineStart = 0;
  3035. length = source.length;
  3036. lookahead = null;
  3037. state = {
  3038. allowIn: true,
  3039. labelSet: {},
  3040. inFunctionBody: false,
  3041. inIteration: false,
  3042. inSwitch: false,
  3043. lastCommentStart: -1
  3044. };
  3045. extra = {};
  3046. if (typeof options !== 'undefined') {
  3047. extra.range = (typeof options.range === 'boolean') && options.range;
  3048. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  3049. extra.attachComment = (typeof options.attachComment === 'boolean') && options.attachComment;
  3050. if (extra.loc && options.source !== null && options.source !== undefined) {
  3051. extra.source = toString(options.source);
  3052. }
  3053. if (typeof options.tokens === 'boolean' && options.tokens) {
  3054. extra.tokens = [];
  3055. }
  3056. if (typeof options.comment === 'boolean' && options.comment) {
  3057. extra.comments = [];
  3058. }
  3059. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  3060. extra.errors = [];
  3061. }
  3062. if (extra.attachComment) {
  3063. extra.range = true;
  3064. extra.comments = [];
  3065. extra.bottomRightStack = [];
  3066. extra.trailingComments = [];
  3067. extra.leadingComments = [];
  3068. }
  3069. }
  3070. try {
  3071. program = parseProgram();
  3072. if (typeof extra.comments !== 'undefined') {
  3073. program.comments = extra.comments;
  3074. }
  3075. if (typeof extra.tokens !== 'undefined') {
  3076. filterTokenLocation();
  3077. program.tokens = extra.tokens;
  3078. }
  3079. if (typeof extra.errors !== 'undefined') {
  3080. program.errors = extra.errors;
  3081. }
  3082. } catch (e) {
  3083. throw e;
  3084. } finally {
  3085. extra = {};
  3086. }
  3087. return program;
  3088. }
  3089. // Sync with *.json manifests.
  3090. exports.version = '1.2.2';
  3091. exports.tokenize = tokenize;
  3092. exports.parse = parse;
  3093. // Deep copy.
  3094. /* istanbul ignore next */
  3095. exports.Syntax = (function () {
  3096. var name, types = {};
  3097. if (typeof Object.create === 'function') {
  3098. types = Object.create(null);
  3099. }
  3100. for (name in Syntax) {
  3101. if (Syntax.hasOwnProperty(name)) {
  3102. types[name] = Syntax[name];
  3103. }
  3104. }
  3105. if (typeof Object.freeze === 'function') {
  3106. Object.freeze(types);
  3107. }
  3108. return types;
  3109. }());
  3110. }));
  3111. /* vim: set sw=4 ts=4 et tw=80 : */
  3112. },{}],1:[function(require,module,exports){
  3113. (function (process){
  3114. /* parser generated by jison 0.4.13 */
  3115. /*
  3116. Returns a Parser object of the following structure:
  3117. Parser: {
  3118. yy: {}
  3119. }
  3120. Parser.prototype: {
  3121. yy: {},
  3122. trace: function(),
  3123. symbols_: {associative list: name ==> number},
  3124. terminals_: {associative list: number ==> name},
  3125. productions_: [...],
  3126. performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate, $$, _$),
  3127. table: [...],
  3128. defaultActions: {...},
  3129. parseError: function(str, hash),
  3130. parse: function(input),
  3131. lexer: {
  3132. EOF: 1,
  3133. parseError: function(str, hash),
  3134. setInput: function(input),
  3135. input: function(),
  3136. unput: function(str),
  3137. more: function(),
  3138. less: function(n),
  3139. pastInput: function(),
  3140. upcomingInput: function(),
  3141. showPosition: function(),
  3142. test_match: function(regex_match_array, rule_index),
  3143. next: function(),
  3144. lex: function(),
  3145. begin: function(condition),
  3146. popState: function(),
  3147. _currentRules: function(),
  3148. topState: function(),
  3149. pushState: function(condition),
  3150. options: {
  3151. ranges: boolean (optional: true ==> token location info will include a .range[] member)
  3152. flex: boolean (optional: true ==> flex-like lexing behaviour where the rules are tested exhaustively to find the longest match)
  3153. backtrack_lexer: boolean (optional: true ==> lexer regexes are tested in order and for each matching regex the action code is invoked; the lexer terminates the scan when a token is returned by the action code)
  3154. },
  3155. performAction: function(yy, yy_, $avoiding_name_collisions, YY_START),
  3156. rules: [...],
  3157. conditions: {associative list: name ==> set},
  3158. }
  3159. }
  3160. token location info (@$, _$, etc.): {
  3161. first_line: n,
  3162. last_line: n,
  3163. first_column: n,
  3164. last_column: n,
  3165. range: [start_number, end_number] (where the numbers are indexes into the input string, regular zero-based)
  3166. }
  3167. the parseError function receives a 'hash' object with these members for lexer and parser errors: {
  3168. text: (matched text)
  3169. token: (the produced terminal token, if any)
  3170. line: (yylineno)
  3171. }
  3172. while parser (grammar) errors will also provide these members, i.e. parser errors deliver a superset of attributes: {
  3173. loc: (yylloc)
  3174. expected: (string describing the set of expected tokens)
  3175. recoverable: (boolean: TRUE when the parser has a error recovery rule available for this particular error)
  3176. }
  3177. */
  3178. var parser = (function(){
  3179. var parser = {trace: function trace() { },
  3180. yy: {},
  3181. symbols_: {"error":2,"JSON_PATH":3,"DOLLAR":4,"PATH_COMPONENTS":5,"LEADING_CHILD_MEMBER_EXPRESSION":6,"PATH_COMPONENT":7,"MEMBER_COMPONENT":8,"SUBSCRIPT_COMPONENT":9,"CHILD_MEMBER_COMPONENT":10,"DESCENDANT_MEMBER_COMPONENT":11,"DOT":12,"MEMBER_EXPRESSION":13,"DOT_DOT":14,"STAR":15,"IDENTIFIER":16,"SCRIPT_EXPRESSION":17,"INTEGER":18,"END":19,"CHILD_SUBSCRIPT_COMPONENT":20,"DESCENDANT_SUBSCRIPT_COMPONENT":21,"[":22,"SUBSCRIPT":23,"]":24,"SUBSCRIPT_EXPRESSION":25,"SUBSCRIPT_EXPRESSION_LIST":26,"SUBSCRIPT_EXPRESSION_LISTABLE":27,",":28,"STRING_LITERAL":29,"ARRAY_SLICE":30,"FILTER_EXPRESSION":31,"QQ_STRING":32,"Q_STRING":33,"$accept":0,"$end":1},
  3182. terminals_: {2:"error",4:"DOLLAR",12:"DOT",14:"DOT_DOT",15:"STAR",16:"IDENTIFIER",17:"SCRIPT_EXPRESSION",18:"INTEGER",19:"END",22:"[",24:"]",28:",",30:"ARRAY_SLICE",31:"FILTER_EXPRESSION",32:"QQ_STRING",33:"Q_STRING"},
  3183. productions_: [0,[3,1],[3,2],[3,1],[3,2],[5,1],[5,2],[7,1],[7,1],[8,1],[8,1],[10,2],[6,1],[11,2],[13,1],[13,1],[13,1],[13,1],[13,1],[9,1],[9,1],[20,3],[21,4],[23,1],[23,1],[26,1],[26,3],[27,1],[27,1],[27,1],[25,1],[25,1],[25,1],[29,1],[29,1]],
  3184. performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */
  3185. /**/) {
  3186. /* this == yyval */
  3187. if (!yy.ast) {
  3188. yy.ast = _ast;
  3189. _ast.initialize();
  3190. }
  3191. var $0 = $$.length - 1;
  3192. switch (yystate) {
  3193. case 1:yy.ast.set({ expression: { type: "root", value: $$[$0] } }); yy.ast.unshift(); return yy.ast.yield()
  3194. break;
  3195. case 2:yy.ast.set({ expression: { type: "root", value: $$[$0-1] } }); yy.ast.unshift(); return yy.ast.yield()
  3196. break;
  3197. case 3:yy.ast.unshift(); return yy.ast.yield()
  3198. break;
  3199. case 4:yy.ast.set({ operation: "member", scope: "child", expression: { type: "identifier", value: $$[$0-1] }}); yy.ast.unshift(); return yy.ast.yield()
  3200. break;
  3201. case 5:
  3202. break;
  3203. case 6:
  3204. break;
  3205. case 7:yy.ast.set({ operation: "member" }); yy.ast.push()
  3206. break;
  3207. case 8:yy.ast.set({ operation: "subscript" }); yy.ast.push()
  3208. break;
  3209. case 9:yy.ast.set({ scope: "child" })
  3210. break;
  3211. case 10:yy.ast.set({ scope: "descendant" })
  3212. break;
  3213. case 11:
  3214. break;
  3215. case 12:yy.ast.set({ scope: "child", operation: "member" })
  3216. break;
  3217. case 13:
  3218. break;
  3219. case 14:yy.ast.set({ expression: { type: "wildcard", value: $$[$0] } })
  3220. break;
  3221. case 15:yy.ast.set({ expression: { type: "identifier", value: $$[$0] } })
  3222. break;
  3223. case 16:yy.ast.set({ expression: { type: "script_expression", value: $$[$0] } })
  3224. break;
  3225. case 17:yy.ast.set({ expression: { type: "numeric_literal", value: parseInt($$[$0]) } })
  3226. break;
  3227. case 18:
  3228. break;
  3229. case 19:yy.ast.set({ scope: "child" })
  3230. break;
  3231. case 20:yy.ast.set({ scope: "descendant" })
  3232. break;
  3233. case 21:
  3234. break;
  3235. case 22:
  3236. break;
  3237. case 23:
  3238. break;
  3239. case 24:$$[$0].length > 1? yy.ast.set({ expression: { type: "union", value: $$[$0] } }) : this.$ = $$[$0]
  3240. break;
  3241. case 25:this.$ = [$$[$0]]
  3242. break;
  3243. case 26:this.$ = $$[$0-2].concat($$[$0])
  3244. break;
  3245. case 27:this.$ = { expression: { type: "numeric_literal", value: parseInt($$[$0]) } }; yy.ast.set(this.$)
  3246. break;
  3247. case 28:this.$ = { expression: { type: "string_literal", value: $$[$0] } }; yy.ast.set(this.$)
  3248. break;
  3249. case 29:this.$ = { expression: { type: "slice", value: $$[$0] } }; yy.ast.set(this.$)
  3250. break;
  3251. case 30:this.$ = { expression: { type: "wildcard", value: $$[$0] } }; yy.ast.set(this.$)
  3252. break;
  3253. case 31:this.$ = { expression: { type: "script_expression", value: $$[$0] } }; yy.ast.set(this.$)
  3254. break;
  3255. case 32:this.$ = { expression: { type: "filter_expression", value: $$[$0] } }; yy.ast.set(this.$)
  3256. break;
  3257. case 33:this.$ = $$[$0]
  3258. break;
  3259. case 34:this.$ = $$[$0]
  3260. break;
  3261. }
  3262. },
  3263. table: [{3:1,4:[1,2],6:3,13:4,15:[1,5],16:[1,6],17:[1,7],18:[1,8],19:[1,9]},{1:[3]},{1:[2,1],5:10,7:11,8:12,9:13,10:14,11:15,12:[1,18],14:[1,19],20:16,21:17,22:[1,20]},{1:[2,3],5:21,7:11,8:12,9:13,10:14,11:15,12:[1,18],14:[1,19],20:16,21:17,22:[1,20]},{1:[2,12],12:[2,12],14:[2,12],22:[2,12]},{1:[2,14],12:[2,14],14:[2,14],22:[2,14]},{1:[2,15],12:[2,15],14:[2,15],22:[2,15]},{1:[2,16],12:[2,16],14:[2,16],22:[2,16]},{1:[2,17],12:[2,17],14:[2,17],22:[2,17]},{1:[2,18],12:[2,18],14:[2,18],22:[2,18]},{1:[2,2],7:22,8:12,9:13,10:14,11:15,12:[1,18],14:[1,19],20:16,21:17,22:[1,20]},{1:[2,5],12:[2,5],14:[2,5],22:[2,5]},{1:[2,7],12:[2,7],14:[2,7],22:[2,7]},{1:[2,8],12:[2,8],14:[2,8],22:[2,8]},{1:[2,9],12:[2,9],14:[2,9],22:[2,9]},{1:[2,10],12:[2,10],14:[2,10],22:[2,10]},{1:[2,19],12:[2,19],14:[2,19],22:[2,19]},{1:[2,20],12:[2,20],14:[2,20],22:[2,20]},{13:23,15:[1,5],16:[1,6],17:[1,7],18:[1,8],19:[1,9]},{13:24,15:[1,5],16:[1,6],17:[1,7],18:[1,8],19:[1,9],22:[1,25]},{15:[1,29],17:[1,30],18:[1,33],23:26,25:27,26:28,27:32,29:34,30:[1,35],31:[1,31],32:[1,36],33:[1,37]},{1:[2,4],7:22,8:12,9:13,10:14,11:15,12:[1,18],14:[1,19],20:16,21:17,22:[1,20]},{1:[2,6],12:[2,6],14:[2,6],22:[2,6]},{1:[2,11],12:[2,11],14:[2,11],22:[2,11]},{1:[2,13],12:[2,13],14:[2,13],22:[2,13]},{15:[1,29],17:[1,30],18:[1,33],23:38,25:27,26:28,27:32,29:34,30:[1,35],31:[1,31],32:[1,36],33:[1,37]},{24:[1,39]},{24:[2,23]},{24:[2,24],28:[1,40]},{24:[2,30]},{24:[2,31]},{24:[2,32]},{24:[2,25],28:[2,25]},{24:[2,27],28:[2,27]},{24:[2,28],28:[2,28]},{24:[2,29],28:[2,29]},{24:[2,33],28:[2,33]},{24:[2,34],28:[2,34]},{24:[1,41]},{1:[2,21],12:[2,21],14:[2,21],22:[2,21]},{18:[1,33],27:42,29:34,30:[1,35],32:[1,36],33:[1,37]},{1:[2,22],12:[2,22],14:[2,22],22:[2,22]},{24:[2,26],28:[2,26]}],
  3264. defaultActions: {27:[2,23],29:[2,30],30:[2,31],31:[2,32]},
  3265. parseError: function parseError(str, hash) {
  3266. if (hash.recoverable) {
  3267. this.trace(str);
  3268. } else {
  3269. throw new Error(str);
  3270. }
  3271. },
  3272. parse: function parse(input) {
  3273. var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = '', yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
  3274. var args = lstack.slice.call(arguments, 1);
  3275. this.lexer.setInput(input);
  3276. this.lexer.yy = this.yy;
  3277. this.yy.lexer = this.lexer;
  3278. this.yy.parser = this;
  3279. if (typeof this.lexer.yylloc == 'undefined') {
  3280. this.lexer.yylloc = {};
  3281. }
  3282. var yyloc = this.lexer.yylloc;
  3283. lstack.push(yyloc);
  3284. var ranges = this.lexer.options && this.lexer.options.ranges;
  3285. if (typeof this.yy.parseError === 'function') {
  3286. this.parseError = this.yy.parseError;
  3287. } else {
  3288. this.parseError = Object.getPrototypeOf(this).parseError;
  3289. }
  3290. function popStack(n) {
  3291. stack.length = stack.length - 2 * n;
  3292. vstack.length = vstack.length - n;
  3293. lstack.length = lstack.length - n;
  3294. }
  3295. function lex() {
  3296. var token;
  3297. token = self.lexer.lex() || EOF;
  3298. if (typeof token !== 'number') {
  3299. token = self.symbols_[token] || token;
  3300. }
  3301. return token;
  3302. }
  3303. var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
  3304. while (true) {
  3305. state = stack[stack.length - 1];
  3306. if (this.defaultActions[state]) {
  3307. action = this.defaultActions[state];
  3308. } else {
  3309. if (symbol === null || typeof symbol == 'undefined') {
  3310. symbol = lex();
  3311. }
  3312. action = table[state] && table[state][symbol];
  3313. }
  3314. if (typeof action === 'undefined' || !action.length || !action[0]) {
  3315. var errStr = '';
  3316. expected = [];
  3317. for (p in table[state]) {
  3318. if (this.terminals_[p] && p > TERROR) {
  3319. expected.push('\'' + this.terminals_[p] + '\'');
  3320. }
  3321. }
  3322. if (this.lexer.showPosition) {
  3323. errStr = 'Parse error on line ' + (yylineno + 1) + ':\n' + this.lexer.showPosition() + '\nExpecting ' + expected.join(', ') + ', got \'' + (this.terminals_[symbol] || symbol) + '\'';
  3324. } else {
  3325. errStr = 'Parse error on line ' + (yylineno + 1) + ': Unexpected ' + (symbol == EOF ? 'end of input' : '\'' + (this.terminals_[symbol] || symbol) + '\'');
  3326. }
  3327. this.parseError(errStr, {
  3328. text: this.lexer.match,
  3329. token: this.terminals_[symbol] || symbol,
  3330. line: this.lexer.yylineno,
  3331. loc: yyloc,
  3332. expected: expected
  3333. });
  3334. }
  3335. if (action[0] instanceof Array && action.length > 1) {
  3336. throw new Error('Parse Error: multiple actions possible at state: ' + state + ', token: ' + symbol);
  3337. }
  3338. switch (action[0]) {
  3339. case 1:
  3340. stack.push(symbol);
  3341. vstack.push(this.lexer.yytext);
  3342. lstack.push(this.lexer.yylloc);
  3343. stack.push(action[1]);
  3344. symbol = null;
  3345. if (!preErrorSymbol) {
  3346. yyleng = this.lexer.yyleng;
  3347. yytext = this.lexer.yytext;
  3348. yylineno = this.lexer.yylineno;
  3349. yyloc = this.lexer.yylloc;
  3350. if (recovering > 0) {
  3351. recovering--;
  3352. }
  3353. } else {
  3354. symbol = preErrorSymbol;
  3355. preErrorSymbol = null;
  3356. }
  3357. break;
  3358. case 2:
  3359. len = this.productions_[action[1]][1];
  3360. yyval.$ = vstack[vstack.length - len];
  3361. yyval._$ = {
  3362. first_line: lstack[lstack.length - (len || 1)].first_line,
  3363. last_line: lstack[lstack.length - 1].last_line,
  3364. first_column: lstack[lstack.length - (len || 1)].first_column,
  3365. last_column: lstack[lstack.length - 1].last_column
  3366. };
  3367. if (ranges) {
  3368. yyval._$.range = [
  3369. lstack[lstack.length - (len || 1)].range[0],
  3370. lstack[lstack.length - 1].range[1]
  3371. ];
  3372. }
  3373. r = this.performAction.apply(yyval, [
  3374. yytext,
  3375. yyleng,
  3376. yylineno,
  3377. this.yy,
  3378. action[1],
  3379. vstack,
  3380. lstack
  3381. ].concat(args));
  3382. if (typeof r !== 'undefined') {
  3383. return r;
  3384. }
  3385. if (len) {
  3386. stack = stack.slice(0, -1 * len * 2);
  3387. vstack = vstack.slice(0, -1 * len);
  3388. lstack = lstack.slice(0, -1 * len);
  3389. }
  3390. stack.push(this.productions_[action[1]][0]);
  3391. vstack.push(yyval.$);
  3392. lstack.push(yyval._$);
  3393. newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
  3394. stack.push(newState);
  3395. break;
  3396. case 3:
  3397. return true;
  3398. }
  3399. }
  3400. return true;
  3401. }};
  3402. var _ast = {
  3403. initialize: function() {
  3404. this._nodes = [];
  3405. this._node = {};
  3406. this._stash = [];
  3407. },
  3408. set: function(props) {
  3409. for (var k in props) this._node[k] = props[k];
  3410. return this._node;
  3411. },
  3412. node: function(obj) {
  3413. if (arguments.length) this._node = obj;
  3414. return this._node;
  3415. },
  3416. push: function() {
  3417. this._nodes.push(this._node);
  3418. this._node = {};
  3419. },
  3420. unshift: function() {
  3421. this._nodes.unshift(this._node);
  3422. this._node = {};
  3423. },
  3424. yield: function() {
  3425. var _nodes = this._nodes;
  3426. this.initialize();
  3427. return _nodes;
  3428. }
  3429. };
  3430. /* generated by jison-lex 0.2.1 */
  3431. var lexer = (function(){
  3432. var lexer = {
  3433. EOF:1,
  3434. parseError:function parseError(str, hash) {
  3435. if (this.yy.parser) {
  3436. this.yy.parser.parseError(str, hash);
  3437. } else {
  3438. throw new Error(str);
  3439. }
  3440. },
  3441. // resets the lexer, sets new input
  3442. setInput:function (input) {
  3443. this._input = input;
  3444. this._more = this._backtrack = this.done = false;
  3445. this.yylineno = this.yyleng = 0;
  3446. this.yytext = this.matched = this.match = '';
  3447. this.conditionStack = ['INITIAL'];
  3448. this.yylloc = {
  3449. first_line: 1,
  3450. first_column: 0,
  3451. last_line: 1,
  3452. last_column: 0
  3453. };
  3454. if (this.options.ranges) {
  3455. this.yylloc.range = [0,0];
  3456. }
  3457. this.offset = 0;
  3458. return this;
  3459. },
  3460. // consumes and returns one char from the input
  3461. input:function () {
  3462. var ch = this._input[0];
  3463. this.yytext += ch;
  3464. this.yyleng++;
  3465. this.offset++;
  3466. this.match += ch;
  3467. this.matched += ch;
  3468. var lines = ch.match(/(?:\r\n?|\n).*/g);
  3469. if (lines) {
  3470. this.yylineno++;
  3471. this.yylloc.last_line++;
  3472. } else {
  3473. this.yylloc.last_column++;
  3474. }
  3475. if (this.options.ranges) {
  3476. this.yylloc.range[1]++;
  3477. }
  3478. this._input = this._input.slice(1);
  3479. return ch;
  3480. },
  3481. // unshifts one char (or a string) into the input
  3482. unput:function (ch) {
  3483. var len = ch.length;
  3484. var lines = ch.split(/(?:\r\n?|\n)/g);
  3485. this._input = ch + this._input;
  3486. this.yytext = this.yytext.substr(0, this.yytext.length - len - 1);
  3487. //this.yyleng -= len;
  3488. this.offset -= len;
  3489. var oldLines = this.match.split(/(?:\r\n?|\n)/g);
  3490. this.match = this.match.substr(0, this.match.length - 1);
  3491. this.matched = this.matched.substr(0, this.matched.length - 1);
  3492. if (lines.length - 1) {
  3493. this.yylineno -= lines.length - 1;
  3494. }
  3495. var r = this.yylloc.range;
  3496. this.yylloc = {
  3497. first_line: this.yylloc.first_line,
  3498. last_line: this.yylineno + 1,
  3499. first_column: this.yylloc.first_column,
  3500. last_column: lines ?
  3501. (lines.length === oldLines.length ? this.yylloc.first_column : 0)
  3502. + oldLines[oldLines.length - lines.length].length - lines[0].length :
  3503. this.yylloc.first_column - len
  3504. };
  3505. if (this.options.ranges) {
  3506. this.yylloc.range = [r[0], r[0] + this.yyleng - len];
  3507. }
  3508. this.yyleng = this.yytext.length;
  3509. return this;
  3510. },
  3511. // When called from action, caches matched text and appends it on next action
  3512. more:function () {
  3513. this._more = true;
  3514. return this;
  3515. },
  3516. // When called from action, signals the lexer that this rule fails to match the input, so the next matching rule (regex) should be tested instead.
  3517. reject:function () {
  3518. if (this.options.backtrack_lexer) {
  3519. this._backtrack = true;
  3520. } else {
  3521. return this.parseError('Lexical error on line ' + (this.yylineno + 1) + '. You can only invoke reject() in the lexer when the lexer is of the backtracking persuasion (options.backtrack_lexer = true).\n' + this.showPosition(), {
  3522. text: "",
  3523. token: null,
  3524. line: this.yylineno
  3525. });
  3526. }
  3527. return this;
  3528. },
  3529. // retain first n characters of the match
  3530. less:function (n) {
  3531. this.unput(this.match.slice(n));
  3532. },
  3533. // displays already matched input, i.e. for error messages
  3534. pastInput:function () {
  3535. var past = this.matched.substr(0, this.matched.length - this.match.length);
  3536. return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
  3537. },
  3538. // displays upcoming input, i.e. for error messages
  3539. upcomingInput:function () {
  3540. var next = this.match;
  3541. if (next.length < 20) {
  3542. next += this._input.substr(0, 20-next.length);
  3543. }
  3544. return (next.substr(0,20) + (next.length > 20 ? '...' : '')).replace(/\n/g, "");
  3545. },
  3546. // displays the character position where the lexing error occurred, i.e. for error messages
  3547. showPosition:function () {
  3548. var pre = this.pastInput();
  3549. var c = new Array(pre.length + 1).join("-");
  3550. return pre + this.upcomingInput() + "\n" + c + "^";
  3551. },
  3552. // test the lexed token: return FALSE when not a match, otherwise return token
  3553. test_match:function (match, indexed_rule) {
  3554. var token,
  3555. lines,
  3556. backup;
  3557. if (this.options.backtrack_lexer) {
  3558. // save context
  3559. backup = {
  3560. yylineno: this.yylineno,
  3561. yylloc: {
  3562. first_line: this.yylloc.first_line,
  3563. last_line: this.last_line,
  3564. first_column: this.yylloc.first_column,
  3565. last_column: this.yylloc.last_column
  3566. },
  3567. yytext: this.yytext,
  3568. match: this.match,
  3569. matches: this.matches,
  3570. matched: this.matched,
  3571. yyleng: this.yyleng,
  3572. offset: this.offset,
  3573. _more: this._more,
  3574. _input: this._input,
  3575. yy: this.yy,
  3576. conditionStack: this.conditionStack.slice(0),
  3577. done: this.done
  3578. };
  3579. if (this.options.ranges) {
  3580. backup.yylloc.range = this.yylloc.range.slice(0);
  3581. }
  3582. }
  3583. lines = match[0].match(/(?:\r\n?|\n).*/g);
  3584. if (lines) {
  3585. this.yylineno += lines.length;
  3586. }
  3587. this.yylloc = {
  3588. first_line: this.yylloc.last_line,
  3589. last_line: this.yylineno + 1,
  3590. first_column: this.yylloc.last_column,
  3591. last_column: lines ?
  3592. lines[lines.length - 1].length - lines[lines.length - 1].match(/\r?\n?/)[0].length :
  3593. this.yylloc.last_column + match[0].length
  3594. };
  3595. this.yytext += match[0];
  3596. this.match += match[0];
  3597. this.matches = match;
  3598. this.yyleng = this.yytext.length;
  3599. if (this.options.ranges) {
  3600. this.yylloc.range = [this.offset, this.offset += this.yyleng];
  3601. }
  3602. this._more = false;
  3603. this._backtrack = false;
  3604. this._input = this._input.slice(match[0].length);
  3605. this.matched += match[0];
  3606. token = this.performAction.call(this, this.yy, this, indexed_rule, this.conditionStack[this.conditionStack.length - 1]);
  3607. if (this.done && this._input) {
  3608. this.done = false;
  3609. }
  3610. if (token) {
  3611. return token;
  3612. } else if (this._backtrack) {
  3613. // recover context
  3614. for (var k in backup) {
  3615. this[k] = backup[k];
  3616. }
  3617. return false; // rule action called reject() implying the next rule should be tested instead.
  3618. }
  3619. return false;
  3620. },
  3621. // return next match in input
  3622. next:function () {
  3623. if (this.done) {
  3624. return this.EOF;
  3625. }
  3626. if (!this._input) {
  3627. this.done = true;
  3628. }
  3629. var token,
  3630. match,
  3631. tempMatch,
  3632. index;
  3633. if (!this._more) {
  3634. this.yytext = '';
  3635. this.match = '';
  3636. }
  3637. var rules = this._currentRules();
  3638. for (var i = 0; i < rules.length; i++) {
  3639. tempMatch = this._input.match(this.rules[rules[i]]);
  3640. if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
  3641. match = tempMatch;
  3642. index = i;
  3643. if (this.options.backtrack_lexer) {
  3644. token = this.test_match(tempMatch, rules[i]);
  3645. if (token !== false) {
  3646. return token;
  3647. } else if (this._backtrack) {
  3648. match = false;
  3649. continue; // rule action called reject() implying a rule MISmatch.
  3650. } else {
  3651. // else: this is a lexer rule which consumes input without producing a token (e.g. whitespace)
  3652. return false;
  3653. }
  3654. } else if (!this.options.flex) {
  3655. break;
  3656. }
  3657. }
  3658. }
  3659. if (match) {
  3660. token = this.test_match(match, rules[index]);
  3661. if (token !== false) {
  3662. return token;
  3663. }
  3664. // else: this is a lexer rule which consumes input without producing a token (e.g. whitespace)
  3665. return false;
  3666. }
  3667. if (this._input === "") {
  3668. return this.EOF;
  3669. } else {
  3670. return this.parseError('Lexical error on line ' + (this.yylineno + 1) + '. Unrecognized text.\n' + this.showPosition(), {
  3671. text: "",
  3672. token: null,
  3673. line: this.yylineno
  3674. });
  3675. }
  3676. },
  3677. // return next match that has a token
  3678. lex:function lex() {
  3679. var r = this.next();
  3680. if (r) {
  3681. return r;
  3682. } else {
  3683. return this.lex();
  3684. }
  3685. },
  3686. // activates a new lexer condition state (pushes the new lexer condition state onto the condition stack)
  3687. begin:function begin(condition) {
  3688. this.conditionStack.push(condition);
  3689. },
  3690. // pop the previously active lexer condition state off the condition stack
  3691. popState:function popState() {
  3692. var n = this.conditionStack.length - 1;
  3693. if (n > 0) {
  3694. return this.conditionStack.pop();
  3695. } else {
  3696. return this.conditionStack[0];
  3697. }
  3698. },
  3699. // produce the lexer rule set which is active for the currently active lexer condition state
  3700. _currentRules:function _currentRules() {
  3701. if (this.conditionStack.length && this.conditionStack[this.conditionStack.length - 1]) {
  3702. return this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules;
  3703. } else {
  3704. return this.conditions["INITIAL"].rules;
  3705. }
  3706. },
  3707. // return the currently active lexer condition state; when an index argument is provided it produces the N-th previous condition state, if available
  3708. topState:function topState(n) {
  3709. n = this.conditionStack.length - 1 - Math.abs(n || 0);
  3710. if (n >= 0) {
  3711. return this.conditionStack[n];
  3712. } else {
  3713. return "INITIAL";
  3714. }
  3715. },
  3716. // alias for begin(condition)
  3717. pushState:function pushState(condition) {
  3718. this.begin(condition);
  3719. },
  3720. // return the number of states currently on the stack
  3721. stateStackSize:function stateStackSize() {
  3722. return this.conditionStack.length;
  3723. },
  3724. options: {},
  3725. performAction: function anonymous(yy,yy_,$avoiding_name_collisions,YY_START
  3726. /**/) {
  3727. var YYSTATE=YY_START;
  3728. switch($avoiding_name_collisions) {
  3729. case 0:return 4
  3730. break;
  3731. case 1:return 14
  3732. break;
  3733. case 2:return 12
  3734. break;
  3735. case 3:return 15
  3736. break;
  3737. case 4:return 16
  3738. break;
  3739. case 5:return 22
  3740. break;
  3741. case 6:return 24
  3742. break;
  3743. case 7:return 28
  3744. break;
  3745. case 8:return 30
  3746. break;
  3747. case 9:return 18
  3748. break;
  3749. case 10:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 32;
  3750. break;
  3751. case 11:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 33;
  3752. break;
  3753. case 12:return 17
  3754. break;
  3755. case 13:return 31
  3756. break;
  3757. }
  3758. },
  3759. rules: [/^(?:\$)/,/^(?:\.\.)/,/^(?:\.)/,/^(?:\*)/,/^(?:[a-zA-Z_]+[a-zA-Z0-9_]*)/,/^(?:\[)/,/^(?:\])/,/^(?:,)/,/^(?:((-?(?:0|[1-9][0-9]*)))?\:((-?(?:0|[1-9][0-9]*)))?(\:((-?(?:0|[1-9][0-9]*)))?)?)/,/^(?:(-?(?:0|[1-9][0-9]*)))/,/^(?:"(?:\\["bfnrt/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*")/,/^(?:'(?:\\['bfnrt/\\]|\\u[a-fA-F0-9]{4}|[^'\\])*')/,/^(?:\(.+?\)(?=\]))/,/^(?:\?\(.+?\)(?=\]))/],
  3760. conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],"inclusive":true}}
  3761. };
  3762. return lexer;
  3763. })();
  3764. parser.lexer = lexer;
  3765. function Parser () {
  3766. this.yy = {};
  3767. }
  3768. Parser.prototype = parser;parser.Parser = Parser;
  3769. return new Parser;
  3770. })();
  3771. if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
  3772. exports.parser = parser;
  3773. exports.Parser = parser.Parser;
  3774. exports.parse = function () { return parser.parse.apply(parser, arguments); };
  3775. exports.main = function commonjsMain(args) {
  3776. if (!args[1]) {
  3777. console.log('Usage: '+args[0]+' FILE');
  3778. process.exit(1);
  3779. }
  3780. var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8");
  3781. return exports.parser.parse(source);
  3782. };
  3783. if (typeof module !== 'undefined' && require.main === module) {
  3784. exports.main(process.argv.slice(1));
  3785. }
  3786. }
  3787. }).call(this,require('_process'))
  3788. },{"_process":14,"fs":12,"path":13}],2:[function(require,module,exports){
  3789. module.exports = {
  3790. identifier: "[a-zA-Z_]+[a-zA-Z0-9_]*",
  3791. integer: "-?(?:0|[1-9][0-9]*)",
  3792. qq_string: "\"(?:\\\\[\"bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\"\\\\])*\"",
  3793. q_string: "'(?:\\\\[\'bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\'\\\\])*'"
  3794. };
  3795. },{}],3:[function(require,module,exports){
  3796. var dict = require('./dict');
  3797. var fs = require('fs');
  3798. var grammar = {
  3799. lex: {
  3800. macros: {
  3801. esc: "\\\\",
  3802. int: dict.integer
  3803. },
  3804. rules: [
  3805. ["\\$", "return 'DOLLAR'"],
  3806. ["\\.\\.", "return 'DOT_DOT'"],
  3807. ["\\.", "return 'DOT'"],
  3808. ["\\*", "return 'STAR'"],
  3809. [dict.identifier, "return 'IDENTIFIER'"],
  3810. ["\\[", "return '['"],
  3811. ["\\]", "return ']'"],
  3812. [",", "return ','"],
  3813. ["({int})?\\:({int})?(\\:({int})?)?", "return 'ARRAY_SLICE'"],
  3814. ["{int}", "return 'INTEGER'"],
  3815. [dict.qq_string, "yytext = yytext.substr(1,yyleng-2); return 'QQ_STRING';"],
  3816. [dict.q_string, "yytext = yytext.substr(1,yyleng-2); return 'Q_STRING';"],
  3817. ["\\(.+?\\)(?=\\])", "return 'SCRIPT_EXPRESSION'"],
  3818. ["\\?\\(.+?\\)(?=\\])", "return 'FILTER_EXPRESSION'"]
  3819. ]
  3820. },
  3821. start: "JSON_PATH",
  3822. bnf: {
  3823. JSON_PATH: [
  3824. [ 'DOLLAR', 'yy.ast.set({ expression: { type: "root", value: $1 } }); yy.ast.unshift(); return yy.ast.yield()' ],
  3825. [ 'DOLLAR PATH_COMPONENTS', 'yy.ast.set({ expression: { type: "root", value: $1 } }); yy.ast.unshift(); return yy.ast.yield()' ],
  3826. [ 'LEADING_CHILD_MEMBER_EXPRESSION', 'yy.ast.unshift(); return yy.ast.yield()' ],
  3827. [ 'LEADING_CHILD_MEMBER_EXPRESSION PATH_COMPONENTS', 'yy.ast.set({ operation: "member", scope: "child", expression: { type: "identifier", value: $1 }}); yy.ast.unshift(); return yy.ast.yield()' ] ],
  3828. PATH_COMPONENTS: [
  3829. [ 'PATH_COMPONENT', '' ],
  3830. [ 'PATH_COMPONENTS PATH_COMPONENT', '' ] ],
  3831. PATH_COMPONENT: [
  3832. [ 'MEMBER_COMPONENT', 'yy.ast.set({ operation: "member" }); yy.ast.push()' ],
  3833. [ 'SUBSCRIPT_COMPONENT', 'yy.ast.set({ operation: "subscript" }); yy.ast.push() ' ] ],
  3834. MEMBER_COMPONENT: [
  3835. [ 'CHILD_MEMBER_COMPONENT', 'yy.ast.set({ scope: "child" })' ],
  3836. [ 'DESCENDANT_MEMBER_COMPONENT', 'yy.ast.set({ scope: "descendant" })' ] ],
  3837. CHILD_MEMBER_COMPONENT: [
  3838. [ 'DOT MEMBER_EXPRESSION', '' ] ],
  3839. LEADING_CHILD_MEMBER_EXPRESSION: [
  3840. [ 'MEMBER_EXPRESSION', 'yy.ast.set({ scope: "child", operation: "member" })' ] ],
  3841. DESCENDANT_MEMBER_COMPONENT: [
  3842. [ 'DOT_DOT MEMBER_EXPRESSION', '' ] ],
  3843. MEMBER_EXPRESSION: [
  3844. [ 'STAR', 'yy.ast.set({ expression: { type: "wildcard", value: $1 } })' ],
  3845. [ 'IDENTIFIER', 'yy.ast.set({ expression: { type: "identifier", value: $1 } })' ],
  3846. [ 'SCRIPT_EXPRESSION', 'yy.ast.set({ expression: { type: "script_expression", value: $1 } })' ],
  3847. [ 'INTEGER', 'yy.ast.set({ expression: { type: "numeric_literal", value: parseInt($1) } })' ],
  3848. [ 'END', '' ] ],
  3849. SUBSCRIPT_COMPONENT: [
  3850. [ 'CHILD_SUBSCRIPT_COMPONENT', 'yy.ast.set({ scope: "child" })' ],
  3851. [ 'DESCENDANT_SUBSCRIPT_COMPONENT', 'yy.ast.set({ scope: "descendant" })' ] ],
  3852. CHILD_SUBSCRIPT_COMPONENT: [
  3853. [ '[ SUBSCRIPT ]', '' ] ],
  3854. DESCENDANT_SUBSCRIPT_COMPONENT: [
  3855. [ 'DOT_DOT [ SUBSCRIPT ]', '' ] ],
  3856. SUBSCRIPT: [
  3857. [ 'SUBSCRIPT_EXPRESSION', '' ],
  3858. [ 'SUBSCRIPT_EXPRESSION_LIST', '$1.length > 1? yy.ast.set({ expression: { type: "union", value: $1 } }) : $$ = $1' ] ],
  3859. SUBSCRIPT_EXPRESSION_LIST: [
  3860. [ 'SUBSCRIPT_EXPRESSION_LISTABLE', '$$ = [$1]'],
  3861. [ 'SUBSCRIPT_EXPRESSION_LIST , SUBSCRIPT_EXPRESSION_LISTABLE', '$$ = $1.concat($3)' ] ],
  3862. SUBSCRIPT_EXPRESSION_LISTABLE: [
  3863. [ 'INTEGER', '$$ = { expression: { type: "numeric_literal", value: parseInt($1) } }; yy.ast.set($$)' ],
  3864. [ 'STRING_LITERAL', '$$ = { expression: { type: "string_literal", value: $1 } }; yy.ast.set($$)' ],
  3865. [ 'ARRAY_SLICE', '$$ = { expression: { type: "slice", value: $1 } }; yy.ast.set($$)' ] ],
  3866. SUBSCRIPT_EXPRESSION: [
  3867. [ 'STAR', '$$ = { expression: { type: "wildcard", value: $1 } }; yy.ast.set($$)' ],
  3868. [ 'SCRIPT_EXPRESSION', '$$ = { expression: { type: "script_expression", value: $1 } }; yy.ast.set($$)' ],
  3869. [ 'FILTER_EXPRESSION', '$$ = { expression: { type: "filter_expression", value: $1 } }; yy.ast.set($$)' ] ],
  3870. STRING_LITERAL: [
  3871. [ 'QQ_STRING', "$$ = $1" ],
  3872. [ 'Q_STRING', "$$ = $1" ] ]
  3873. }
  3874. };
  3875. if (fs.readFileSync) {
  3876. grammar.moduleInclude = fs.readFileSync(require.resolve("../include/module.js"));
  3877. grammar.actionInclude = fs.readFileSync(require.resolve("../include/action.js"));
  3878. }
  3879. module.exports = grammar;
  3880. },{"./dict":2,"fs":12}],4:[function(require,module,exports){
  3881. var aesprim = require('./aesprim');
  3882. var slice = require('./slice');
  3883. var _evaluate = require('static-eval');
  3884. var _uniq = require('underscore').uniq;
  3885. var Handlers = function() {
  3886. return this.initialize.apply(this, arguments);
  3887. }
  3888. Handlers.prototype.initialize = function() {
  3889. this.traverse = traverser(true);
  3890. this.descend = traverser();
  3891. }
  3892. Handlers.prototype.keys = Object.keys;
  3893. Handlers.prototype.resolve = function(component) {
  3894. var key = [ component.operation, component.scope, component.expression.type ].join('-');
  3895. var method = this._fns[key];
  3896. if (!method) throw new Error("couldn't resolve key: " + key);
  3897. return method.bind(this);
  3898. };
  3899. Handlers.prototype.register = function(key, handler) {
  3900. if (!handler instanceof Function) {
  3901. throw new Error("handler must be a function");
  3902. }
  3903. this._fns[key] = handler;
  3904. };
  3905. Handlers.prototype._fns = {
  3906. 'member-child-identifier': function(component, partial) {
  3907. var key = component.expression.value;
  3908. var value = partial.value;
  3909. if (value instanceof Object && key in value) {
  3910. return [ { value: value[key], path: partial.path.concat(key) } ]
  3911. }
  3912. },
  3913. 'member-descendant-identifier':
  3914. _traverse(function(key, value, ref) { return key == ref }),
  3915. 'subscript-child-numeric_literal':
  3916. _descend(function(key, value, ref) { return key === ref }),
  3917. 'member-child-numeric_literal':
  3918. _descend(function(key, value, ref) { return String(key) === String(ref) }),
  3919. 'subscript-descendant-numeric_literal':
  3920. _traverse(function(key, value, ref) { return key === ref }),
  3921. 'member-child-wildcard':
  3922. _descend(function() { return true }),
  3923. 'member-descendant-wildcard':
  3924. _traverse(function() { return true }),
  3925. 'subscript-descendant-wildcard':
  3926. _traverse(function() { return true }),
  3927. 'subscript-child-wildcard':
  3928. _descend(function() { return true }),
  3929. 'subscript-child-slice': function(component, partial) {
  3930. if (is_array(partial.value)) {
  3931. var args = component.expression.value.split(':').map(_parse_nullable_int);
  3932. var values = partial.value.map(function(v, i) { return { value: v, path: partial.path.concat(i) } });
  3933. return slice.apply(null, [values].concat(args));
  3934. }
  3935. },
  3936. 'subscript-child-union': function(component, partial) {
  3937. var results = [];
  3938. component.expression.value.forEach(function(component) {
  3939. var _component = { operation: 'subscript', scope: 'child', expression: component.expression };
  3940. var handler = this.resolve(_component);
  3941. var _results = handler(_component, partial);
  3942. if (_results) {
  3943. results = results.concat(_results);
  3944. }
  3945. }, this);
  3946. return unique(results);
  3947. },
  3948. 'subscript-descendant-union': function(component, partial, count) {
  3949. var jp = require('..');
  3950. var self = this;
  3951. var results = [];
  3952. var nodes = jp.nodes(partial, '$..*').slice(1);
  3953. nodes.forEach(function(node) {
  3954. if (results.length >= count) return;
  3955. component.expression.value.forEach(function(component) {
  3956. var _component = { operation: 'subscript', scope: 'child', expression: component.expression };
  3957. var handler = self.resolve(_component);
  3958. var _results = handler(_component, node);
  3959. results = results.concat(_results);
  3960. });
  3961. });
  3962. return unique(results);
  3963. },
  3964. 'subscript-child-filter_expression': function(component, partial, count) {
  3965. // slice out the expression from ?(expression)
  3966. var src = component.expression.value.slice(2, -1);
  3967. var ast = aesprim.parse(src).body[0].expression;
  3968. var passable = function(key, value) {
  3969. return evaluate(ast, { '@': value });
  3970. }
  3971. return this.descend(partial, null, passable, count);
  3972. },
  3973. 'subscript-descendant-filter_expression': function(component, partial, count) {
  3974. // slice out the expression from ?(expression)
  3975. var src = component.expression.value.slice(2, -1);
  3976. var ast = aesprim.parse(src).body[0].expression;
  3977. var passable = function(key, value) {
  3978. return evaluate(ast, { '@': value });
  3979. }
  3980. return this.traverse(partial, null, passable, count);
  3981. },
  3982. 'subscript-child-script_expression': function(component, partial) {
  3983. var exp = component.expression.value.slice(1, -1);
  3984. return eval_recurse(partial, exp, '$[{{value}}]');
  3985. },
  3986. 'member-child-script_expression': function(component, partial) {
  3987. var exp = component.expression.value.slice(1, -1);
  3988. return eval_recurse(partial, exp, '$.{{value}}');
  3989. },
  3990. 'member-descendant-script_expression': function(component, partial) {
  3991. var exp = component.expression.value.slice(1, -1);
  3992. return eval_recurse(partial, exp, '$..value');
  3993. }
  3994. };
  3995. Handlers.prototype._fns['subscript-child-string_literal'] =
  3996. Handlers.prototype._fns['member-child-identifier'];
  3997. Handlers.prototype._fns['member-descendant-numeric_literal'] =
  3998. Handlers.prototype._fns['subscript-descendant-string_literal'] =
  3999. Handlers.prototype._fns['member-descendant-identifier'];
  4000. function eval_recurse(partial, src, template) {
  4001. var jp = require('./index');
  4002. var ast = aesprim.parse(src).body[0].expression;
  4003. var value = evaluate(ast, { '@': partial.value });
  4004. var path = template.replace(/\{\{\s*value\s*\}\}/g, value);
  4005. var results = jp.nodes(partial.value, path);
  4006. results.forEach(function(r) {
  4007. r.path = partial.path.concat(r.path.slice(1));
  4008. });
  4009. return results;
  4010. }
  4011. function is_array(val) {
  4012. return Array.isArray(val);
  4013. }
  4014. function is_object(val) {
  4015. // is this a non-array, non-null object?
  4016. return val && !(val instanceof Array) && val instanceof Object;
  4017. }
  4018. function traverser(recurse) {
  4019. return function(partial, ref, passable, count) {
  4020. var value = partial.value;
  4021. var path = partial.path;
  4022. var results = [];
  4023. var descend = function(value, path) {
  4024. if (is_array(value)) {
  4025. value.forEach(function(element, index) {
  4026. if (results.length >= count) { return }
  4027. if (passable(index, element, ref)) {
  4028. results.push({ path: path.concat(index), value: element });
  4029. }
  4030. });
  4031. value.forEach(function(element, index) {
  4032. if (results.length >= count) { return }
  4033. if (recurse) {
  4034. descend(element, path.concat(index));
  4035. }
  4036. });
  4037. } else if (is_object(value)) {
  4038. this.keys(value).forEach(function(k) {
  4039. if (results.length >= count) { return }
  4040. if (passable(k, value[k], ref)) {
  4041. results.push({ path: path.concat(k), value: value[k] });
  4042. }
  4043. })
  4044. this.keys(value).forEach(function(k) {
  4045. if (results.length >= count) { return }
  4046. if (recurse) {
  4047. descend(value[k], path.concat(k));
  4048. }
  4049. });
  4050. }
  4051. }.bind(this);
  4052. descend(value, path);
  4053. return results;
  4054. }
  4055. }
  4056. function _descend(passable) {
  4057. return function(component, partial, count) {
  4058. return this.descend(partial, component.expression.value, passable, count);
  4059. }
  4060. }
  4061. function _traverse(passable) {
  4062. return function(component, partial, count) {
  4063. return this.traverse(partial, component.expression.value, passable, count);
  4064. }
  4065. }
  4066. function evaluate() {
  4067. try { return _evaluate.apply(this, arguments) }
  4068. catch (e) { }
  4069. }
  4070. function unique(results) {
  4071. results = results.filter(function(d) { return d })
  4072. return _uniq(
  4073. results,
  4074. function(r) { return r.path.map(function(c) { return String(c).replace('-', '--') }).join('-') }
  4075. );
  4076. }
  4077. function _parse_nullable_int(val) {
  4078. var sval = String(val);
  4079. return sval.match(/^-?[0-9]+$/) ? parseInt(sval) : null;
  4080. }
  4081. module.exports = Handlers;
  4082. },{"..":"jsonpath","./aesprim":"./aesprim","./index":5,"./slice":7,"static-eval":15,"underscore":12}],5:[function(require,module,exports){
  4083. var assert = require('assert');
  4084. var dict = require('./dict');
  4085. var Parser = require('./parser');
  4086. var Handlers = require('./handlers');
  4087. var JSONPath = function() {
  4088. this.initialize.apply(this, arguments);
  4089. };
  4090. JSONPath.prototype.initialize = function() {
  4091. this.parser = new Parser();
  4092. this.handlers = new Handlers();
  4093. };
  4094. JSONPath.prototype.parse = function(string) {
  4095. assert.ok(_is_string(string), "we need a path");
  4096. return this.parser.parse(string);
  4097. };
  4098. JSONPath.prototype.parent = function(obj, string) {
  4099. assert.ok(obj instanceof Object, "obj needs to be an object");
  4100. assert.ok(string, "we need a path");
  4101. var node = this.nodes(obj, string)[0];
  4102. var key = node.path.pop(); /* jshint unused:false */
  4103. return this.value(obj, node.path);
  4104. }
  4105. JSONPath.prototype.apply = function(obj, string, fn) {
  4106. assert.ok(obj instanceof Object, "obj needs to be an object");
  4107. assert.ok(string, "we need a path");
  4108. assert.equal(typeof fn, "function", "fn needs to be function")
  4109. var nodes = this.nodes(obj, string).sort(function(a, b) {
  4110. // sort nodes so we apply from the bottom up
  4111. return b.path.length - a.path.length;
  4112. });
  4113. nodes.forEach(function(node) {
  4114. var key = node.path.pop();
  4115. var parent = this.value(obj, this.stringify(node.path));
  4116. var val = node.value = fn.call(obj, parent[key]);
  4117. parent[key] = val;
  4118. }, this);
  4119. return nodes;
  4120. }
  4121. JSONPath.prototype.value = function(obj, path, value) {
  4122. assert.ok(obj instanceof Object, "obj needs to be an object");
  4123. assert.ok(path, "we need a path");
  4124. if (arguments.length >= 3) {
  4125. var node = this.nodes(obj, path).shift();
  4126. if (!node) return this._vivify(obj, path, value);
  4127. var key = node.path.slice(-1).shift();
  4128. var parent = this.parent(obj, this.stringify(node.path));
  4129. parent[key] = value;
  4130. }
  4131. return this.query(obj, this.stringify(path), 1).shift();
  4132. }
  4133. JSONPath.prototype._vivify = function(obj, string, value) {
  4134. var self = this;
  4135. assert.ok(obj instanceof Object, "obj needs to be an object");
  4136. assert.ok(string, "we need a path");
  4137. var path = this.parser.parse(string)
  4138. .map(function(component) { return component.expression.value });
  4139. var setValue = function(path, value) {
  4140. var key = path.pop();
  4141. var node = self.value(obj, path);
  4142. if (!node) {
  4143. setValue(path.concat(), typeof key === 'string' ? {} : []);
  4144. node = self.value(obj, path);
  4145. }
  4146. node[key] = value;
  4147. }
  4148. setValue(path, value);
  4149. return this.query(obj, string)[0];
  4150. }
  4151. JSONPath.prototype.query = function(obj, string, count) {
  4152. assert.ok(obj instanceof Object, "obj needs to be an object");
  4153. assert.ok(_is_string(string), "we need a path");
  4154. var results = this.nodes(obj, string, count)
  4155. .map(function(r) { return r.value });
  4156. return results;
  4157. };
  4158. JSONPath.prototype.paths = function(obj, string, count) {
  4159. assert.ok(obj instanceof Object, "obj needs to be an object");
  4160. assert.ok(string, "we need a path");
  4161. var results = this.nodes(obj, string, count)
  4162. .map(function(r) { return r.path });
  4163. return results;
  4164. };
  4165. JSONPath.prototype.nodes = function(obj, string, count) {
  4166. assert.ok(obj instanceof Object, "obj needs to be an object");
  4167. assert.ok(string, "we need a path");
  4168. if (count === 0) return [];
  4169. var path = this.parser.parse(string);
  4170. var handlers = this.handlers;
  4171. var partials = [ { path: ['$'], value: obj } ];
  4172. var matches = [];
  4173. if (path.length && path[0].expression.type == 'root') path.shift();
  4174. if (!path.length) return partials;
  4175. path.forEach(function(component, index) {
  4176. if (matches.length >= count) return;
  4177. var handler = handlers.resolve(component);
  4178. var _partials = [];
  4179. partials.forEach(function(p) {
  4180. if (matches.length >= count) return;
  4181. var results = handler(component, p, count);
  4182. if (index == path.length - 1) {
  4183. // if we're through the components we're done
  4184. matches = matches.concat(results || []);
  4185. } else {
  4186. // otherwise accumulate and carry on through
  4187. _partials = _partials.concat(results || []);
  4188. }
  4189. });
  4190. partials = _partials;
  4191. });
  4192. return count ? matches.slice(0, count) : matches;
  4193. };
  4194. JSONPath.prototype.stringify = function(path) {
  4195. assert.ok(path, "we need a path");
  4196. var string = '$';
  4197. var templates = {
  4198. 'descendant-member': '..{{value}}',
  4199. 'child-member': '.{{value}}',
  4200. 'descendant-subscript': '..[{{value}}]',
  4201. 'child-subscript': '[{{value}}]'
  4202. };
  4203. path = this._normalize(path);
  4204. path.forEach(function(component) {
  4205. if (component.expression.type == 'root') return;
  4206. var key = [component.scope, component.operation].join('-');
  4207. var template = templates[key];
  4208. var value;
  4209. if (component.expression.type == 'string_literal') {
  4210. value = JSON.stringify(component.expression.value)
  4211. } else {
  4212. value = component.expression.value;
  4213. }
  4214. if (!template) throw new Error("couldn't find template " + key);
  4215. string += template.replace(/{{value}}/, value);
  4216. });
  4217. return string;
  4218. }
  4219. JSONPath.prototype._normalize = function(path) {
  4220. assert.ok(path, "we need a path");
  4221. if (typeof path == "string") {
  4222. return this.parser.parse(path);
  4223. } else if (Array.isArray(path) && typeof path[0] == "string") {
  4224. var _path = [ { expression: { type: "root", value: "$" } } ];
  4225. path.forEach(function(component, index) {
  4226. if (component == '$' && index === 0) return;
  4227. if (typeof component == "string" && component.match("^" + dict.identifier + "$")) {
  4228. _path.push({
  4229. operation: 'member',
  4230. scope: 'child',
  4231. expression: { value: component, type: 'identifier' }
  4232. });
  4233. } else {
  4234. var type = typeof component == "number" ?
  4235. 'numeric_literal' : 'string_literal';
  4236. _path.push({
  4237. operation: 'subscript',
  4238. scope: 'child',
  4239. expression: { value: component, type: type }
  4240. });
  4241. }
  4242. });
  4243. return _path;
  4244. } else if (Array.isArray(path) && typeof path[0] == "object") {
  4245. return path
  4246. }
  4247. throw new Error("couldn't understand path " + path);
  4248. }
  4249. function _is_string(obj) {
  4250. return Object.prototype.toString.call(obj) == '[object String]';
  4251. }
  4252. JSONPath.Handlers = Handlers;
  4253. JSONPath.Parser = Parser;
  4254. var instance = new JSONPath;
  4255. instance.JSONPath = JSONPath;
  4256. module.exports = instance;
  4257. },{"./dict":2,"./handlers":4,"./parser":6,"assert":8}],6:[function(require,module,exports){
  4258. var grammar = require('./grammar');
  4259. var gparser = require('../generated/parser');
  4260. var Parser = function() {
  4261. var parser = new gparser.Parser();
  4262. var _parseError = parser.parseError;
  4263. parser.yy.parseError = function() {
  4264. if (parser.yy.ast) {
  4265. parser.yy.ast.initialize();
  4266. }
  4267. _parseError.apply(parser, arguments);
  4268. }
  4269. return parser;
  4270. };
  4271. Parser.grammar = grammar;
  4272. module.exports = Parser;
  4273. },{"../generated/parser":1,"./grammar":3}],7:[function(require,module,exports){
  4274. module.exports = function(arr, start, end, step) {
  4275. if (typeof start == 'string') throw new Error("start cannot be a string");
  4276. if (typeof end == 'string') throw new Error("end cannot be a string");
  4277. if (typeof step == 'string') throw new Error("step cannot be a string");
  4278. var len = arr.length;
  4279. if (step === 0) throw new Error("step cannot be zero");
  4280. step = step ? integer(step) : 1;
  4281. // normalize negative values
  4282. start = start < 0 ? len + start : start;
  4283. end = end < 0 ? len + end : end;
  4284. // default extents to extents
  4285. start = integer(start === 0 ? 0 : !start ? (step > 0 ? 0 : len - 1) : start);
  4286. end = integer(end === 0 ? 0 : !end ? (step > 0 ? len : -1) : end);
  4287. // clamp extents
  4288. start = step > 0 ? Math.max(0, start) : Math.min(len, start);
  4289. end = step > 0 ? Math.min(end, len) : Math.max(-1, end);
  4290. // return empty if extents are backwards
  4291. if (step > 0 && end <= start) return [];
  4292. if (step < 0 && start <= end) return [];
  4293. var result = [];
  4294. for (var i = start; i != end; i += step) {
  4295. if ((step < 0 && i <= end) || (step > 0 && i >= end)) break;
  4296. result.push(arr[i]);
  4297. }
  4298. return result;
  4299. }
  4300. function integer(val) {
  4301. return String(val).match(/^[0-9]+$/) ? parseInt(val) :
  4302. Number.isFinite(val) ? parseInt(val, 10) : 0;
  4303. }
  4304. },{}],8:[function(require,module,exports){
  4305. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  4306. //
  4307. // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
  4308. //
  4309. // Originally from narwhal.js (http://narwhaljs.org)
  4310. // Copyright (c) 2009 Thomas Robinson <280north.com>
  4311. //
  4312. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4313. // of this software and associated documentation files (the 'Software'), to
  4314. // deal in the Software without restriction, including without limitation the
  4315. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  4316. // sell copies of the Software, and to permit persons to whom the Software is
  4317. // furnished to do so, subject to the following conditions:
  4318. //
  4319. // The above copyright notice and this permission notice shall be included in
  4320. // all copies or substantial portions of the Software.
  4321. //
  4322. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4323. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4324. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4325. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  4326. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  4327. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  4328. // when used in node, this will actually load the util module we depend on
  4329. // versus loading the builtin util module as happens otherwise
  4330. // this is a bug in node module loading as far as I am concerned
  4331. var util = require('util/');
  4332. var pSlice = Array.prototype.slice;
  4333. var hasOwn = Object.prototype.hasOwnProperty;
  4334. // 1. The assert module provides functions that throw
  4335. // AssertionError's when particular conditions are not met. The
  4336. // assert module must conform to the following interface.
  4337. var assert = module.exports = ok;
  4338. // 2. The AssertionError is defined in assert.
  4339. // new assert.AssertionError({ message: message,
  4340. // actual: actual,
  4341. // expected: expected })
  4342. assert.AssertionError = function AssertionError(options) {
  4343. this.name = 'AssertionError';
  4344. this.actual = options.actual;
  4345. this.expected = options.expected;
  4346. this.operator = options.operator;
  4347. if (options.message) {
  4348. this.message = options.message;
  4349. this.generatedMessage = false;
  4350. } else {
  4351. this.message = getMessage(this);
  4352. this.generatedMessage = true;
  4353. }
  4354. var stackStartFunction = options.stackStartFunction || fail;
  4355. if (Error.captureStackTrace) {
  4356. Error.captureStackTrace(this, stackStartFunction);
  4357. }
  4358. else {
  4359. // non v8 browsers so we can have a stacktrace
  4360. var err = new Error();
  4361. if (err.stack) {
  4362. var out = err.stack;
  4363. // try to strip useless frames
  4364. var fn_name = stackStartFunction.name;
  4365. var idx = out.indexOf('\n' + fn_name);
  4366. if (idx >= 0) {
  4367. // once we have located the function frame
  4368. // we need to strip out everything before it (and its line)
  4369. var next_line = out.indexOf('\n', idx + 1);
  4370. out = out.substring(next_line + 1);
  4371. }
  4372. this.stack = out;
  4373. }
  4374. }
  4375. };
  4376. // assert.AssertionError instanceof Error
  4377. util.inherits(assert.AssertionError, Error);
  4378. function replacer(key, value) {
  4379. if (util.isUndefined(value)) {
  4380. return '' + value;
  4381. }
  4382. if (util.isNumber(value) && !isFinite(value)) {
  4383. return value.toString();
  4384. }
  4385. if (util.isFunction(value) || util.isRegExp(value)) {
  4386. return value.toString();
  4387. }
  4388. return value;
  4389. }
  4390. function truncate(s, n) {
  4391. if (util.isString(s)) {
  4392. return s.length < n ? s : s.slice(0, n);
  4393. } else {
  4394. return s;
  4395. }
  4396. }
  4397. function getMessage(self) {
  4398. return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
  4399. self.operator + ' ' +
  4400. truncate(JSON.stringify(self.expected, replacer), 128);
  4401. }
  4402. // At present only the three keys mentioned above are used and
  4403. // understood by the spec. Implementations or sub modules can pass
  4404. // other keys to the AssertionError's constructor - they will be
  4405. // ignored.
  4406. // 3. All of the following functions must throw an AssertionError
  4407. // when a corresponding condition is not met, with a message that
  4408. // may be undefined if not provided. All assertion methods provide
  4409. // both the actual and expected values to the assertion error for
  4410. // display purposes.
  4411. function fail(actual, expected, message, operator, stackStartFunction) {
  4412. throw new assert.AssertionError({
  4413. message: message,
  4414. actual: actual,
  4415. expected: expected,
  4416. operator: operator,
  4417. stackStartFunction: stackStartFunction
  4418. });
  4419. }
  4420. // EXTENSION! allows for well behaved errors defined elsewhere.
  4421. assert.fail = fail;
  4422. // 4. Pure assertion tests whether a value is truthy, as determined
  4423. // by !!guard.
  4424. // assert.ok(guard, message_opt);
  4425. // This statement is equivalent to assert.equal(true, !!guard,
  4426. // message_opt);. To test strictly for the value true, use
  4427. // assert.strictEqual(true, guard, message_opt);.
  4428. function ok(value, message) {
  4429. if (!value) fail(value, true, message, '==', assert.ok);
  4430. }
  4431. assert.ok = ok;
  4432. // 5. The equality assertion tests shallow, coercive equality with
  4433. // ==.
  4434. // assert.equal(actual, expected, message_opt);
  4435. assert.equal = function equal(actual, expected, message) {
  4436. if (actual != expected) fail(actual, expected, message, '==', assert.equal);
  4437. };
  4438. // 6. The non-equality assertion tests for whether two objects are not equal
  4439. // with != assert.notEqual(actual, expected, message_opt);
  4440. assert.notEqual = function notEqual(actual, expected, message) {
  4441. if (actual == expected) {
  4442. fail(actual, expected, message, '!=', assert.notEqual);
  4443. }
  4444. };
  4445. // 7. The equivalence assertion tests a deep equality relation.
  4446. // assert.deepEqual(actual, expected, message_opt);
  4447. assert.deepEqual = function deepEqual(actual, expected, message) {
  4448. if (!_deepEqual(actual, expected)) {
  4449. fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  4450. }
  4451. };
  4452. function _deepEqual(actual, expected) {
  4453. // 7.1. All identical values are equivalent, as determined by ===.
  4454. if (actual === expected) {
  4455. return true;
  4456. } else if (util.isBuffer(actual) && util.isBuffer(expected)) {
  4457. if (actual.length != expected.length) return false;
  4458. for (var i = 0; i < actual.length; i++) {
  4459. if (actual[i] !== expected[i]) return false;
  4460. }
  4461. return true;
  4462. // 7.2. If the expected value is a Date object, the actual value is
  4463. // equivalent if it is also a Date object that refers to the same time.
  4464. } else if (util.isDate(actual) && util.isDate(expected)) {
  4465. return actual.getTime() === expected.getTime();
  4466. // 7.3 If the expected value is a RegExp object, the actual value is
  4467. // equivalent if it is also a RegExp object with the same source and
  4468. // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
  4469. } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
  4470. return actual.source === expected.source &&
  4471. actual.global === expected.global &&
  4472. actual.multiline === expected.multiline &&
  4473. actual.lastIndex === expected.lastIndex &&
  4474. actual.ignoreCase === expected.ignoreCase;
  4475. // 7.4. Other pairs that do not both pass typeof value == 'object',
  4476. // equivalence is determined by ==.
  4477. } else if (!util.isObject(actual) && !util.isObject(expected)) {
  4478. return actual == expected;
  4479. // 7.5 For all other Object pairs, including Array objects, equivalence is
  4480. // determined by having the same number of owned properties (as verified
  4481. // with Object.prototype.hasOwnProperty.call), the same set of keys
  4482. // (although not necessarily the same order), equivalent values for every
  4483. // corresponding key, and an identical 'prototype' property. Note: this
  4484. // accounts for both named and indexed properties on Arrays.
  4485. } else {
  4486. return objEquiv(actual, expected);
  4487. }
  4488. }
  4489. function isArguments(object) {
  4490. return Object.prototype.toString.call(object) == '[object Arguments]';
  4491. }
  4492. function objEquiv(a, b) {
  4493. if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
  4494. return false;
  4495. // an identical 'prototype' property.
  4496. if (a.prototype !== b.prototype) return false;
  4497. // if one is a primitive, the other must be same
  4498. if (util.isPrimitive(a) || util.isPrimitive(b)) {
  4499. return a === b;
  4500. }
  4501. var aIsArgs = isArguments(a),
  4502. bIsArgs = isArguments(b);
  4503. if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
  4504. return false;
  4505. if (aIsArgs) {
  4506. a = pSlice.call(a);
  4507. b = pSlice.call(b);
  4508. return _deepEqual(a, b);
  4509. }
  4510. var ka = objectKeys(a),
  4511. kb = objectKeys(b),
  4512. key, i;
  4513. // having the same number of owned properties (keys incorporates
  4514. // hasOwnProperty)
  4515. if (ka.length != kb.length)
  4516. return false;
  4517. //the same set of keys (although not necessarily the same order),
  4518. ka.sort();
  4519. kb.sort();
  4520. //~~~cheap key test
  4521. for (i = ka.length - 1; i >= 0; i--) {
  4522. if (ka[i] != kb[i])
  4523. return false;
  4524. }
  4525. //equivalent values for every corresponding key, and
  4526. //~~~possibly expensive deep test
  4527. for (i = ka.length - 1; i >= 0; i--) {
  4528. key = ka[i];
  4529. if (!_deepEqual(a[key], b[key])) return false;
  4530. }
  4531. return true;
  4532. }
  4533. // 8. The non-equivalence assertion tests for any deep inequality.
  4534. // assert.notDeepEqual(actual, expected, message_opt);
  4535. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  4536. if (_deepEqual(actual, expected)) {
  4537. fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
  4538. }
  4539. };
  4540. // 9. The strict equality assertion tests strict equality, as determined by ===.
  4541. // assert.strictEqual(actual, expected, message_opt);
  4542. assert.strictEqual = function strictEqual(actual, expected, message) {
  4543. if (actual !== expected) {
  4544. fail(actual, expected, message, '===', assert.strictEqual);
  4545. }
  4546. };
  4547. // 10. The strict non-equality assertion tests for strict inequality, as
  4548. // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
  4549. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  4550. if (actual === expected) {
  4551. fail(actual, expected, message, '!==', assert.notStrictEqual);
  4552. }
  4553. };
  4554. function expectedException(actual, expected) {
  4555. if (!actual || !expected) {
  4556. return false;
  4557. }
  4558. if (Object.prototype.toString.call(expected) == '[object RegExp]') {
  4559. return expected.test(actual);
  4560. } else if (actual instanceof expected) {
  4561. return true;
  4562. } else if (expected.call({}, actual) === true) {
  4563. return true;
  4564. }
  4565. return false;
  4566. }
  4567. function _throws(shouldThrow, block, expected, message) {
  4568. var actual;
  4569. if (util.isString(expected)) {
  4570. message = expected;
  4571. expected = null;
  4572. }
  4573. try {
  4574. block();
  4575. } catch (e) {
  4576. actual = e;
  4577. }
  4578. message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
  4579. (message ? ' ' + message : '.');
  4580. if (shouldThrow && !actual) {
  4581. fail(actual, expected, 'Missing expected exception' + message);
  4582. }
  4583. if (!shouldThrow && expectedException(actual, expected)) {
  4584. fail(actual, expected, 'Got unwanted exception' + message);
  4585. }
  4586. if ((shouldThrow && actual && expected &&
  4587. !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  4588. throw actual;
  4589. }
  4590. }
  4591. // 11. Expected to throw an error:
  4592. // assert.throws(block, Error_opt, message_opt);
  4593. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  4594. _throws.apply(this, [true].concat(pSlice.call(arguments)));
  4595. };
  4596. // EXTENSION! This is annoying to write outside this module.
  4597. assert.doesNotThrow = function(block, /*optional*/message) {
  4598. _throws.apply(this, [false].concat(pSlice.call(arguments)));
  4599. };
  4600. assert.ifError = function(err) { if (err) {throw err;}};
  4601. var objectKeys = Object.keys || function (obj) {
  4602. var keys = [];
  4603. for (var key in obj) {
  4604. if (hasOwn.call(obj, key)) keys.push(key);
  4605. }
  4606. return keys;
  4607. };
  4608. },{"util/":11}],9:[function(require,module,exports){
  4609. if (typeof Object.create === 'function') {
  4610. // implementation from standard node.js 'util' module
  4611. module.exports = function inherits(ctor, superCtor) {
  4612. ctor.super_ = superCtor
  4613. ctor.prototype = Object.create(superCtor.prototype, {
  4614. constructor: {
  4615. value: ctor,
  4616. enumerable: false,
  4617. writable: true,
  4618. configurable: true
  4619. }
  4620. });
  4621. };
  4622. } else {
  4623. // old school shim for old browsers
  4624. module.exports = function inherits(ctor, superCtor) {
  4625. ctor.super_ = superCtor
  4626. var TempCtor = function () {}
  4627. TempCtor.prototype = superCtor.prototype
  4628. ctor.prototype = new TempCtor()
  4629. ctor.prototype.constructor = ctor
  4630. }
  4631. }
  4632. },{}],10:[function(require,module,exports){
  4633. module.exports = function isBuffer(arg) {
  4634. return arg && typeof arg === 'object'
  4635. && typeof arg.copy === 'function'
  4636. && typeof arg.fill === 'function'
  4637. && typeof arg.readUInt8 === 'function';
  4638. }
  4639. },{}],11:[function(require,module,exports){
  4640. (function (process,global){
  4641. // Copyright Joyent, Inc. and other Node contributors.
  4642. //
  4643. // Permission is hereby granted, free of charge, to any person obtaining a
  4644. // copy of this software and associated documentation files (the
  4645. // "Software"), to deal in the Software without restriction, including
  4646. // without limitation the rights to use, copy, modify, merge, publish,
  4647. // distribute, sublicense, and/or sell copies of the Software, and to permit
  4648. // persons to whom the Software is furnished to do so, subject to the
  4649. // following conditions:
  4650. //
  4651. // The above copyright notice and this permission notice shall be included
  4652. // in all copies or substantial portions of the Software.
  4653. //
  4654. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  4655. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  4656. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  4657. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  4658. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  4659. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  4660. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  4661. var formatRegExp = /%[sdj%]/g;
  4662. exports.format = function(f) {
  4663. if (!isString(f)) {
  4664. var objects = [];
  4665. for (var i = 0; i < arguments.length; i++) {
  4666. objects.push(inspect(arguments[i]));
  4667. }
  4668. return objects.join(' ');
  4669. }
  4670. var i = 1;
  4671. var args = arguments;
  4672. var len = args.length;
  4673. var str = String(f).replace(formatRegExp, function(x) {
  4674. if (x === '%%') return '%';
  4675. if (i >= len) return x;
  4676. switch (x) {
  4677. case '%s': return String(args[i++]);
  4678. case '%d': return Number(args[i++]);
  4679. case '%j':
  4680. try {
  4681. return JSON.stringify(args[i++]);
  4682. } catch (_) {
  4683. return '[Circular]';
  4684. }
  4685. default:
  4686. return x;
  4687. }
  4688. });
  4689. for (var x = args[i]; i < len; x = args[++i]) {
  4690. if (isNull(x) || !isObject(x)) {
  4691. str += ' ' + x;
  4692. } else {
  4693. str += ' ' + inspect(x);
  4694. }
  4695. }
  4696. return str;
  4697. };
  4698. // Mark that a method should not be used.
  4699. // Returns a modified function which warns once by default.
  4700. // If --no-deprecation is set, then it is a no-op.
  4701. exports.deprecate = function(fn, msg) {
  4702. // Allow for deprecating things in the process of starting up.
  4703. if (isUndefined(global.process)) {
  4704. return function() {
  4705. return exports.deprecate(fn, msg).apply(this, arguments);
  4706. };
  4707. }
  4708. if (process.noDeprecation === true) {
  4709. return fn;
  4710. }
  4711. var warned = false;
  4712. function deprecated() {
  4713. if (!warned) {
  4714. if (process.throwDeprecation) {
  4715. throw new Error(msg);
  4716. } else if (process.traceDeprecation) {
  4717. console.trace(msg);
  4718. } else {
  4719. console.error(msg);
  4720. }
  4721. warned = true;
  4722. }
  4723. return fn.apply(this, arguments);
  4724. }
  4725. return deprecated;
  4726. };
  4727. var debugs = {};
  4728. var debugEnviron;
  4729. exports.debuglog = function(set) {
  4730. if (isUndefined(debugEnviron))
  4731. debugEnviron = process.env.NODE_DEBUG || '';
  4732. set = set.toUpperCase();
  4733. if (!debugs[set]) {
  4734. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  4735. var pid = process.pid;
  4736. debugs[set] = function() {
  4737. var msg = exports.format.apply(exports, arguments);
  4738. console.error('%s %d: %s', set, pid, msg);
  4739. };
  4740. } else {
  4741. debugs[set] = function() {};
  4742. }
  4743. }
  4744. return debugs[set];
  4745. };
  4746. /**
  4747. * Echos the value of a value. Trys to print the value out
  4748. * in the best way possible given the different types.
  4749. *
  4750. * @param {Object} obj The object to print out.
  4751. * @param {Object} opts Optional options object that alters the output.
  4752. */
  4753. /* legacy: obj, showHidden, depth, colors*/
  4754. function inspect(obj, opts) {
  4755. // default options
  4756. var ctx = {
  4757. seen: [],
  4758. stylize: stylizeNoColor
  4759. };
  4760. // legacy...
  4761. if (arguments.length >= 3) ctx.depth = arguments[2];
  4762. if (arguments.length >= 4) ctx.colors = arguments[3];
  4763. if (isBoolean(opts)) {
  4764. // legacy...
  4765. ctx.showHidden = opts;
  4766. } else if (opts) {
  4767. // got an "options" object
  4768. exports._extend(ctx, opts);
  4769. }
  4770. // set default options
  4771. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  4772. if (isUndefined(ctx.depth)) ctx.depth = 2;
  4773. if (isUndefined(ctx.colors)) ctx.colors = false;
  4774. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  4775. if (ctx.colors) ctx.stylize = stylizeWithColor;
  4776. return formatValue(ctx, obj, ctx.depth);
  4777. }
  4778. exports.inspect = inspect;
  4779. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  4780. inspect.colors = {
  4781. 'bold' : [1, 22],
  4782. 'italic' : [3, 23],
  4783. 'underline' : [4, 24],
  4784. 'inverse' : [7, 27],
  4785. 'white' : [37, 39],
  4786. 'grey' : [90, 39],
  4787. 'black' : [30, 39],
  4788. 'blue' : [34, 39],
  4789. 'cyan' : [36, 39],
  4790. 'green' : [32, 39],
  4791. 'magenta' : [35, 39],
  4792. 'red' : [31, 39],
  4793. 'yellow' : [33, 39]
  4794. };
  4795. // Don't use 'blue' not visible on cmd.exe
  4796. inspect.styles = {
  4797. 'special': 'cyan',
  4798. 'number': 'yellow',
  4799. 'boolean': 'yellow',
  4800. 'undefined': 'grey',
  4801. 'null': 'bold',
  4802. 'string': 'green',
  4803. 'date': 'magenta',
  4804. // "name": intentionally not styling
  4805. 'regexp': 'red'
  4806. };
  4807. function stylizeWithColor(str, styleType) {
  4808. var style = inspect.styles[styleType];
  4809. if (style) {
  4810. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  4811. '\u001b[' + inspect.colors[style][1] + 'm';
  4812. } else {
  4813. return str;
  4814. }
  4815. }
  4816. function stylizeNoColor(str, styleType) {
  4817. return str;
  4818. }
  4819. function arrayToHash(array) {
  4820. var hash = {};
  4821. array.forEach(function(val, idx) {
  4822. hash[val] = true;
  4823. });
  4824. return hash;
  4825. }
  4826. function formatValue(ctx, value, recurseTimes) {
  4827. // Provide a hook for user-specified inspect functions.
  4828. // Check that value is an object with an inspect function on it
  4829. if (ctx.customInspect &&
  4830. value &&
  4831. isFunction(value.inspect) &&
  4832. // Filter out the util module, it's inspect function is special
  4833. value.inspect !== exports.inspect &&
  4834. // Also filter out any prototype objects using the circular check.
  4835. !(value.constructor && value.constructor.prototype === value)) {
  4836. var ret = value.inspect(recurseTimes, ctx);
  4837. if (!isString(ret)) {
  4838. ret = formatValue(ctx, ret, recurseTimes);
  4839. }
  4840. return ret;
  4841. }
  4842. // Primitive types cannot have properties
  4843. var primitive = formatPrimitive(ctx, value);
  4844. if (primitive) {
  4845. return primitive;
  4846. }
  4847. // Look up the keys of the object.
  4848. var keys = Object.keys(value);
  4849. var visibleKeys = arrayToHash(keys);
  4850. if (ctx.showHidden) {
  4851. keys = Object.getOwnPropertyNames(value);
  4852. }
  4853. // IE doesn't make error fields non-enumerable
  4854. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  4855. if (isError(value)
  4856. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  4857. return formatError(value);
  4858. }
  4859. // Some type of object without properties can be shortcutted.
  4860. if (keys.length === 0) {
  4861. if (isFunction(value)) {
  4862. var name = value.name ? ': ' + value.name : '';
  4863. return ctx.stylize('[Function' + name + ']', 'special');
  4864. }
  4865. if (isRegExp(value)) {
  4866. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  4867. }
  4868. if (isDate(value)) {
  4869. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  4870. }
  4871. if (isError(value)) {
  4872. return formatError(value);
  4873. }
  4874. }
  4875. var base = '', array = false, braces = ['{', '}'];
  4876. // Make Array say that they are Array
  4877. if (isArray(value)) {
  4878. array = true;
  4879. braces = ['[', ']'];
  4880. }
  4881. // Make functions say that they are functions
  4882. if (isFunction(value)) {
  4883. var n = value.name ? ': ' + value.name : '';
  4884. base = ' [Function' + n + ']';
  4885. }
  4886. // Make RegExps say that they are RegExps
  4887. if (isRegExp(value)) {
  4888. base = ' ' + RegExp.prototype.toString.call(value);
  4889. }
  4890. // Make dates with properties first say the date
  4891. if (isDate(value)) {
  4892. base = ' ' + Date.prototype.toUTCString.call(value);
  4893. }
  4894. // Make error with message first say the error
  4895. if (isError(value)) {
  4896. base = ' ' + formatError(value);
  4897. }
  4898. if (keys.length === 0 && (!array || value.length == 0)) {
  4899. return braces[0] + base + braces[1];
  4900. }
  4901. if (recurseTimes < 0) {
  4902. if (isRegExp(value)) {
  4903. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  4904. } else {
  4905. return ctx.stylize('[Object]', 'special');
  4906. }
  4907. }
  4908. ctx.seen.push(value);
  4909. var output;
  4910. if (array) {
  4911. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  4912. } else {
  4913. output = keys.map(function(key) {
  4914. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  4915. });
  4916. }
  4917. ctx.seen.pop();
  4918. return reduceToSingleString(output, base, braces);
  4919. }
  4920. function formatPrimitive(ctx, value) {
  4921. if (isUndefined(value))
  4922. return ctx.stylize('undefined', 'undefined');
  4923. if (isString(value)) {
  4924. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  4925. .replace(/'/g, "\\'")
  4926. .replace(/\\"/g, '"') + '\'';
  4927. return ctx.stylize(simple, 'string');
  4928. }
  4929. if (isNumber(value))
  4930. return ctx.stylize('' + value, 'number');
  4931. if (isBoolean(value))
  4932. return ctx.stylize('' + value, 'boolean');
  4933. // For some reason typeof null is "object", so special case here.
  4934. if (isNull(value))
  4935. return ctx.stylize('null', 'null');
  4936. }
  4937. function formatError(value) {
  4938. return '[' + Error.prototype.toString.call(value) + ']';
  4939. }
  4940. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  4941. var output = [];
  4942. for (var i = 0, l = value.length; i < l; ++i) {
  4943. if (hasOwnProperty(value, String(i))) {
  4944. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  4945. String(i), true));
  4946. } else {
  4947. output.push('');
  4948. }
  4949. }
  4950. keys.forEach(function(key) {
  4951. if (!key.match(/^\d+$/)) {
  4952. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  4953. key, true));
  4954. }
  4955. });
  4956. return output;
  4957. }
  4958. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  4959. var name, str, desc;
  4960. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  4961. if (desc.get) {
  4962. if (desc.set) {
  4963. str = ctx.stylize('[Getter/Setter]', 'special');
  4964. } else {
  4965. str = ctx.stylize('[Getter]', 'special');
  4966. }
  4967. } else {
  4968. if (desc.set) {
  4969. str = ctx.stylize('[Setter]', 'special');
  4970. }
  4971. }
  4972. if (!hasOwnProperty(visibleKeys, key)) {
  4973. name = '[' + key + ']';
  4974. }
  4975. if (!str) {
  4976. if (ctx.seen.indexOf(desc.value) < 0) {
  4977. if (isNull(recurseTimes)) {
  4978. str = formatValue(ctx, desc.value, null);
  4979. } else {
  4980. str = formatValue(ctx, desc.value, recurseTimes - 1);
  4981. }
  4982. if (str.indexOf('\n') > -1) {
  4983. if (array) {
  4984. str = str.split('\n').map(function(line) {
  4985. return ' ' + line;
  4986. }).join('\n').substr(2);
  4987. } else {
  4988. str = '\n' + str.split('\n').map(function(line) {
  4989. return ' ' + line;
  4990. }).join('\n');
  4991. }
  4992. }
  4993. } else {
  4994. str = ctx.stylize('[Circular]', 'special');
  4995. }
  4996. }
  4997. if (isUndefined(name)) {
  4998. if (array && key.match(/^\d+$/)) {
  4999. return str;
  5000. }
  5001. name = JSON.stringify('' + key);
  5002. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  5003. name = name.substr(1, name.length - 2);
  5004. name = ctx.stylize(name, 'name');
  5005. } else {
  5006. name = name.replace(/'/g, "\\'")
  5007. .replace(/\\"/g, '"')
  5008. .replace(/(^"|"$)/g, "'");
  5009. name = ctx.stylize(name, 'string');
  5010. }
  5011. }
  5012. return name + ': ' + str;
  5013. }
  5014. function reduceToSingleString(output, base, braces) {
  5015. var numLinesEst = 0;
  5016. var length = output.reduce(function(prev, cur) {
  5017. numLinesEst++;
  5018. if (cur.indexOf('\n') >= 0) numLinesEst++;
  5019. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  5020. }, 0);
  5021. if (length > 60) {
  5022. return braces[0] +
  5023. (base === '' ? '' : base + '\n ') +
  5024. ' ' +
  5025. output.join(',\n ') +
  5026. ' ' +
  5027. braces[1];
  5028. }
  5029. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  5030. }
  5031. // NOTE: These type checking functions intentionally don't use `instanceof`
  5032. // because it is fragile and can be easily faked with `Object.create()`.
  5033. function isArray(ar) {
  5034. return Array.isArray(ar);
  5035. }
  5036. exports.isArray = isArray;
  5037. function isBoolean(arg) {
  5038. return typeof arg === 'boolean';
  5039. }
  5040. exports.isBoolean = isBoolean;
  5041. function isNull(arg) {
  5042. return arg === null;
  5043. }
  5044. exports.isNull = isNull;
  5045. function isNullOrUndefined(arg) {
  5046. return arg == null;
  5047. }
  5048. exports.isNullOrUndefined = isNullOrUndefined;
  5049. function isNumber(arg) {
  5050. return typeof arg === 'number';
  5051. }
  5052. exports.isNumber = isNumber;
  5053. function isString(arg) {
  5054. return typeof arg === 'string';
  5055. }
  5056. exports.isString = isString;
  5057. function isSymbol(arg) {
  5058. return typeof arg === 'symbol';
  5059. }
  5060. exports.isSymbol = isSymbol;
  5061. function isUndefined(arg) {
  5062. return arg === void 0;
  5063. }
  5064. exports.isUndefined = isUndefined;
  5065. function isRegExp(re) {
  5066. return isObject(re) && objectToString(re) === '[object RegExp]';
  5067. }
  5068. exports.isRegExp = isRegExp;
  5069. function isObject(arg) {
  5070. return typeof arg === 'object' && arg !== null;
  5071. }
  5072. exports.isObject = isObject;
  5073. function isDate(d) {
  5074. return isObject(d) && objectToString(d) === '[object Date]';
  5075. }
  5076. exports.isDate = isDate;
  5077. function isError(e) {
  5078. return isObject(e) &&
  5079. (objectToString(e) === '[object Error]' || e instanceof Error);
  5080. }
  5081. exports.isError = isError;
  5082. function isFunction(arg) {
  5083. return typeof arg === 'function';
  5084. }
  5085. exports.isFunction = isFunction;
  5086. function isPrimitive(arg) {
  5087. return arg === null ||
  5088. typeof arg === 'boolean' ||
  5089. typeof arg === 'number' ||
  5090. typeof arg === 'string' ||
  5091. typeof arg === 'symbol' || // ES6 symbol
  5092. typeof arg === 'undefined';
  5093. }
  5094. exports.isPrimitive = isPrimitive;
  5095. exports.isBuffer = require('./support/isBuffer');
  5096. function objectToString(o) {
  5097. return Object.prototype.toString.call(o);
  5098. }
  5099. function pad(n) {
  5100. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  5101. }
  5102. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  5103. 'Oct', 'Nov', 'Dec'];
  5104. // 26 Feb 16:19:34
  5105. function timestamp() {
  5106. var d = new Date();
  5107. var time = [pad(d.getHours()),
  5108. pad(d.getMinutes()),
  5109. pad(d.getSeconds())].join(':');
  5110. return [d.getDate(), months[d.getMonth()], time].join(' ');
  5111. }
  5112. // log is just a thin wrapper to console.log that prepends a timestamp
  5113. exports.log = function() {
  5114. console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
  5115. };
  5116. /**
  5117. * Inherit the prototype methods from one constructor into another.
  5118. *
  5119. * The Function.prototype.inherits from lang.js rewritten as a standalone
  5120. * function (not on Function.prototype). NOTE: If this file is to be loaded
  5121. * during bootstrapping this function needs to be rewritten using some native
  5122. * functions as prototype setup using normal JavaScript does not work as
  5123. * expected during bootstrapping (see mirror.js in r114903).
  5124. *
  5125. * @param {function} ctor Constructor function which needs to inherit the
  5126. * prototype.
  5127. * @param {function} superCtor Constructor function to inherit prototype from.
  5128. */
  5129. exports.inherits = require('inherits');
  5130. exports._extend = function(origin, add) {
  5131. // Don't do anything if add isn't an object
  5132. if (!add || !isObject(add)) return origin;
  5133. var keys = Object.keys(add);
  5134. var i = keys.length;
  5135. while (i--) {
  5136. origin[keys[i]] = add[keys[i]];
  5137. }
  5138. return origin;
  5139. };
  5140. function hasOwnProperty(obj, prop) {
  5141. return Object.prototype.hasOwnProperty.call(obj, prop);
  5142. }
  5143. }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  5144. },{"./support/isBuffer":10,"_process":14,"inherits":9}],12:[function(require,module,exports){
  5145. },{}],13:[function(require,module,exports){
  5146. (function (process){
  5147. // .dirname, .basename, and .extname methods are extracted from Node.js v8.11.1,
  5148. // backported and transplited with Babel, with backwards-compat fixes
  5149. // Copyright Joyent, Inc. and other Node contributors.
  5150. //
  5151. // Permission is hereby granted, free of charge, to any person obtaining a
  5152. // copy of this software and associated documentation files (the
  5153. // "Software"), to deal in the Software without restriction, including
  5154. // without limitation the rights to use, copy, modify, merge, publish,
  5155. // distribute, sublicense, and/or sell copies of the Software, and to permit
  5156. // persons to whom the Software is furnished to do so, subject to the
  5157. // following conditions:
  5158. //
  5159. // The above copyright notice and this permission notice shall be included
  5160. // in all copies or substantial portions of the Software.
  5161. //
  5162. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  5163. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  5164. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  5165. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  5166. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  5167. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  5168. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  5169. // resolves . and .. elements in a path array with directory names there
  5170. // must be no slashes, empty elements, or device names (c:\) in the array
  5171. // (so also no leading and trailing slashes - it does not distinguish
  5172. // relative and absolute paths)
  5173. function normalizeArray(parts, allowAboveRoot) {
  5174. // if the path tries to go above the root, `up` ends up > 0
  5175. var up = 0;
  5176. for (var i = parts.length - 1; i >= 0; i--) {
  5177. var last = parts[i];
  5178. if (last === '.') {
  5179. parts.splice(i, 1);
  5180. } else if (last === '..') {
  5181. parts.splice(i, 1);
  5182. up++;
  5183. } else if (up) {
  5184. parts.splice(i, 1);
  5185. up--;
  5186. }
  5187. }
  5188. // if the path is allowed to go above the root, restore leading ..s
  5189. if (allowAboveRoot) {
  5190. for (; up--; up) {
  5191. parts.unshift('..');
  5192. }
  5193. }
  5194. return parts;
  5195. }
  5196. // path.resolve([from ...], to)
  5197. // posix version
  5198. exports.resolve = function() {
  5199. var resolvedPath = '',
  5200. resolvedAbsolute = false;
  5201. for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  5202. var path = (i >= 0) ? arguments[i] : process.cwd();
  5203. // Skip empty and invalid entries
  5204. if (typeof path !== 'string') {
  5205. throw new TypeError('Arguments to path.resolve must be strings');
  5206. } else if (!path) {
  5207. continue;
  5208. }
  5209. resolvedPath = path + '/' + resolvedPath;
  5210. resolvedAbsolute = path.charAt(0) === '/';
  5211. }
  5212. // At this point the path should be resolved to a full absolute path, but
  5213. // handle relative paths to be safe (might happen when process.cwd() fails)
  5214. // Normalize the path
  5215. resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
  5216. return !!p;
  5217. }), !resolvedAbsolute).join('/');
  5218. return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
  5219. };
  5220. // path.normalize(path)
  5221. // posix version
  5222. exports.normalize = function(path) {
  5223. var isAbsolute = exports.isAbsolute(path),
  5224. trailingSlash = substr(path, -1) === '/';
  5225. // Normalize the path
  5226. path = normalizeArray(filter(path.split('/'), function(p) {
  5227. return !!p;
  5228. }), !isAbsolute).join('/');
  5229. if (!path && !isAbsolute) {
  5230. path = '.';
  5231. }
  5232. if (path && trailingSlash) {
  5233. path += '/';
  5234. }
  5235. return (isAbsolute ? '/' : '') + path;
  5236. };
  5237. // posix version
  5238. exports.isAbsolute = function(path) {
  5239. return path.charAt(0) === '/';
  5240. };
  5241. // posix version
  5242. exports.join = function() {
  5243. var paths = Array.prototype.slice.call(arguments, 0);
  5244. return exports.normalize(filter(paths, function(p, index) {
  5245. if (typeof p !== 'string') {
  5246. throw new TypeError('Arguments to path.join must be strings');
  5247. }
  5248. return p;
  5249. }).join('/'));
  5250. };
  5251. // path.relative(from, to)
  5252. // posix version
  5253. exports.relative = function(from, to) {
  5254. from = exports.resolve(from).substr(1);
  5255. to = exports.resolve(to).substr(1);
  5256. function trim(arr) {
  5257. var start = 0;
  5258. for (; start < arr.length; start++) {
  5259. if (arr[start] !== '') break;
  5260. }
  5261. var end = arr.length - 1;
  5262. for (; end >= 0; end--) {
  5263. if (arr[end] !== '') break;
  5264. }
  5265. if (start > end) return [];
  5266. return arr.slice(start, end - start + 1);
  5267. }
  5268. var fromParts = trim(from.split('/'));
  5269. var toParts = trim(to.split('/'));
  5270. var length = Math.min(fromParts.length, toParts.length);
  5271. var samePartsLength = length;
  5272. for (var i = 0; i < length; i++) {
  5273. if (fromParts[i] !== toParts[i]) {
  5274. samePartsLength = i;
  5275. break;
  5276. }
  5277. }
  5278. var outputParts = [];
  5279. for (var i = samePartsLength; i < fromParts.length; i++) {
  5280. outputParts.push('..');
  5281. }
  5282. outputParts = outputParts.concat(toParts.slice(samePartsLength));
  5283. return outputParts.join('/');
  5284. };
  5285. exports.sep = '/';
  5286. exports.delimiter = ':';
  5287. exports.dirname = function (path) {
  5288. if (typeof path !== 'string') path = path + '';
  5289. if (path.length === 0) return '.';
  5290. var code = path.charCodeAt(0);
  5291. var hasRoot = code === 47 /*/*/;
  5292. var end = -1;
  5293. var matchedSlash = true;
  5294. for (var i = path.length - 1; i >= 1; --i) {
  5295. code = path.charCodeAt(i);
  5296. if (code === 47 /*/*/) {
  5297. if (!matchedSlash) {
  5298. end = i;
  5299. break;
  5300. }
  5301. } else {
  5302. // We saw the first non-path separator
  5303. matchedSlash = false;
  5304. }
  5305. }
  5306. if (end === -1) return hasRoot ? '/' : '.';
  5307. if (hasRoot && end === 1) {
  5308. // return '//';
  5309. // Backwards-compat fix:
  5310. return '/';
  5311. }
  5312. return path.slice(0, end);
  5313. };
  5314. function basename(path) {
  5315. if (typeof path !== 'string') path = path + '';
  5316. var start = 0;
  5317. var end = -1;
  5318. var matchedSlash = true;
  5319. var i;
  5320. for (i = path.length - 1; i >= 0; --i) {
  5321. if (path.charCodeAt(i) === 47 /*/*/) {
  5322. // If we reached a path separator that was not part of a set of path
  5323. // separators at the end of the string, stop now
  5324. if (!matchedSlash) {
  5325. start = i + 1;
  5326. break;
  5327. }
  5328. } else if (end === -1) {
  5329. // We saw the first non-path separator, mark this as the end of our
  5330. // path component
  5331. matchedSlash = false;
  5332. end = i + 1;
  5333. }
  5334. }
  5335. if (end === -1) return '';
  5336. return path.slice(start, end);
  5337. }
  5338. // Uses a mixed approach for backwards-compatibility, as ext behavior changed
  5339. // in new Node.js versions, so only basename() above is backported here
  5340. exports.basename = function (path, ext) {
  5341. var f = basename(path);
  5342. if (ext && f.substr(-1 * ext.length) === ext) {
  5343. f = f.substr(0, f.length - ext.length);
  5344. }
  5345. return f;
  5346. };
  5347. exports.extname = function (path) {
  5348. if (typeof path !== 'string') path = path + '';
  5349. var startDot = -1;
  5350. var startPart = 0;
  5351. var end = -1;
  5352. var matchedSlash = true;
  5353. // Track the state of characters (if any) we see before our first dot and
  5354. // after any path separator we find
  5355. var preDotState = 0;
  5356. for (var i = path.length - 1; i >= 0; --i) {
  5357. var code = path.charCodeAt(i);
  5358. if (code === 47 /*/*/) {
  5359. // If we reached a path separator that was not part of a set of path
  5360. // separators at the end of the string, stop now
  5361. if (!matchedSlash) {
  5362. startPart = i + 1;
  5363. break;
  5364. }
  5365. continue;
  5366. }
  5367. if (end === -1) {
  5368. // We saw the first non-path separator, mark this as the end of our
  5369. // extension
  5370. matchedSlash = false;
  5371. end = i + 1;
  5372. }
  5373. if (code === 46 /*.*/) {
  5374. // If this is our first dot, mark it as the start of our extension
  5375. if (startDot === -1)
  5376. startDot = i;
  5377. else if (preDotState !== 1)
  5378. preDotState = 1;
  5379. } else if (startDot !== -1) {
  5380. // We saw a non-dot and non-path separator before our dot, so we should
  5381. // have a good chance at having a non-empty extension
  5382. preDotState = -1;
  5383. }
  5384. }
  5385. if (startDot === -1 || end === -1 ||
  5386. // We saw a non-dot character immediately before the dot
  5387. preDotState === 0 ||
  5388. // The (right-most) trimmed path component is exactly '..'
  5389. preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
  5390. return '';
  5391. }
  5392. return path.slice(startDot, end);
  5393. };
  5394. function filter (xs, f) {
  5395. if (xs.filter) return xs.filter(f);
  5396. var res = [];
  5397. for (var i = 0; i < xs.length; i++) {
  5398. if (f(xs[i], i, xs)) res.push(xs[i]);
  5399. }
  5400. return res;
  5401. }
  5402. // String.prototype.substr - negative index don't work in IE8
  5403. var substr = 'ab'.substr(-1) === 'b'
  5404. ? function (str, start, len) { return str.substr(start, len) }
  5405. : function (str, start, len) {
  5406. if (start < 0) start = str.length + start;
  5407. return str.substr(start, len);
  5408. }
  5409. ;
  5410. }).call(this,require('_process'))
  5411. },{"_process":14}],14:[function(require,module,exports){
  5412. // shim for using process in browser
  5413. var process = module.exports = {};
  5414. // cached from whatever global is present so that test runners that stub it
  5415. // don't break things. But we need to wrap it in a try catch in case it is
  5416. // wrapped in strict mode code which doesn't define any globals. It's inside a
  5417. // function because try/catches deoptimize in certain engines.
  5418. var cachedSetTimeout;
  5419. var cachedClearTimeout;
  5420. function defaultSetTimout() {
  5421. throw new Error('setTimeout has not been defined');
  5422. }
  5423. function defaultClearTimeout () {
  5424. throw new Error('clearTimeout has not been defined');
  5425. }
  5426. (function () {
  5427. try {
  5428. if (typeof setTimeout === 'function') {
  5429. cachedSetTimeout = setTimeout;
  5430. } else {
  5431. cachedSetTimeout = defaultSetTimout;
  5432. }
  5433. } catch (e) {
  5434. cachedSetTimeout = defaultSetTimout;
  5435. }
  5436. try {
  5437. if (typeof clearTimeout === 'function') {
  5438. cachedClearTimeout = clearTimeout;
  5439. } else {
  5440. cachedClearTimeout = defaultClearTimeout;
  5441. }
  5442. } catch (e) {
  5443. cachedClearTimeout = defaultClearTimeout;
  5444. }
  5445. } ())
  5446. function runTimeout(fun) {
  5447. if (cachedSetTimeout === setTimeout) {
  5448. //normal enviroments in sane situations
  5449. return setTimeout(fun, 0);
  5450. }
  5451. // if setTimeout wasn't available but was latter defined
  5452. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  5453. cachedSetTimeout = setTimeout;
  5454. return setTimeout(fun, 0);
  5455. }
  5456. try {
  5457. // when when somebody has screwed with setTimeout but no I.E. maddness
  5458. return cachedSetTimeout(fun, 0);
  5459. } catch(e){
  5460. try {
  5461. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  5462. return cachedSetTimeout.call(null, fun, 0);
  5463. } catch(e){
  5464. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  5465. return cachedSetTimeout.call(this, fun, 0);
  5466. }
  5467. }
  5468. }
  5469. function runClearTimeout(marker) {
  5470. if (cachedClearTimeout === clearTimeout) {
  5471. //normal enviroments in sane situations
  5472. return clearTimeout(marker);
  5473. }
  5474. // if clearTimeout wasn't available but was latter defined
  5475. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  5476. cachedClearTimeout = clearTimeout;
  5477. return clearTimeout(marker);
  5478. }
  5479. try {
  5480. // when when somebody has screwed with setTimeout but no I.E. maddness
  5481. return cachedClearTimeout(marker);
  5482. } catch (e){
  5483. try {
  5484. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  5485. return cachedClearTimeout.call(null, marker);
  5486. } catch (e){
  5487. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  5488. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  5489. return cachedClearTimeout.call(this, marker);
  5490. }
  5491. }
  5492. }
  5493. var queue = [];
  5494. var draining = false;
  5495. var currentQueue;
  5496. var queueIndex = -1;
  5497. function cleanUpNextTick() {
  5498. if (!draining || !currentQueue) {
  5499. return;
  5500. }
  5501. draining = false;
  5502. if (currentQueue.length) {
  5503. queue = currentQueue.concat(queue);
  5504. } else {
  5505. queueIndex = -1;
  5506. }
  5507. if (queue.length) {
  5508. drainQueue();
  5509. }
  5510. }
  5511. function drainQueue() {
  5512. if (draining) {
  5513. return;
  5514. }
  5515. var timeout = runTimeout(cleanUpNextTick);
  5516. draining = true;
  5517. var len = queue.length;
  5518. while(len) {
  5519. currentQueue = queue;
  5520. queue = [];
  5521. while (++queueIndex < len) {
  5522. if (currentQueue) {
  5523. currentQueue[queueIndex].run();
  5524. }
  5525. }
  5526. queueIndex = -1;
  5527. len = queue.length;
  5528. }
  5529. currentQueue = null;
  5530. draining = false;
  5531. runClearTimeout(timeout);
  5532. }
  5533. process.nextTick = function (fun) {
  5534. var args = new Array(arguments.length - 1);
  5535. if (arguments.length > 1) {
  5536. for (var i = 1; i < arguments.length; i++) {
  5537. args[i - 1] = arguments[i];
  5538. }
  5539. }
  5540. queue.push(new Item(fun, args));
  5541. if (queue.length === 1 && !draining) {
  5542. runTimeout(drainQueue);
  5543. }
  5544. };
  5545. // v8 likes predictible objects
  5546. function Item(fun, array) {
  5547. this.fun = fun;
  5548. this.array = array;
  5549. }
  5550. Item.prototype.run = function () {
  5551. this.fun.apply(null, this.array);
  5552. };
  5553. process.title = 'browser';
  5554. process.browser = true;
  5555. process.env = {};
  5556. process.argv = [];
  5557. process.version = ''; // empty string to avoid regexp issues
  5558. process.versions = {};
  5559. function noop() {}
  5560. process.on = noop;
  5561. process.addListener = noop;
  5562. process.once = noop;
  5563. process.off = noop;
  5564. process.removeListener = noop;
  5565. process.removeAllListeners = noop;
  5566. process.emit = noop;
  5567. process.prependListener = noop;
  5568. process.prependOnceListener = noop;
  5569. process.listeners = function (name) { return [] }
  5570. process.binding = function (name) {
  5571. throw new Error('process.binding is not supported');
  5572. };
  5573. process.cwd = function () { return '/' };
  5574. process.chdir = function (dir) {
  5575. throw new Error('process.chdir is not supported');
  5576. };
  5577. process.umask = function() { return 0; };
  5578. },{}],15:[function(require,module,exports){
  5579. var unparse = require('escodegen').generate;
  5580. module.exports = function (ast, vars) {
  5581. if (!vars) vars = {};
  5582. var FAIL = {};
  5583. var result = (function walk (node, scopeVars) {
  5584. if (node.type === 'Literal') {
  5585. return node.value;
  5586. }
  5587. else if (node.type === 'UnaryExpression'){
  5588. var val = walk(node.argument)
  5589. if (node.operator === '+') return +val
  5590. if (node.operator === '-') return -val
  5591. if (node.operator === '~') return ~val
  5592. if (node.operator === '!') return !val
  5593. return FAIL
  5594. }
  5595. else if (node.type === 'ArrayExpression') {
  5596. var xs = [];
  5597. for (var i = 0, l = node.elements.length; i < l; i++) {
  5598. var x = walk(node.elements[i]);
  5599. if (x === FAIL) return FAIL;
  5600. xs.push(x);
  5601. }
  5602. return xs;
  5603. }
  5604. else if (node.type === 'ObjectExpression') {
  5605. var obj = {};
  5606. for (var i = 0; i < node.properties.length; i++) {
  5607. var prop = node.properties[i];
  5608. var value = prop.value === null
  5609. ? prop.value
  5610. : walk(prop.value)
  5611. ;
  5612. if (value === FAIL) return FAIL;
  5613. obj[prop.key.value || prop.key.name] = value;
  5614. }
  5615. return obj;
  5616. }
  5617. else if (node.type === 'BinaryExpression' ||
  5618. node.type === 'LogicalExpression') {
  5619. var l = walk(node.left);
  5620. if (l === FAIL) return FAIL;
  5621. var r = walk(node.right);
  5622. if (r === FAIL) return FAIL;
  5623. var op = node.operator;
  5624. if (op === '==') return l == r;
  5625. if (op === '===') return l === r;
  5626. if (op === '!=') return l != r;
  5627. if (op === '!==') return l !== r;
  5628. if (op === '+') return l + r;
  5629. if (op === '-') return l - r;
  5630. if (op === '*') return l * r;
  5631. if (op === '/') return l / r;
  5632. if (op === '%') return l % r;
  5633. if (op === '<') return l < r;
  5634. if (op === '<=') return l <= r;
  5635. if (op === '>') return l > r;
  5636. if (op === '>=') return l >= r;
  5637. if (op === '|') return l | r;
  5638. if (op === '&') return l & r;
  5639. if (op === '^') return l ^ r;
  5640. if (op === '&&') return l && r;
  5641. if (op === '||') return l || r;
  5642. return FAIL;
  5643. }
  5644. else if (node.type === 'Identifier') {
  5645. if ({}.hasOwnProperty.call(vars, node.name)) {
  5646. return vars[node.name];
  5647. }
  5648. else return FAIL;
  5649. }
  5650. else if (node.type === 'ThisExpression') {
  5651. if ({}.hasOwnProperty.call(vars, 'this')) {
  5652. return vars['this'];
  5653. }
  5654. else return FAIL;
  5655. }
  5656. else if (node.type === 'CallExpression') {
  5657. var callee = walk(node.callee);
  5658. if (callee === FAIL) return FAIL;
  5659. if (typeof callee !== 'function') return FAIL;
  5660. var ctx = node.callee.object ? walk(node.callee.object) : FAIL;
  5661. if (ctx === FAIL) ctx = null;
  5662. var args = [];
  5663. for (var i = 0, l = node.arguments.length; i < l; i++) {
  5664. var x = walk(node.arguments[i]);
  5665. if (x === FAIL) return FAIL;
  5666. args.push(x);
  5667. }
  5668. return callee.apply(ctx, args);
  5669. }
  5670. else if (node.type === 'MemberExpression') {
  5671. var obj = walk(node.object);
  5672. // do not allow access to methods on Function
  5673. if((obj === FAIL) || (typeof obj == 'function')){
  5674. return FAIL;
  5675. }
  5676. if (node.property.type === 'Identifier') {
  5677. return obj[node.property.name];
  5678. }
  5679. var prop = walk(node.property);
  5680. if (prop === FAIL) return FAIL;
  5681. return obj[prop];
  5682. }
  5683. else if (node.type === 'ConditionalExpression') {
  5684. var val = walk(node.test)
  5685. if (val === FAIL) return FAIL;
  5686. return val ? walk(node.consequent) : walk(node.alternate)
  5687. }
  5688. else if (node.type === 'ExpressionStatement') {
  5689. var val = walk(node.expression)
  5690. if (val === FAIL) return FAIL;
  5691. return val;
  5692. }
  5693. else if (node.type === 'ReturnStatement') {
  5694. return walk(node.argument)
  5695. }
  5696. else if (node.type === 'FunctionExpression') {
  5697. var bodies = node.body.body;
  5698. // Create a "scope" for our arguments
  5699. var oldVars = {};
  5700. Object.keys(vars).forEach(function(element){
  5701. oldVars[element] = vars[element];
  5702. })
  5703. for(var i=0; i<node.params.length; i++){
  5704. var key = node.params[i];
  5705. if(key.type == 'Identifier'){
  5706. vars[key.name] = null;
  5707. }
  5708. else return FAIL;
  5709. }
  5710. for(var i in bodies){
  5711. if(walk(bodies[i]) === FAIL){
  5712. return FAIL;
  5713. }
  5714. }
  5715. // restore the vars and scope after we walk
  5716. vars = oldVars;
  5717. var keys = Object.keys(vars);
  5718. var vals = keys.map(function(key) {
  5719. return vars[key];
  5720. });
  5721. return Function(keys.join(', '), 'return ' + unparse(node)).apply(null, vals);
  5722. }
  5723. else if (node.type === 'TemplateLiteral') {
  5724. var str = '';
  5725. for (var i = 0; i < node.expressions.length; i++) {
  5726. str += walk(node.quasis[i]);
  5727. str += walk(node.expressions[i]);
  5728. }
  5729. str += walk(node.quasis[i]);
  5730. return str;
  5731. }
  5732. else if (node.type === 'TaggedTemplateExpression') {
  5733. var tag = walk(node.tag);
  5734. var quasi = node.quasi;
  5735. var strings = quasi.quasis.map(walk);
  5736. var values = quasi.expressions.map(walk);
  5737. return tag.apply(null, [strings].concat(values));
  5738. }
  5739. else if (node.type === 'TemplateElement') {
  5740. return node.value.cooked;
  5741. }
  5742. else return FAIL;
  5743. })(ast);
  5744. return result === FAIL ? undefined : result;
  5745. };
  5746. },{"escodegen":12}],"jsonpath":[function(require,module,exports){
  5747. module.exports = require('./lib/index');
  5748. },{"./lib/index":5}]},{},["jsonpath"])("jsonpath")
  5749. });