parse.js 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Parser based on parse-js (http://marijn.haverbeke.nl/parse-js/).
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions
  13. are met:
  14. * Redistributions of source code must retain the above
  15. copyright notice, this list of conditions and the following
  16. disclaimer.
  17. * Redistributions in binary form must reproduce the above
  18. copyright notice, this list of conditions and the following
  19. disclaimer in the documentation and/or other materials
  20. provided with the distribution.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  22. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  25. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  26. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  30. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. SUCH DAMAGE.
  33. ***********************************************************************/
  34. "use strict";
  35. var KEYWORDS = 'break case catch class const continue debugger default delete do else export extends finally for function if in instanceof let new return switch throw try typeof var void while with';
  36. var KEYWORDS_ATOM = 'false null true';
  37. var RESERVED_WORDS = 'enum implements import interface package private protected public static super this ' + KEYWORDS_ATOM + " " + KEYWORDS;
  38. var KEYWORDS_BEFORE_EXPRESSION = 'return new delete throw else case yield await';
  39. KEYWORDS = makePredicate(KEYWORDS);
  40. RESERVED_WORDS = makePredicate(RESERVED_WORDS);
  41. KEYWORDS_BEFORE_EXPRESSION = makePredicate(KEYWORDS_BEFORE_EXPRESSION);
  42. KEYWORDS_ATOM = makePredicate(KEYWORDS_ATOM);
  43. var OPERATOR_CHARS = makePredicate(characters("+-*&%=<>!?|~^"));
  44. var RE_NUM_LITERAL = /[0-9a-f]/i;
  45. var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i;
  46. var RE_OCT_NUMBER = /^0[0-7]+$/;
  47. var RE_ES6_OCT_NUMBER = /^0o[0-7]+$/i;
  48. var RE_BIN_NUMBER = /^0b[01]+$/i;
  49. var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i;
  50. var OPERATORS = makePredicate([
  51. "in",
  52. "instanceof",
  53. "typeof",
  54. "new",
  55. "void",
  56. "delete",
  57. "++",
  58. "--",
  59. "+",
  60. "-",
  61. "!",
  62. "~",
  63. "&",
  64. "|",
  65. "^",
  66. "*",
  67. "**",
  68. "/",
  69. "%",
  70. ">>",
  71. "<<",
  72. ">>>",
  73. "<",
  74. ">",
  75. "<=",
  76. ">=",
  77. "==",
  78. "===",
  79. "!=",
  80. "!==",
  81. "?",
  82. "=",
  83. "+=",
  84. "-=",
  85. "/=",
  86. "*=",
  87. "**=",
  88. "%=",
  89. ">>=",
  90. "<<=",
  91. ">>>=",
  92. "|=",
  93. "^=",
  94. "&=",
  95. "&&",
  96. "||"
  97. ]);
  98. var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\uFEFF"));
  99. var NEWLINE_CHARS = makePredicate(characters("\n\r\u2028\u2029"));
  100. var PUNC_AFTER_EXPRESSION = makePredicate(characters(";]),:"));
  101. var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,;:"));
  102. var PUNC_CHARS = makePredicate(characters("[]{}(),;:"));
  103. /* -----[ Tokenizer ]----- */
  104. // surrogate safe regexps adapted from https://github.com/mathiasbynens/unicode-8.0.0/tree/89b412d8a71ecca9ed593d9e9fa073ab64acfebe/Binary_Property
  105. var UNICODE = {
  106. ID_Start: /[A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0-\u08B4\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\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\u0D5F-\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-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309B-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FD5\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC03-\uDC37\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDF00-\uDF19]|\uD806[\uDCA0-\uDCDF\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50\uDF93-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD83A[\uDC00-\uDCC4]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1]|\uD87E[\uDC00-\uDE1D]/,
  107. ID_Continue: /[0-9A-Z_a-z\xAA\xB5\xB7\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0-\u08B4\u08E3-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0AF9\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58-\u0C5A\u0C60-\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D01-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D5F-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1369-\u1371\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1CF8\u1CF9\u1D00-\u1DF5\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FD5\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA8FD\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\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-\uFE2F\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDDFD\uDE80-\uDE9C\uDEA0-\uDED0\uDEE0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF7A\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCA0-\uDCA9\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00-\uDE03\uDE05\uDE06\uDE0C-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE38-\uDE3A\uDE3F\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE6\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC00-\uDC46\uDC66-\uDC6F\uDC7F-\uDCBA\uDCD0-\uDCE8\uDCF0-\uDCF9\uDD00-\uDD34\uDD36-\uDD3F\uDD50-\uDD73\uDD76\uDD80-\uDDC4\uDDCA-\uDDCC\uDDD0-\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE37\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3C-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF50\uDF57\uDF5D-\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC80-\uDCC5\uDCC7\uDCD0-\uDCD9\uDD80-\uDDB5\uDDB8-\uDDC0\uDDD8-\uDDDD\uDE00-\uDE40\uDE44\uDE50-\uDE59\uDE80-\uDEB7\uDEC0-\uDEC9\uDF00-\uDF19\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDCA0-\uDCE9\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDED0-\uDEED\uDEF0-\uDEF4\uDF00-\uDF36\uDF40-\uDF43\uDF50-\uDF59\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50-\uDF7E\uDF8F-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD83A[\uDC00-\uDCC4\uDCD0-\uDCD6]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1]|\uD87E[\uDC00-\uDE1D]|\uDB40[\uDD00-\uDDEF]/,
  108. };
  109. function get_full_char(str, pos) {
  110. var char = str.charAt(pos);
  111. if (is_surrogate_pair_head(char)) {
  112. var next = str.charAt(pos + 1);
  113. if (is_surrogate_pair_tail(next)) {
  114. return char + next;
  115. }
  116. }
  117. if (is_surrogate_pair_tail(char)) {
  118. var prev = str.charAt(pos - 1);
  119. if (is_surrogate_pair_head(prev)) {
  120. return prev + char;
  121. }
  122. }
  123. return char;
  124. }
  125. function get_full_char_code(str, pos) {
  126. // https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
  127. if (is_surrogate_pair_head(str.charAt(pos))) {
  128. return 0x10000 + (str.charCodeAt(pos) - 0xd800 << 10) + str.charCodeAt(pos + 1) - 0xdc00;
  129. }
  130. return str.charCodeAt(pos);
  131. }
  132. function get_full_char_length(str) {
  133. var surrogates = 0;
  134. for (var i = 0; i < str.length; i++) {
  135. if (is_surrogate_pair_head(str.charCodeAt(i))) {
  136. if (is_surrogate_pair_tail(str.charCodeAt(i + 1))) {
  137. surrogates++;
  138. i++;
  139. }
  140. }
  141. }
  142. return str.length - surrogates;
  143. }
  144. function from_char_code(code) {
  145. // Based on https://github.com/mathiasbynens/String.fromCodePoint/blob/master/fromcodepoint.js
  146. if (code > 0xFFFF) {
  147. code -= 0x10000;
  148. return (String.fromCharCode((code >> 10) + 0xD800) +
  149. String.fromCharCode((code % 0x400) + 0xDC00));
  150. }
  151. return String.fromCharCode(code);
  152. }
  153. function is_surrogate_pair_head(code) {
  154. if (typeof code === "string")
  155. code = code.charCodeAt(0);
  156. return code >= 0xd800 && code <= 0xdbff;
  157. }
  158. function is_surrogate_pair_tail(code) {
  159. if (typeof code === "string")
  160. code = code.charCodeAt(0);
  161. return code >= 0xdc00 && code <= 0xdfff;
  162. }
  163. function is_digit(code) {
  164. return code >= 48 && code <= 57;
  165. };
  166. function is_identifier(name) {
  167. if (typeof name !== "string" || RESERVED_WORDS(name))
  168. return false;
  169. return true;
  170. };
  171. function is_identifier_start(ch) {
  172. var code = ch.charCodeAt(0);
  173. return UNICODE.ID_Start.test(ch) || code == 36 || code == 95;
  174. };
  175. function is_identifier_char(ch) {
  176. var code = ch.charCodeAt(0);
  177. return UNICODE.ID_Continue.test(ch)
  178. || code == 36
  179. || code == 95
  180. || code == 8204 // \u200c: zero-width non-joiner <ZWNJ>
  181. || code == 8205 // \u200d: zero-width joiner <ZWJ> (in my ECMA-262 PDF, this is also 200c)
  182. ;
  183. };
  184. function is_identifier_string(str){
  185. return /^[a-z_$][a-z0-9_$]*$/i.test(str);
  186. };
  187. function parse_js_number(num) {
  188. if (RE_HEX_NUMBER.test(num)) {
  189. return parseInt(num.substr(2), 16);
  190. } else if (RE_OCT_NUMBER.test(num)) {
  191. return parseInt(num.substr(1), 8);
  192. } else if (RE_ES6_OCT_NUMBER.test(num)) {
  193. return parseInt(num.substr(2), 8);
  194. } else if (RE_BIN_NUMBER.test(num)) {
  195. return parseInt(num.substr(2), 2);
  196. } else if (RE_DEC_NUMBER.test(num)) {
  197. return parseFloat(num);
  198. } else {
  199. var val = parseFloat(num);
  200. if (val == num) return val;
  201. }
  202. };
  203. function JS_Parse_Error(message, filename, line, col, pos) {
  204. this.message = message;
  205. this.filename = filename;
  206. this.line = line;
  207. this.col = col;
  208. this.pos = pos;
  209. };
  210. JS_Parse_Error.prototype = Object.create(Error.prototype);
  211. JS_Parse_Error.prototype.constructor = JS_Parse_Error;
  212. JS_Parse_Error.prototype.name = "SyntaxError";
  213. configure_error_stack(JS_Parse_Error);
  214. function js_error(message, filename, line, col, pos) {
  215. throw new JS_Parse_Error(message, filename, line, col, pos);
  216. };
  217. function is_token(token, type, val) {
  218. return token.type == type && (val == null || token.value == val);
  219. };
  220. var EX_EOF = {};
  221. function tokenizer($TEXT, filename, html5_comments, shebang) {
  222. var S = {
  223. text : $TEXT,
  224. filename : filename,
  225. pos : 0,
  226. tokpos : 0,
  227. line : 1,
  228. tokline : 0,
  229. col : 0,
  230. tokcol : 0,
  231. newline_before : false,
  232. regex_allowed : false,
  233. brace_counter : 0,
  234. template_braces : [],
  235. comments_before : [],
  236. directives : {},
  237. directive_stack : []
  238. };
  239. function peek() { return get_full_char(S.text, S.pos); };
  240. function next(signal_eof, in_string) {
  241. var ch = get_full_char(S.text, S.pos++);
  242. if (signal_eof && !ch)
  243. throw EX_EOF;
  244. if (NEWLINE_CHARS(ch)) {
  245. S.newline_before = S.newline_before || !in_string;
  246. ++S.line;
  247. S.col = 0;
  248. if (!in_string && ch == "\r" && peek() == "\n") {
  249. // treat a \r\n sequence as a single \n
  250. ++S.pos;
  251. ch = "\n";
  252. }
  253. } else {
  254. if (ch.length > 1) {
  255. ++S.pos;
  256. ++S.col;
  257. }
  258. ++S.col;
  259. }
  260. return ch;
  261. };
  262. function forward(i) {
  263. while (i-- > 0) next();
  264. };
  265. function looking_at(str) {
  266. return S.text.substr(S.pos, str.length) == str;
  267. };
  268. function find_eol() {
  269. var text = S.text;
  270. for (var i = S.pos, n = S.text.length; i < n; ++i) {
  271. var ch = text[i];
  272. if (NEWLINE_CHARS(ch))
  273. return i;
  274. }
  275. return -1;
  276. };
  277. function find(what, signal_eof) {
  278. var pos = S.text.indexOf(what, S.pos);
  279. if (signal_eof && pos == -1) throw EX_EOF;
  280. return pos;
  281. };
  282. function start_token() {
  283. S.tokline = S.line;
  284. S.tokcol = S.col;
  285. S.tokpos = S.pos;
  286. };
  287. var prev_was_dot = false;
  288. function token(type, value, is_comment) {
  289. S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX(value)) ||
  290. (type == "keyword" && KEYWORDS_BEFORE_EXPRESSION(value)) ||
  291. (type == "punc" && PUNC_BEFORE_EXPRESSION(value))) ||
  292. (type == "arrow");
  293. if (type == "punc" && value == ".") {
  294. prev_was_dot = true;
  295. } else if (!is_comment) {
  296. prev_was_dot = false;
  297. }
  298. var ret = {
  299. type : type,
  300. value : value,
  301. line : S.tokline,
  302. col : S.tokcol,
  303. pos : S.tokpos,
  304. endline : S.line,
  305. endcol : S.col,
  306. endpos : S.pos,
  307. nlb : S.newline_before,
  308. file : filename
  309. };
  310. if (/^(?:num|string|regexp)$/i.test(type)) {
  311. ret.raw = $TEXT.substring(ret.pos, ret.endpos);
  312. }
  313. if (!is_comment) {
  314. ret.comments_before = S.comments_before;
  315. ret.comments_after = S.comments_before = [];
  316. }
  317. S.newline_before = false;
  318. return new AST_Token(ret);
  319. };
  320. function skip_whitespace() {
  321. while (WHITESPACE_CHARS(peek()))
  322. next();
  323. };
  324. function read_while(pred) {
  325. var ret = "", ch, i = 0;
  326. while ((ch = peek()) && pred(ch, i++))
  327. ret += next();
  328. return ret;
  329. };
  330. function parse_error(err) {
  331. js_error(err, filename, S.tokline, S.tokcol, S.tokpos);
  332. };
  333. function read_num(prefix) {
  334. var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".";
  335. var num = read_while(function(ch, i){
  336. var code = ch.charCodeAt(0);
  337. switch (code) {
  338. case 98: case 66: // bB
  339. return (has_x = true); // Can occur in hex sequence, don't return false yet
  340. case 111: case 79: // oO
  341. case 120: case 88: // xX
  342. return has_x ? false : (has_x = true);
  343. case 101: case 69: // eE
  344. return has_x ? true : has_e ? false : (has_e = after_e = true);
  345. case 45: // -
  346. return after_e || (i == 0 && !prefix);
  347. case 43: // +
  348. return after_e;
  349. case (after_e = false, 46): // .
  350. return (!has_dot && !has_x && !has_e) ? (has_dot = true) : false;
  351. }
  352. return RE_NUM_LITERAL.test(ch);
  353. });
  354. if (prefix) num = prefix + num;
  355. if (RE_OCT_NUMBER.test(num) && next_token.has_directive("use strict")) {
  356. parse_error("Legacy octal literals are not allowed in strict mode");
  357. }
  358. var valid = parse_js_number(num);
  359. if (!isNaN(valid)) {
  360. return token("num", valid);
  361. } else {
  362. parse_error("Invalid syntax: " + num);
  363. }
  364. };
  365. function read_escaped_char(in_string) {
  366. var ch = next(true, in_string);
  367. switch (ch.charCodeAt(0)) {
  368. case 110 : return "\n";
  369. case 114 : return "\r";
  370. case 116 : return "\t";
  371. case 98 : return "\b";
  372. case 118 : return "\u000b"; // \v
  373. case 102 : return "\f";
  374. case 120 : return String.fromCharCode(hex_bytes(2)); // \x
  375. case 117 : // \u
  376. if (peek() == "{") {
  377. next(true);
  378. if (peek() === "}")
  379. parse_error("Expecting hex-character between {}");
  380. while (peek() == "0") next(true); // No significance
  381. var result, length = find("}", true) - S.pos;
  382. // Avoid 32 bit integer overflow (1 << 32 === 1)
  383. // We know first character isn't 0 and thus out of range anyway
  384. if (length > 6 || (result = hex_bytes(length)) > 0x10FFFF) {
  385. parse_error("Unicode reference out of bounce");
  386. }
  387. next(true);
  388. return from_char_code(result);
  389. }
  390. return String.fromCharCode(hex_bytes(4));
  391. case 10 : return ""; // newline
  392. case 13 : // \r
  393. if (peek() == "\n") { // DOS newline
  394. next(true, in_string);
  395. return "";
  396. }
  397. }
  398. if (ch >= "0" && ch <= "7")
  399. return read_octal_escape_sequence(ch);
  400. return ch;
  401. };
  402. function read_octal_escape_sequence(ch) {
  403. // Read
  404. var p = peek();
  405. if (p >= "0" && p <= "7") {
  406. ch += next(true);
  407. if (ch[0] <= "3" && (p = peek()) >= "0" && p <= "7")
  408. ch += next(true);
  409. }
  410. // Parse
  411. if (ch === "0") return "\0";
  412. if (ch.length > 0 && next_token.has_directive("use strict"))
  413. parse_error("Legacy octal escape sequences are not allowed in strict mode");
  414. return String.fromCharCode(parseInt(ch, 8));
  415. }
  416. function hex_bytes(n) {
  417. var num = 0;
  418. for (; n > 0; --n) {
  419. var digit = parseInt(next(true), 16);
  420. if (isNaN(digit))
  421. parse_error("Invalid hex-character pattern in string");
  422. num = (num << 4) | digit;
  423. }
  424. return num;
  425. };
  426. var read_string = with_eof_error("Unterminated string constant", function(quote_char){
  427. var quote = next(), ret = "";
  428. for (;;) {
  429. var ch = next(true, true);
  430. if (ch == "\\") ch = read_escaped_char(true);
  431. else if (NEWLINE_CHARS(ch)) parse_error("Unterminated string constant");
  432. else if (ch == quote) break;
  433. ret += ch;
  434. }
  435. var tok = token("string", ret);
  436. tok.quote = quote_char;
  437. return tok;
  438. });
  439. var read_template_characters = with_eof_error("Unterminated template", function(begin){
  440. if (begin) {
  441. S.template_braces.push(S.brace_counter);
  442. }
  443. var content = "", raw = "", ch, tok;
  444. next(true, true);
  445. while ((ch = next(true, true)) != "`") {
  446. if (ch == "\r") {
  447. if (peek() == "\n") ++S.pos;
  448. ch = "\n";
  449. } else if (ch == "$" && peek() == "{") {
  450. next(true, true);
  451. S.brace_counter++;
  452. tok = token(begin ? "template_head" : "template_substitution", content);
  453. tok.begin = begin;
  454. tok.raw = raw;
  455. tok.end = false;
  456. return tok;
  457. }
  458. raw += ch;
  459. if (ch == "\\") {
  460. var tmp = S.pos;
  461. ch = read_escaped_char();
  462. raw += S.text.substr(tmp, S.pos - tmp);
  463. }
  464. content += ch;
  465. }
  466. S.template_braces.pop();
  467. tok = token(begin ? "template_head" : "template_substitution", content);
  468. tok.begin = begin;
  469. tok.raw = raw;
  470. tok.end = true;
  471. return tok;
  472. });
  473. function skip_line_comment(type) {
  474. var regex_allowed = S.regex_allowed;
  475. var i = find_eol(), ret;
  476. if (i == -1) {
  477. ret = S.text.substr(S.pos);
  478. S.pos = S.text.length;
  479. } else {
  480. ret = S.text.substring(S.pos, i);
  481. S.pos = i;
  482. }
  483. S.col = S.tokcol + (S.pos - S.tokpos);
  484. S.comments_before.push(token(type, ret, true));
  485. S.regex_allowed = regex_allowed;
  486. return next_token;
  487. };
  488. var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function(){
  489. var regex_allowed = S.regex_allowed;
  490. var i = find("*/", true);
  491. var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, '\n');
  492. // update stream position
  493. forward(get_full_char_length(text) /* text length doesn't count \r\n as 2 char while S.pos - i does */ + 2);
  494. S.comments_before.push(token("comment2", text, true));
  495. S.newline_before = S.newline_before || text.indexOf("\n") >= 0;
  496. S.regex_allowed = regex_allowed;
  497. return next_token;
  498. });
  499. var read_name = with_eof_error("Unterminated identifier name", function() {
  500. var name = "", ch, escaped = false, hex;
  501. var read_escaped_identifier_char = function() {
  502. escaped = true;
  503. next();
  504. if (peek() !== "u") {
  505. parse_error("Expecting UnicodeEscapeSequence -- uXXXX or u{XXXX}");
  506. }
  507. return read_escaped_char();
  508. }
  509. // Read first character (ID_Start)
  510. if ((name = peek()) === "\\") {
  511. name = read_escaped_identifier_char();
  512. if (!is_identifier_start(name)) {
  513. parse_error("First identifier char is an invalid identifier char");
  514. }
  515. } else if (is_identifier_start(name)){
  516. next();
  517. } else {
  518. return "";
  519. }
  520. // Read ID_Continue
  521. while ((ch = peek()) != null) {
  522. if ((ch = peek()) === "\\") {
  523. ch = read_escaped_identifier_char();
  524. if (!is_identifier_char(ch)) {
  525. parse_error("Invalid escaped identifier char");
  526. }
  527. } else {
  528. if (!is_identifier_char(ch)) {
  529. break;
  530. }
  531. next();
  532. }
  533. name += ch;
  534. }
  535. if (RESERVED_WORDS(name) && escaped) {
  536. parse_error("Escaped characters are not allowed in keywords");
  537. }
  538. return name;
  539. });
  540. var read_regexp = with_eof_error("Unterminated regular expression", function(source) {
  541. var prev_backslash = false, ch, in_class = false;
  542. while ((ch = next(true))) if (NEWLINE_CHARS(ch)) {
  543. parse_error("Unexpected line terminator");
  544. } else if (prev_backslash) {
  545. source += "\\" + ch;
  546. prev_backslash = false;
  547. } else if (ch == "[") {
  548. in_class = true;
  549. source += ch;
  550. } else if (ch == "]" && in_class) {
  551. in_class = false;
  552. source += ch;
  553. } else if (ch == "/" && !in_class) {
  554. break;
  555. } else if (ch == "\\") {
  556. prev_backslash = true;
  557. } else {
  558. source += ch;
  559. }
  560. var mods = read_name();
  561. try {
  562. var regexp = new RegExp(source, mods);
  563. regexp.raw_source = source;
  564. return token("regexp", regexp);
  565. } catch(e) {
  566. parse_error(e.message);
  567. }
  568. });
  569. function read_operator(prefix) {
  570. function grow(op) {
  571. if (!peek()) return op;
  572. var bigger = op + peek();
  573. if (OPERATORS(bigger)) {
  574. next();
  575. return grow(bigger);
  576. } else {
  577. return op;
  578. }
  579. };
  580. return token("operator", grow(prefix || next()));
  581. };
  582. function handle_slash() {
  583. next();
  584. switch (peek()) {
  585. case "/":
  586. next();
  587. return skip_line_comment("comment1");
  588. case "*":
  589. next();
  590. return skip_multiline_comment();
  591. }
  592. return S.regex_allowed ? read_regexp("") : read_operator("/");
  593. };
  594. function handle_eq_sign() {
  595. next();
  596. if (peek() === ">") {
  597. next();
  598. return token("arrow", "=>");
  599. } else {
  600. return read_operator("=");
  601. }
  602. };
  603. function handle_dot() {
  604. next();
  605. if (is_digit(peek().charCodeAt(0))) {
  606. return read_num(".");
  607. }
  608. if (peek() === ".") {
  609. next(); // Consume second dot
  610. next(); // Consume third dot
  611. return token("expand", "...");
  612. }
  613. return token("punc", ".");
  614. };
  615. function read_word() {
  616. var word = read_name();
  617. if (prev_was_dot) return token("name", word);
  618. return KEYWORDS_ATOM(word) ? token("atom", word)
  619. : !KEYWORDS(word) ? token("name", word)
  620. : OPERATORS(word) ? token("operator", word)
  621. : token("keyword", word);
  622. };
  623. function with_eof_error(eof_error, cont) {
  624. return function(x) {
  625. try {
  626. return cont(x);
  627. } catch(ex) {
  628. if (ex === EX_EOF) parse_error(eof_error);
  629. else throw ex;
  630. }
  631. };
  632. };
  633. function next_token(force_regexp) {
  634. if (force_regexp != null)
  635. return read_regexp(force_regexp);
  636. if (shebang && S.pos == 0 && looking_at("#!")) {
  637. start_token();
  638. forward(2);
  639. skip_line_comment("comment5");
  640. }
  641. for (;;) {
  642. skip_whitespace();
  643. start_token();
  644. if (html5_comments) {
  645. if (looking_at("<!--")) {
  646. forward(4);
  647. skip_line_comment("comment3");
  648. continue;
  649. }
  650. if (looking_at("-->") && S.newline_before) {
  651. forward(3);
  652. skip_line_comment("comment4");
  653. continue;
  654. }
  655. }
  656. var ch = peek();
  657. if (!ch) return token("eof");
  658. var code = ch.charCodeAt(0);
  659. switch (code) {
  660. case 34: case 39: return read_string(ch);
  661. case 46: return handle_dot();
  662. case 47: {
  663. var tok = handle_slash();
  664. if (tok === next_token) continue;
  665. return tok;
  666. }
  667. case 61: return handle_eq_sign();
  668. case 96: return read_template_characters(true);
  669. case 123:
  670. S.brace_counter++;
  671. break;
  672. case 125:
  673. S.brace_counter--;
  674. if (S.template_braces.length > 0
  675. && S.template_braces[S.template_braces.length - 1] === S.brace_counter)
  676. return read_template_characters(false);
  677. break;
  678. }
  679. if (is_digit(code)) return read_num();
  680. if (PUNC_CHARS(ch)) return token("punc", next());
  681. if (OPERATOR_CHARS(ch)) return read_operator();
  682. if (code == 92 || is_identifier_start(ch)) return read_word();
  683. break;
  684. }
  685. parse_error("Unexpected character '" + ch + "'");
  686. };
  687. next_token.next = next;
  688. next_token.peek = peek;
  689. next_token.context = function(nc) {
  690. if (nc) S = nc;
  691. return S;
  692. };
  693. next_token.add_directive = function(directive) {
  694. S.directive_stack[S.directive_stack.length - 1].push(directive);
  695. if (S.directives[directive] === undefined) {
  696. S.directives[directive] = 1;
  697. } else {
  698. S.directives[directive]++;
  699. }
  700. }
  701. next_token.push_directives_stack = function() {
  702. S.directive_stack.push([]);
  703. }
  704. next_token.pop_directives_stack = function() {
  705. var directives = S.directive_stack[S.directive_stack.length - 1];
  706. for (var i = 0; i < directives.length; i++) {
  707. S.directives[directives[i]]--;
  708. }
  709. S.directive_stack.pop();
  710. }
  711. next_token.has_directive = function(directive) {
  712. return S.directives[directive] > 0;
  713. }
  714. return next_token;
  715. };
  716. /* -----[ Parser (constants) ]----- */
  717. var UNARY_PREFIX = makePredicate([
  718. "typeof",
  719. "void",
  720. "delete",
  721. "--",
  722. "++",
  723. "!",
  724. "~",
  725. "-",
  726. "+"
  727. ]);
  728. var UNARY_POSTFIX = makePredicate([ "--", "++" ]);
  729. var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "/=", "*=", "**=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]);
  730. var PRECEDENCE = (function(a, ret){
  731. for (var i = 0; i < a.length; ++i) {
  732. var b = a[i];
  733. for (var j = 0; j < b.length; ++j) {
  734. ret[b[j]] = i + 1;
  735. }
  736. }
  737. return ret;
  738. })(
  739. [
  740. ["||"],
  741. ["&&"],
  742. ["|"],
  743. ["^"],
  744. ["&"],
  745. ["==", "===", "!=", "!=="],
  746. ["<", ">", "<=", ">=", "in", "instanceof"],
  747. [">>", "<<", ">>>"],
  748. ["+", "-"],
  749. ["*", "/", "%"],
  750. ["**"]
  751. ],
  752. {}
  753. );
  754. var ATOMIC_START_TOKEN = makePredicate([ "atom", "num", "string", "regexp", "name" ]);
  755. /* -----[ Parser ]----- */
  756. function parse($TEXT, options) {
  757. options = defaults(options, {
  758. bare_returns : false,
  759. ecma : 8,
  760. expression : false,
  761. filename : null,
  762. html5_comments : true,
  763. shebang : true,
  764. strict : false,
  765. toplevel : null,
  766. }, true);
  767. var S = {
  768. input : (typeof $TEXT == "string"
  769. ? tokenizer($TEXT, options.filename,
  770. options.html5_comments, options.shebang)
  771. : $TEXT),
  772. token : null,
  773. prev : null,
  774. peeked : null,
  775. in_function : 0,
  776. in_async : -1,
  777. in_generator : -1,
  778. in_directives : true,
  779. in_loop : 0,
  780. labels : []
  781. };
  782. S.token = next();
  783. function is(type, value) {
  784. return is_token(S.token, type, value);
  785. };
  786. function peek() { return S.peeked || (S.peeked = S.input()); };
  787. function next() {
  788. S.prev = S.token;
  789. if (S.peeked) {
  790. S.token = S.peeked;
  791. S.peeked = null;
  792. } else {
  793. S.token = S.input();
  794. }
  795. S.in_directives = S.in_directives && (
  796. S.token.type == "string" || is("punc", ";")
  797. );
  798. return S.token;
  799. };
  800. function prev() {
  801. return S.prev;
  802. };
  803. function croak(msg, line, col, pos) {
  804. var ctx = S.input.context();
  805. js_error(msg,
  806. ctx.filename,
  807. line != null ? line : ctx.tokline,
  808. col != null ? col : ctx.tokcol,
  809. pos != null ? pos : ctx.tokpos);
  810. };
  811. function token_error(token, msg) {
  812. croak(msg, token.line, token.col);
  813. };
  814. function unexpected(token) {
  815. if (token == null)
  816. token = S.token;
  817. token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
  818. };
  819. function expect_token(type, val) {
  820. if (is(type, val)) {
  821. return next();
  822. }
  823. token_error(S.token, "Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
  824. };
  825. function expect(punc) { return expect_token("punc", punc); };
  826. function has_newline_before(token) {
  827. return token.nlb || !all(token.comments_before, function(comment) {
  828. return !comment.nlb;
  829. });
  830. }
  831. function can_insert_semicolon() {
  832. return !options.strict
  833. && (is("eof") || is("punc", "}") || has_newline_before(S.token));
  834. };
  835. function is_in_generator() {
  836. return S.in_generator === S.in_function;
  837. }
  838. function is_in_async() {
  839. return S.in_async === S.in_function;
  840. }
  841. function semicolon(optional) {
  842. if (is("punc", ";")) next();
  843. else if (!optional && !can_insert_semicolon()) unexpected();
  844. };
  845. function parenthesised() {
  846. expect("(");
  847. var exp = expression(true);
  848. expect(")");
  849. return exp;
  850. };
  851. function embed_tokens(parser) {
  852. return function() {
  853. var start = S.token;
  854. var expr = parser.apply(null, arguments);
  855. var end = prev();
  856. expr.start = start;
  857. expr.end = end;
  858. return expr;
  859. };
  860. };
  861. function handle_regexp() {
  862. if (is("operator", "/") || is("operator", "/=")) {
  863. S.peeked = null;
  864. S.token = S.input(S.token.value.substr(1)); // force regexp
  865. }
  866. };
  867. var statement = embed_tokens(function(is_export_default) {
  868. handle_regexp();
  869. switch (S.token.type) {
  870. case "string":
  871. if (S.in_directives) {
  872. var token = peek();
  873. if (S.token.raw.indexOf("\\") == -1
  874. && (is_token(token, "punc", ";")
  875. || is_token(token, "punc", "}")
  876. || has_newline_before(token)
  877. || is_token(token, "eof"))) {
  878. S.input.add_directive(S.token.value);
  879. } else {
  880. S.in_directives = false;
  881. }
  882. }
  883. var dir = S.in_directives, stat = simple_statement();
  884. return dir ? new AST_Directive(stat.body) : stat;
  885. case "template_head":
  886. case "num":
  887. case "regexp":
  888. case "operator":
  889. case "atom":
  890. return simple_statement();
  891. case "name":
  892. if (S.token.value == "async" && is_token(peek(), "keyword", "function")) {
  893. next();
  894. next();
  895. return function_(AST_Defun, false, true, is_export_default);
  896. }
  897. if (S.token.value == "import" && !is_token(peek(), "punc", "(")) {
  898. next();
  899. var node = import_();
  900. semicolon();
  901. return node;
  902. }
  903. return is_token(peek(), "punc", ":")
  904. ? labeled_statement()
  905. : simple_statement();
  906. case "punc":
  907. switch (S.token.value) {
  908. case "{":
  909. return new AST_BlockStatement({
  910. start : S.token,
  911. body : block_(),
  912. end : prev()
  913. });
  914. case "[":
  915. case "(":
  916. return simple_statement();
  917. case ";":
  918. S.in_directives = false;
  919. next();
  920. return new AST_EmptyStatement();
  921. default:
  922. unexpected();
  923. }
  924. case "keyword":
  925. switch (S.token.value) {
  926. case "break":
  927. next();
  928. return break_cont(AST_Break);
  929. case "continue":
  930. next();
  931. return break_cont(AST_Continue);
  932. case "debugger":
  933. next();
  934. semicolon();
  935. return new AST_Debugger();
  936. case "do":
  937. next();
  938. var body = in_loop(statement);
  939. expect_token("keyword", "while");
  940. var condition = parenthesised();
  941. semicolon(true);
  942. return new AST_Do({
  943. body : body,
  944. condition : condition
  945. });
  946. case "while":
  947. next();
  948. return new AST_While({
  949. condition : parenthesised(),
  950. body : in_loop(statement)
  951. });
  952. case "for":
  953. next();
  954. return for_();
  955. case "class":
  956. next();
  957. return class_(AST_DefClass);
  958. case "function":
  959. next();
  960. return function_(AST_Defun, false, false, is_export_default);
  961. case "if":
  962. next();
  963. return if_();
  964. case "return":
  965. if (S.in_function == 0 && !options.bare_returns)
  966. croak("'return' outside of function");
  967. next();
  968. var value = null;
  969. if (is("punc", ";")) {
  970. next();
  971. } else if (!can_insert_semicolon()) {
  972. value = expression(true);
  973. semicolon();
  974. }
  975. return new AST_Return({
  976. value: value
  977. });
  978. case "switch":
  979. next();
  980. return new AST_Switch({
  981. expression : parenthesised(),
  982. body : in_loop(switch_body_)
  983. });
  984. case "throw":
  985. next();
  986. if (has_newline_before(S.token))
  987. croak("Illegal newline after 'throw'");
  988. var value = expression(true);
  989. semicolon();
  990. return new AST_Throw({
  991. value: value
  992. });
  993. case "try":
  994. next();
  995. return try_();
  996. case "var":
  997. next();
  998. var node = var_();
  999. semicolon();
  1000. return node;
  1001. case "let":
  1002. next();
  1003. var node = let_();
  1004. semicolon();
  1005. return node;
  1006. case "const":
  1007. next();
  1008. var node = const_();
  1009. semicolon();
  1010. return node;
  1011. case "with":
  1012. if (S.input.has_directive("use strict")) {
  1013. croak("Strict mode may not include a with statement");
  1014. }
  1015. next();
  1016. return new AST_With({
  1017. expression : parenthesised(),
  1018. body : statement()
  1019. });
  1020. case "export":
  1021. if (!is_token(peek(), "punc", "(")) {
  1022. next();
  1023. return export_();
  1024. }
  1025. }
  1026. }
  1027. unexpected();
  1028. });
  1029. function labeled_statement() {
  1030. var label = as_symbol(AST_Label);
  1031. if (label.name === "await" && is_in_async()) {
  1032. token_error(S.prev, "await cannot be used as label inside async function");
  1033. }
  1034. if (find_if(function(l){ return l.name == label.name }, S.labels)) {
  1035. // ECMA-262, 12.12: An ECMAScript program is considered
  1036. // syntactically incorrect if it contains a
  1037. // LabelledStatement that is enclosed by a
  1038. // LabelledStatement with the same Identifier as label.
  1039. croak("Label " + label.name + " defined twice");
  1040. }
  1041. expect(":");
  1042. S.labels.push(label);
  1043. var stat = statement();
  1044. S.labels.pop();
  1045. if (!(stat instanceof AST_IterationStatement)) {
  1046. // check for `continue` that refers to this label.
  1047. // those should be reported as syntax errors.
  1048. // https://github.com/mishoo/UglifyJS2/issues/287
  1049. label.references.forEach(function(ref){
  1050. if (ref instanceof AST_Continue) {
  1051. ref = ref.label.start;
  1052. croak("Continue label `" + label.name + "` refers to non-IterationStatement.",
  1053. ref.line, ref.col, ref.pos);
  1054. }
  1055. });
  1056. }
  1057. return new AST_LabeledStatement({ body: stat, label: label });
  1058. };
  1059. function simple_statement(tmp) {
  1060. return new AST_SimpleStatement({ body: (tmp = expression(true), semicolon(), tmp) });
  1061. };
  1062. function break_cont(type) {
  1063. var label = null, ldef;
  1064. if (!can_insert_semicolon()) {
  1065. label = as_symbol(AST_LabelRef, true);
  1066. }
  1067. if (label != null) {
  1068. ldef = find_if(function(l){ return l.name == label.name }, S.labels);
  1069. if (!ldef)
  1070. croak("Undefined label " + label.name);
  1071. label.thedef = ldef;
  1072. }
  1073. else if (S.in_loop == 0)
  1074. croak(type.TYPE + " not inside a loop or switch");
  1075. semicolon();
  1076. var stat = new type({ label: label });
  1077. if (ldef) ldef.references.push(stat);
  1078. return stat;
  1079. };
  1080. function for_() {
  1081. expect("(");
  1082. var init = null;
  1083. if (!is("punc", ";")) {
  1084. init =
  1085. is("keyword", "var") ? (next(), var_(true)) :
  1086. is("keyword", "let") ? (next(), let_(true)) :
  1087. is("keyword", "const") ? (next(), const_(true)) :
  1088. expression(true, true);
  1089. var is_in = is("operator", "in");
  1090. var is_of = is("name", "of");
  1091. if (is_in || is_of) {
  1092. if (init instanceof AST_Definitions) {
  1093. if (init.definitions.length > 1)
  1094. croak("Only one variable declaration allowed in for..in loop", init.start.line, init.start.col, init.start.pos);
  1095. } else if (!(is_assignable(init) || (init = to_destructuring(init)) instanceof AST_Destructuring)) {
  1096. croak("Invalid left-hand side in for..in loop", init.start.line, init.start.col, init.start.pos);
  1097. }
  1098. next();
  1099. if (is_in) {
  1100. return for_in(init);
  1101. } else {
  1102. return for_of(init);
  1103. }
  1104. }
  1105. }
  1106. return regular_for(init);
  1107. };
  1108. function regular_for(init) {
  1109. expect(";");
  1110. var test = is("punc", ";") ? null : expression(true);
  1111. expect(";");
  1112. var step = is("punc", ")") ? null : expression(true);
  1113. expect(")");
  1114. return new AST_For({
  1115. init : init,
  1116. condition : test,
  1117. step : step,
  1118. body : in_loop(statement)
  1119. });
  1120. };
  1121. function for_of(init) {
  1122. var lhs = init instanceof AST_Definitions ? init.definitions[0].name : null;
  1123. var obj = expression(true);
  1124. expect(")");
  1125. return new AST_ForOf({
  1126. init : init,
  1127. name : lhs,
  1128. object : obj,
  1129. body : in_loop(statement)
  1130. });
  1131. };
  1132. function for_in(init) {
  1133. var obj = expression(true);
  1134. expect(")");
  1135. return new AST_ForIn({
  1136. init : init,
  1137. object : obj,
  1138. body : in_loop(statement)
  1139. });
  1140. };
  1141. var arrow_function = function(start, argnames, is_async) {
  1142. if (has_newline_before(S.token)) {
  1143. croak("Unexpected newline before arrow (=>)");
  1144. }
  1145. expect_token("arrow", "=>");
  1146. var body = _function_body(is("punc", "{"), false, is_async);
  1147. return new AST_Arrow({
  1148. start : start,
  1149. end : body.end,
  1150. async : is_async,
  1151. argnames : argnames,
  1152. body : body
  1153. });
  1154. };
  1155. var function_ = function(ctor, is_generator_property, is_async, is_export_default) {
  1156. if (is_generator_property && is_async) croak("generators cannot be async");
  1157. var start = S.token;
  1158. var in_statement = ctor === AST_Defun;
  1159. var is_generator = is("operator", "*");
  1160. if (is_generator) {
  1161. next();
  1162. }
  1163. var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null;
  1164. if (in_statement && !name) {
  1165. if (is_export_default) {
  1166. ctor = AST_Function;
  1167. } else {
  1168. unexpected();
  1169. }
  1170. }
  1171. if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration))
  1172. unexpected(prev());
  1173. var args = [];
  1174. var body = _function_body(true, is_generator || is_generator_property, is_async, name, args);
  1175. return new ctor({
  1176. start : args.start,
  1177. end : body.end,
  1178. is_generator: is_generator,
  1179. async : is_async,
  1180. name : name,
  1181. argnames: args,
  1182. body : body
  1183. });
  1184. };
  1185. function track_used_binding_identifiers(is_parameter, strict) {
  1186. var parameters = {};
  1187. var duplicate = false;
  1188. var default_assignment = false;
  1189. var spread = false;
  1190. var strict_mode = !!strict;
  1191. var tracker = {
  1192. add_parameter: function(token) {
  1193. if (parameters["$" + token.value] !== undefined) {
  1194. if (duplicate === false) {
  1195. duplicate = token;
  1196. }
  1197. tracker.check_strict();
  1198. } else {
  1199. parameters["$" + token.value] = true;
  1200. if (is_parameter) {
  1201. switch (token.value) {
  1202. case "arguments":
  1203. case "eval":
  1204. case "yield":
  1205. if (strict_mode) {
  1206. token_error(token, "Unexpected " + token.value + " identifier as parameter inside strict mode");
  1207. }
  1208. break;
  1209. default:
  1210. if (RESERVED_WORDS(token.value)) {
  1211. unexpected();
  1212. }
  1213. }
  1214. }
  1215. }
  1216. },
  1217. mark_default_assignment: function(token) {
  1218. if (default_assignment === false) {
  1219. default_assignment = token;
  1220. }
  1221. },
  1222. mark_spread: function(token) {
  1223. if (spread === false) {
  1224. spread = token;
  1225. }
  1226. },
  1227. mark_strict_mode: function() {
  1228. strict_mode = true;
  1229. },
  1230. is_strict: function() {
  1231. return default_assignment !== false || spread !== false || strict_mode
  1232. },
  1233. check_strict: function() {
  1234. if (tracker.is_strict() && duplicate !== false) {
  1235. token_error(duplicate, "Parameter " + duplicate.value + " was used already");
  1236. }
  1237. }
  1238. };
  1239. return tracker;
  1240. }
  1241. function parameters(params) {
  1242. var start = S.token;
  1243. var used_parameters = track_used_binding_identifiers(true, S.input.has_directive("use strict"));
  1244. expect("(");
  1245. while (!is("punc", ")")) {
  1246. var param = parameter(used_parameters);
  1247. params.push(param);
  1248. if (!is("punc", ")")) {
  1249. expect(",");
  1250. if (is("punc", ")") && options.ecma < 8) unexpected();
  1251. }
  1252. if (param instanceof AST_Expansion) {
  1253. break;
  1254. }
  1255. }
  1256. next();
  1257. }
  1258. function parameter(used_parameters, symbol_type) {
  1259. var param;
  1260. var expand = false;
  1261. if (used_parameters === undefined) {
  1262. used_parameters = track_used_binding_identifiers(true, S.input.has_directive("use strict"));
  1263. }
  1264. if (is("expand", "...")) {
  1265. expand = S.token;
  1266. used_parameters.mark_spread(S.token);
  1267. next();
  1268. }
  1269. param = binding_element(used_parameters, symbol_type);
  1270. if (is("operator", "=") && expand === false) {
  1271. used_parameters.mark_default_assignment(S.token);
  1272. next();
  1273. param = new AST_DefaultAssign({
  1274. start: param.start,
  1275. left: param,
  1276. operator: "=",
  1277. right: expression(false),
  1278. end: S.token
  1279. });
  1280. }
  1281. if (expand !== false) {
  1282. if (!is("punc", ")")) {
  1283. unexpected();
  1284. }
  1285. param = new AST_Expansion({
  1286. start: expand,
  1287. expression: param,
  1288. end: expand
  1289. });
  1290. }
  1291. used_parameters.check_strict();
  1292. return param;
  1293. }
  1294. function binding_element(used_parameters, symbol_type) {
  1295. var elements = [];
  1296. var first = true;
  1297. var is_expand = false;
  1298. var expand_token;
  1299. var first_token = S.token;
  1300. if (used_parameters === undefined) {
  1301. used_parameters = track_used_binding_identifiers(false, S.input.has_directive("use strict"));
  1302. }
  1303. symbol_type = symbol_type === undefined ? AST_SymbolFunarg : symbol_type;
  1304. if (is("punc", "[")) {
  1305. next();
  1306. while (!is("punc", "]")) {
  1307. if (first) {
  1308. first = false;
  1309. } else {
  1310. expect(",");
  1311. }
  1312. if (is("expand", "...")) {
  1313. is_expand = true;
  1314. expand_token = S.token;
  1315. used_parameters.mark_spread(S.token);
  1316. next();
  1317. }
  1318. if (is("punc")) {
  1319. switch (S.token.value) {
  1320. case ",":
  1321. elements.push(new AST_Hole({
  1322. start: S.token,
  1323. end: S.token
  1324. }));
  1325. continue;
  1326. case "]": // Trailing comma after last element
  1327. break;
  1328. case "[":
  1329. case "{":
  1330. elements.push(binding_element(used_parameters, symbol_type));
  1331. break;
  1332. default:
  1333. unexpected();
  1334. }
  1335. } else if (is("name")) {
  1336. used_parameters.add_parameter(S.token);
  1337. elements.push(as_symbol(symbol_type));
  1338. } else {
  1339. croak("Invalid function parameter");
  1340. }
  1341. if (is("operator", "=") && is_expand === false) {
  1342. used_parameters.mark_default_assignment(S.token);
  1343. next();
  1344. elements[elements.length - 1] = new AST_DefaultAssign({
  1345. start: elements[elements.length - 1].start,
  1346. left: elements[elements.length - 1],
  1347. operator: "=",
  1348. right: expression(false),
  1349. end: S.token
  1350. });
  1351. }
  1352. if (is_expand) {
  1353. if (!is("punc", "]")) {
  1354. croak("Rest element must be last element");
  1355. }
  1356. elements[elements.length - 1] = new AST_Expansion({
  1357. start: expand_token,
  1358. expression: elements[elements.length - 1],
  1359. end: expand_token
  1360. });
  1361. }
  1362. }
  1363. expect("]");
  1364. used_parameters.check_strict();
  1365. return new AST_Destructuring({
  1366. start: first_token,
  1367. names: elements,
  1368. is_array: true,
  1369. end: prev()
  1370. });
  1371. } else if (is("punc", "{")) {
  1372. next();
  1373. while (!is("punc", "}")) {
  1374. if (first) {
  1375. first = false;
  1376. } else {
  1377. expect(",");
  1378. }
  1379. if (is("expand", "...")) {
  1380. is_expand = true;
  1381. expand_token = S.token;
  1382. used_parameters.mark_spread(S.token);
  1383. next();
  1384. }
  1385. if (is("name") && (is_token(peek(), "punc") || is_token(peek(), "operator")) && [",", "}", "="].indexOf(peek().value) !== -1) {
  1386. used_parameters.add_parameter(S.token);
  1387. var start = prev();
  1388. var value = as_symbol(symbol_type);
  1389. if (is_expand) {
  1390. elements.push(new AST_Expansion({
  1391. start: expand_token,
  1392. expression: value,
  1393. end: value.end,
  1394. }));
  1395. } else {
  1396. elements.push(new AST_ObjectKeyVal({
  1397. start: start,
  1398. key: value.name,
  1399. value: value,
  1400. end: value.end,
  1401. }));
  1402. }
  1403. } else if (is("punc", "}")) {
  1404. continue; // Allow trailing hole
  1405. } else {
  1406. var property_token = S.token;
  1407. var property = as_property_name();
  1408. if (property === null) {
  1409. unexpected(prev());
  1410. } else if (prev().type === "name" && !is("punc", ":")) {
  1411. elements.push(new AST_ObjectKeyVal({
  1412. start: prev(),
  1413. key: property,
  1414. value: new symbol_type({
  1415. start: prev(),
  1416. name: property,
  1417. end: prev()
  1418. }),
  1419. end: prev()
  1420. }));
  1421. } else {
  1422. expect(":");
  1423. elements.push(new AST_ObjectKeyVal({
  1424. start: property_token,
  1425. quote: property_token.quote,
  1426. key: property,
  1427. value: binding_element(used_parameters, symbol_type),
  1428. end: prev()
  1429. }));
  1430. }
  1431. }
  1432. if (is_expand) {
  1433. if (!is("punc", "}")) {
  1434. croak("Rest element must be last element");
  1435. }
  1436. }
  1437. else if (is("operator", "=")) {
  1438. used_parameters.mark_default_assignment(S.token);
  1439. next();
  1440. elements[elements.length - 1].value = new AST_DefaultAssign({
  1441. start: elements[elements.length - 1].value.start,
  1442. left: elements[elements.length - 1].value,
  1443. operator: "=",
  1444. right: expression(false),
  1445. end: S.token
  1446. });
  1447. }
  1448. }
  1449. expect("}");
  1450. used_parameters.check_strict();
  1451. return new AST_Destructuring({
  1452. start: first_token,
  1453. names: elements,
  1454. is_array: false,
  1455. end: prev()
  1456. });
  1457. } else if (is("name")) {
  1458. used_parameters.add_parameter(S.token);
  1459. return as_symbol(symbol_type);
  1460. } else {
  1461. croak("Invalid function parameter");
  1462. }
  1463. }
  1464. function params_or_seq_(allow_arrows, maybe_sequence) {
  1465. var spread_token;
  1466. var invalid_sequence;
  1467. var trailing_comma;
  1468. var a = [];
  1469. expect("(");
  1470. while (!is("punc", ")")) {
  1471. if (spread_token) unexpected(spread_token);
  1472. if (is("expand", "...")) {
  1473. spread_token = S.token;
  1474. if (maybe_sequence) invalid_sequence = S.token;
  1475. next();
  1476. a.push(new AST_Expansion({
  1477. start: prev(),
  1478. expression: expression(),
  1479. end: S.token,
  1480. }));
  1481. } else {
  1482. a.push(expression());
  1483. }
  1484. if (!is("punc", ")")) {
  1485. expect(",");
  1486. if (is("punc", ")")) {
  1487. if (options.ecma < 8) unexpected();
  1488. trailing_comma = prev();
  1489. if (maybe_sequence) invalid_sequence = trailing_comma;
  1490. }
  1491. }
  1492. }
  1493. expect(")");
  1494. if (allow_arrows && is("arrow", "=>")) {
  1495. if (spread_token && trailing_comma) unexpected(trailing_comma);
  1496. } else if (invalid_sequence) {
  1497. unexpected(invalid_sequence);
  1498. }
  1499. return a;
  1500. }
  1501. function _function_body(block, generator, is_async, name, args) {
  1502. var loop = S.in_loop;
  1503. var labels = S.labels;
  1504. var current_generator = S.in_generator;
  1505. var current_async = S.in_async;
  1506. ++S.in_function;
  1507. if (generator)
  1508. S.in_generator = S.in_function;
  1509. if (is_async)
  1510. S.in_async = S.in_function;
  1511. if (args) parameters(args);
  1512. if (block)
  1513. S.in_directives = true;
  1514. S.in_loop = 0;
  1515. S.labels = [];
  1516. if (block) {
  1517. S.input.push_directives_stack();
  1518. var a = block_();
  1519. if (name) _verify_symbol(name);
  1520. if (args) args.forEach(_verify_symbol);
  1521. S.input.pop_directives_stack();
  1522. } else {
  1523. var a = expression(false);
  1524. }
  1525. --S.in_function;
  1526. S.in_loop = loop;
  1527. S.labels = labels;
  1528. S.in_generator = current_generator;
  1529. S.in_async = current_async;
  1530. return a;
  1531. }
  1532. function _await_expression() {
  1533. // Previous token must be "await" and not be interpreted as an identifier
  1534. if (!is_in_async()) {
  1535. croak("Unexpected await expression outside async function",
  1536. S.prev.line, S.prev.col, S.prev.pos);
  1537. }
  1538. // the await expression is parsed as a unary expression in Babel
  1539. return new AST_Await({
  1540. expression : maybe_unary(true),
  1541. });
  1542. }
  1543. function _yield_expression() {
  1544. // Previous token must be keyword yield and not be interpret as an identifier
  1545. if (!is_in_generator()) {
  1546. croak("Unexpected yield expression outside generator function",
  1547. S.prev.line, S.prev.col, S.prev.pos);
  1548. }
  1549. var star = false;
  1550. var has_expression = true;
  1551. // Attempt to get expression or star (and then the mandatory expression)
  1552. // behind yield on the same line.
  1553. //
  1554. // If nothing follows on the same line of the yieldExpression,
  1555. // it should default to the value `undefined` for yield to return.
  1556. // In that case, the `undefined` stored as `null` in ast.
  1557. //
  1558. // Note 1: It isn't allowed for yield* to close without an expression
  1559. // Note 2: If there is a nlb between yield and star, it is interpret as
  1560. // yield <explicit undefined> <inserted automatic semicolon> *
  1561. if (can_insert_semicolon() ||
  1562. (is("punc") && PUNC_AFTER_EXPRESSION(S.token.value))) {
  1563. has_expression = false;
  1564. } else if (is("operator", "*")) {
  1565. star = true;
  1566. next();
  1567. }
  1568. return new AST_Yield({
  1569. is_star : star,
  1570. expression : has_expression ? expression() : null
  1571. });
  1572. }
  1573. function if_() {
  1574. var cond = parenthesised(), body = statement(), belse = null;
  1575. if (is("keyword", "else")) {
  1576. next();
  1577. belse = statement();
  1578. }
  1579. return new AST_If({
  1580. condition : cond,
  1581. body : body,
  1582. alternative : belse
  1583. });
  1584. };
  1585. function block_() {
  1586. expect("{");
  1587. var a = [];
  1588. while (!is("punc", "}")) {
  1589. if (is("eof")) unexpected();
  1590. a.push(statement());
  1591. }
  1592. next();
  1593. return a;
  1594. };
  1595. function switch_body_() {
  1596. expect("{");
  1597. var a = [], cur = null, branch = null, tmp;
  1598. while (!is("punc", "}")) {
  1599. if (is("eof")) unexpected();
  1600. if (is("keyword", "case")) {
  1601. if (branch) branch.end = prev();
  1602. cur = [];
  1603. branch = new AST_Case({
  1604. start : (tmp = S.token, next(), tmp),
  1605. expression : expression(true),
  1606. body : cur
  1607. });
  1608. a.push(branch);
  1609. expect(":");
  1610. }
  1611. else if (is("keyword", "default")) {
  1612. if (branch) branch.end = prev();
  1613. cur = [];
  1614. branch = new AST_Default({
  1615. start : (tmp = S.token, next(), expect(":"), tmp),
  1616. body : cur
  1617. });
  1618. a.push(branch);
  1619. }
  1620. else {
  1621. if (!cur) unexpected();
  1622. cur.push(statement());
  1623. }
  1624. }
  1625. if (branch) branch.end = prev();
  1626. next();
  1627. return a;
  1628. };
  1629. function try_() {
  1630. var body = block_(), bcatch = null, bfinally = null;
  1631. if (is("keyword", "catch")) {
  1632. var start = S.token;
  1633. next();
  1634. expect("(");
  1635. var name = parameter(undefined, AST_SymbolCatch);
  1636. expect(")");
  1637. bcatch = new AST_Catch({
  1638. start : start,
  1639. argname : name,
  1640. body : block_(),
  1641. end : prev()
  1642. });
  1643. }
  1644. if (is("keyword", "finally")) {
  1645. var start = S.token;
  1646. next();
  1647. bfinally = new AST_Finally({
  1648. start : start,
  1649. body : block_(),
  1650. end : prev()
  1651. });
  1652. }
  1653. if (!bcatch && !bfinally)
  1654. croak("Missing catch/finally blocks");
  1655. return new AST_Try({
  1656. body : body,
  1657. bcatch : bcatch,
  1658. bfinally : bfinally
  1659. });
  1660. };
  1661. function vardefs(no_in, kind) {
  1662. var a = [];
  1663. var def;
  1664. for (;;) {
  1665. var sym_type =
  1666. kind === "var" ? AST_SymbolVar :
  1667. kind === "const" ? AST_SymbolConst :
  1668. kind === "let" ? AST_SymbolLet : null;
  1669. if (is("punc", "{") || is("punc", "[")) {
  1670. def = new AST_VarDef({
  1671. start: S.token,
  1672. name: binding_element(undefined ,sym_type),
  1673. value: is("operator", "=") ? (expect_token("operator", "="), expression(false, no_in)) : null,
  1674. end: prev()
  1675. });
  1676. } else {
  1677. def = new AST_VarDef({
  1678. start : S.token,
  1679. name : as_symbol(sym_type),
  1680. value : is("operator", "=")
  1681. ? (next(), expression(false, no_in))
  1682. : !no_in && kind === "const"
  1683. ? croak("Missing initializer in const declaration") : null,
  1684. end : prev()
  1685. });
  1686. if (def.name.name == "import") croak("Unexpected token: import");
  1687. }
  1688. a.push(def);
  1689. if (!is("punc", ","))
  1690. break;
  1691. next();
  1692. }
  1693. return a;
  1694. };
  1695. var var_ = function(no_in) {
  1696. return new AST_Var({
  1697. start : prev(),
  1698. definitions : vardefs(no_in, "var"),
  1699. end : prev()
  1700. });
  1701. };
  1702. var let_ = function(no_in) {
  1703. return new AST_Let({
  1704. start : prev(),
  1705. definitions : vardefs(no_in, "let"),
  1706. end : prev()
  1707. });
  1708. };
  1709. var const_ = function(no_in) {
  1710. return new AST_Const({
  1711. start : prev(),
  1712. definitions : vardefs(no_in, "const"),
  1713. end : prev()
  1714. });
  1715. };
  1716. var new_ = function(allow_calls) {
  1717. var start = S.token;
  1718. expect_token("operator", "new");
  1719. if (is("punc", ".")) {
  1720. next();
  1721. expect_token("name", "target");
  1722. return subscripts(new AST_NewTarget({
  1723. start : start,
  1724. end : prev()
  1725. }), allow_calls);
  1726. }
  1727. var newexp = expr_atom(false), args;
  1728. if (is("punc", "(")) {
  1729. next();
  1730. args = expr_list(")", options.ecma >= 8);
  1731. } else {
  1732. args = [];
  1733. }
  1734. var call = new AST_New({
  1735. start : start,
  1736. expression : newexp,
  1737. args : args,
  1738. end : prev()
  1739. });
  1740. mark_pure(call);
  1741. return subscripts(call, allow_calls);
  1742. };
  1743. function as_atom_node() {
  1744. var tok = S.token, ret;
  1745. switch (tok.type) {
  1746. case "name":
  1747. ret = _make_symbol(AST_SymbolRef);
  1748. break;
  1749. case "num":
  1750. ret = new AST_Number({ start: tok, end: tok, value: tok.value });
  1751. break;
  1752. case "string":
  1753. ret = new AST_String({
  1754. start : tok,
  1755. end : tok,
  1756. value : tok.value,
  1757. quote : tok.quote
  1758. });
  1759. break;
  1760. case "regexp":
  1761. ret = new AST_RegExp({ start: tok, end: tok, value: tok.value });
  1762. break;
  1763. case "atom":
  1764. switch (tok.value) {
  1765. case "false":
  1766. ret = new AST_False({ start: tok, end: tok });
  1767. break;
  1768. case "true":
  1769. ret = new AST_True({ start: tok, end: tok });
  1770. break;
  1771. case "null":
  1772. ret = new AST_Null({ start: tok, end: tok });
  1773. break;
  1774. }
  1775. break;
  1776. }
  1777. next();
  1778. return ret;
  1779. };
  1780. function to_fun_args(ex, _, __, default_seen_above) {
  1781. var insert_default = function(ex, default_value) {
  1782. if (default_value) {
  1783. return new AST_DefaultAssign({
  1784. start: ex.start,
  1785. left: ex,
  1786. operator: "=",
  1787. right: default_value,
  1788. end: default_value.end
  1789. });
  1790. }
  1791. return ex;
  1792. }
  1793. if (ex instanceof AST_Object) {
  1794. return insert_default(new AST_Destructuring({
  1795. start: ex.start,
  1796. end: ex.end,
  1797. is_array: false,
  1798. names: ex.properties.map(to_fun_args)
  1799. }), default_seen_above);
  1800. } else if (ex instanceof AST_ObjectKeyVal) {
  1801. ex.value = to_fun_args(ex.value, 0, [ex.key]);
  1802. return insert_default(ex, default_seen_above);
  1803. } else if (ex instanceof AST_Hole) {
  1804. return ex;
  1805. } else if (ex instanceof AST_Destructuring) {
  1806. ex.names = ex.names.map(to_fun_args);
  1807. return insert_default(ex, default_seen_above);
  1808. } else if (ex instanceof AST_SymbolRef) {
  1809. return insert_default(new AST_SymbolFunarg({
  1810. name: ex.name,
  1811. start: ex.start,
  1812. end: ex.end
  1813. }), default_seen_above);
  1814. } else if (ex instanceof AST_Expansion) {
  1815. ex.expression = to_fun_args(ex.expression);
  1816. return insert_default(ex, default_seen_above);
  1817. } else if (ex instanceof AST_Array) {
  1818. return insert_default(new AST_Destructuring({
  1819. start: ex.start,
  1820. end: ex.end,
  1821. is_array: true,
  1822. names: ex.elements.map(to_fun_args)
  1823. }), default_seen_above);
  1824. } else if (ex instanceof AST_Assign) {
  1825. return insert_default(to_fun_args(ex.left, undefined, undefined, ex.right), default_seen_above);
  1826. } else if (ex instanceof AST_DefaultAssign) {
  1827. ex.left = to_fun_args(ex.left, 0, [ex.left]);
  1828. return ex;
  1829. } else {
  1830. croak("Invalid function parameter", ex.start.line, ex.start.col);
  1831. }
  1832. }
  1833. var expr_atom = function(allow_calls, allow_arrows) {
  1834. if (is("operator", "new")) {
  1835. return new_(allow_calls);
  1836. }
  1837. var start = S.token;
  1838. var async = is("name", "async") && as_atom_node();
  1839. if (is("punc")) {
  1840. switch (S.token.value) {
  1841. case "(":
  1842. if (async && !allow_calls) break;
  1843. var exprs = params_or_seq_(allow_arrows, !async);
  1844. if (allow_arrows && is("arrow", "=>")) {
  1845. return arrow_function(start, exprs.map(to_fun_args), !!async);
  1846. }
  1847. var ex = async ? new AST_Call({
  1848. expression: async,
  1849. args: exprs
  1850. }) : exprs.length == 1 ? exprs[0] : new AST_Sequence({
  1851. expressions: exprs
  1852. });
  1853. if (ex.start) {
  1854. var len = start.comments_before.length;
  1855. [].unshift.apply(ex.start.comments_before, start.comments_before);
  1856. start.comments_before = ex.start.comments_before;
  1857. start.comments_before_length = len;
  1858. if (len == 0 && start.comments_before.length > 0) {
  1859. var comment = start.comments_before[0];
  1860. if (!comment.nlb) {
  1861. comment.nlb = start.nlb;
  1862. start.nlb = false;
  1863. }
  1864. }
  1865. start.comments_after = ex.start.comments_after;
  1866. }
  1867. ex.start = start;
  1868. var end = prev();
  1869. if (ex.end) {
  1870. end.comments_before = ex.end.comments_before;
  1871. [].push.apply(ex.end.comments_after, end.comments_after);
  1872. end.comments_after = ex.end.comments_after;
  1873. }
  1874. ex.end = end;
  1875. if (ex instanceof AST_Call) mark_pure(ex);
  1876. return subscripts(ex, allow_calls);
  1877. case "[":
  1878. return subscripts(array_(), allow_calls);
  1879. case "{":
  1880. return subscripts(object_or_destructuring_(), allow_calls);
  1881. }
  1882. if (!async) unexpected();
  1883. }
  1884. if (allow_arrows && is("name") && is_token(peek(), "arrow")) {
  1885. var param = new AST_SymbolFunarg({
  1886. name: S.token.value,
  1887. start: start,
  1888. end: start,
  1889. });
  1890. next();
  1891. return arrow_function(start, [param], !!async);
  1892. }
  1893. if (is("keyword", "function")) {
  1894. next();
  1895. var func = function_(AST_Function, false, !!async);
  1896. func.start = start;
  1897. func.end = prev();
  1898. return subscripts(func, allow_calls);
  1899. }
  1900. if (async) return subscripts(async, allow_calls);
  1901. if (is("keyword", "class")) {
  1902. next();
  1903. var cls = class_(AST_ClassExpression);
  1904. cls.start = start;
  1905. cls.end = prev();
  1906. return subscripts(cls, allow_calls);
  1907. }
  1908. if (is("template_head")) {
  1909. return subscripts(template_string(), allow_calls);
  1910. }
  1911. if (ATOMIC_START_TOKEN(S.token.type)) {
  1912. return subscripts(as_atom_node(), allow_calls);
  1913. }
  1914. unexpected();
  1915. };
  1916. function template_string() {
  1917. var segments = [], start = S.token;
  1918. segments.push(new AST_TemplateSegment({
  1919. start: S.token,
  1920. raw: S.token.raw,
  1921. value: S.token.value,
  1922. end: S.token
  1923. }));
  1924. while (S.token.end === false) {
  1925. next();
  1926. handle_regexp();
  1927. segments.push(expression(true));
  1928. if (!is_token("template_substitution")) {
  1929. unexpected();
  1930. }
  1931. segments.push(new AST_TemplateSegment({
  1932. start: S.token,
  1933. raw: S.token.raw,
  1934. value: S.token.value,
  1935. end: S.token
  1936. }));
  1937. }
  1938. next();
  1939. return new AST_TemplateString({
  1940. start: start,
  1941. segments: segments,
  1942. end: S.token
  1943. });
  1944. }
  1945. function expr_list(closing, allow_trailing_comma, allow_empty) {
  1946. var first = true, a = [];
  1947. while (!is("punc", closing)) {
  1948. if (first) first = false; else expect(",");
  1949. if (allow_trailing_comma && is("punc", closing)) break;
  1950. if (is("punc", ",") && allow_empty) {
  1951. a.push(new AST_Hole({ start: S.token, end: S.token }));
  1952. } else if (is("expand", "...")) {
  1953. next();
  1954. a.push(new AST_Expansion({start: prev(), expression: expression(),end: S.token}));
  1955. } else {
  1956. a.push(expression(false));
  1957. }
  1958. }
  1959. next();
  1960. return a;
  1961. };
  1962. var array_ = embed_tokens(function() {
  1963. expect("[");
  1964. return new AST_Array({
  1965. elements: expr_list("]", !options.strict, true)
  1966. });
  1967. });
  1968. var create_accessor = embed_tokens(function(is_generator, is_async) {
  1969. return function_(AST_Accessor, is_generator, is_async);
  1970. });
  1971. var object_or_destructuring_ = embed_tokens(function object_or_destructuring_() {
  1972. var start = S.token, first = true, a = [];
  1973. expect("{");
  1974. while (!is("punc", "}")) {
  1975. if (first) first = false; else expect(",");
  1976. if (!options.strict && is("punc", "}"))
  1977. // allow trailing comma
  1978. break;
  1979. start = S.token;
  1980. if (start.type == "expand") {
  1981. next();
  1982. a.push(new AST_Expansion({
  1983. start: start,
  1984. expression: expression(false),
  1985. end: prev(),
  1986. }));
  1987. continue;
  1988. }
  1989. var name = as_property_name();
  1990. var value;
  1991. // Check property and fetch value
  1992. if (!is("punc", ":")) {
  1993. var concise = concise_method_or_getset(name, start);
  1994. if (concise) {
  1995. a.push(concise);
  1996. continue;
  1997. }
  1998. value = new AST_SymbolRef({
  1999. start: prev(),
  2000. name: name,
  2001. end: prev()
  2002. });
  2003. } else if (name === null) {
  2004. unexpected(prev());
  2005. } else {
  2006. next(); // `:` - see first condition
  2007. value = expression(false);
  2008. }
  2009. // Check for default value and alter value accordingly if necessary
  2010. if (is("operator", "=")) {
  2011. next();
  2012. value = new AST_Assign({
  2013. start: start,
  2014. left: value,
  2015. operator: "=",
  2016. right: expression(false),
  2017. end: prev()
  2018. });
  2019. }
  2020. // Create property
  2021. a.push(new AST_ObjectKeyVal({
  2022. start: start,
  2023. quote: start.quote,
  2024. key: name instanceof AST_Node ? name : "" + name,
  2025. value: value,
  2026. end: prev()
  2027. }));
  2028. }
  2029. next();
  2030. return new AST_Object({ properties: a })
  2031. });
  2032. function class_(KindOfClass) {
  2033. var start, method, class_name, extends_, a = [];
  2034. S.input.push_directives_stack(); // Push directive stack, but not scope stack
  2035. S.input.add_directive("use strict");
  2036. if (S.token.type == "name" && S.token.value != "extends") {
  2037. class_name = as_symbol(KindOfClass === AST_DefClass ? AST_SymbolDefClass : AST_SymbolClass);
  2038. }
  2039. if (KindOfClass === AST_DefClass && !class_name) {
  2040. unexpected();
  2041. }
  2042. if (S.token.value == "extends") {
  2043. next();
  2044. extends_ = expression(true);
  2045. }
  2046. expect("{");
  2047. if (is("punc", ";")) { next(); } // Leading semicolons are okay in class bodies.
  2048. while (!is("punc", "}")) {
  2049. start = S.token;
  2050. method = concise_method_or_getset(as_property_name(), start, true);
  2051. if (!method) { unexpected(); }
  2052. a.push(method);
  2053. if (is("punc", ";")) { next(); }
  2054. }
  2055. S.input.pop_directives_stack();
  2056. next();
  2057. return new KindOfClass({
  2058. start: start,
  2059. name: class_name,
  2060. extends: extends_,
  2061. properties: a,
  2062. end: prev(),
  2063. });
  2064. }
  2065. function concise_method_or_getset(name, start, is_class) {
  2066. var get_ast = function(name, token) {
  2067. if (typeof name === "string" || typeof name === "number") {
  2068. return new AST_SymbolMethod({
  2069. start: token,
  2070. name: "" + name,
  2071. end: prev()
  2072. });
  2073. } else if (name === null) {
  2074. unexpected();
  2075. }
  2076. return name;
  2077. }
  2078. var is_async = false;
  2079. var is_static = false;
  2080. var is_generator = false;
  2081. var property_token = start;
  2082. if (is_class && name === "static" && !is("punc", "(")) {
  2083. is_static = true;
  2084. property_token = S.token;
  2085. name = as_property_name();
  2086. }
  2087. if (name === "async" && !is("punc", "(") && !is("punc", ",") && !is("punc", "}")) {
  2088. is_async = true;
  2089. property_token = S.token;
  2090. name = as_property_name();
  2091. }
  2092. if (name === null) {
  2093. is_generator = true;
  2094. property_token = S.token;
  2095. name = as_property_name();
  2096. if (name === null) {
  2097. unexpected();
  2098. }
  2099. }
  2100. if (is("punc", "(")) {
  2101. name = get_ast(name, start);
  2102. var node = new AST_ConciseMethod({
  2103. start : start,
  2104. static : is_static,
  2105. is_generator: is_generator,
  2106. async : is_async,
  2107. key : name,
  2108. quote : name instanceof AST_SymbolMethod ?
  2109. property_token.quote : undefined,
  2110. value : create_accessor(is_generator, is_async),
  2111. end : prev()
  2112. });
  2113. return node;
  2114. }
  2115. property_token = S.token;
  2116. if (name == "get") {
  2117. if (!is("punc") || is("punc", "[")) {
  2118. name = get_ast(as_property_name(), start);
  2119. return new AST_ObjectGetter({
  2120. start : start,
  2121. static: is_static,
  2122. key : name,
  2123. quote : name instanceof AST_SymbolMethod ?
  2124. property_token.quote : undefined,
  2125. value : create_accessor(),
  2126. end : prev()
  2127. });
  2128. }
  2129. }
  2130. else if (name == "set") {
  2131. if (!is("punc") || is("punc", "[")) {
  2132. name = get_ast(as_property_name(), start);
  2133. return new AST_ObjectSetter({
  2134. start : start,
  2135. static: is_static,
  2136. key : name,
  2137. quote : name instanceof AST_SymbolMethod ?
  2138. property_token.quote : undefined,
  2139. value : create_accessor(),
  2140. end : prev()
  2141. });
  2142. }
  2143. }
  2144. }
  2145. function import_() {
  2146. var start = prev();
  2147. var imported_name;
  2148. var imported_names;
  2149. if (is("name")) {
  2150. imported_name = as_symbol(AST_SymbolImport);
  2151. }
  2152. if (is("punc", ",")) {
  2153. next();
  2154. }
  2155. imported_names = map_names(true);
  2156. if (imported_names || imported_name) {
  2157. expect_token("name", "from");
  2158. }
  2159. var mod_str = S.token;
  2160. if (mod_str.type !== 'string') {
  2161. unexpected();
  2162. }
  2163. next();
  2164. return new AST_Import({
  2165. start: start,
  2166. imported_name: imported_name,
  2167. imported_names: imported_names,
  2168. module_name: new AST_String({
  2169. start: mod_str,
  2170. value: mod_str.value,
  2171. quote: mod_str.quote,
  2172. end: mod_str,
  2173. }),
  2174. end: S.token,
  2175. });
  2176. }
  2177. function map_name(is_import) {
  2178. function make_symbol(type) {
  2179. return new type({
  2180. name: as_property_name(),
  2181. start: prev(),
  2182. end: prev()
  2183. });
  2184. }
  2185. var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
  2186. var type = is_import ? AST_SymbolImport : AST_SymbolExport;
  2187. var start = S.token;
  2188. var foreign_name;
  2189. var name;
  2190. if (is_import) {
  2191. foreign_name = make_symbol(foreign_type);
  2192. } else {
  2193. name = make_symbol(type);
  2194. }
  2195. if (is("name", "as")) {
  2196. next(); // The "as" word
  2197. if (is_import) {
  2198. name = make_symbol(type);
  2199. } else {
  2200. foreign_name = make_symbol(foreign_type);
  2201. }
  2202. } else if (is_import) {
  2203. name = new type(foreign_name);
  2204. } else {
  2205. foreign_name = new foreign_type(name);
  2206. }
  2207. return new AST_NameMapping({
  2208. start: start,
  2209. foreign_name: foreign_name,
  2210. name: name,
  2211. end: prev(),
  2212. })
  2213. }
  2214. function map_nameAsterisk(is_import, name) {
  2215. var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
  2216. var type = is_import ? AST_SymbolImport : AST_SymbolExport;
  2217. var start = S.token;
  2218. var foreign_name;
  2219. var end = prev();
  2220. name = name || new type({
  2221. name: '*',
  2222. start: start,
  2223. end: end,
  2224. });
  2225. foreign_name = new foreign_type({
  2226. name: '*',
  2227. start: start,
  2228. end: end,
  2229. });
  2230. return new AST_NameMapping({
  2231. start: start,
  2232. foreign_name: foreign_name,
  2233. name: name,
  2234. end: end,
  2235. })
  2236. }
  2237. function map_names(is_import) {
  2238. var names;
  2239. if (is("punc", "{")) {
  2240. next();
  2241. names = [];
  2242. while (!is("punc", "}")) {
  2243. names.push(map_name(is_import));
  2244. if (is("punc", ",")) {
  2245. next();
  2246. }
  2247. }
  2248. next();
  2249. } else if (is("operator", "*")) {
  2250. var name;
  2251. next();
  2252. if (is_import && is("name", "as")) {
  2253. next(); // The "as" word
  2254. name = as_symbol(AST_SymbolImportForeign);
  2255. }
  2256. names = [map_nameAsterisk(is_import, name)];
  2257. }
  2258. return names;
  2259. }
  2260. function export_() {
  2261. var start = S.token;
  2262. var is_default;
  2263. var exported_names;
  2264. if (is("keyword", "default")) {
  2265. is_default = true;
  2266. next();
  2267. } else if (exported_names = map_names(false)) {
  2268. if (is("name", "from")) {
  2269. next();
  2270. var mod_str = S.token;
  2271. if (mod_str.type !== 'string') {
  2272. unexpected();
  2273. }
  2274. next();
  2275. return new AST_Export({
  2276. start: start,
  2277. is_default: is_default,
  2278. exported_names: exported_names,
  2279. module_name: new AST_String({
  2280. start: mod_str,
  2281. value: mod_str.value,
  2282. quote: mod_str.quote,
  2283. end: mod_str,
  2284. }),
  2285. end: prev(),
  2286. });
  2287. } else {
  2288. return new AST_Export({
  2289. start: start,
  2290. is_default: is_default,
  2291. exported_names: exported_names,
  2292. end: prev(),
  2293. });
  2294. }
  2295. }
  2296. var node;
  2297. var exported_value;
  2298. var exported_definition;
  2299. if (is("punc", "{")
  2300. || is_default
  2301. && (is("keyword", "class") || is("keyword", "function"))
  2302. && is_token(peek(), "punc")) {
  2303. exported_value = expression(false);
  2304. semicolon();
  2305. } else if ((node = statement(is_default)) instanceof AST_Definitions && is_default) {
  2306. unexpected(node.start);
  2307. } else if (node instanceof AST_Definitions || node instanceof AST_Lambda || node instanceof AST_DefClass) {
  2308. exported_definition = node;
  2309. } else if (node instanceof AST_SimpleStatement) {
  2310. exported_value = node.body;
  2311. } else {
  2312. unexpected(node.start);
  2313. }
  2314. return new AST_Export({
  2315. start: start,
  2316. is_default: is_default,
  2317. exported_value: exported_value,
  2318. exported_definition: exported_definition,
  2319. end: prev(),
  2320. });
  2321. }
  2322. function as_property_name() {
  2323. var tmp = S.token;
  2324. switch (tmp.type) {
  2325. case "punc":
  2326. if (tmp.value === "[") {
  2327. next();
  2328. var ex = expression(false);
  2329. expect("]");
  2330. return ex;
  2331. } else unexpected(tmp);
  2332. case "operator":
  2333. if (tmp.value === "*") {
  2334. next();
  2335. return null;
  2336. }
  2337. if (["delete", "in", "instanceof", "new", "typeof", "void"].indexOf(tmp.value) === -1) {
  2338. unexpected(tmp);
  2339. }
  2340. case "name":
  2341. if (tmp.value == "yield") {
  2342. if (is_in_generator()) {
  2343. token_error(tmp, "Yield cannot be used as identifier inside generators");
  2344. } else if (!is_token(peek(), "punc", ":")
  2345. && !is_token(peek(), "punc", "(")
  2346. && S.input.has_directive("use strict")) {
  2347. token_error(tmp, "Unexpected yield identifier inside strict mode");
  2348. }
  2349. }
  2350. case "string":
  2351. case "num":
  2352. case "keyword":
  2353. case "atom":
  2354. next();
  2355. return tmp.value;
  2356. default:
  2357. unexpected(tmp);
  2358. }
  2359. };
  2360. function as_name() {
  2361. var tmp = S.token;
  2362. if (tmp.type != "name") unexpected();
  2363. next();
  2364. return tmp.value;
  2365. };
  2366. function _make_symbol(type) {
  2367. var name = S.token.value;
  2368. return new (name == "this" ? AST_This :
  2369. name == "super" ? AST_Super :
  2370. type)({
  2371. name : String(name),
  2372. start : S.token,
  2373. end : S.token
  2374. });
  2375. };
  2376. function _verify_symbol(sym) {
  2377. var name = sym.name;
  2378. if (is_in_generator() && name == "yield") {
  2379. token_error(sym.start, "Yield cannot be used as identifier inside generators");
  2380. }
  2381. if (S.input.has_directive("use strict")) {
  2382. if (name == "yield") {
  2383. token_error(sym.start, "Unexpected yield identifier inside strict mode");
  2384. }
  2385. if (sym instanceof AST_SymbolDeclaration && (name == "arguments" || name == "eval")) {
  2386. token_error(sym.start, "Unexpected " + name + " in strict mode");
  2387. }
  2388. }
  2389. }
  2390. function as_symbol(type, noerror) {
  2391. if (!is("name")) {
  2392. if (!noerror) croak("Name expected");
  2393. return null;
  2394. }
  2395. var sym = _make_symbol(type);
  2396. _verify_symbol(sym);
  2397. next();
  2398. return sym;
  2399. };
  2400. function mark_pure(call) {
  2401. var start = call.start;
  2402. var comments = start.comments_before;
  2403. var i = HOP(start, "comments_before_length") ? start.comments_before_length : comments.length;
  2404. while (--i >= 0) {
  2405. var comment = comments[i];
  2406. if (/[@#]__PURE__/.test(comment.value)) {
  2407. call.pure = comment;
  2408. break;
  2409. }
  2410. }
  2411. }
  2412. var subscripts = function(expr, allow_calls) {
  2413. var start = expr.start;
  2414. if (is("punc", ".")) {
  2415. next();
  2416. return subscripts(new AST_Dot({
  2417. start : start,
  2418. expression : expr,
  2419. property : as_name(),
  2420. end : prev()
  2421. }), allow_calls);
  2422. }
  2423. if (is("punc", "[")) {
  2424. next();
  2425. var prop = expression(true);
  2426. expect("]");
  2427. return subscripts(new AST_Sub({
  2428. start : start,
  2429. expression : expr,
  2430. property : prop,
  2431. end : prev()
  2432. }), allow_calls);
  2433. }
  2434. if (allow_calls && is("punc", "(")) {
  2435. next();
  2436. var call = new AST_Call({
  2437. start : start,
  2438. expression : expr,
  2439. args : call_args(),
  2440. end : prev()
  2441. });
  2442. mark_pure(call);
  2443. return subscripts(call, true);
  2444. }
  2445. if (is("template_head")) {
  2446. return subscripts(new AST_PrefixedTemplateString({
  2447. start: start,
  2448. prefix: expr,
  2449. template_string: template_string()
  2450. }), allow_calls);
  2451. }
  2452. return expr;
  2453. };
  2454. var call_args = embed_tokens(function _call_args() {
  2455. var args = [];
  2456. while (!is("punc", ")")) {
  2457. if (is("expand", "...")) {
  2458. next();
  2459. args.push(new AST_Expansion({
  2460. start: prev(),
  2461. expression: expression(false)
  2462. }));
  2463. } else {
  2464. args.push(expression(false));
  2465. }
  2466. if (!is("punc", ")")) {
  2467. expect(",");
  2468. if (is("punc", ")") && options.ecma < 8) unexpected();
  2469. }
  2470. }
  2471. next();
  2472. return args;
  2473. });
  2474. var maybe_unary = function(allow_calls, allow_arrows) {
  2475. var start = S.token;
  2476. if (start.type == "name" && start.value == "await") {
  2477. if (is_in_async()) {
  2478. next();
  2479. return _await_expression();
  2480. } else if (S.input.has_directive("use strict")) {
  2481. token_error(S.token, "Unexpected await identifier inside strict mode")
  2482. }
  2483. }
  2484. if (is("operator") && UNARY_PREFIX(start.value)) {
  2485. next();
  2486. handle_regexp();
  2487. var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls));
  2488. ex.start = start;
  2489. ex.end = prev();
  2490. return ex;
  2491. }
  2492. var val = expr_atom(allow_calls, allow_arrows);
  2493. while (is("operator") && UNARY_POSTFIX(S.token.value) && !has_newline_before(S.token)) {
  2494. if (val instanceof AST_Arrow) unexpected();
  2495. val = make_unary(AST_UnaryPostfix, S.token, val);
  2496. val.start = start;
  2497. val.end = S.token;
  2498. next();
  2499. }
  2500. return val;
  2501. };
  2502. function make_unary(ctor, token, expr) {
  2503. var op = token.value;
  2504. switch (op) {
  2505. case "++":
  2506. case "--":
  2507. if (!is_assignable(expr))
  2508. croak("Invalid use of " + op + " operator", token.line, token.col, token.pos);
  2509. break;
  2510. case "delete":
  2511. if (expr instanceof AST_SymbolRef && S.input.has_directive("use strict"))
  2512. croak("Calling delete on expression not allowed in strict mode", expr.start.line, expr.start.col, expr.start.pos);
  2513. break;
  2514. }
  2515. return new ctor({ operator: op, expression: expr });
  2516. };
  2517. var expr_op = function(left, min_prec, no_in) {
  2518. var op = is("operator") ? S.token.value : null;
  2519. if (op == "in" && no_in) op = null;
  2520. if (op == "**" && left instanceof AST_UnaryPrefix
  2521. /* unary token in front not allowed - parenthesis required */
  2522. && !is_token(left.start, "punc", "(")
  2523. && left.operator !== "--" && left.operator !== "++")
  2524. unexpected(left.start);
  2525. var prec = op != null ? PRECEDENCE[op] : null;
  2526. if (prec != null && (prec > min_prec || (op === "**" && min_prec === prec))) {
  2527. next();
  2528. var right = expr_op(maybe_unary(true), prec, no_in);
  2529. return expr_op(new AST_Binary({
  2530. start : left.start,
  2531. left : left,
  2532. operator : op,
  2533. right : right,
  2534. end : right.end
  2535. }), min_prec, no_in);
  2536. }
  2537. return left;
  2538. };
  2539. function expr_ops(no_in) {
  2540. return expr_op(maybe_unary(true, true), 0, no_in);
  2541. };
  2542. var maybe_conditional = function(no_in) {
  2543. var start = S.token;
  2544. var expr = expr_ops(no_in);
  2545. if (is("operator", "?")) {
  2546. next();
  2547. var yes = expression(false);
  2548. expect(":");
  2549. return new AST_Conditional({
  2550. start : start,
  2551. condition : expr,
  2552. consequent : yes,
  2553. alternative : expression(false, no_in),
  2554. end : prev()
  2555. });
  2556. }
  2557. return expr;
  2558. };
  2559. function is_assignable(expr) {
  2560. return expr instanceof AST_PropAccess || expr instanceof AST_SymbolRef;
  2561. };
  2562. function to_destructuring(node) {
  2563. if (node instanceof AST_Object) {
  2564. node = new AST_Destructuring({
  2565. start: node.start,
  2566. names: node.properties.map(to_destructuring),
  2567. is_array: false,
  2568. end: node.end
  2569. });
  2570. } else if (node instanceof AST_Array) {
  2571. var names = [];
  2572. for (var i = 0; i < node.elements.length; i++) {
  2573. // Only allow expansion as last element
  2574. if (node.elements[i] instanceof AST_Expansion) {
  2575. if (i + 1 !== node.elements.length) {
  2576. token_error(node.elements[i].start, "Spread must the be last element in destructuring array");
  2577. }
  2578. node.elements[i].expression = to_destructuring(node.elements[i].expression);
  2579. }
  2580. names.push(to_destructuring(node.elements[i]));
  2581. }
  2582. node = new AST_Destructuring({
  2583. start: node.start,
  2584. names: names,
  2585. is_array: true,
  2586. end: node.end
  2587. });
  2588. } else if (node instanceof AST_ObjectProperty) {
  2589. node.value = to_destructuring(node.value);
  2590. } else if (node instanceof AST_Assign) {
  2591. node = new AST_DefaultAssign({
  2592. start: node.start,
  2593. left: node.left,
  2594. operator: "=",
  2595. right: node.right,
  2596. end: node.end
  2597. });
  2598. }
  2599. return node;
  2600. }
  2601. // In ES6, AssignmentExpression can also be an ArrowFunction
  2602. var maybe_assign = function(no_in) {
  2603. var start = S.token;
  2604. if (start.type == "name" && start.value == "yield") {
  2605. if (is_in_generator()) {
  2606. next();
  2607. return _yield_expression();
  2608. } else if (S.input.has_directive("use strict")) {
  2609. token_error(S.token, "Unexpected yield identifier inside strict mode");
  2610. }
  2611. }
  2612. var left = maybe_conditional(no_in);
  2613. var val = S.token.value;
  2614. if (is("operator") && ASSIGNMENT(val)) {
  2615. if (is_assignable(left) || (left = to_destructuring(left)) instanceof AST_Destructuring) {
  2616. next();
  2617. return new AST_Assign({
  2618. start : start,
  2619. left : left,
  2620. operator : val,
  2621. right : maybe_assign(no_in),
  2622. end : prev()
  2623. });
  2624. }
  2625. croak("Invalid assignment");
  2626. }
  2627. return left;
  2628. };
  2629. var expression = function(commas, no_in) {
  2630. var start = S.token;
  2631. var exprs = [];
  2632. while (true) {
  2633. exprs.push(maybe_assign(no_in));
  2634. if (!commas || !is("punc", ",")) break;
  2635. next();
  2636. commas = true;
  2637. }
  2638. return exprs.length == 1 ? exprs[0] : new AST_Sequence({
  2639. start : start,
  2640. expressions : exprs,
  2641. end : peek()
  2642. });
  2643. };
  2644. function in_loop(cont) {
  2645. ++S.in_loop;
  2646. var ret = cont();
  2647. --S.in_loop;
  2648. return ret;
  2649. };
  2650. if (options.expression) {
  2651. return expression(true);
  2652. }
  2653. return (function(){
  2654. var start = S.token;
  2655. var body = [];
  2656. S.input.push_directives_stack();
  2657. while (!is("eof"))
  2658. body.push(statement());
  2659. S.input.pop_directives_stack();
  2660. var end = prev();
  2661. var toplevel = options.toplevel;
  2662. if (toplevel) {
  2663. toplevel.body = toplevel.body.concat(body);
  2664. toplevel.end = end;
  2665. } else {
  2666. toplevel = new AST_Toplevel({ start: start, body: body, end: end });
  2667. }
  2668. return toplevel;
  2669. })();
  2670. };