parse.js 105KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899
  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. S.comments_before = [];
  316. // make note of any newlines in the comments that came before
  317. for (var i = 0, len = ret.comments_before.length; i < len; i++) {
  318. ret.nlb = ret.nlb || ret.comments_before[i].nlb;
  319. }
  320. }
  321. S.newline_before = false;
  322. return new AST_Token(ret);
  323. };
  324. function skip_whitespace() {
  325. while (WHITESPACE_CHARS(peek()))
  326. next();
  327. };
  328. function read_while(pred) {
  329. var ret = "", ch, i = 0;
  330. while ((ch = peek()) && pred(ch, i++))
  331. ret += next();
  332. return ret;
  333. };
  334. function parse_error(err) {
  335. js_error(err, filename, S.tokline, S.tokcol, S.tokpos);
  336. };
  337. function read_num(prefix) {
  338. var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".";
  339. var num = read_while(function(ch, i){
  340. var code = ch.charCodeAt(0);
  341. switch (code) {
  342. case 98: case 66: // bB
  343. return (has_x = true); // Can occur in hex sequence, don't return false yet
  344. case 111: case 79: // oO
  345. case 120: case 88: // xX
  346. return has_x ? false : (has_x = true);
  347. case 101: case 69: // eE
  348. return has_x ? true : has_e ? false : (has_e = after_e = true);
  349. case 45: // -
  350. return after_e || (i == 0 && !prefix);
  351. case 43: // +
  352. return after_e;
  353. case (after_e = false, 46): // .
  354. return (!has_dot && !has_x && !has_e) ? (has_dot = true) : false;
  355. }
  356. return RE_NUM_LITERAL.test(ch);
  357. });
  358. if (prefix) num = prefix + num;
  359. if (RE_OCT_NUMBER.test(num) && next_token.has_directive("use strict")) {
  360. parse_error("Legacy octal literals are not allowed in strict mode");
  361. }
  362. var valid = parse_js_number(num);
  363. if (!isNaN(valid)) {
  364. return token("num", valid);
  365. } else {
  366. parse_error("Invalid syntax: " + num);
  367. }
  368. };
  369. function read_escaped_char(in_string) {
  370. var ch = next(true, in_string);
  371. switch (ch.charCodeAt(0)) {
  372. case 110 : return "\n";
  373. case 114 : return "\r";
  374. case 116 : return "\t";
  375. case 98 : return "\b";
  376. case 118 : return "\u000b"; // \v
  377. case 102 : return "\f";
  378. case 120 : return String.fromCharCode(hex_bytes(2)); // \x
  379. case 117 : // \u
  380. if (peek() == "{") {
  381. next(true);
  382. if (peek() === "}")
  383. parse_error("Expecting hex-character between {}");
  384. while (peek() == "0") next(true); // No significance
  385. var result, length = find("}", true) - S.pos;
  386. // Avoid 32 bit integer overflow (1 << 32 === 1)
  387. // We know first character isn't 0 and thus out of range anyway
  388. if (length > 6 || (result = hex_bytes(length)) > 0x10FFFF) {
  389. parse_error("Unicode reference out of bounce");
  390. }
  391. next(true);
  392. return from_char_code(result);
  393. }
  394. return String.fromCharCode(hex_bytes(4));
  395. case 10 : return ""; // newline
  396. case 13 : // \r
  397. if (peek() == "\n") { // DOS newline
  398. next(true, in_string);
  399. return "";
  400. }
  401. }
  402. if (ch >= "0" && ch <= "7")
  403. return read_octal_escape_sequence(ch);
  404. return ch;
  405. };
  406. function read_octal_escape_sequence(ch) {
  407. // Read
  408. var p = peek();
  409. if (p >= "0" && p <= "7") {
  410. ch += next(true);
  411. if (ch[0] <= "3" && (p = peek()) >= "0" && p <= "7")
  412. ch += next(true);
  413. }
  414. // Parse
  415. if (ch === "0") return "\0";
  416. if (ch.length > 0 && next_token.has_directive("use strict"))
  417. parse_error("Legacy octal escape sequences are not allowed in strict mode");
  418. return String.fromCharCode(parseInt(ch, 8));
  419. }
  420. function hex_bytes(n) {
  421. var num = 0;
  422. for (; n > 0; --n) {
  423. var digit = parseInt(next(true), 16);
  424. if (isNaN(digit))
  425. parse_error("Invalid hex-character pattern in string");
  426. num = (num << 4) | digit;
  427. }
  428. return num;
  429. };
  430. var read_string = with_eof_error("Unterminated string constant", function(quote_char){
  431. var quote = next(), ret = "";
  432. for (;;) {
  433. var ch = next(true, true);
  434. if (ch == "\\") ch = read_escaped_char(true);
  435. else if (NEWLINE_CHARS(ch)) parse_error("Unterminated string constant");
  436. else if (ch == quote) break;
  437. ret += ch;
  438. }
  439. var tok = token("string", ret);
  440. tok.quote = quote_char;
  441. return tok;
  442. });
  443. var read_template_characters = with_eof_error("Unterminated template", function(begin){
  444. if (begin) {
  445. S.template_braces.push(S.brace_counter);
  446. }
  447. var content = "", raw = "", ch, tok;
  448. next(true, true);
  449. while ((ch = next(true, true)) != "`") {
  450. if (ch == "\r") {
  451. if (peek() == "\n") ++S.pos;
  452. ch = "\n";
  453. } else if (ch == "$" && peek() == "{") {
  454. next(true, true);
  455. S.brace_counter++;
  456. tok = token(begin ? "template_head" : "template_substitution", content);
  457. tok.begin = begin;
  458. tok.raw = raw;
  459. tok.end = false;
  460. return tok;
  461. }
  462. raw += ch;
  463. if (ch == "\\") {
  464. var tmp = S.pos;
  465. ch = read_escaped_char();
  466. raw += S.text.substr(tmp, S.pos - tmp);
  467. }
  468. content += ch;
  469. }
  470. S.template_braces.pop();
  471. tok = token(begin ? "template_head" : "template_substitution", content);
  472. tok.begin = begin;
  473. tok.raw = raw;
  474. tok.end = true;
  475. return tok;
  476. });
  477. function skip_line_comment(type) {
  478. var regex_allowed = S.regex_allowed;
  479. var i = find_eol(), ret;
  480. if (i == -1) {
  481. ret = S.text.substr(S.pos);
  482. S.pos = S.text.length;
  483. } else {
  484. ret = S.text.substring(S.pos, i);
  485. S.pos = i;
  486. }
  487. S.col = S.tokcol + (S.pos - S.tokpos);
  488. S.comments_before.push(token(type, ret, true));
  489. S.regex_allowed = regex_allowed;
  490. return next_token;
  491. };
  492. var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function(){
  493. var regex_allowed = S.regex_allowed;
  494. var i = find("*/", true);
  495. var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, '\n');
  496. // update stream position
  497. forward(get_full_char_length(text) /* text length doesn't count \r\n as 2 char while S.pos - i does */ + 2);
  498. S.comments_before.push(token("comment2", text, true));
  499. S.newline_before = S.newline_before || text.indexOf("\n") >= 0;
  500. S.regex_allowed = regex_allowed;
  501. return next_token;
  502. });
  503. var read_name = with_eof_error("Unterminated identifier name", function() {
  504. var name = "", ch, escaped = false, hex;
  505. var read_escaped_identifier_char = function() {
  506. escaped = true;
  507. next();
  508. if (peek() !== "u") {
  509. parse_error("Expecting UnicodeEscapeSequence -- uXXXX or u{XXXX}");
  510. }
  511. return read_escaped_char();
  512. }
  513. // Read first character (ID_Start)
  514. if ((name = peek()) === "\\") {
  515. name = read_escaped_identifier_char();
  516. if (!is_identifier_start(name)) {
  517. parse_error("First identifier char is an invalid identifier char");
  518. }
  519. } else if (is_identifier_start(name)){
  520. next();
  521. } else {
  522. return "";
  523. }
  524. // Read ID_Continue
  525. while ((ch = peek()) != null) {
  526. if ((ch = peek()) === "\\") {
  527. ch = read_escaped_identifier_char();
  528. if (!is_identifier_char(ch)) {
  529. parse_error("Invalid escaped identifier char");
  530. }
  531. } else {
  532. if (!is_identifier_char(ch)) {
  533. break;
  534. }
  535. next();
  536. }
  537. name += ch;
  538. }
  539. if (RESERVED_WORDS(name) && escaped) {
  540. parse_error("Escaped characters are not allowed in keywords");
  541. }
  542. return name;
  543. });
  544. var read_regexp = with_eof_error("Unterminated regular expression", function(source) {
  545. var prev_backslash = false, ch, in_class = false;
  546. while ((ch = next(true))) if (NEWLINE_CHARS(ch)) {
  547. parse_error("Unexpected line terminator");
  548. } else if (prev_backslash) {
  549. source += "\\" + ch;
  550. prev_backslash = false;
  551. } else if (ch == "[") {
  552. in_class = true;
  553. source += ch;
  554. } else if (ch == "]" && in_class) {
  555. in_class = false;
  556. source += ch;
  557. } else if (ch == "/" && !in_class) {
  558. break;
  559. } else if (ch == "\\") {
  560. prev_backslash = true;
  561. } else {
  562. source += ch;
  563. }
  564. var mods = read_name();
  565. try {
  566. var regexp = new RegExp(source, mods);
  567. regexp.raw_source = source;
  568. return token("regexp", regexp);
  569. } catch(e) {
  570. parse_error(e.message);
  571. }
  572. });
  573. function read_operator(prefix) {
  574. function grow(op) {
  575. if (!peek()) return op;
  576. var bigger = op + peek();
  577. if (OPERATORS(bigger)) {
  578. next();
  579. return grow(bigger);
  580. } else {
  581. return op;
  582. }
  583. };
  584. return token("operator", grow(prefix || next()));
  585. };
  586. function handle_slash() {
  587. next();
  588. switch (peek()) {
  589. case "/":
  590. next();
  591. return skip_line_comment("comment1");
  592. case "*":
  593. next();
  594. return skip_multiline_comment();
  595. }
  596. return S.regex_allowed ? read_regexp("") : read_operator("/");
  597. };
  598. function handle_eq_sign() {
  599. next();
  600. if (peek() === ">") {
  601. next();
  602. return token("arrow", "=>");
  603. } else {
  604. return read_operator("=");
  605. }
  606. };
  607. function handle_dot() {
  608. next();
  609. if (is_digit(peek().charCodeAt(0))) {
  610. return read_num(".");
  611. }
  612. if (peek() === ".") {
  613. next(); // Consume second dot
  614. next(); // Consume third dot
  615. return token("expand", "...");
  616. }
  617. return token("punc", ".");
  618. };
  619. function read_word() {
  620. var word = read_name();
  621. if (prev_was_dot) return token("name", word);
  622. return KEYWORDS_ATOM(word) ? token("atom", word)
  623. : !KEYWORDS(word) ? token("name", word)
  624. : OPERATORS(word) ? token("operator", word)
  625. : token("keyword", word);
  626. };
  627. function with_eof_error(eof_error, cont) {
  628. return function(x) {
  629. try {
  630. return cont(x);
  631. } catch(ex) {
  632. if (ex === EX_EOF) parse_error(eof_error);
  633. else throw ex;
  634. }
  635. };
  636. };
  637. function next_token(force_regexp) {
  638. if (force_regexp != null)
  639. return read_regexp(force_regexp);
  640. if (shebang && S.pos == 0 && looking_at("#!")) {
  641. start_token();
  642. forward(2);
  643. skip_line_comment("comment5");
  644. }
  645. for (;;) {
  646. skip_whitespace();
  647. start_token();
  648. if (html5_comments) {
  649. if (looking_at("<!--")) {
  650. forward(4);
  651. skip_line_comment("comment3");
  652. continue;
  653. }
  654. if (looking_at("-->") && S.newline_before) {
  655. forward(3);
  656. skip_line_comment("comment4");
  657. continue;
  658. }
  659. }
  660. var ch = peek();
  661. if (!ch) return token("eof");
  662. var code = ch.charCodeAt(0);
  663. switch (code) {
  664. case 34: case 39: return read_string(ch);
  665. case 46: return handle_dot();
  666. case 47: {
  667. var tok = handle_slash();
  668. if (tok === next_token) continue;
  669. return tok;
  670. }
  671. case 61: return handle_eq_sign();
  672. case 96: return read_template_characters(true);
  673. case 123:
  674. S.brace_counter++;
  675. break;
  676. case 125:
  677. S.brace_counter--;
  678. if (S.template_braces.length > 0
  679. && S.template_braces[S.template_braces.length - 1] === S.brace_counter)
  680. return read_template_characters(false);
  681. break;
  682. }
  683. if (is_digit(code)) return read_num();
  684. if (PUNC_CHARS(ch)) return token("punc", next());
  685. if (OPERATOR_CHARS(ch)) return read_operator();
  686. if (code == 92 || is_identifier_start(ch)) return read_word();
  687. break;
  688. }
  689. parse_error("Unexpected character '" + ch + "'");
  690. };
  691. next_token.next = next;
  692. next_token.peek = peek;
  693. next_token.context = function(nc) {
  694. if (nc) S = nc;
  695. return S;
  696. };
  697. next_token.add_directive = function(directive) {
  698. S.directive_stack[S.directive_stack.length - 1].push(directive);
  699. if (S.directives[directive] === undefined) {
  700. S.directives[directive] = 1;
  701. } else {
  702. S.directives[directive]++;
  703. }
  704. }
  705. next_token.push_directives_stack = function() {
  706. S.directive_stack.push([]);
  707. }
  708. next_token.pop_directives_stack = function() {
  709. var directives = S.directive_stack[S.directive_stack.length - 1];
  710. for (var i = 0; i < directives.length; i++) {
  711. S.directives[directives[i]]--;
  712. }
  713. S.directive_stack.pop();
  714. }
  715. next_token.has_directive = function(directive) {
  716. return S.directives[directive] > 0;
  717. }
  718. return next_token;
  719. };
  720. /* -----[ Parser (constants) ]----- */
  721. var UNARY_PREFIX = makePredicate([
  722. "typeof",
  723. "void",
  724. "delete",
  725. "--",
  726. "++",
  727. "!",
  728. "~",
  729. "-",
  730. "+"
  731. ]);
  732. var UNARY_POSTFIX = makePredicate([ "--", "++" ]);
  733. var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "/=", "*=", "**=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]);
  734. var PRECEDENCE = (function(a, ret){
  735. for (var i = 0; i < a.length; ++i) {
  736. var b = a[i];
  737. for (var j = 0; j < b.length; ++j) {
  738. ret[b[j]] = i + 1;
  739. }
  740. }
  741. return ret;
  742. })(
  743. [
  744. ["||"],
  745. ["&&"],
  746. ["|"],
  747. ["^"],
  748. ["&"],
  749. ["==", "===", "!=", "!=="],
  750. ["<", ">", "<=", ">=", "in", "instanceof"],
  751. [">>", "<<", ">>>"],
  752. ["+", "-"],
  753. ["*", "/", "%"],
  754. ["**"]
  755. ],
  756. {}
  757. );
  758. var ATOMIC_START_TOKEN = makePredicate([ "atom", "num", "string", "regexp", "name" ]);
  759. /* -----[ Parser ]----- */
  760. function parse($TEXT, options) {
  761. options = defaults(options, {
  762. bare_returns : false,
  763. ecma : 8,
  764. expression : false,
  765. filename : null,
  766. html5_comments : true,
  767. shebang : true,
  768. strict : false,
  769. toplevel : null,
  770. }, true);
  771. var S = {
  772. input : (typeof $TEXT == "string"
  773. ? tokenizer($TEXT, options.filename,
  774. options.html5_comments, options.shebang)
  775. : $TEXT),
  776. token : null,
  777. prev : null,
  778. peeked : null,
  779. in_function : 0,
  780. in_async : -1,
  781. in_generator : -1,
  782. in_directives : true,
  783. in_loop : 0,
  784. labels : []
  785. };
  786. S.token = next();
  787. function is(type, value) {
  788. return is_token(S.token, type, value);
  789. };
  790. function peek() { return S.peeked || (S.peeked = S.input()); };
  791. function next() {
  792. S.prev = S.token;
  793. if (S.peeked) {
  794. S.token = S.peeked;
  795. S.peeked = null;
  796. } else {
  797. S.token = S.input();
  798. }
  799. S.in_directives = S.in_directives && (
  800. S.token.type == "string" || is("punc", ";")
  801. );
  802. return S.token;
  803. };
  804. function prev() {
  805. return S.prev;
  806. };
  807. function croak(msg, line, col, pos) {
  808. var ctx = S.input.context();
  809. js_error(msg,
  810. ctx.filename,
  811. line != null ? line : ctx.tokline,
  812. col != null ? col : ctx.tokcol,
  813. pos != null ? pos : ctx.tokpos);
  814. };
  815. function token_error(token, msg) {
  816. croak(msg, token.line, token.col);
  817. };
  818. function unexpected(token) {
  819. if (token == null)
  820. token = S.token;
  821. token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
  822. };
  823. function expect_token(type, val) {
  824. if (is(type, val)) {
  825. return next();
  826. }
  827. token_error(S.token, "Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
  828. };
  829. function expect(punc) { return expect_token("punc", punc); };
  830. function can_insert_semicolon() {
  831. return !options.strict && (
  832. S.token.nlb || is("eof") || is("punc", "}")
  833. );
  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() {
  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. && (token.nlb
  875. || is_token(token, "eof")
  876. || is_token(token, "punc", ";")
  877. || is_token(token, "punc", "}"))) {
  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);
  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);
  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 (S.token.nlb)
  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 === "yield" && is_in_generator()) {
  1032. // Ecma-262, 12.1.1 Static Semantics: Early Errors
  1033. token_error(S.prev, "Yield cannot be used as label inside generators");
  1034. }
  1035. if (label.name === "await" && is_in_async()) {
  1036. token_error(S.prev, "await cannot be used as label inside async function");
  1037. }
  1038. if (find_if(function(l){ return l.name == label.name }, S.labels)) {
  1039. // ECMA-262, 12.12: An ECMAScript program is considered
  1040. // syntactically incorrect if it contains a
  1041. // LabelledStatement that is enclosed by a
  1042. // LabelledStatement with the same Identifier as label.
  1043. croak("Label " + label.name + " defined twice");
  1044. }
  1045. expect(":");
  1046. S.labels.push(label);
  1047. var stat = statement();
  1048. S.labels.pop();
  1049. if (!(stat instanceof AST_IterationStatement)) {
  1050. // check for `continue` that refers to this label.
  1051. // those should be reported as syntax errors.
  1052. // https://github.com/mishoo/UglifyJS2/issues/287
  1053. label.references.forEach(function(ref){
  1054. if (ref instanceof AST_Continue) {
  1055. ref = ref.label.start;
  1056. croak("Continue label `" + label.name + "` refers to non-IterationStatement.",
  1057. ref.line, ref.col, ref.pos);
  1058. }
  1059. });
  1060. }
  1061. return new AST_LabeledStatement({ body: stat, label: label });
  1062. };
  1063. function simple_statement(tmp) {
  1064. return new AST_SimpleStatement({ body: (tmp = expression(true), semicolon(), tmp) });
  1065. };
  1066. function break_cont(type) {
  1067. var label = null, ldef;
  1068. if (!can_insert_semicolon()) {
  1069. label = as_symbol(AST_LabelRef, true);
  1070. }
  1071. if (label != null) {
  1072. ldef = find_if(function(l){ return l.name == label.name }, S.labels);
  1073. if (!ldef)
  1074. croak("Undefined label " + label.name);
  1075. label.thedef = ldef;
  1076. }
  1077. else if (S.in_loop == 0)
  1078. croak(type.TYPE + " not inside a loop or switch");
  1079. semicolon();
  1080. var stat = new type({ label: label });
  1081. if (ldef) ldef.references.push(stat);
  1082. return stat;
  1083. };
  1084. function for_() {
  1085. expect("(");
  1086. var init = null;
  1087. if (!is("punc", ";")) {
  1088. init =
  1089. is("keyword", "var") ? (next(), var_(true)) :
  1090. is("keyword", "let") ? (next(), let_(true)) :
  1091. is("keyword", "const") ? (next(), const_(true)) :
  1092. expression(true, true);
  1093. var is_in = is("operator", "in");
  1094. var is_of = is("name", "of");
  1095. if (is_in || is_of) {
  1096. if (init instanceof AST_Definitions) {
  1097. if (init.definitions.length > 1)
  1098. croak("Only one variable declaration allowed in for..in loop", init.start.line, init.start.col, init.start.pos);
  1099. } else if (!(is_assignable(init) || (init = to_destructuring(init)) instanceof AST_Destructuring)) {
  1100. croak("Invalid left-hand side in for..in loop", init.start.line, init.start.col, init.start.pos);
  1101. }
  1102. next();
  1103. if (is_in) {
  1104. return for_in(init);
  1105. } else {
  1106. return for_of(init);
  1107. }
  1108. }
  1109. }
  1110. return regular_for(init);
  1111. };
  1112. function regular_for(init) {
  1113. expect(";");
  1114. var test = is("punc", ";") ? null : expression(true);
  1115. expect(";");
  1116. var step = is("punc", ")") ? null : expression(true);
  1117. expect(")");
  1118. return new AST_For({
  1119. init : init,
  1120. condition : test,
  1121. step : step,
  1122. body : in_loop(statement)
  1123. });
  1124. };
  1125. function for_of(init) {
  1126. var lhs = init instanceof AST_Definitions ? init.definitions[0].name : null;
  1127. var obj = expression(true);
  1128. expect(")");
  1129. return new AST_ForOf({
  1130. init : init,
  1131. name : lhs,
  1132. object : obj,
  1133. body : in_loop(statement)
  1134. });
  1135. };
  1136. function for_in(init) {
  1137. var lhs = init instanceof AST_Definitions ? init.definitions[0].name : null;
  1138. var obj = expression(true);
  1139. expect(")");
  1140. return new AST_ForIn({
  1141. init : init,
  1142. name : lhs,
  1143. object : obj,
  1144. body : in_loop(statement)
  1145. });
  1146. };
  1147. var arrow_function = function(start, argnames, is_async) {
  1148. if (S.token.nlb) {
  1149. croak("Unexpected newline before arrow (=>)");
  1150. }
  1151. expect_token("arrow", "=>");
  1152. var body = _function_body(is("punc", "{"), false, is_async);
  1153. return new AST_Arrow({
  1154. start : start,
  1155. end : body.end,
  1156. async : is_async,
  1157. argnames : argnames,
  1158. body : body
  1159. });
  1160. };
  1161. var function_ = function(ctor, is_generator_property, is_async) {
  1162. if (is_generator_property && is_async) croak("generators cannot be async");
  1163. var start = S.token
  1164. var in_statement = ctor === AST_Defun;
  1165. var is_generator = is("operator", "*");
  1166. if (is_generator) {
  1167. next();
  1168. }
  1169. var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null;
  1170. if (in_statement && !name)
  1171. unexpected();
  1172. if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration))
  1173. unexpected(prev());
  1174. var args = parameters();
  1175. var body = _function_body(true, is_generator || is_generator_property, is_async, name, args);
  1176. return new ctor({
  1177. start : args.start,
  1178. end : body.end,
  1179. is_generator: is_generator,
  1180. async : is_async,
  1181. name : name,
  1182. argnames: args,
  1183. body : body
  1184. });
  1185. };
  1186. function track_used_binding_identifiers(is_parameter, strict) {
  1187. var parameters = {};
  1188. var duplicate = false;
  1189. var default_assignment = false;
  1190. var spread = false;
  1191. var strict_mode = !!strict;
  1192. var tracker = {
  1193. add_parameter: function(token) {
  1194. if (parameters["$" + token.value] !== undefined) {
  1195. if (duplicate === false) {
  1196. duplicate = token;
  1197. }
  1198. tracker.check_strict();
  1199. } else {
  1200. parameters["$" + token.value] = true;
  1201. if (is_parameter) {
  1202. switch (token.value) {
  1203. case "arguments":
  1204. case "eval":
  1205. case "yield":
  1206. if (strict_mode) {
  1207. token_error(token, "Unexpected " + token.value + " identifier as parameter inside strict mode");
  1208. }
  1209. break;
  1210. default:
  1211. if (RESERVED_WORDS(token.value)) {
  1212. unexpected();
  1213. }
  1214. }
  1215. }
  1216. }
  1217. },
  1218. mark_default_assignment: function(token) {
  1219. if (default_assignment === false) {
  1220. default_assignment = token;
  1221. }
  1222. },
  1223. mark_spread: function(token) {
  1224. if (spread === false) {
  1225. spread = token;
  1226. }
  1227. },
  1228. mark_strict_mode: function() {
  1229. strict_mode = true;
  1230. },
  1231. is_strict: function() {
  1232. return default_assignment !== false || spread !== false || strict_mode
  1233. },
  1234. check_strict: function() {
  1235. if (tracker.is_strict() && duplicate !== false) {
  1236. token_error(duplicate, "Parameter " + duplicate.value + " was used already");
  1237. }
  1238. }
  1239. };
  1240. return tracker;
  1241. }
  1242. function parameters() {
  1243. var start = S.token;
  1244. var params = [];
  1245. var used_parameters = track_used_binding_identifiers(true, S.input.has_directive("use strict"));
  1246. expect("(");
  1247. while (!is("punc", ")")) {
  1248. var param = parameter(used_parameters);
  1249. params.push(param);
  1250. if (!is("punc", ")")) {
  1251. expect(",");
  1252. if (is("punc", ")") && options.ecma < 8) unexpected();
  1253. }
  1254. if (param instanceof AST_Expansion) {
  1255. break;
  1256. }
  1257. }
  1258. next();
  1259. return params;
  1260. }
  1261. function parameter(used_parameters, symbol_type) {
  1262. var param;
  1263. var expand = false;
  1264. if (used_parameters === undefined) {
  1265. used_parameters = track_used_binding_identifiers(true, S.input.has_directive("use strict"));
  1266. }
  1267. if (is("expand", "...")) {
  1268. expand = S.token;
  1269. used_parameters.mark_spread(S.token);
  1270. next();
  1271. }
  1272. param = binding_element(used_parameters, symbol_type);
  1273. if (is("operator", "=") && expand === false) {
  1274. used_parameters.mark_default_assignment(S.token);
  1275. next();
  1276. param = new AST_DefaultAssign({
  1277. start: param.start,
  1278. left: param,
  1279. operator: "=",
  1280. right: expression(false),
  1281. end: S.token
  1282. });
  1283. }
  1284. if (expand !== false) {
  1285. if (!is("punc", ")")) {
  1286. unexpected();
  1287. }
  1288. param = new AST_Expansion({
  1289. start: expand,
  1290. expression: param,
  1291. end: expand
  1292. });
  1293. }
  1294. used_parameters.check_strict();
  1295. return param;
  1296. }
  1297. function binding_element(used_parameters, symbol_type) {
  1298. var elements = [];
  1299. var first = true;
  1300. var is_expand = false;
  1301. var expand_token;
  1302. var first_token = S.token;
  1303. if (used_parameters === undefined) {
  1304. used_parameters = track_used_binding_identifiers(false, S.input.has_directive("use strict"));
  1305. }
  1306. symbol_type = symbol_type === undefined ? AST_SymbolFunarg : symbol_type;
  1307. if (is("punc", "[")) {
  1308. next();
  1309. while (!is("punc", "]")) {
  1310. if (first) {
  1311. first = false;
  1312. } else {
  1313. expect(",");
  1314. }
  1315. if (is("expand", "...")) {
  1316. is_expand = true;
  1317. expand_token = S.token;
  1318. used_parameters.mark_spread(S.token);
  1319. next();
  1320. }
  1321. if (is("punc")) {
  1322. switch (S.token.value) {
  1323. case ",":
  1324. elements.push(new AST_Hole({
  1325. start: S.token,
  1326. end: S.token
  1327. }));
  1328. continue;
  1329. case "]": // Trailing comma after last element
  1330. break;
  1331. case "[":
  1332. case "{":
  1333. elements.push(binding_element(used_parameters, symbol_type));
  1334. break;
  1335. default:
  1336. unexpected();
  1337. }
  1338. } else if (is("name")) {
  1339. used_parameters.add_parameter(S.token);
  1340. elements.push(new symbol_type({
  1341. start: S.token,
  1342. name: S.token.value,
  1343. end: S.token
  1344. }));
  1345. next();
  1346. } else {
  1347. croak("Invalid function parameter");
  1348. }
  1349. if (is("operator", "=") && is_expand === false) {
  1350. used_parameters.mark_default_assignment(S.token);
  1351. next();
  1352. elements[elements.length - 1] = new AST_DefaultAssign({
  1353. start: elements[elements.length - 1].start,
  1354. left: elements[elements.length - 1],
  1355. operator: "=",
  1356. right: expression(false),
  1357. end: S.token
  1358. });
  1359. }
  1360. if (is_expand) {
  1361. if (!is("punc", "]")) {
  1362. croak("Rest element must be last element");
  1363. }
  1364. elements[elements.length - 1] = new AST_Expansion({
  1365. start: expand_token,
  1366. expression: elements[elements.length - 1],
  1367. end: expand_token
  1368. });
  1369. }
  1370. }
  1371. expect("]");
  1372. used_parameters.check_strict();
  1373. return new AST_Destructuring({
  1374. start: first_token,
  1375. names: elements,
  1376. is_array: true,
  1377. end: prev()
  1378. });
  1379. } else if (is("punc", "{")) {
  1380. next();
  1381. while (!is("punc", "}")) {
  1382. if (first) {
  1383. first = false;
  1384. } else {
  1385. expect(",");
  1386. }
  1387. if (is("expand", "...")) {
  1388. is_expand = true;
  1389. expand_token = S.token;
  1390. used_parameters.mark_spread(S.token);
  1391. next();
  1392. }
  1393. if (is("name") && (is_token(peek(), "punc") || is_token(peek(), "operator")) && [",", "}", "="].indexOf(peek().value) !== -1) {
  1394. used_parameters.add_parameter(S.token);
  1395. var value = new symbol_type({
  1396. start: S.token,
  1397. name: S.token.value,
  1398. end: S.token,
  1399. });
  1400. if (is_expand) {
  1401. elements.push(new AST_Expansion({
  1402. start: expand_token,
  1403. expression: value,
  1404. end: value.end,
  1405. }));
  1406. } else {
  1407. elements.push(new AST_ObjectKeyVal({
  1408. start: prev(),
  1409. key: S.token.value,
  1410. value: value,
  1411. end: value.end,
  1412. }));
  1413. }
  1414. next();
  1415. } else if (is("punc", "}")) {
  1416. continue; // Allow trailing hole
  1417. } else {
  1418. var property_token = S.token;
  1419. var property = as_property_name();
  1420. if (property === null) {
  1421. unexpected(prev());
  1422. } else if (prev().type === "name" && !is("punc", ":")) {
  1423. elements.push(new AST_ObjectKeyVal({
  1424. start: prev(),
  1425. key: property,
  1426. value: new symbol_type({
  1427. start: prev(),
  1428. name: property,
  1429. end: prev()
  1430. }),
  1431. end: prev()
  1432. }));
  1433. } else {
  1434. expect(":");
  1435. elements.push(new AST_ObjectKeyVal({
  1436. start: property_token,
  1437. quote: property_token.quote,
  1438. key: property,
  1439. value: binding_element(used_parameters, symbol_type),
  1440. end: prev()
  1441. }));
  1442. }
  1443. }
  1444. if (is_expand) {
  1445. if (!is("punc", "}")) {
  1446. croak("Rest element must be last element");
  1447. }
  1448. }
  1449. else if (is("operator", "=")) {
  1450. used_parameters.mark_default_assignment(S.token);
  1451. next();
  1452. elements[elements.length - 1].value = new AST_DefaultAssign({
  1453. start: elements[elements.length - 1].value.start,
  1454. left: elements[elements.length - 1].value,
  1455. operator: "=",
  1456. right: expression(false),
  1457. end: S.token
  1458. });
  1459. }
  1460. }
  1461. expect("}");
  1462. used_parameters.check_strict();
  1463. return new AST_Destructuring({
  1464. start: first_token,
  1465. names: elements,
  1466. is_array: false,
  1467. end: prev()
  1468. });
  1469. } else if (is("name")) {
  1470. used_parameters.add_parameter(S.token);
  1471. next();
  1472. return new symbol_type({
  1473. start: prev(),
  1474. name: prev().value,
  1475. end: prev()
  1476. });
  1477. } else {
  1478. croak("Invalid function parameter");
  1479. }
  1480. }
  1481. function params_or_seq_(allow_arrows, maybe_sequence) {
  1482. var spread_token;
  1483. var invalid_sequence;
  1484. var trailing_comma;
  1485. var a = [];
  1486. expect("(");
  1487. while (!is("punc", ")")) {
  1488. if (spread_token) unexpected(spread_token);
  1489. if (is("expand", "...")) {
  1490. spread_token = S.token;
  1491. if (maybe_sequence) invalid_sequence = S.token;
  1492. next();
  1493. a.push(new AST_Expansion({
  1494. start: prev(),
  1495. expression: expression(),
  1496. end: S.token,
  1497. }));
  1498. } else {
  1499. a.push(expression());
  1500. }
  1501. if (!is("punc", ")")) {
  1502. expect(",");
  1503. if (is("punc", ")")) {
  1504. if (options.ecma < 8) unexpected();
  1505. trailing_comma = prev();
  1506. if (maybe_sequence) invalid_sequence = trailing_comma;
  1507. }
  1508. }
  1509. }
  1510. expect(")");
  1511. if (allow_arrows && is("arrow", "=>")) {
  1512. if (spread_token && trailing_comma) unexpected(trailing_comma);
  1513. } else if (invalid_sequence) {
  1514. unexpected(invalid_sequence);
  1515. }
  1516. return a;
  1517. }
  1518. function _function_body(block, generator, is_async, name, args) {
  1519. var loop = S.in_loop;
  1520. var labels = S.labels;
  1521. var current_generator = S.in_generator;
  1522. var current_async = S.in_async;
  1523. ++S.in_function;
  1524. if (generator)
  1525. S.in_generator = S.in_function;
  1526. if (is_async)
  1527. S.in_async = S.in_function;
  1528. if (block)
  1529. S.in_directives = true;
  1530. S.in_loop = 0;
  1531. S.labels = [];
  1532. if (block) {
  1533. S.input.push_directives_stack();
  1534. var a = block_();
  1535. if (S.input.has_directive("use strict")) {
  1536. if (name) strict_verify_symbol(name);
  1537. if (args) args.forEach(strict_verify_symbol);
  1538. }
  1539. S.input.pop_directives_stack();
  1540. } else {
  1541. var a = expression(false);
  1542. }
  1543. --S.in_function;
  1544. S.in_loop = loop;
  1545. S.labels = labels;
  1546. S.in_generator = current_generator;
  1547. S.in_async = current_async;
  1548. return a;
  1549. }
  1550. function _await_expression() {
  1551. // Previous token must be "await" and not be interpreted as an identifier
  1552. if (!is_in_async()) {
  1553. croak("Unexpected await expression outside async function",
  1554. S.prev.line, S.prev.col, S.prev.pos);
  1555. }
  1556. // the await expression is parsed as a unary expression in Babel
  1557. return new AST_Await({
  1558. expression : maybe_unary(true),
  1559. });
  1560. }
  1561. function _yield_expression() {
  1562. // Previous token must be keyword yield and not be interpret as an identifier
  1563. if (!is_in_generator()) {
  1564. croak("Unexpected yield expression outside generator function",
  1565. S.prev.line, S.prev.col, S.prev.pos);
  1566. }
  1567. var star = false;
  1568. var has_expression = true;
  1569. // Attempt to get expression or star (and then the mandatory expression)
  1570. // behind yield on the same line.
  1571. //
  1572. // If nothing follows on the same line of the yieldExpression,
  1573. // it should default to the value `undefined` for yield to return.
  1574. // In that case, the `undefined` stored as `null` in ast.
  1575. //
  1576. // Note 1: It isn't allowed for yield* to close without an expression
  1577. // Note 2: If there is a nlb between yield and star, it is interpret as
  1578. // yield <explicit undefined> <inserted automatic semicolon> *
  1579. if (can_insert_semicolon() ||
  1580. (is("punc") && PUNC_AFTER_EXPRESSION(S.token.value))) {
  1581. has_expression = false;
  1582. } else if (is("operator", "*")) {
  1583. star = true;
  1584. next();
  1585. }
  1586. return new AST_Yield({
  1587. is_star : star,
  1588. expression : has_expression ? expression() : null
  1589. });
  1590. }
  1591. function if_() {
  1592. var cond = parenthesised(), body = statement(), belse = null;
  1593. if (is("keyword", "else")) {
  1594. next();
  1595. belse = statement();
  1596. }
  1597. return new AST_If({
  1598. condition : cond,
  1599. body : body,
  1600. alternative : belse
  1601. });
  1602. };
  1603. function block_() {
  1604. expect("{");
  1605. var a = [];
  1606. while (!is("punc", "}")) {
  1607. if (is("eof")) unexpected();
  1608. a.push(statement());
  1609. }
  1610. next();
  1611. return a;
  1612. };
  1613. function switch_body_() {
  1614. expect("{");
  1615. var a = [], cur = null, branch = null, tmp;
  1616. while (!is("punc", "}")) {
  1617. if (is("eof")) unexpected();
  1618. if (is("keyword", "case")) {
  1619. if (branch) branch.end = prev();
  1620. cur = [];
  1621. branch = new AST_Case({
  1622. start : (tmp = S.token, next(), tmp),
  1623. expression : expression(true),
  1624. body : cur
  1625. });
  1626. a.push(branch);
  1627. expect(":");
  1628. }
  1629. else if (is("keyword", "default")) {
  1630. if (branch) branch.end = prev();
  1631. cur = [];
  1632. branch = new AST_Default({
  1633. start : (tmp = S.token, next(), expect(":"), tmp),
  1634. body : cur
  1635. });
  1636. a.push(branch);
  1637. }
  1638. else {
  1639. if (!cur) unexpected();
  1640. cur.push(statement());
  1641. }
  1642. }
  1643. if (branch) branch.end = prev();
  1644. next();
  1645. return a;
  1646. };
  1647. function try_() {
  1648. var body = block_(), bcatch = null, bfinally = null;
  1649. if (is("keyword", "catch")) {
  1650. var start = S.token;
  1651. next();
  1652. expect("(");
  1653. var name = parameter(undefined, AST_SymbolCatch);
  1654. expect(")");
  1655. bcatch = new AST_Catch({
  1656. start : start,
  1657. argname : name,
  1658. body : block_(),
  1659. end : prev()
  1660. });
  1661. }
  1662. if (is("keyword", "finally")) {
  1663. var start = S.token;
  1664. next();
  1665. bfinally = new AST_Finally({
  1666. start : start,
  1667. body : block_(),
  1668. end : prev()
  1669. });
  1670. }
  1671. if (!bcatch && !bfinally)
  1672. croak("Missing catch/finally blocks");
  1673. return new AST_Try({
  1674. body : body,
  1675. bcatch : bcatch,
  1676. bfinally : bfinally
  1677. });
  1678. };
  1679. function vardefs(no_in, kind) {
  1680. var a = [];
  1681. var def;
  1682. for (;;) {
  1683. var sym_type =
  1684. kind === "var" ? AST_SymbolVar :
  1685. kind === "const" ? AST_SymbolConst :
  1686. kind === "let" ? AST_SymbolLet : null;
  1687. if (is("punc", "{") || is("punc", "[")) {
  1688. def = new AST_VarDef({
  1689. start: S.token,
  1690. name: binding_element(undefined ,sym_type),
  1691. value: is("operator", "=") ? (expect_token("operator", "="), expression(false, no_in)) : null,
  1692. end: prev()
  1693. });
  1694. } else {
  1695. def = new AST_VarDef({
  1696. start : S.token,
  1697. name : as_symbol(sym_type),
  1698. value : is("operator", "=")
  1699. ? (next(), expression(false, no_in))
  1700. : !no_in && kind === "const" && S.input.has_directive("use strict")
  1701. ? croak("Missing initializer in const declaration") : null,
  1702. end : prev()
  1703. });
  1704. if (def.name.name == "import") croak("Unexpected token: import");
  1705. }
  1706. a.push(def);
  1707. if (!is("punc", ","))
  1708. break;
  1709. next();
  1710. }
  1711. return a;
  1712. };
  1713. var var_ = function(no_in) {
  1714. return new AST_Var({
  1715. start : prev(),
  1716. definitions : vardefs(no_in, "var"),
  1717. end : prev()
  1718. });
  1719. };
  1720. var let_ = function(no_in) {
  1721. return new AST_Let({
  1722. start : prev(),
  1723. definitions : vardefs(no_in, "let"),
  1724. end : prev()
  1725. });
  1726. };
  1727. var const_ = function(no_in) {
  1728. return new AST_Const({
  1729. start : prev(),
  1730. definitions : vardefs(no_in, "const"),
  1731. end : prev()
  1732. });
  1733. };
  1734. var new_ = function(allow_calls) {
  1735. var start = S.token;
  1736. expect_token("operator", "new");
  1737. if (is("punc", ".")) {
  1738. next();
  1739. expect_token("name", "target");
  1740. return subscripts(new AST_NewTarget({
  1741. start : start,
  1742. end : prev()
  1743. }), allow_calls);
  1744. }
  1745. var newexp = expr_atom(false), args;
  1746. if (is("punc", "(")) {
  1747. next();
  1748. args = expr_list(")", options.ecma >= 8);
  1749. } else {
  1750. args = [];
  1751. }
  1752. return subscripts(new AST_New({
  1753. start : start,
  1754. expression : newexp,
  1755. args : args,
  1756. end : prev()
  1757. }), allow_calls);
  1758. };
  1759. function as_atom_node() {
  1760. var tok = S.token, ret;
  1761. switch (tok.type) {
  1762. case "name":
  1763. ret = _make_symbol(AST_SymbolRef);
  1764. break;
  1765. case "num":
  1766. ret = new AST_Number({ start: tok, end: tok, value: tok.value });
  1767. break;
  1768. case "string":
  1769. ret = new AST_String({
  1770. start : tok,
  1771. end : tok,
  1772. value : tok.value,
  1773. quote : tok.quote
  1774. });
  1775. break;
  1776. case "regexp":
  1777. ret = new AST_RegExp({ start: tok, end: tok, value: tok.value });
  1778. break;
  1779. case "atom":
  1780. switch (tok.value) {
  1781. case "false":
  1782. ret = new AST_False({ start: tok, end: tok });
  1783. break;
  1784. case "true":
  1785. ret = new AST_True({ start: tok, end: tok });
  1786. break;
  1787. case "null":
  1788. ret = new AST_Null({ start: tok, end: tok });
  1789. break;
  1790. }
  1791. break;
  1792. }
  1793. next();
  1794. return ret;
  1795. };
  1796. function to_fun_args(ex, _, __, default_seen_above) {
  1797. var insert_default = function(ex, default_value) {
  1798. if (default_value) {
  1799. return new AST_DefaultAssign({
  1800. start: ex.start,
  1801. left: ex,
  1802. operator: "=",
  1803. right: default_value,
  1804. end: default_value.end
  1805. });
  1806. }
  1807. return ex;
  1808. }
  1809. if (ex instanceof AST_Object) {
  1810. return insert_default(new AST_Destructuring({
  1811. start: ex.start,
  1812. end: ex.end,
  1813. is_array: false,
  1814. names: ex.properties.map(to_fun_args)
  1815. }), default_seen_above);
  1816. } else if (ex instanceof AST_ObjectKeyVal) {
  1817. ex.value = to_fun_args(ex.value, 0, [ex.key]);
  1818. return insert_default(ex, default_seen_above);
  1819. } else if (ex instanceof AST_Hole) {
  1820. return ex;
  1821. } else if (ex instanceof AST_Destructuring) {
  1822. ex.names = ex.names.map(to_fun_args);
  1823. return insert_default(ex, default_seen_above);
  1824. } else if (ex instanceof AST_SymbolRef) {
  1825. return insert_default(new AST_SymbolFunarg({
  1826. name: ex.name,
  1827. start: ex.start,
  1828. end: ex.end
  1829. }), default_seen_above);
  1830. } else if (ex instanceof AST_Expansion) {
  1831. ex.expression = to_fun_args(ex.expression);
  1832. return insert_default(ex, default_seen_above);
  1833. } else if (ex instanceof AST_Array) {
  1834. return insert_default(new AST_Destructuring({
  1835. start: ex.start,
  1836. end: ex.end,
  1837. is_array: true,
  1838. names: ex.elements.map(to_fun_args)
  1839. }), default_seen_above);
  1840. } else if (ex instanceof AST_Assign) {
  1841. return insert_default(to_fun_args(ex.left, undefined, undefined, ex.right), default_seen_above);
  1842. } else if (ex instanceof AST_DefaultAssign) {
  1843. ex.left = to_fun_args(ex.left, 0, [ex.left]);
  1844. return ex;
  1845. } else {
  1846. croak("Invalid function parameter", ex.start.line, ex.start.col);
  1847. }
  1848. }
  1849. var expr_atom = function(allow_calls, allow_arrows) {
  1850. if (is("operator", "new")) {
  1851. return new_(allow_calls);
  1852. }
  1853. var start = S.token;
  1854. var async = is("name", "async") && as_atom_node();
  1855. if (is("punc")) {
  1856. switch (S.token.value) {
  1857. case "(":
  1858. if (async && !allow_calls) break;
  1859. var exprs = params_or_seq_(allow_arrows, !async);
  1860. if (allow_arrows && is("arrow", "=>")) {
  1861. return arrow_function(start, exprs.map(to_fun_args), !!async);
  1862. }
  1863. var ex = async ? new AST_Call({
  1864. expression: async,
  1865. args: exprs
  1866. }) : exprs.length == 1 ? exprs[0] : new AST_Sequence({
  1867. expressions: exprs
  1868. });
  1869. ex.start = start;
  1870. ex.end = S.token;
  1871. return subscripts(ex, allow_calls);
  1872. case "[":
  1873. return subscripts(array_(), allow_calls);
  1874. case "{":
  1875. return subscripts(object_or_destructuring_(), allow_calls);
  1876. }
  1877. if (!async) unexpected();
  1878. }
  1879. if (allow_arrows && is("name") && is_token(peek(), "arrow")) {
  1880. var param = new AST_SymbolFunarg({
  1881. name: S.token.value,
  1882. start: start,
  1883. end: start,
  1884. });
  1885. next();
  1886. return arrow_function(start, [param], !!async);
  1887. }
  1888. if (is("keyword", "function")) {
  1889. next();
  1890. var func = function_(AST_Function, false, !!async);
  1891. func.start = start;
  1892. func.end = prev();
  1893. return subscripts(func, allow_calls);
  1894. }
  1895. if (async) return subscripts(async, allow_calls);
  1896. if (is("keyword", "class")) {
  1897. next();
  1898. var cls = class_(AST_ClassExpression);
  1899. cls.start = start;
  1900. cls.end = prev();
  1901. return subscripts(cls, allow_calls);
  1902. }
  1903. if (is("template_head")) {
  1904. return subscripts(template_string(), allow_calls);
  1905. }
  1906. if (ATOMIC_START_TOKEN(S.token.type)) {
  1907. return subscripts(as_atom_node(), allow_calls);
  1908. }
  1909. unexpected();
  1910. };
  1911. function template_string() {
  1912. var segments = [], start = S.token;
  1913. segments.push(new AST_TemplateSegment({
  1914. start: S.token,
  1915. raw: S.token.raw,
  1916. value: S.token.value,
  1917. end: S.token
  1918. }));
  1919. while (S.token.end === false) {
  1920. next();
  1921. handle_regexp();
  1922. segments.push(expression(true));
  1923. if (!is_token("template_substitution")) {
  1924. unexpected();
  1925. }
  1926. segments.push(new AST_TemplateSegment({
  1927. start: S.token,
  1928. raw: S.token.raw,
  1929. value: S.token.value,
  1930. end: S.token
  1931. }));
  1932. }
  1933. next();
  1934. return new AST_TemplateString({
  1935. start: start,
  1936. segments: segments,
  1937. end: S.token
  1938. });
  1939. }
  1940. function expr_list(closing, allow_trailing_comma, allow_empty) {
  1941. var first = true, a = [];
  1942. while (!is("punc", closing)) {
  1943. if (first) first = false; else expect(",");
  1944. if (allow_trailing_comma && is("punc", closing)) break;
  1945. if (is("punc", ",") && allow_empty) {
  1946. a.push(new AST_Hole({ start: S.token, end: S.token }));
  1947. } else if (is("expand", "...")) {
  1948. next();
  1949. a.push(new AST_Expansion({start: prev(), expression: expression(),end: S.token}));
  1950. } else {
  1951. a.push(expression(false));
  1952. }
  1953. }
  1954. next();
  1955. return a;
  1956. };
  1957. var array_ = embed_tokens(function() {
  1958. expect("[");
  1959. return new AST_Array({
  1960. elements: expr_list("]", !options.strict, true)
  1961. });
  1962. });
  1963. var create_accessor = embed_tokens(function(is_generator, is_async) {
  1964. return function_(AST_Accessor, is_generator, is_async);
  1965. });
  1966. var object_or_destructuring_ = embed_tokens(function object_or_destructuring_() {
  1967. var start = S.token, first = true, a = [];
  1968. expect("{");
  1969. while (!is("punc", "}")) {
  1970. if (first) first = false; else expect(",");
  1971. if (!options.strict && is("punc", "}"))
  1972. // allow trailing comma
  1973. break;
  1974. start = S.token;
  1975. if (start.type == "expand") {
  1976. next();
  1977. a.push(new AST_Expansion({
  1978. start: start,
  1979. expression: expression(false),
  1980. end: prev(),
  1981. }));
  1982. continue;
  1983. }
  1984. var name = as_property_name();
  1985. var value;
  1986. // Check property and fetch value
  1987. if (!is("punc", ":")) {
  1988. var concise = concise_method_or_getset(name, start);
  1989. if (concise) {
  1990. a.push(concise);
  1991. continue;
  1992. }
  1993. value = new AST_SymbolRef({
  1994. start: prev(),
  1995. name: name,
  1996. end: prev()
  1997. });
  1998. } else if (name === null) {
  1999. unexpected(prev());
  2000. } else {
  2001. next(); // `:` - see first condition
  2002. value = expression(false);
  2003. }
  2004. // Check for default value and alter value accordingly if necessary
  2005. if (is("operator", "=")) {
  2006. next();
  2007. value = new AST_Assign({
  2008. start: start,
  2009. left: value,
  2010. operator: "=",
  2011. right: expression(false),
  2012. end: prev()
  2013. });
  2014. }
  2015. // Create property
  2016. a.push(new AST_ObjectKeyVal({
  2017. start: start,
  2018. quote: start.quote,
  2019. key: name,
  2020. value: value,
  2021. end: prev()
  2022. }));
  2023. }
  2024. next();
  2025. return new AST_Object({ properties: a })
  2026. });
  2027. function class_(KindOfClass) {
  2028. var start, method, class_name, extends_, a = [];
  2029. S.input.push_directives_stack(); // Push directive stack, but not scope stack
  2030. S.input.add_directive("use strict");
  2031. if (S.token.type == "name" && S.token.value != "extends") {
  2032. class_name = as_symbol(KindOfClass === AST_DefClass ? AST_SymbolDefClass : AST_SymbolClass);
  2033. }
  2034. if (KindOfClass === AST_DefClass && !class_name) {
  2035. unexpected();
  2036. }
  2037. if (S.token.value == "extends") {
  2038. next();
  2039. extends_ = expression(true);
  2040. }
  2041. expect("{");
  2042. if (is("punc", ";")) { next(); } // Leading semicolons are okay in class bodies.
  2043. while (!is("punc", "}")) {
  2044. start = S.token;
  2045. method = concise_method_or_getset(as_property_name(), start, true);
  2046. if (!method) { unexpected(); }
  2047. a.push(method);
  2048. if (is("punc", ";")) { next(); }
  2049. }
  2050. S.input.pop_directives_stack();
  2051. next();
  2052. return new KindOfClass({
  2053. start: start,
  2054. name: class_name,
  2055. extends: extends_,
  2056. properties: a,
  2057. end: prev(),
  2058. });
  2059. }
  2060. function concise_method_or_getset(name, start, is_class) {
  2061. var get_ast = function(name, token) {
  2062. if (typeof name === "string" || typeof name === "number") {
  2063. return new AST_SymbolMethod({
  2064. start: token,
  2065. name: name,
  2066. end: prev()
  2067. });
  2068. } else if (name === null) {
  2069. unexpected();
  2070. }
  2071. return name;
  2072. }
  2073. var is_async = false;
  2074. var is_static = false;
  2075. var is_generator = false;
  2076. var property_token = start;
  2077. if (is_class && name === "static" && !is("punc", "(")) {
  2078. is_static = true;
  2079. property_token = S.token;
  2080. name = as_property_name();
  2081. }
  2082. if (name === "async" && !is("punc", "(") && !is("punc", ",") && !is("punc", "}")) {
  2083. is_async = true;
  2084. property_token = S.token;
  2085. name = as_property_name();
  2086. }
  2087. if (name === null) {
  2088. is_generator = true;
  2089. property_token = S.token;
  2090. name = as_property_name();
  2091. if (name === null) {
  2092. unexpected();
  2093. }
  2094. }
  2095. if (is("punc", "(")) {
  2096. name = get_ast(name, start);
  2097. var node = new AST_ConciseMethod({
  2098. start : start,
  2099. static : is_static,
  2100. is_generator: is_generator,
  2101. async : is_async,
  2102. key : name,
  2103. quote : name instanceof AST_SymbolMethod ?
  2104. property_token.quote : undefined,
  2105. value : create_accessor(is_generator, is_async),
  2106. end : prev()
  2107. });
  2108. return node;
  2109. }
  2110. property_token = S.token;
  2111. if (name == "get") {
  2112. if (!is("punc") || is("punc", "[")) {
  2113. name = get_ast(as_property_name(), start);
  2114. return new AST_ObjectGetter({
  2115. start : start,
  2116. static: is_static,
  2117. key : name,
  2118. quote : name instanceof AST_SymbolMethod ?
  2119. property_token.quote : undefined,
  2120. value : create_accessor(),
  2121. end : prev()
  2122. });
  2123. }
  2124. }
  2125. else if (name == "set") {
  2126. if (!is("punc") || is("punc", "[")) {
  2127. name = get_ast(as_property_name(), start);
  2128. return new AST_ObjectSetter({
  2129. start : start,
  2130. static: is_static,
  2131. key : name,
  2132. quote : name instanceof AST_SymbolMethod ?
  2133. property_token.quote : undefined,
  2134. value : create_accessor(),
  2135. end : prev()
  2136. });
  2137. }
  2138. }
  2139. }
  2140. function import_() {
  2141. var start = prev();
  2142. var imported_name;
  2143. var imported_names;
  2144. if (is("name")) {
  2145. imported_name = as_symbol(AST_SymbolImport);
  2146. }
  2147. if (is("punc", ",")) {
  2148. next();
  2149. }
  2150. imported_names = map_names(true);
  2151. if (imported_names || imported_name) {
  2152. expect_token("name", "from");
  2153. }
  2154. var mod_str = S.token;
  2155. if (mod_str.type !== 'string') {
  2156. unexpected();
  2157. }
  2158. next();
  2159. return new AST_Import({
  2160. start: start,
  2161. imported_name: imported_name,
  2162. imported_names: imported_names,
  2163. module_name: new AST_String({
  2164. start: mod_str,
  2165. value: mod_str.value,
  2166. quote: mod_str.quote,
  2167. end: mod_str,
  2168. }),
  2169. end: S.token,
  2170. });
  2171. }
  2172. function map_name(is_import) {
  2173. function make_symbol(type) {
  2174. return new type({
  2175. name: as_property_name(),
  2176. start: prev(),
  2177. end: prev()
  2178. });
  2179. }
  2180. var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
  2181. var type = is_import ? AST_SymbolImport : AST_SymbolExport;
  2182. var start = S.token;
  2183. var foreign_name;
  2184. var name;
  2185. if (is_import) {
  2186. foreign_name = make_symbol(foreign_type);
  2187. } else {
  2188. name = make_symbol(type);
  2189. }
  2190. if (is("name", "as")) {
  2191. next(); // The "as" word
  2192. if (is_import) {
  2193. name = make_symbol(type);
  2194. } else {
  2195. foreign_name = make_symbol(foreign_type);
  2196. }
  2197. } else if (is_import) {
  2198. name = new type(foreign_name);
  2199. } else {
  2200. foreign_name = new foreign_type(name);
  2201. }
  2202. return new AST_NameMapping({
  2203. start: start,
  2204. foreign_name: foreign_name,
  2205. name: name,
  2206. end: prev(),
  2207. })
  2208. }
  2209. function map_nameAsterisk(is_import, name) {
  2210. var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
  2211. var type = is_import ? AST_SymbolImport : AST_SymbolExport;
  2212. var start = S.token;
  2213. var foreign_name;
  2214. var end = prev();
  2215. name = name || new type({
  2216. name: '*',
  2217. start: start,
  2218. end: end,
  2219. });
  2220. foreign_name = new foreign_type({
  2221. name: '*',
  2222. start: start,
  2223. end: end,
  2224. });
  2225. return new AST_NameMapping({
  2226. start: start,
  2227. foreign_name: foreign_name,
  2228. name: name,
  2229. end: end,
  2230. })
  2231. }
  2232. function map_names(is_import) {
  2233. var names;
  2234. if (is("punc", "{")) {
  2235. next();
  2236. names = [];
  2237. while (!is("punc", "}")) {
  2238. names.push(map_name(is_import));
  2239. if (is("punc", ",")) {
  2240. next();
  2241. }
  2242. }
  2243. next();
  2244. } else if (is("operator", "*")) {
  2245. var name;
  2246. next();
  2247. if (is_import && is("name", "as")) {
  2248. next(); // The "as" word
  2249. name = as_symbol(AST_SymbolImportForeign);
  2250. }
  2251. names = [map_nameAsterisk(is_import, name)];
  2252. }
  2253. return names;
  2254. }
  2255. function export_() {
  2256. var start = S.token;
  2257. var is_default;
  2258. var exported_names;
  2259. if (is("keyword", "default")) {
  2260. is_default = true;
  2261. next();
  2262. } else if (exported_names = map_names(false)) {
  2263. if (is("name", "from")) {
  2264. next();
  2265. var mod_str = S.token;
  2266. if (mod_str.type !== 'string') {
  2267. unexpected();
  2268. }
  2269. next();
  2270. return new AST_Export({
  2271. start: start,
  2272. is_default: is_default,
  2273. exported_names: exported_names,
  2274. module_name: new AST_String({
  2275. start: mod_str,
  2276. value: mod_str.value,
  2277. quote: mod_str.quote,
  2278. end: mod_str,
  2279. }),
  2280. end: prev(),
  2281. });
  2282. } else {
  2283. return new AST_Export({
  2284. start: start,
  2285. is_default: is_default,
  2286. exported_names: exported_names,
  2287. end: prev(),
  2288. });
  2289. }
  2290. }
  2291. var node;
  2292. var exported_value;
  2293. var exported_definition;
  2294. if (is("punc", "{")
  2295. || is_default
  2296. && (is("keyword", "class") || is("keyword", "function"))
  2297. && is_token(peek(), "punc")) {
  2298. exported_value = expression(false);
  2299. semicolon();
  2300. } else if ((node = statement()) instanceof AST_Definitions && is_default) {
  2301. unexpected(node.start);
  2302. } else if (node instanceof AST_Definitions || node instanceof AST_Defun || node instanceof AST_DefClass) {
  2303. exported_definition = node;
  2304. } else if (node instanceof AST_SimpleStatement) {
  2305. exported_value = node.body;
  2306. } else {
  2307. unexpected(node.start);
  2308. }
  2309. return new AST_Export({
  2310. start: start,
  2311. is_default: is_default,
  2312. exported_value: exported_value,
  2313. exported_definition: exported_definition,
  2314. end: prev(),
  2315. });
  2316. }
  2317. function as_property_name() {
  2318. var tmp = S.token;
  2319. switch (tmp.type) {
  2320. case "punc":
  2321. if (tmp.value === "[") {
  2322. next();
  2323. var ex = expression(false);
  2324. expect("]");
  2325. return ex;
  2326. } else unexpected(tmp);
  2327. case "operator":
  2328. if (tmp.value === "*") {
  2329. next();
  2330. return null;
  2331. }
  2332. if (["delete", "in", "instanceof", "new", "typeof", "void"].indexOf(tmp.value) === -1) {
  2333. unexpected(tmp);
  2334. }
  2335. case "name":
  2336. if (tmp.value == "yield" && !is_token(peek(), "punc", ":") && !is_token(peek(), "punc", "(")
  2337. && S.input.has_directive("use strict") && !is_in_generator()) {
  2338. token_error(tmp, "Unexpected yield identifier inside strict mode");
  2339. }
  2340. case "string":
  2341. case "num":
  2342. case "keyword":
  2343. case "atom":
  2344. next();
  2345. return tmp.value;
  2346. default:
  2347. unexpected(tmp);
  2348. }
  2349. };
  2350. function as_name() {
  2351. var tmp = S.token;
  2352. if (tmp.type != "name") unexpected();
  2353. next();
  2354. return tmp.value;
  2355. };
  2356. function _make_symbol(type) {
  2357. var name = S.token.value;
  2358. return new (name == "this" ? AST_This :
  2359. name == "super" ? AST_Super :
  2360. type)({
  2361. name : String(name),
  2362. start : S.token,
  2363. end : S.token
  2364. });
  2365. };
  2366. function strict_verify_symbol(sym) {
  2367. if (sym.name == "arguments" || sym.name == "eval")
  2368. croak("Unexpected " + sym.name + " in strict mode", sym.start.line, sym.start.col, sym.start.pos);
  2369. }
  2370. function as_symbol(type, noerror) {
  2371. if (!is("name")) {
  2372. if (!noerror) croak("Name expected");
  2373. return null;
  2374. }
  2375. if (is("name", "yield") && S.input.has_directive("use strict")) {
  2376. token_error(S.prev, "Unexpected yield identifier inside strict mode");
  2377. }
  2378. var sym = _make_symbol(type);
  2379. if (S.input.has_directive("use strict") && sym instanceof AST_SymbolDeclaration) {
  2380. strict_verify_symbol(sym);
  2381. }
  2382. next();
  2383. return sym;
  2384. };
  2385. var subscripts = function(expr, allow_calls) {
  2386. var start = expr.start;
  2387. if (is("punc", ".")) {
  2388. next();
  2389. return subscripts(new AST_Dot({
  2390. start : start,
  2391. expression : expr,
  2392. property : as_name(),
  2393. end : prev()
  2394. }), allow_calls);
  2395. }
  2396. if (is("punc", "[")) {
  2397. next();
  2398. var prop = expression(true);
  2399. expect("]");
  2400. return subscripts(new AST_Sub({
  2401. start : start,
  2402. expression : expr,
  2403. property : prop,
  2404. end : prev()
  2405. }), allow_calls);
  2406. }
  2407. if (allow_calls && is("punc", "(")) {
  2408. next();
  2409. return subscripts(new AST_Call({
  2410. start : start,
  2411. expression : expr,
  2412. args : call_args(),
  2413. end : prev()
  2414. }), true);
  2415. }
  2416. if (is("template_head")) {
  2417. return subscripts(new AST_PrefixedTemplateString({
  2418. start: start,
  2419. prefix: expr,
  2420. template_string: template_string()
  2421. }), allow_calls);
  2422. }
  2423. return expr;
  2424. };
  2425. var call_args = embed_tokens(function _call_args() {
  2426. var args = [];
  2427. while (!is("punc", ")")) {
  2428. if (is("expand", "...")) {
  2429. next();
  2430. args.push(new AST_Expansion({
  2431. start: prev(),
  2432. expression: expression(false)
  2433. }));
  2434. } else {
  2435. args.push(expression(false));
  2436. }
  2437. if (!is("punc", ")")) {
  2438. expect(",");
  2439. if (is("punc", ")") && options.ecma < 8) unexpected();
  2440. }
  2441. }
  2442. next();
  2443. return args;
  2444. });
  2445. var maybe_unary = function(allow_calls, allow_arrows) {
  2446. var start = S.token;
  2447. if (start.type == "name" && start.value == "await") {
  2448. if (is_in_async()) {
  2449. next();
  2450. return _await_expression();
  2451. } else if (S.input.has_directive("use strict")) {
  2452. token_error(S.token, "Unexpected await identifier inside strict mode")
  2453. }
  2454. }
  2455. if (is("operator") && UNARY_PREFIX(start.value)) {
  2456. next();
  2457. handle_regexp();
  2458. var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls));
  2459. ex.start = start;
  2460. ex.end = prev();
  2461. return ex;
  2462. }
  2463. var val = expr_atom(allow_calls, allow_arrows);
  2464. while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) {
  2465. if (val instanceof AST_Arrow) unexpected();
  2466. val = make_unary(AST_UnaryPostfix, S.token, val);
  2467. val.start = start;
  2468. val.end = S.token;
  2469. next();
  2470. }
  2471. return val;
  2472. };
  2473. function make_unary(ctor, token, expr) {
  2474. var op = token.value;
  2475. switch (op) {
  2476. case "++":
  2477. case "--":
  2478. if (!is_assignable(expr))
  2479. croak("Invalid use of " + op + " operator", token.line, token.col, token.pos);
  2480. break;
  2481. case "delete":
  2482. if (expr instanceof AST_SymbolRef && S.input.has_directive("use strict"))
  2483. croak("Calling delete on expression not allowed in strict mode", expr.start.line, expr.start.col, expr.start.pos);
  2484. break;
  2485. }
  2486. return new ctor({ operator: op, expression: expr });
  2487. };
  2488. var expr_op = function(left, min_prec, no_in) {
  2489. var op = is("operator") ? S.token.value : null;
  2490. if (op == "in" && no_in) op = null;
  2491. if (op == "**" && left instanceof AST_UnaryPrefix
  2492. && left.end === S.prev /* unary token in front not allowed, but allowed if prev is for example `)` */
  2493. && left.operator !== "--" && left.operator !== "++")
  2494. unexpected(left.start);
  2495. var prec = op != null ? PRECEDENCE[op] : null;
  2496. if (prec != null && (prec > min_prec || (op === "**" && min_prec === prec))) {
  2497. next();
  2498. var right = expr_op(maybe_unary(true), prec, no_in);
  2499. return expr_op(new AST_Binary({
  2500. start : left.start,
  2501. left : left,
  2502. operator : op,
  2503. right : right,
  2504. end : right.end
  2505. }), min_prec, no_in);
  2506. }
  2507. return left;
  2508. };
  2509. function expr_ops(no_in) {
  2510. return expr_op(maybe_unary(true, true), 0, no_in);
  2511. };
  2512. var maybe_conditional = function(no_in) {
  2513. var start = S.token;
  2514. var expr = expr_ops(no_in);
  2515. if (is("operator", "?")) {
  2516. next();
  2517. var yes = expression(false);
  2518. expect(":");
  2519. return new AST_Conditional({
  2520. start : start,
  2521. condition : expr,
  2522. consequent : yes,
  2523. alternative : expression(false, no_in),
  2524. end : prev()
  2525. });
  2526. }
  2527. return expr;
  2528. };
  2529. function is_assignable(expr) {
  2530. return expr instanceof AST_PropAccess || expr instanceof AST_SymbolRef;
  2531. };
  2532. function to_destructuring(node) {
  2533. if (node instanceof AST_Object) {
  2534. node = new AST_Destructuring({
  2535. start: node.start,
  2536. names: node.properties.map(to_destructuring),
  2537. is_array: false,
  2538. end: node.end
  2539. });
  2540. } else if (node instanceof AST_Array) {
  2541. var names = [];
  2542. for (var i = 0; i < node.elements.length; i++) {
  2543. // Only allow expansion as last element
  2544. if (node.elements[i] instanceof AST_Expansion) {
  2545. if (i + 1 !== node.elements.length) {
  2546. token_error(node.elements[i].start, "Spread must the be last element in destructuring array");
  2547. }
  2548. node.elements[i].expression = to_destructuring(node.elements[i].expression);
  2549. }
  2550. names.push(to_destructuring(node.elements[i]));
  2551. }
  2552. node = new AST_Destructuring({
  2553. start: node.start,
  2554. names: names,
  2555. is_array: true,
  2556. end: node.end
  2557. });
  2558. } else if (node instanceof AST_ObjectProperty) {
  2559. node.value = to_destructuring(node.value);
  2560. } else if (node instanceof AST_Assign) {
  2561. node = new AST_DefaultAssign({
  2562. start: node.start,
  2563. left: node.left,
  2564. operator: "=",
  2565. right: node.right,
  2566. end: node.end
  2567. });
  2568. }
  2569. return node;
  2570. }
  2571. // In ES6, AssignmentExpression can also be an ArrowFunction
  2572. var maybe_assign = function(no_in) {
  2573. var start = S.token;
  2574. if (start.type == "name" && start.value == "yield") {
  2575. if (is_in_generator()) {
  2576. next();
  2577. return _yield_expression();
  2578. } else if (S.input.has_directive("use strict")) {
  2579. token_error(S.token, "Unexpected yield identifier inside strict mode")
  2580. }
  2581. }
  2582. var left = maybe_conditional(no_in);
  2583. var val = S.token.value;
  2584. if (is("operator") && ASSIGNMENT(val)) {
  2585. if (is_assignable(left) || (left = to_destructuring(left)) instanceof AST_Destructuring) {
  2586. next();
  2587. return new AST_Assign({
  2588. start : start,
  2589. left : left,
  2590. operator : val,
  2591. right : maybe_assign(no_in),
  2592. end : prev()
  2593. });
  2594. }
  2595. croak("Invalid assignment");
  2596. }
  2597. return left;
  2598. };
  2599. var expression = function(commas, no_in) {
  2600. var start = S.token;
  2601. var exprs = [];
  2602. while (true) {
  2603. exprs.push(maybe_assign(no_in));
  2604. if (!commas || !is("punc", ",")) break;
  2605. next();
  2606. commas = true;
  2607. }
  2608. return exprs.length == 1 ? exprs[0] : new AST_Sequence({
  2609. start : start,
  2610. expressions : exprs,
  2611. end : peek()
  2612. });
  2613. };
  2614. function in_loop(cont) {
  2615. ++S.in_loop;
  2616. var ret = cont();
  2617. --S.in_loop;
  2618. return ret;
  2619. };
  2620. if (options.expression) {
  2621. return expression(true);
  2622. }
  2623. return (function(){
  2624. var start = S.token;
  2625. var body = [];
  2626. S.input.push_directives_stack();
  2627. while (!is("eof"))
  2628. body.push(statement());
  2629. S.input.pop_directives_stack();
  2630. var end = prev();
  2631. var toplevel = options.toplevel;
  2632. if (toplevel) {
  2633. toplevel.body = toplevel.body.concat(body);
  2634. toplevel.end = end;
  2635. } else {
  2636. toplevel = new AST_Toplevel({ start: start, body: body, end: end });
  2637. }
  2638. return toplevel;
  2639. })();
  2640. };