esprima.js 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960
  1. /*
  2. Copyright (C) 2013 Ariya Hidayat <ariya.hidayat@gmail.com>
  3. Copyright (C) 2013 Thaddee Tyl <thaddee.tyl@gmail.com>
  4. Copyright (C) 2013 Mathias Bynens <mathias@qiwi.be>
  5. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  6. Copyright (C) 2012 Mathias Bynens <mathias@qiwi.be>
  7. Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
  8. Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
  9. Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
  10. Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
  11. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are met:
  14. * Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. * Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*jslint bitwise:true plusplus:true */
  31. /*global esprima:true, define:true, exports:true, window: true,
  32. throwErrorTolerant: true,
  33. throwError: true, generateStatement: true, peek: true,
  34. parseAssignmentExpression: true, parseBlock: true, parseExpression: true,
  35. parseFunctionDeclaration: true, parseFunctionExpression: true,
  36. parseFunctionSourceElements: true, parseVariableIdentifier: true,
  37. parseLeftHandSideExpression: true, parseParams: true, validateParam: true,
  38. parseUnaryExpression: true,
  39. parseStatement: true, parseSourceElement: true */
  40. (function (root, factory) {
  41. 'use strict';
  42. // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
  43. // Rhino, and plain browser loading.
  44. /* istanbul ignore next */
  45. if (typeof define === 'function' && define.amd) {
  46. define(['exports'], factory);
  47. } else if (typeof exports !== 'undefined') {
  48. factory(exports);
  49. } else {
  50. factory((root.esprima = {}));
  51. }
  52. }(this, function (exports) {
  53. 'use strict';
  54. var Token,
  55. TokenName,
  56. FnExprTokens,
  57. Syntax,
  58. PlaceHolders,
  59. PropertyKind,
  60. Messages,
  61. Regex,
  62. source,
  63. strict,
  64. index,
  65. lineNumber,
  66. lineStart,
  67. length,
  68. lookahead,
  69. state,
  70. extra;
  71. Token = {
  72. BooleanLiteral: 1,
  73. EOF: 2,
  74. Identifier: 3,
  75. Keyword: 4,
  76. NullLiteral: 5,
  77. NumericLiteral: 6,
  78. Punctuator: 7,
  79. StringLiteral: 8,
  80. RegularExpression: 9
  81. };
  82. TokenName = {};
  83. TokenName[Token.BooleanLiteral] = 'Boolean';
  84. TokenName[Token.EOF] = '<end>';
  85. TokenName[Token.Identifier] = 'Identifier';
  86. TokenName[Token.Keyword] = 'Keyword';
  87. TokenName[Token.NullLiteral] = 'Null';
  88. TokenName[Token.NumericLiteral] = 'Numeric';
  89. TokenName[Token.Punctuator] = 'Punctuator';
  90. TokenName[Token.StringLiteral] = 'String';
  91. TokenName[Token.RegularExpression] = 'RegularExpression';
  92. // A function following one of those tokens is an expression.
  93. FnExprTokens = ['(', '{', '[', 'in', 'typeof', 'instanceof', 'new',
  94. 'return', 'case', 'delete', 'throw', 'void',
  95. // assignment operators
  96. '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=',
  97. '&=', '|=', '^=', ',',
  98. // binary/unary operators
  99. '+', '-', '*', '/', '%', '++', '--', '<<', '>>', '>>>', '&',
  100. '|', '^', '!', '~', '&&', '||', '?', ':', '===', '==', '>=',
  101. '<=', '<', '>', '!=', '!=='];
  102. Syntax = {
  103. AssignmentExpression: 'AssignmentExpression',
  104. ArrayExpression: 'ArrayExpression',
  105. ArrowFunctionExpression: 'ArrowFunctionExpression',
  106. BlockStatement: 'BlockStatement',
  107. BinaryExpression: 'BinaryExpression',
  108. BreakStatement: 'BreakStatement',
  109. CallExpression: 'CallExpression',
  110. CatchClause: 'CatchClause',
  111. ConditionalExpression: 'ConditionalExpression',
  112. ContinueStatement: 'ContinueStatement',
  113. DoWhileStatement: 'DoWhileStatement',
  114. DebuggerStatement: 'DebuggerStatement',
  115. EmptyStatement: 'EmptyStatement',
  116. ExpressionStatement: 'ExpressionStatement',
  117. ForStatement: 'ForStatement',
  118. ForInStatement: 'ForInStatement',
  119. FunctionDeclaration: 'FunctionDeclaration',
  120. FunctionExpression: 'FunctionExpression',
  121. Identifier: 'Identifier',
  122. IfStatement: 'IfStatement',
  123. Literal: 'Literal',
  124. LabeledStatement: 'LabeledStatement',
  125. LogicalExpression: 'LogicalExpression',
  126. MemberExpression: 'MemberExpression',
  127. NewExpression: 'NewExpression',
  128. ObjectExpression: 'ObjectExpression',
  129. Program: 'Program',
  130. Property: 'Property',
  131. ReturnStatement: 'ReturnStatement',
  132. SequenceExpression: 'SequenceExpression',
  133. SwitchStatement: 'SwitchStatement',
  134. SwitchCase: 'SwitchCase',
  135. ThisExpression: 'ThisExpression',
  136. ThrowStatement: 'ThrowStatement',
  137. TryStatement: 'TryStatement',
  138. UnaryExpression: 'UnaryExpression',
  139. UpdateExpression: 'UpdateExpression',
  140. VariableDeclaration: 'VariableDeclaration',
  141. VariableDeclarator: 'VariableDeclarator',
  142. WhileStatement: 'WhileStatement',
  143. WithStatement: 'WithStatement'
  144. };
  145. PlaceHolders = {
  146. ArrowParameterPlaceHolder: {
  147. type: 'ArrowParameterPlaceHolder'
  148. }
  149. };
  150. PropertyKind = {
  151. Data: 1,
  152. Get: 2,
  153. Set: 4
  154. };
  155. // Error messages should be identical to V8.
  156. Messages = {
  157. UnexpectedToken: 'Unexpected token %0',
  158. UnexpectedNumber: 'Unexpected number',
  159. UnexpectedString: 'Unexpected string',
  160. UnexpectedIdentifier: 'Unexpected identifier',
  161. UnexpectedReserved: 'Unexpected reserved word',
  162. UnexpectedEOS: 'Unexpected end of input',
  163. NewlineAfterThrow: 'Illegal newline after throw',
  164. InvalidRegExp: 'Invalid regular expression',
  165. UnterminatedRegExp: 'Invalid regular expression: missing /',
  166. InvalidLHSInAssignment: 'Invalid left-hand side in assignment',
  167. InvalidLHSInForIn: 'Invalid left-hand side in for-in',
  168. MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
  169. NoCatchOrFinally: 'Missing catch or finally after try',
  170. UnknownLabel: 'Undefined label \'%0\'',
  171. Redeclaration: '%0 \'%1\' has already been declared',
  172. IllegalContinue: 'Illegal continue statement',
  173. IllegalBreak: 'Illegal break statement',
  174. IllegalReturn: 'Illegal return statement',
  175. StrictModeWith: 'Strict mode code may not include a with statement',
  176. StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
  177. StrictVarName: 'Variable name may not be eval or arguments in strict mode',
  178. StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
  179. StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
  180. StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
  181. StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
  182. StrictDelete: 'Delete of an unqualified identifier in strict mode.',
  183. StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',
  184. AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',
  185. AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',
  186. StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
  187. StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
  188. StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
  189. StrictReservedWord: 'Use of future reserved word in strict mode'
  190. };
  191. // See also tools/generate-unicode-regex.py.
  192. Regex = {
  193. NonAsciiIdentifierStart: new RegExp('[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0\u08A2-\u08AC\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F0\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA697\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]'),
  194. NonAsciiIdentifierPart: new RegExp('[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0\u08A2-\u08AC\u08E4-\u08FE\u0900-\u0963\u0966-\u096F\u0971-\u0977\u0979-\u097F\u0981-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C01-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58\u0C59\u0C60-\u0C63\u0C66-\u0C6F\u0C82\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D02\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D60-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F0\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191C\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19D9\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1D00-\u1DE6\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u2E2F\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099\u309A\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA697\uA69F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A\uAA7B\uAA80-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE26\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]')
  195. };
  196. // Ensure the condition is true, otherwise throw an error.
  197. // This is only to have a better contract semantic, i.e. another safety net
  198. // to catch a logic error. The condition shall be fulfilled in normal case.
  199. // Do NOT use this to enforce a certain condition on any user input.
  200. function assert(condition, message) {
  201. /* istanbul ignore if */
  202. if (!condition) {
  203. throw new Error('ASSERT: ' + message);
  204. }
  205. }
  206. function isDecimalDigit(ch) {
  207. return (ch >= 48 && ch <= 57); // 0..9
  208. }
  209. function isHexDigit(ch) {
  210. return '0123456789abcdefABCDEF'.indexOf(ch) >= 0;
  211. }
  212. function isOctalDigit(ch) {
  213. return '01234567'.indexOf(ch) >= 0;
  214. }
  215. // 7.2 White Space
  216. function isWhiteSpace(ch) {
  217. return (ch === 0x20) || (ch === 0x09) || (ch === 0x0B) || (ch === 0x0C) || (ch === 0xA0) ||
  218. (ch >= 0x1680 && [0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x202F, 0x205F, 0x3000, 0xFEFF].indexOf(ch) >= 0);
  219. }
  220. // 7.3 Line Terminators
  221. function isLineTerminator(ch) {
  222. return (ch === 0x0A) || (ch === 0x0D) || (ch === 0x2028) || (ch === 0x2029);
  223. }
  224. // 7.6 Identifier Names and Identifiers
  225. function isIdentifierStart(ch) {
  226. return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore)
  227. (ch >= 0x41 && ch <= 0x5A) || // A..Z
  228. (ch >= 0x61 && ch <= 0x7A) || // a..z
  229. (ch === 0x5C) || // \ (backslash)
  230. ((ch >= 0x80) && Regex.NonAsciiIdentifierStart.test(String.fromCharCode(ch)));
  231. }
  232. function isIdentifierPart(ch) {
  233. return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore)
  234. (ch >= 0x41 && ch <= 0x5A) || // A..Z
  235. (ch >= 0x61 && ch <= 0x7A) || // a..z
  236. (ch >= 0x30 && ch <= 0x39) || // 0..9
  237. (ch === 0x5C) || // \ (backslash)
  238. ((ch >= 0x80) && Regex.NonAsciiIdentifierPart.test(String.fromCharCode(ch)));
  239. }
  240. // 7.6.1.2 Future Reserved Words
  241. function isFutureReservedWord(id) {
  242. switch (id) {
  243. case 'class':
  244. case 'enum':
  245. case 'export':
  246. case 'extends':
  247. case 'import':
  248. case 'super':
  249. return true;
  250. default:
  251. return false;
  252. }
  253. }
  254. function isStrictModeReservedWord(id) {
  255. switch (id) {
  256. case 'implements':
  257. case 'interface':
  258. case 'package':
  259. case 'private':
  260. case 'protected':
  261. case 'public':
  262. case 'static':
  263. case 'yield':
  264. case 'let':
  265. return true;
  266. default:
  267. return false;
  268. }
  269. }
  270. function isRestrictedWord(id) {
  271. return id === 'eval' || id === 'arguments';
  272. }
  273. // 7.6.1.1 Keywords
  274. function isKeyword(id) {
  275. if (strict && isStrictModeReservedWord(id)) {
  276. return true;
  277. }
  278. // 'const' is specialized as Keyword in V8.
  279. // 'yield' and 'let' are for compatiblity with SpiderMonkey and ES.next.
  280. // Some others are from future reserved words.
  281. switch (id.length) {
  282. case 2:
  283. return (id === 'if') || (id === 'in') || (id === 'do');
  284. case 3:
  285. return (id === 'var') || (id === 'for') || (id === 'new') ||
  286. (id === 'try') || (id === 'let');
  287. case 4:
  288. return (id === 'this') || (id === 'else') || (id === 'case') ||
  289. (id === 'void') || (id === 'with') || (id === 'enum');
  290. case 5:
  291. return (id === 'while') || (id === 'break') || (id === 'catch') ||
  292. (id === 'throw') || (id === 'const') || (id === 'yield') ||
  293. (id === 'class') || (id === 'super');
  294. case 6:
  295. return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
  296. (id === 'switch') || (id === 'export') || (id === 'import');
  297. case 7:
  298. return (id === 'default') || (id === 'finally') || (id === 'extends');
  299. case 8:
  300. return (id === 'function') || (id === 'continue') || (id === 'debugger');
  301. case 10:
  302. return (id === 'instanceof');
  303. default:
  304. return false;
  305. }
  306. }
  307. // 7.4 Comments
  308. function addComment(type, value, start, end, loc) {
  309. var comment;
  310. assert(typeof start === 'number', 'Comment must have valid position');
  311. // Because the way the actual token is scanned, often the comments
  312. // (if any) are skipped twice during the lexical analysis.
  313. // Thus, we need to skip adding a comment if the comment array already
  314. // handled it.
  315. if (state.lastCommentStart >= start) {
  316. return;
  317. }
  318. state.lastCommentStart = start;
  319. comment = {
  320. type: type,
  321. value: value
  322. };
  323. if (extra.range) {
  324. comment.range = [start, end];
  325. }
  326. if (extra.loc) {
  327. comment.loc = loc;
  328. }
  329. extra.comments.push(comment);
  330. if (extra.attachComment) {
  331. extra.leadingComments.push(comment);
  332. extra.trailingComments.push(comment);
  333. }
  334. }
  335. function skipSingleLineComment(offset) {
  336. var start, loc, ch, comment;
  337. start = index - offset;
  338. loc = {
  339. start: {
  340. line: lineNumber,
  341. column: index - lineStart - offset
  342. }
  343. };
  344. while (index < length) {
  345. ch = source.charCodeAt(index);
  346. ++index;
  347. if (isLineTerminator(ch)) {
  348. if (extra.comments) {
  349. comment = source.slice(start + offset, index - 1);
  350. loc.end = {
  351. line: lineNumber,
  352. column: index - lineStart - 1
  353. };
  354. addComment('Line', comment, start, index - 1, loc);
  355. }
  356. if (ch === 13 && source.charCodeAt(index) === 10) {
  357. ++index;
  358. }
  359. ++lineNumber;
  360. lineStart = index;
  361. return;
  362. }
  363. }
  364. if (extra.comments) {
  365. comment = source.slice(start + offset, index);
  366. loc.end = {
  367. line: lineNumber,
  368. column: index - lineStart
  369. };
  370. addComment('Line', comment, start, index, loc);
  371. }
  372. }
  373. function skipMultiLineComment() {
  374. var start, loc, ch, comment;
  375. if (extra.comments) {
  376. start = index - 2;
  377. loc = {
  378. start: {
  379. line: lineNumber,
  380. column: index - lineStart - 2
  381. }
  382. };
  383. }
  384. while (index < length) {
  385. ch = source.charCodeAt(index);
  386. if (isLineTerminator(ch)) {
  387. if (ch === 0x0D && source.charCodeAt(index + 1) === 0x0A) {
  388. ++index;
  389. }
  390. ++lineNumber;
  391. ++index;
  392. lineStart = index;
  393. if (index >= length) {
  394. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  395. }
  396. } else if (ch === 0x2A) {
  397. // Block comment ends with '*/'.
  398. if (source.charCodeAt(index + 1) === 0x2F) {
  399. ++index;
  400. ++index;
  401. if (extra.comments) {
  402. comment = source.slice(start + 2, index - 2);
  403. loc.end = {
  404. line: lineNumber,
  405. column: index - lineStart
  406. };
  407. addComment('Block', comment, start, index, loc);
  408. }
  409. return;
  410. }
  411. ++index;
  412. } else {
  413. ++index;
  414. }
  415. }
  416. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  417. }
  418. function skipComment() {
  419. var ch, start;
  420. start = (index === 0);
  421. while (index < length) {
  422. ch = source.charCodeAt(index);
  423. if (isWhiteSpace(ch)) {
  424. ++index;
  425. } else if (isLineTerminator(ch)) {
  426. ++index;
  427. if (ch === 0x0D && source.charCodeAt(index) === 0x0A) {
  428. ++index;
  429. }
  430. ++lineNumber;
  431. lineStart = index;
  432. start = true;
  433. } else if (ch === 0x2F) { // U+002F is '/'
  434. ch = source.charCodeAt(index + 1);
  435. if (ch === 0x2F) {
  436. ++index;
  437. ++index;
  438. skipSingleLineComment(2);
  439. start = true;
  440. } else if (ch === 0x2A) { // U+002A is '*'
  441. ++index;
  442. ++index;
  443. skipMultiLineComment();
  444. } else {
  445. break;
  446. }
  447. } else if (start && ch === 0x2D) { // U+002D is '-'
  448. // U+003E is '>'
  449. if ((source.charCodeAt(index + 1) === 0x2D) && (source.charCodeAt(index + 2) === 0x3E)) {
  450. // '-->' is a single-line comment
  451. index += 3;
  452. skipSingleLineComment(3);
  453. } else {
  454. break;
  455. }
  456. } else if (ch === 0x3C) { // U+003C is '<'
  457. if (source.slice(index + 1, index + 4) === '!--') {
  458. ++index; // `<`
  459. ++index; // `!`
  460. ++index; // `-`
  461. ++index; // `-`
  462. skipSingleLineComment(4);
  463. } else {
  464. break;
  465. }
  466. } else {
  467. break;
  468. }
  469. }
  470. }
  471. function scanHexEscape(prefix) {
  472. var i, len, ch, code = 0;
  473. len = (prefix === 'u') ? 4 : 2;
  474. for (i = 0; i < len; ++i) {
  475. if (index < length && isHexDigit(source[index])) {
  476. ch = source[index++];
  477. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  478. } else {
  479. return '';
  480. }
  481. }
  482. return String.fromCharCode(code);
  483. }
  484. function scanUnicodeCodePointEscape() {
  485. var ch, code, cu1, cu2;
  486. ch = source[index];
  487. code = 0;
  488. // At least, one hex digit is required.
  489. if (ch === '}') {
  490. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  491. }
  492. while (index < length) {
  493. ch = source[index++];
  494. if (!isHexDigit(ch)) {
  495. break;
  496. }
  497. code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
  498. }
  499. if (code > 0x10FFFF || ch !== '}') {
  500. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  501. }
  502. // UTF-16 Encoding
  503. if (code <= 0xFFFF) {
  504. return String.fromCharCode(code);
  505. }
  506. cu1 = ((code - 0x10000) >> 10) + 0xD800;
  507. cu2 = ((code - 0x10000) & 1023) + 0xDC00;
  508. return String.fromCharCode(cu1, cu2);
  509. }
  510. function getEscapedIdentifier() {
  511. var ch, id;
  512. ch = source.charCodeAt(index++);
  513. id = String.fromCharCode(ch);
  514. // '\u' (U+005C, U+0075) denotes an escaped character.
  515. if (ch === 0x5C) {
  516. if (source.charCodeAt(index) !== 0x75) {
  517. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  518. }
  519. ++index;
  520. ch = scanHexEscape('u');
  521. if (!ch || ch === '\\' || !isIdentifierStart(ch.charCodeAt(0))) {
  522. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  523. }
  524. id = ch;
  525. }
  526. while (index < length) {
  527. ch = source.charCodeAt(index);
  528. if (!isIdentifierPart(ch)) {
  529. break;
  530. }
  531. ++index;
  532. id += String.fromCharCode(ch);
  533. // '\u' (U+005C, U+0075) denotes an escaped character.
  534. if (ch === 0x5C) {
  535. id = id.substr(0, id.length - 1);
  536. if (source.charCodeAt(index) !== 0x75) {
  537. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  538. }
  539. ++index;
  540. ch = scanHexEscape('u');
  541. if (!ch || ch === '\\' || !isIdentifierPart(ch.charCodeAt(0))) {
  542. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  543. }
  544. id += ch;
  545. }
  546. }
  547. return id;
  548. }
  549. function getIdentifier() {
  550. var start, ch;
  551. start = index++;
  552. while (index < length) {
  553. ch = source.charCodeAt(index);
  554. if (ch === 0x5C) {
  555. // Blackslash (U+005C) marks Unicode escape sequence.
  556. index = start;
  557. return getEscapedIdentifier();
  558. }
  559. if (isIdentifierPart(ch)) {
  560. ++index;
  561. } else {
  562. break;
  563. }
  564. }
  565. return source.slice(start, index);
  566. }
  567. function scanIdentifier() {
  568. var start, id, type;
  569. start = index;
  570. // Backslash (U+005C) starts an escaped character.
  571. id = (source.charCodeAt(index) === 0x5C) ? getEscapedIdentifier() : getIdentifier();
  572. // There is no keyword or literal with only one character.
  573. // Thus, it must be an identifier.
  574. if (id.length === 1) {
  575. type = Token.Identifier;
  576. } else if (isKeyword(id)) {
  577. type = Token.Keyword;
  578. } else if (id === 'null') {
  579. type = Token.NullLiteral;
  580. } else if (id === 'true' || id === 'false') {
  581. type = Token.BooleanLiteral;
  582. } else {
  583. type = Token.Identifier;
  584. }
  585. return {
  586. type: type,
  587. value: id,
  588. lineNumber: lineNumber,
  589. lineStart: lineStart,
  590. start: start,
  591. end: index
  592. };
  593. }
  594. // 7.7 Punctuators
  595. function scanPunctuator() {
  596. var start = index,
  597. code = source.charCodeAt(index),
  598. code2,
  599. ch1 = source[index],
  600. ch2,
  601. ch3,
  602. ch4;
  603. switch (code) {
  604. // Check for most common single-character punctuators.
  605. case 0x2E: // . dot
  606. case 0x28: // ( open bracket
  607. case 0x29: // ) close bracket
  608. case 0x3B: // ; semicolon
  609. case 0x2C: // , comma
  610. case 0x7B: // { open curly brace
  611. case 0x7D: // } close curly brace
  612. case 0x5B: // [
  613. case 0x5D: // ]
  614. case 0x3A: // :
  615. case 0x3F: // ?
  616. case 0x7E: // ~
  617. ++index;
  618. if (extra.tokenize) {
  619. if (code === 0x28) {
  620. extra.openParenToken = extra.tokens.length;
  621. } else if (code === 0x7B) {
  622. extra.openCurlyToken = extra.tokens.length;
  623. }
  624. }
  625. return {
  626. type: Token.Punctuator,
  627. value: String.fromCharCode(code),
  628. lineNumber: lineNumber,
  629. lineStart: lineStart,
  630. start: start,
  631. end: index
  632. };
  633. default:
  634. code2 = source.charCodeAt(index + 1);
  635. // '=' (U+003D) marks an assignment or comparison operator.
  636. if (code2 === 0x3D) {
  637. switch (code) {
  638. case 0x2B: // +
  639. case 0x2D: // -
  640. case 0x2F: // /
  641. case 0x3C: // <
  642. case 0x3E: // >
  643. case 0x5E: // ^
  644. case 0x7C: // |
  645. case 0x25: // %
  646. case 0x26: // &
  647. case 0x2A: // *
  648. index += 2;
  649. return {
  650. type: Token.Punctuator,
  651. value: String.fromCharCode(code) + String.fromCharCode(code2),
  652. lineNumber: lineNumber,
  653. lineStart: lineStart,
  654. start: start,
  655. end: index
  656. };
  657. case 0x21: // !
  658. case 0x3D: // =
  659. index += 2;
  660. // !== and ===
  661. if (source.charCodeAt(index) === 0x3D) {
  662. ++index;
  663. }
  664. return {
  665. type: Token.Punctuator,
  666. value: source.slice(start, index),
  667. lineNumber: lineNumber,
  668. lineStart: lineStart,
  669. start: start,
  670. end: index
  671. };
  672. }
  673. }
  674. }
  675. // 4-character punctuator: >>>=
  676. ch4 = source.substr(index, 4);
  677. if (ch4 === '>>>=') {
  678. index += 4;
  679. return {
  680. type: Token.Punctuator,
  681. value: ch4,
  682. lineNumber: lineNumber,
  683. lineStart: lineStart,
  684. start: start,
  685. end: index
  686. };
  687. }
  688. // 3-character punctuators: === !== >>> <<= >>=
  689. ch3 = ch4.substr(0, 3);
  690. if (ch3 === '>>>' || ch3 === '<<=' || ch3 === '>>=') {
  691. index += 3;
  692. return {
  693. type: Token.Punctuator,
  694. value: ch3,
  695. lineNumber: lineNumber,
  696. lineStart: lineStart,
  697. start: start,
  698. end: index
  699. };
  700. }
  701. // Other 2-character punctuators: ++ -- << >> && ||
  702. ch2 = ch3.substr(0, 2);
  703. if ((ch1 === ch2[1] && ('+-<>&|'.indexOf(ch1) >= 0)) || ch2 === '=>') {
  704. index += 2;
  705. return {
  706. type: Token.Punctuator,
  707. value: ch2,
  708. lineNumber: lineNumber,
  709. lineStart: lineStart,
  710. start: start,
  711. end: index
  712. };
  713. }
  714. // 1-character punctuators: < > = ! + - * % & | ^ /
  715. if ('<>=!+-*%&|^/'.indexOf(ch1) >= 0) {
  716. ++index;
  717. return {
  718. type: Token.Punctuator,
  719. value: ch1,
  720. lineNumber: lineNumber,
  721. lineStart: lineStart,
  722. start: start,
  723. end: index
  724. };
  725. }
  726. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  727. }
  728. // 7.8.3 Numeric Literals
  729. function scanHexLiteral(start) {
  730. var number = '';
  731. while (index < length) {
  732. if (!isHexDigit(source[index])) {
  733. break;
  734. }
  735. number += source[index++];
  736. }
  737. if (number.length === 0) {
  738. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  739. }
  740. if (isIdentifierStart(source.charCodeAt(index))) {
  741. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  742. }
  743. return {
  744. type: Token.NumericLiteral,
  745. value: parseInt('0x' + number, 16),
  746. lineNumber: lineNumber,
  747. lineStart: lineStart,
  748. start: start,
  749. end: index
  750. };
  751. }
  752. function scanOctalLiteral(start) {
  753. var number = '0' + source[index++];
  754. while (index < length) {
  755. if (!isOctalDigit(source[index])) {
  756. break;
  757. }
  758. number += source[index++];
  759. }
  760. if (isIdentifierStart(source.charCodeAt(index)) || isDecimalDigit(source.charCodeAt(index))) {
  761. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  762. }
  763. return {
  764. type: Token.NumericLiteral,
  765. value: parseInt(number, 8),
  766. octal: true,
  767. lineNumber: lineNumber,
  768. lineStart: lineStart,
  769. start: start,
  770. end: index
  771. };
  772. }
  773. function scanNumericLiteral() {
  774. var number, start, ch;
  775. ch = source[index];
  776. assert(isDecimalDigit(ch.charCodeAt(0)) || (ch === '.'),
  777. 'Numeric literal must start with a decimal digit or a decimal point');
  778. start = index;
  779. number = '';
  780. if (ch !== '.') {
  781. number = source[index++];
  782. ch = source[index];
  783. // Hex number starts with '0x'.
  784. // Octal number starts with '0'.
  785. if (number === '0') {
  786. if (ch === 'x' || ch === 'X') {
  787. ++index;
  788. return scanHexLiteral(start);
  789. }
  790. if (isOctalDigit(ch)) {
  791. return scanOctalLiteral(start);
  792. }
  793. // decimal number starts with '0' such as '09' is illegal.
  794. if (ch && isDecimalDigit(ch.charCodeAt(0))) {
  795. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  796. }
  797. }
  798. while (isDecimalDigit(source.charCodeAt(index))) {
  799. number += source[index++];
  800. }
  801. ch = source[index];
  802. }
  803. if (ch === '.') {
  804. number += source[index++];
  805. while (isDecimalDigit(source.charCodeAt(index))) {
  806. number += source[index++];
  807. }
  808. ch = source[index];
  809. }
  810. if (ch === 'e' || ch === 'E') {
  811. number += source[index++];
  812. ch = source[index];
  813. if (ch === '+' || ch === '-') {
  814. number += source[index++];
  815. }
  816. if (isDecimalDigit(source.charCodeAt(index))) {
  817. while (isDecimalDigit(source.charCodeAt(index))) {
  818. number += source[index++];
  819. }
  820. } else {
  821. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  822. }
  823. }
  824. if (isIdentifierStart(source.charCodeAt(index))) {
  825. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  826. }
  827. return {
  828. type: Token.NumericLiteral,
  829. value: parseFloat(number),
  830. lineNumber: lineNumber,
  831. lineStart: lineStart,
  832. start: start,
  833. end: index
  834. };
  835. }
  836. // 7.8.4 String Literals
  837. function scanStringLiteral() {
  838. var str = '', quote, start, ch, code, unescaped, restore, octal = false, startLineNumber, startLineStart;
  839. startLineNumber = lineNumber;
  840. startLineStart = lineStart;
  841. quote = source[index];
  842. assert((quote === '\'' || quote === '"'),
  843. 'String literal must starts with a quote');
  844. start = index;
  845. ++index;
  846. while (index < length) {
  847. ch = source[index++];
  848. if (ch === quote) {
  849. quote = '';
  850. break;
  851. } else if (ch === '\\') {
  852. ch = source[index++];
  853. if (!ch || !isLineTerminator(ch.charCodeAt(0))) {
  854. switch (ch) {
  855. case 'u':
  856. case 'x':
  857. if (source[index] === '{') {
  858. ++index;
  859. str += scanUnicodeCodePointEscape();
  860. } else {
  861. restore = index;
  862. unescaped = scanHexEscape(ch);
  863. if (unescaped) {
  864. str += unescaped;
  865. } else {
  866. index = restore;
  867. str += ch;
  868. }
  869. }
  870. break;
  871. case 'n':
  872. str += '\n';
  873. break;
  874. case 'r':
  875. str += '\r';
  876. break;
  877. case 't':
  878. str += '\t';
  879. break;
  880. case 'b':
  881. str += '\b';
  882. break;
  883. case 'f':
  884. str += '\f';
  885. break;
  886. case 'v':
  887. str += '\x0B';
  888. break;
  889. default:
  890. if (isOctalDigit(ch)) {
  891. code = '01234567'.indexOf(ch);
  892. // \0 is not octal escape sequence
  893. if (code !== 0) {
  894. octal = true;
  895. }
  896. if (index < length && isOctalDigit(source[index])) {
  897. octal = true;
  898. code = code * 8 + '01234567'.indexOf(source[index++]);
  899. // 3 digits are only allowed when string starts
  900. // with 0, 1, 2, 3
  901. if ('0123'.indexOf(ch) >= 0 &&
  902. index < length &&
  903. isOctalDigit(source[index])) {
  904. code = code * 8 + '01234567'.indexOf(source[index++]);
  905. }
  906. }
  907. str += String.fromCharCode(code);
  908. } else {
  909. str += ch;
  910. }
  911. break;
  912. }
  913. } else {
  914. ++lineNumber;
  915. if (ch === '\r' && source[index] === '\n') {
  916. ++index;
  917. }
  918. lineStart = index;
  919. }
  920. } else if (isLineTerminator(ch.charCodeAt(0))) {
  921. break;
  922. } else {
  923. str += ch;
  924. }
  925. }
  926. if (quote !== '') {
  927. throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
  928. }
  929. return {
  930. type: Token.StringLiteral,
  931. value: str,
  932. octal: octal,
  933. startLineNumber: startLineNumber,
  934. startLineStart: startLineStart,
  935. lineNumber: lineNumber,
  936. lineStart: lineStart,
  937. start: start,
  938. end: index
  939. };
  940. }
  941. function testRegExp(pattern, flags) {
  942. var value;
  943. try {
  944. value = new RegExp(pattern, flags);
  945. } catch (e) {
  946. throwError({}, Messages.InvalidRegExp);
  947. }
  948. return value;
  949. }
  950. function scanRegExpBody() {
  951. var ch, str, classMarker, terminated, body;
  952. ch = source[index];
  953. assert(ch === '/', 'Regular expression literal must start with a slash');
  954. str = source[index++];
  955. classMarker = false;
  956. terminated = false;
  957. while (index < length) {
  958. ch = source[index++];
  959. str += ch;
  960. if (ch === '\\') {
  961. ch = source[index++];
  962. // ECMA-262 7.8.5
  963. if (isLineTerminator(ch.charCodeAt(0))) {
  964. throwError({}, Messages.UnterminatedRegExp);
  965. }
  966. str += ch;
  967. } else if (isLineTerminator(ch.charCodeAt(0))) {
  968. throwError({}, Messages.UnterminatedRegExp);
  969. } else if (classMarker) {
  970. if (ch === ']') {
  971. classMarker = false;
  972. }
  973. } else {
  974. if (ch === '/') {
  975. terminated = true;
  976. break;
  977. } else if (ch === '[') {
  978. classMarker = true;
  979. }
  980. }
  981. }
  982. if (!terminated) {
  983. throwError({}, Messages.UnterminatedRegExp);
  984. }
  985. // Exclude leading and trailing slash.
  986. body = str.substr(1, str.length - 2);
  987. return {
  988. value: body,
  989. literal: str
  990. };
  991. }
  992. function scanRegExpFlags() {
  993. var ch, str, flags, restore;
  994. str = '';
  995. flags = '';
  996. while (index < length) {
  997. ch = source[index];
  998. if (!isIdentifierPart(ch.charCodeAt(0))) {
  999. break;
  1000. }
  1001. ++index;
  1002. if (ch === '\\' && index < length) {
  1003. ch = source[index];
  1004. if (ch === 'u') {
  1005. ++index;
  1006. restore = index;
  1007. ch = scanHexEscape('u');
  1008. if (ch) {
  1009. flags += ch;
  1010. for (str += '\\u'; restore < index; ++restore) {
  1011. str += source[restore];
  1012. }
  1013. } else {
  1014. index = restore;
  1015. flags += 'u';
  1016. str += '\\u';
  1017. }
  1018. throwErrorTolerant({}, Messages.UnexpectedToken, 'ILLEGAL');
  1019. } else {
  1020. str += '\\';
  1021. throwErrorTolerant({}, Messages.UnexpectedToken, 'ILLEGAL');
  1022. }
  1023. } else {
  1024. flags += ch;
  1025. str += ch;
  1026. }
  1027. }
  1028. return {
  1029. value: flags,
  1030. literal: str
  1031. };
  1032. }
  1033. function scanRegExp() {
  1034. var start, body, flags, value;
  1035. lookahead = null;
  1036. skipComment();
  1037. start = index;
  1038. body = scanRegExpBody();
  1039. flags = scanRegExpFlags();
  1040. value = testRegExp(body.value, flags.value);
  1041. if (extra.tokenize) {
  1042. return {
  1043. type: Token.RegularExpression,
  1044. value: value,
  1045. lineNumber: lineNumber,
  1046. lineStart: lineStart,
  1047. start: start,
  1048. end: index
  1049. };
  1050. }
  1051. return {
  1052. literal: body.literal + flags.literal,
  1053. value: value,
  1054. start: start,
  1055. end: index
  1056. };
  1057. }
  1058. function collectRegex() {
  1059. var pos, loc, regex, token;
  1060. skipComment();
  1061. pos = index;
  1062. loc = {
  1063. start: {
  1064. line: lineNumber,
  1065. column: index - lineStart
  1066. }
  1067. };
  1068. regex = scanRegExp();
  1069. loc.end = {
  1070. line: lineNumber,
  1071. column: index - lineStart
  1072. };
  1073. /* istanbul ignore next */
  1074. if (!extra.tokenize) {
  1075. // Pop the previous token, which is likely '/' or '/='
  1076. if (extra.tokens.length > 0) {
  1077. token = extra.tokens[extra.tokens.length - 1];
  1078. if (token.range[0] === pos && token.type === 'Punctuator') {
  1079. if (token.value === '/' || token.value === '/=') {
  1080. extra.tokens.pop();
  1081. }
  1082. }
  1083. }
  1084. extra.tokens.push({
  1085. type: 'RegularExpression',
  1086. value: regex.literal,
  1087. range: [pos, index],
  1088. loc: loc
  1089. });
  1090. }
  1091. return regex;
  1092. }
  1093. function isIdentifierName(token) {
  1094. return token.type === Token.Identifier ||
  1095. token.type === Token.Keyword ||
  1096. token.type === Token.BooleanLiteral ||
  1097. token.type === Token.NullLiteral;
  1098. }
  1099. function advanceSlash() {
  1100. var prevToken,
  1101. checkToken;
  1102. // Using the following algorithm:
  1103. // https://github.com/mozilla/sweet.js/wiki/design
  1104. prevToken = extra.tokens[extra.tokens.length - 1];
  1105. if (!prevToken) {
  1106. // Nothing before that: it cannot be a division.
  1107. return collectRegex();
  1108. }
  1109. if (prevToken.type === 'Punctuator') {
  1110. if (prevToken.value === ']') {
  1111. return scanPunctuator();
  1112. }
  1113. if (prevToken.value === ')') {
  1114. checkToken = extra.tokens[extra.openParenToken - 1];
  1115. if (checkToken &&
  1116. checkToken.type === 'Keyword' &&
  1117. (checkToken.value === 'if' ||
  1118. checkToken.value === 'while' ||
  1119. checkToken.value === 'for' ||
  1120. checkToken.value === 'with')) {
  1121. return collectRegex();
  1122. }
  1123. return scanPunctuator();
  1124. }
  1125. if (prevToken.value === '}') {
  1126. // Dividing a function by anything makes little sense,
  1127. // but we have to check for that.
  1128. if (extra.tokens[extra.openCurlyToken - 3] &&
  1129. extra.tokens[extra.openCurlyToken - 3].type === 'Keyword') {
  1130. // Anonymous function.
  1131. checkToken = extra.tokens[extra.openCurlyToken - 4];
  1132. if (!checkToken) {
  1133. return scanPunctuator();
  1134. }
  1135. } else if (extra.tokens[extra.openCurlyToken - 4] &&
  1136. extra.tokens[extra.openCurlyToken - 4].type === 'Keyword') {
  1137. // Named function.
  1138. checkToken = extra.tokens[extra.openCurlyToken - 5];
  1139. if (!checkToken) {
  1140. return collectRegex();
  1141. }
  1142. } else {
  1143. return scanPunctuator();
  1144. }
  1145. // checkToken determines whether the function is
  1146. // a declaration or an expression.
  1147. if (FnExprTokens.indexOf(checkToken.value) >= 0) {
  1148. // It is an expression.
  1149. return scanPunctuator();
  1150. }
  1151. // It is a declaration.
  1152. return collectRegex();
  1153. }
  1154. return collectRegex();
  1155. }
  1156. if (prevToken.type === 'Keyword') {
  1157. return collectRegex();
  1158. }
  1159. return scanPunctuator();
  1160. }
  1161. function advance() {
  1162. var ch;
  1163. skipComment();
  1164. if (index >= length) {
  1165. return {
  1166. type: Token.EOF,
  1167. lineNumber: lineNumber,
  1168. lineStart: lineStart,
  1169. start: index,
  1170. end: index
  1171. };
  1172. }
  1173. ch = source.charCodeAt(index);
  1174. if (isIdentifierStart(ch)) {
  1175. return scanIdentifier();
  1176. }
  1177. // Very common: ( and ) and ;
  1178. if (ch === 0x28 || ch === 0x29 || ch === 0x3B) {
  1179. return scanPunctuator();
  1180. }
  1181. // String literal starts with single quote (U+0027) or double quote (U+0022).
  1182. if (ch === 0x27 || ch === 0x22) {
  1183. return scanStringLiteral();
  1184. }
  1185. // Dot (.) U+002E can also start a floating-point number, hence the need
  1186. // to check the next character.
  1187. if (ch === 0x2E) {
  1188. if (isDecimalDigit(source.charCodeAt(index + 1))) {
  1189. return scanNumericLiteral();
  1190. }
  1191. return scanPunctuator();
  1192. }
  1193. if (isDecimalDigit(ch)) {
  1194. return scanNumericLiteral();
  1195. }
  1196. // Slash (/) U+002F can also start a regex.
  1197. if (extra.tokenize && ch === 0x2F) {
  1198. return advanceSlash();
  1199. }
  1200. return scanPunctuator();
  1201. }
  1202. function collectToken() {
  1203. var loc, token, value;
  1204. skipComment();
  1205. loc = {
  1206. start: {
  1207. line: lineNumber,
  1208. column: index - lineStart
  1209. }
  1210. };
  1211. token = advance();
  1212. loc.end = {
  1213. line: lineNumber,
  1214. column: index - lineStart
  1215. };
  1216. if (token.type !== Token.EOF) {
  1217. value = source.slice(token.start, token.end);
  1218. extra.tokens.push({
  1219. type: TokenName[token.type],
  1220. value: value,
  1221. range: [token.start, token.end],
  1222. loc: loc
  1223. });
  1224. }
  1225. return token;
  1226. }
  1227. function lex() {
  1228. var token;
  1229. token = lookahead;
  1230. index = token.end;
  1231. lineNumber = token.lineNumber;
  1232. lineStart = token.lineStart;
  1233. lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance();
  1234. index = token.end;
  1235. lineNumber = token.lineNumber;
  1236. lineStart = token.lineStart;
  1237. return token;
  1238. }
  1239. function peek() {
  1240. var pos, line, start;
  1241. pos = index;
  1242. line = lineNumber;
  1243. start = lineStart;
  1244. lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance();
  1245. index = pos;
  1246. lineNumber = line;
  1247. lineStart = start;
  1248. }
  1249. function Position() {
  1250. this.line = lineNumber;
  1251. this.column = index - lineStart;
  1252. }
  1253. function SourceLocation() {
  1254. this.start = new Position();
  1255. this.end = null;
  1256. }
  1257. function WrappingSourceLocation(startToken) {
  1258. if (startToken.type === Token.StringLiteral) {
  1259. this.start = {
  1260. line: startToken.startLineNumber,
  1261. column: startToken.start - startToken.startLineStart
  1262. };
  1263. } else {
  1264. this.start = {
  1265. line: startToken.lineNumber,
  1266. column: startToken.start - startToken.lineStart
  1267. };
  1268. }
  1269. this.end = null;
  1270. }
  1271. function Node() {
  1272. // Skip comment.
  1273. index = lookahead.start;
  1274. if (lookahead.type === Token.StringLiteral) {
  1275. lineNumber = lookahead.startLineNumber;
  1276. lineStart = lookahead.startLineStart;
  1277. } else {
  1278. lineNumber = lookahead.lineNumber;
  1279. lineStart = lookahead.lineStart;
  1280. }
  1281. if (extra.range) {
  1282. this.range = [index, 0];
  1283. }
  1284. if (extra.loc) {
  1285. this.loc = new SourceLocation();
  1286. }
  1287. }
  1288. function WrappingNode(startToken) {
  1289. if (extra.range) {
  1290. this.range = [startToken.start, 0];
  1291. }
  1292. if (extra.loc) {
  1293. this.loc = new WrappingSourceLocation(startToken);
  1294. }
  1295. }
  1296. WrappingNode.prototype = Node.prototype = {
  1297. processComment: function () {
  1298. var lastChild,
  1299. trailingComments,
  1300. bottomRight = extra.bottomRightStack,
  1301. last = bottomRight[bottomRight.length - 1];
  1302. if (this.type === Syntax.Program) {
  1303. if (this.body.length > 0) {
  1304. return;
  1305. }
  1306. }
  1307. if (extra.trailingComments.length > 0) {
  1308. if (extra.trailingComments[0].range[0] >= this.range[1]) {
  1309. trailingComments = extra.trailingComments;
  1310. extra.trailingComments = [];
  1311. } else {
  1312. extra.trailingComments.length = 0;
  1313. }
  1314. } else {
  1315. if (last && last.trailingComments && last.trailingComments[0].range[0] >= this.range[1]) {
  1316. trailingComments = last.trailingComments;
  1317. delete last.trailingComments;
  1318. }
  1319. }
  1320. // Eating the stack.
  1321. if (last) {
  1322. while (last && last.range[0] >= this.range[0]) {
  1323. lastChild = last;
  1324. last = bottomRight.pop();
  1325. }
  1326. }
  1327. if (lastChild) {
  1328. if (lastChild.leadingComments && lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= this.range[0]) {
  1329. this.leadingComments = lastChild.leadingComments;
  1330. lastChild.leadingComments = undefined;
  1331. }
  1332. } else if (extra.leadingComments.length > 0 && extra.leadingComments[extra.leadingComments.length - 1].range[1] <= this.range[0]) {
  1333. this.leadingComments = extra.leadingComments;
  1334. extra.leadingComments = [];
  1335. }
  1336. if (trailingComments) {
  1337. this.trailingComments = trailingComments;
  1338. }
  1339. bottomRight.push(this);
  1340. },
  1341. finish: function () {
  1342. if (extra.range) {
  1343. this.range[1] = index;
  1344. }
  1345. if (extra.loc) {
  1346. this.loc.end = new Position();
  1347. if (extra.source) {
  1348. this.loc.source = extra.source;
  1349. }
  1350. }
  1351. if (extra.attachComment) {
  1352. this.processComment();
  1353. }
  1354. },
  1355. finishArrayExpression: function (elements) {
  1356. this.type = Syntax.ArrayExpression;
  1357. this.elements = elements;
  1358. this.finish();
  1359. return this;
  1360. },
  1361. finishArrowFunctionExpression: function (params, defaults, body, expression) {
  1362. this.type = Syntax.ArrowFunctionExpression;
  1363. this.id = null;
  1364. this.params = params;
  1365. this.defaults = defaults;
  1366. this.body = body;
  1367. this.rest = null;
  1368. this.generator = false;
  1369. this.expression = expression;
  1370. this.finish();
  1371. return this;
  1372. },
  1373. finishAssignmentExpression: function (operator, left, right) {
  1374. this.type = Syntax.AssignmentExpression;
  1375. this.operator = operator;
  1376. this.left = left;
  1377. this.right = right;
  1378. this.finish();
  1379. return this;
  1380. },
  1381. finishBinaryExpression: function (operator, left, right) {
  1382. this.type = (operator === '||' || operator === '&&') ? Syntax.LogicalExpression : Syntax.BinaryExpression;
  1383. this.operator = operator;
  1384. this.left = left;
  1385. this.right = right;
  1386. this.finish();
  1387. return this;
  1388. },
  1389. finishBlockStatement: function (body) {
  1390. this.type = Syntax.BlockStatement;
  1391. this.body = body;
  1392. this.finish();
  1393. return this;
  1394. },
  1395. finishBreakStatement: function (label) {
  1396. this.type = Syntax.BreakStatement;
  1397. this.label = label;
  1398. this.finish();
  1399. return this;
  1400. },
  1401. finishCallExpression: function (callee, args) {
  1402. this.type = Syntax.CallExpression;
  1403. this.callee = callee;
  1404. this.arguments = args;
  1405. this.finish();
  1406. return this;
  1407. },
  1408. finishCatchClause: function (param, body) {
  1409. this.type = Syntax.CatchClause;
  1410. this.param = param;
  1411. this.body = body;
  1412. this.finish();
  1413. return this;
  1414. },
  1415. finishConditionalExpression: function (test, consequent, alternate) {
  1416. this.type = Syntax.ConditionalExpression;
  1417. this.test = test;
  1418. this.consequent = consequent;
  1419. this.alternate = alternate;
  1420. this.finish();
  1421. return this;
  1422. },
  1423. finishContinueStatement: function (label) {
  1424. this.type = Syntax.ContinueStatement;
  1425. this.label = label;
  1426. this.finish();
  1427. return this;
  1428. },
  1429. finishDebuggerStatement: function () {
  1430. this.type = Syntax.DebuggerStatement;
  1431. this.finish();
  1432. return this;
  1433. },
  1434. finishDoWhileStatement: function (body, test) {
  1435. this.type = Syntax.DoWhileStatement;
  1436. this.body = body;
  1437. this.test = test;
  1438. this.finish();
  1439. return this;
  1440. },
  1441. finishEmptyStatement: function () {
  1442. this.type = Syntax.EmptyStatement;
  1443. this.finish();
  1444. return this;
  1445. },
  1446. finishExpressionStatement: function (expression) {
  1447. this.type = Syntax.ExpressionStatement;
  1448. this.expression = expression;
  1449. this.finish();
  1450. return this;
  1451. },
  1452. finishForStatement: function (init, test, update, body) {
  1453. this.type = Syntax.ForStatement;
  1454. this.init = init;
  1455. this.test = test;
  1456. this.update = update;
  1457. this.body = body;
  1458. this.finish();
  1459. return this;
  1460. },
  1461. finishForInStatement: function (left, right, body) {
  1462. this.type = Syntax.ForInStatement;
  1463. this.left = left;
  1464. this.right = right;
  1465. this.body = body;
  1466. this.each = false;
  1467. this.finish();
  1468. return this;
  1469. },
  1470. finishFunctionDeclaration: function (id, params, defaults, body) {
  1471. this.type = Syntax.FunctionDeclaration;
  1472. this.id = id;
  1473. this.params = params;
  1474. this.defaults = defaults;
  1475. this.body = body;
  1476. this.rest = null;
  1477. this.generator = false;
  1478. this.expression = false;
  1479. this.finish();
  1480. return this;
  1481. },
  1482. finishFunctionExpression: function (id, params, defaults, body) {
  1483. this.type = Syntax.FunctionExpression;
  1484. this.id = id;
  1485. this.params = params;
  1486. this.defaults = defaults;
  1487. this.body = body;
  1488. this.rest = null;
  1489. this.generator = false;
  1490. this.expression = false;
  1491. this.finish();
  1492. return this;
  1493. },
  1494. finishIdentifier: function (name) {
  1495. this.type = Syntax.Identifier;
  1496. this.name = name;
  1497. this.finish();
  1498. return this;
  1499. },
  1500. finishIfStatement: function (test, consequent, alternate) {
  1501. this.type = Syntax.IfStatement;
  1502. this.test = test;
  1503. this.consequent = consequent;
  1504. this.alternate = alternate;
  1505. this.finish();
  1506. return this;
  1507. },
  1508. finishLabeledStatement: function (label, body) {
  1509. this.type = Syntax.LabeledStatement;
  1510. this.label = label;
  1511. this.body = body;
  1512. this.finish();
  1513. return this;
  1514. },
  1515. finishLiteral: function (token) {
  1516. this.type = Syntax.Literal;
  1517. this.value = token.value;
  1518. this.raw = source.slice(token.start, token.end);
  1519. this.finish();
  1520. return this;
  1521. },
  1522. finishMemberExpression: function (accessor, object, property) {
  1523. this.type = Syntax.MemberExpression;
  1524. this.computed = accessor === '[';
  1525. this.object = object;
  1526. this.property = property;
  1527. this.finish();
  1528. return this;
  1529. },
  1530. finishNewExpression: function (callee, args) {
  1531. this.type = Syntax.NewExpression;
  1532. this.callee = callee;
  1533. this.arguments = args;
  1534. this.finish();
  1535. return this;
  1536. },
  1537. finishObjectExpression: function (properties) {
  1538. this.type = Syntax.ObjectExpression;
  1539. this.properties = properties;
  1540. this.finish();
  1541. return this;
  1542. },
  1543. finishPostfixExpression: function (operator, argument) {
  1544. this.type = Syntax.UpdateExpression;
  1545. this.operator = operator;
  1546. this.argument = argument;
  1547. this.prefix = false;
  1548. this.finish();
  1549. return this;
  1550. },
  1551. finishProgram: function (body) {
  1552. this.type = Syntax.Program;
  1553. this.body = body;
  1554. this.finish();
  1555. return this;
  1556. },
  1557. finishProperty: function (kind, key, value) {
  1558. this.type = Syntax.Property;
  1559. this.key = key;
  1560. this.value = value;
  1561. this.kind = kind;
  1562. this.finish();
  1563. return this;
  1564. },
  1565. finishReturnStatement: function (argument) {
  1566. this.type = Syntax.ReturnStatement;
  1567. this.argument = argument;
  1568. this.finish();
  1569. return this;
  1570. },
  1571. finishSequenceExpression: function (expressions) {
  1572. this.type = Syntax.SequenceExpression;
  1573. this.expressions = expressions;
  1574. this.finish();
  1575. return this;
  1576. },
  1577. finishSwitchCase: function (test, consequent) {
  1578. this.type = Syntax.SwitchCase;
  1579. this.test = test;
  1580. this.consequent = consequent;
  1581. this.finish();
  1582. return this;
  1583. },
  1584. finishSwitchStatement: function (discriminant, cases) {
  1585. this.type = Syntax.SwitchStatement;
  1586. this.discriminant = discriminant;
  1587. this.cases = cases;
  1588. this.finish();
  1589. return this;
  1590. },
  1591. finishThisExpression: function () {
  1592. this.type = Syntax.ThisExpression;
  1593. this.finish();
  1594. return this;
  1595. },
  1596. finishThrowStatement: function (argument) {
  1597. this.type = Syntax.ThrowStatement;
  1598. this.argument = argument;
  1599. this.finish();
  1600. return this;
  1601. },
  1602. finishTryStatement: function (block, guardedHandlers, handlers, finalizer) {
  1603. this.type = Syntax.TryStatement;
  1604. this.block = block;
  1605. this.guardedHandlers = guardedHandlers;
  1606. this.handlers = handlers;
  1607. this.finalizer = finalizer;
  1608. this.finish();
  1609. return this;
  1610. },
  1611. finishUnaryExpression: function (operator, argument) {
  1612. this.type = (operator === '++' || operator === '--') ? Syntax.UpdateExpression : Syntax.UnaryExpression;
  1613. this.operator = operator;
  1614. this.argument = argument;
  1615. this.prefix = true;
  1616. this.finish();
  1617. return this;
  1618. },
  1619. finishVariableDeclaration: function (declarations, kind) {
  1620. this.type = Syntax.VariableDeclaration;
  1621. this.declarations = declarations;
  1622. this.kind = kind;
  1623. this.finish();
  1624. return this;
  1625. },
  1626. finishVariableDeclarator: function (id, init) {
  1627. this.type = Syntax.VariableDeclarator;
  1628. this.id = id;
  1629. this.init = init;
  1630. this.finish();
  1631. return this;
  1632. },
  1633. finishWhileStatement: function (test, body) {
  1634. this.type = Syntax.WhileStatement;
  1635. this.test = test;
  1636. this.body = body;
  1637. this.finish();
  1638. return this;
  1639. },
  1640. finishWithStatement: function (object, body) {
  1641. this.type = Syntax.WithStatement;
  1642. this.object = object;
  1643. this.body = body;
  1644. this.finish();
  1645. return this;
  1646. }
  1647. };
  1648. // Return true if there is a line terminator before the next token.
  1649. function peekLineTerminator() {
  1650. var pos, line, start, found;
  1651. pos = index;
  1652. line = lineNumber;
  1653. start = lineStart;
  1654. skipComment();
  1655. found = lineNumber !== line;
  1656. index = pos;
  1657. lineNumber = line;
  1658. lineStart = start;
  1659. return found;
  1660. }
  1661. // Throw an exception
  1662. function throwError(token, messageFormat) {
  1663. var error,
  1664. args = Array.prototype.slice.call(arguments, 2),
  1665. msg = messageFormat.replace(
  1666. /%(\d)/g,
  1667. function (whole, index) {
  1668. assert(index < args.length, 'Message reference must be in range');
  1669. return args[index];
  1670. }
  1671. );
  1672. if (typeof token.lineNumber === 'number') {
  1673. error = new Error('Line ' + token.lineNumber + ': ' + msg);
  1674. error.index = token.start;
  1675. error.lineNumber = token.lineNumber;
  1676. error.column = token.start - lineStart + 1;
  1677. } else {
  1678. error = new Error('Line ' + lineNumber + ': ' + msg);
  1679. error.index = index;
  1680. error.lineNumber = lineNumber;
  1681. error.column = index - lineStart + 1;
  1682. }
  1683. error.description = msg;
  1684. throw error;
  1685. }
  1686. function throwErrorTolerant() {
  1687. try {
  1688. throwError.apply(null, arguments);
  1689. } catch (e) {
  1690. if (extra.errors) {
  1691. extra.errors.push(e);
  1692. } else {
  1693. throw e;
  1694. }
  1695. }
  1696. }
  1697. // Throw an exception because of the token.
  1698. function throwUnexpected(token) {
  1699. if (token.type === Token.EOF) {
  1700. throwError(token, Messages.UnexpectedEOS);
  1701. }
  1702. if (token.type === Token.NumericLiteral) {
  1703. throwError(token, Messages.UnexpectedNumber);
  1704. }
  1705. if (token.type === Token.StringLiteral) {
  1706. throwError(token, Messages.UnexpectedString);
  1707. }
  1708. if (token.type === Token.Identifier) {
  1709. throwError(token, Messages.UnexpectedIdentifier);
  1710. }
  1711. if (token.type === Token.Keyword) {
  1712. if (isFutureReservedWord(token.value)) {
  1713. throwError(token, Messages.UnexpectedReserved);
  1714. } else if (strict && isStrictModeReservedWord(token.value)) {
  1715. throwErrorTolerant(token, Messages.StrictReservedWord);
  1716. return;
  1717. }
  1718. throwError(token, Messages.UnexpectedToken, token.value);
  1719. }
  1720. // BooleanLiteral, NullLiteral, or Punctuator.
  1721. throwError(token, Messages.UnexpectedToken, token.value);
  1722. }
  1723. // Expect the next token to match the specified punctuator.
  1724. // If not, an exception will be thrown.
  1725. function expect(value) {
  1726. var token = lex();
  1727. if (token.type !== Token.Punctuator || token.value !== value) {
  1728. throwUnexpected(token);
  1729. }
  1730. }
  1731. // Expect the next token to match the specified keyword.
  1732. // If not, an exception will be thrown.
  1733. function expectKeyword(keyword) {
  1734. var token = lex();
  1735. if (token.type !== Token.Keyword || token.value !== keyword) {
  1736. throwUnexpected(token);
  1737. }
  1738. }
  1739. // Return true if the next token matches the specified punctuator.
  1740. function match(value) {
  1741. return lookahead.type === Token.Punctuator && lookahead.value === value;
  1742. }
  1743. // Return true if the next token matches the specified keyword
  1744. function matchKeyword(keyword) {
  1745. return lookahead.type === Token.Keyword && lookahead.value === keyword;
  1746. }
  1747. // Return true if the next token is an assignment operator
  1748. function matchAssign() {
  1749. var op;
  1750. if (lookahead.type !== Token.Punctuator) {
  1751. return false;
  1752. }
  1753. op = lookahead.value;
  1754. return op === '=' ||
  1755. op === '*=' ||
  1756. op === '/=' ||
  1757. op === '%=' ||
  1758. op === '+=' ||
  1759. op === '-=' ||
  1760. op === '<<=' ||
  1761. op === '>>=' ||
  1762. op === '>>>=' ||
  1763. op === '&=' ||
  1764. op === '^=' ||
  1765. op === '|=';
  1766. }
  1767. function consumeSemicolon() {
  1768. var line;
  1769. // Catch the very common case first: immediately a semicolon (U+003B).
  1770. if (source.charCodeAt(index) === 0x3B || match(';')) {
  1771. lex();
  1772. return;
  1773. }
  1774. line = lineNumber;
  1775. skipComment();
  1776. if (lineNumber !== line) {
  1777. return;
  1778. }
  1779. if (lookahead.type !== Token.EOF && !match('}')) {
  1780. throwUnexpected(lookahead);
  1781. }
  1782. }
  1783. // Return true if provided expression is LeftHandSideExpression
  1784. function isLeftHandSide(expr) {
  1785. return expr.type === Syntax.Identifier || expr.type === Syntax.MemberExpression;
  1786. }
  1787. // 11.1.4 Array Initialiser
  1788. function parseArrayInitialiser() {
  1789. var elements = [], node = new Node();
  1790. expect('[');
  1791. while (!match(']')) {
  1792. if (match(',')) {
  1793. lex();
  1794. elements.push(null);
  1795. } else {
  1796. elements.push(parseAssignmentExpression());
  1797. if (!match(']')) {
  1798. expect(',');
  1799. }
  1800. }
  1801. }
  1802. lex();
  1803. return node.finishArrayExpression(elements);
  1804. }
  1805. // 11.1.5 Object Initialiser
  1806. function parsePropertyFunction(param, first) {
  1807. var previousStrict, body, node = new Node();
  1808. previousStrict = strict;
  1809. body = parseFunctionSourceElements();
  1810. if (first && strict && isRestrictedWord(param[0].name)) {
  1811. throwErrorTolerant(first, Messages.StrictParamName);
  1812. }
  1813. strict = previousStrict;
  1814. return node.finishFunctionExpression(null, param, [], body);
  1815. }
  1816. function parseObjectPropertyKey() {
  1817. var token, node = new Node();
  1818. token = lex();
  1819. // Note: This function is called only from parseObjectProperty(), where
  1820. // EOF and Punctuator tokens are already filtered out.
  1821. if (token.type === Token.StringLiteral || token.type === Token.NumericLiteral) {
  1822. if (strict && token.octal) {
  1823. throwErrorTolerant(token, Messages.StrictOctalLiteral);
  1824. }
  1825. return node.finishLiteral(token);
  1826. }
  1827. return node.finishIdentifier(token.value);
  1828. }
  1829. function parseObjectProperty() {
  1830. var token, key, id, value, param, node = new Node();
  1831. token = lookahead;
  1832. if (token.type === Token.Identifier) {
  1833. id = parseObjectPropertyKey();
  1834. // Property Assignment: Getter and Setter.
  1835. if (token.value === 'get' && !match(':')) {
  1836. key = parseObjectPropertyKey();
  1837. expect('(');
  1838. expect(')');
  1839. value = parsePropertyFunction([]);
  1840. return node.finishProperty('get', key, value);
  1841. }
  1842. if (token.value === 'set' && !match(':')) {
  1843. key = parseObjectPropertyKey();
  1844. expect('(');
  1845. token = lookahead;
  1846. if (token.type !== Token.Identifier) {
  1847. expect(')');
  1848. throwErrorTolerant(token, Messages.UnexpectedToken, token.value);
  1849. value = parsePropertyFunction([]);
  1850. } else {
  1851. param = [ parseVariableIdentifier() ];
  1852. expect(')');
  1853. value = parsePropertyFunction(param, token);
  1854. }
  1855. return node.finishProperty('set', key, value);
  1856. }
  1857. expect(':');
  1858. value = parseAssignmentExpression();
  1859. return node.finishProperty('init', id, value);
  1860. }
  1861. if (token.type === Token.EOF || token.type === Token.Punctuator) {
  1862. throwUnexpected(token);
  1863. } else {
  1864. key = parseObjectPropertyKey();
  1865. expect(':');
  1866. value = parseAssignmentExpression();
  1867. return node.finishProperty('init', key, value);
  1868. }
  1869. }
  1870. function parseObjectInitialiser() {
  1871. var properties = [], token, property, name, key, kind, map = {}, toString = String, node = new Node();
  1872. expect('{');
  1873. while (!match('}')) {
  1874. property = parseObjectProperty();
  1875. if (property.key.type === Syntax.Identifier) {
  1876. name = property.key.name;
  1877. } else {
  1878. name = toString(property.key.value);
  1879. }
  1880. kind = (property.kind === 'init') ? PropertyKind.Data : (property.kind === 'get') ? PropertyKind.Get : PropertyKind.Set;
  1881. key = '$' + name;
  1882. if (Object.prototype.hasOwnProperty.call(map, key)) {
  1883. if (map[key] === PropertyKind.Data) {
  1884. if (strict && kind === PropertyKind.Data) {
  1885. throwErrorTolerant({}, Messages.StrictDuplicateProperty);
  1886. } else if (kind !== PropertyKind.Data) {
  1887. throwErrorTolerant({}, Messages.AccessorDataProperty);
  1888. }
  1889. } else {
  1890. if (kind === PropertyKind.Data) {
  1891. throwErrorTolerant({}, Messages.AccessorDataProperty);
  1892. } else if (map[key] & kind) {
  1893. throwErrorTolerant({}, Messages.AccessorGetSet);
  1894. }
  1895. }
  1896. map[key] |= kind;
  1897. } else {
  1898. map[key] = kind;
  1899. }
  1900. properties.push(property);
  1901. if (!match('}')) {
  1902. if (extra.errors) {
  1903. token = lookahead;
  1904. if (token.type !== Token.Punctuator && token.value !== ',') {
  1905. throwErrorTolerant(token, Messages.UnexpectedToken, token.value);
  1906. } else {
  1907. lex();
  1908. }
  1909. } else {
  1910. expect(',');
  1911. }
  1912. }
  1913. }
  1914. expect('}');
  1915. return node.finishObjectExpression(properties);
  1916. }
  1917. // 11.1.6 The Grouping Operator
  1918. function parseGroupExpression() {
  1919. var expr;
  1920. expect('(');
  1921. if (match(')')) {
  1922. lex();
  1923. return PlaceHolders.ArrowParameterPlaceHolder;
  1924. }
  1925. ++state.parenthesisCount;
  1926. expr = parseExpression();
  1927. expect(')');
  1928. return expr;
  1929. }
  1930. // 11.1 Primary Expressions
  1931. function parsePrimaryExpression() {
  1932. var type, token, expr, node;
  1933. if (match('(')) {
  1934. return parseGroupExpression();
  1935. }
  1936. if (match('[')) {
  1937. return parseArrayInitialiser();
  1938. }
  1939. if (match('{')) {
  1940. return parseObjectInitialiser();
  1941. }
  1942. type = lookahead.type;
  1943. node = new Node();
  1944. if (type === Token.Identifier) {
  1945. expr = node.finishIdentifier(lex().value);
  1946. } else if (type === Token.StringLiteral || type === Token.NumericLiteral) {
  1947. if (strict && lookahead.octal) {
  1948. throwErrorTolerant(lookahead, Messages.StrictOctalLiteral);
  1949. }
  1950. expr = node.finishLiteral(lex());
  1951. } else if (type === Token.Keyword) {
  1952. if (matchKeyword('function')) {
  1953. return parseFunctionExpression();
  1954. }
  1955. if (matchKeyword('this')) {
  1956. lex();
  1957. expr = node.finishThisExpression();
  1958. } else {
  1959. throwUnexpected(lex());
  1960. }
  1961. } else if (type === Token.BooleanLiteral) {
  1962. token = lex();
  1963. token.value = (token.value === 'true');
  1964. expr = node.finishLiteral(token);
  1965. } else if (type === Token.NullLiteral) {
  1966. token = lex();
  1967. token.value = null;
  1968. expr = node.finishLiteral(token);
  1969. } else if (match('/') || match('/=')) {
  1970. if (typeof extra.tokens !== 'undefined') {
  1971. expr = node.finishLiteral(collectRegex());
  1972. } else {
  1973. expr = node.finishLiteral(scanRegExp());
  1974. }
  1975. peek();
  1976. } else {
  1977. throwUnexpected(lex());
  1978. }
  1979. return expr;
  1980. }
  1981. // 11.2 Left-Hand-Side Expressions
  1982. function parseArguments() {
  1983. var args = [];
  1984. expect('(');
  1985. if (!match(')')) {
  1986. while (index < length) {
  1987. args.push(parseAssignmentExpression());
  1988. if (match(')')) {
  1989. break;
  1990. }
  1991. expect(',');
  1992. }
  1993. }
  1994. expect(')');
  1995. return args;
  1996. }
  1997. function parseNonComputedProperty() {
  1998. var token, node = new Node();
  1999. token = lex();
  2000. if (!isIdentifierName(token)) {
  2001. throwUnexpected(token);
  2002. }
  2003. return node.finishIdentifier(token.value);
  2004. }
  2005. function parseNonComputedMember() {
  2006. expect('.');
  2007. return parseNonComputedProperty();
  2008. }
  2009. function parseComputedMember() {
  2010. var expr;
  2011. expect('[');
  2012. expr = parseExpression();
  2013. expect(']');
  2014. return expr;
  2015. }
  2016. function parseNewExpression() {
  2017. var callee, args, node = new Node();
  2018. expectKeyword('new');
  2019. callee = parseLeftHandSideExpression();
  2020. args = match('(') ? parseArguments() : [];
  2021. return node.finishNewExpression(callee, args);
  2022. }
  2023. function parseLeftHandSideExpressionAllowCall() {
  2024. var previousAllowIn, expr, args, property, startToken;
  2025. startToken = lookahead;
  2026. previousAllowIn = state.allowIn;
  2027. state.allowIn = true;
  2028. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  2029. state.allowIn = previousAllowIn;
  2030. for (;;) {
  2031. if (match('.')) {
  2032. property = parseNonComputedMember();
  2033. expr = new WrappingNode(startToken).finishMemberExpression('.', expr, property);
  2034. } else if (match('(')) {
  2035. args = parseArguments();
  2036. expr = new WrappingNode(startToken).finishCallExpression(expr, args);
  2037. } else if (match('[')) {
  2038. property = parseComputedMember();
  2039. expr = new WrappingNode(startToken).finishMemberExpression('[', expr, property);
  2040. } else {
  2041. break;
  2042. }
  2043. }
  2044. return expr;
  2045. }
  2046. function parseLeftHandSideExpression() {
  2047. var previousAllowIn, expr, property, startToken;
  2048. startToken = lookahead;
  2049. previousAllowIn = state.allowIn;
  2050. expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
  2051. state.allowIn = previousAllowIn;
  2052. for (;;) {
  2053. if (match('[')) {
  2054. property = parseComputedMember();
  2055. expr = new WrappingNode(startToken).finishMemberExpression('[', expr, property);
  2056. } else if (match('.')) {
  2057. property = parseNonComputedMember();
  2058. expr = new WrappingNode(startToken).finishMemberExpression('.', expr, property);
  2059. } else {
  2060. break;
  2061. }
  2062. }
  2063. return expr;
  2064. }
  2065. // 11.3 Postfix Expressions
  2066. function parsePostfixExpression() {
  2067. var expr, token, startToken = lookahead;
  2068. expr = parseLeftHandSideExpressionAllowCall();
  2069. if (lookahead.type === Token.Punctuator) {
  2070. if ((match('++') || match('--')) && !peekLineTerminator()) {
  2071. // 11.3.1, 11.3.2
  2072. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2073. throwErrorTolerant({}, Messages.StrictLHSPostfix);
  2074. }
  2075. if (!isLeftHandSide(expr)) {
  2076. throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
  2077. }
  2078. token = lex();
  2079. expr = new WrappingNode(startToken).finishPostfixExpression(token.value, expr);
  2080. }
  2081. }
  2082. return expr;
  2083. }
  2084. // 11.4 Unary Operators
  2085. function parseUnaryExpression() {
  2086. var token, expr, startToken;
  2087. if (lookahead.type !== Token.Punctuator && lookahead.type !== Token.Keyword) {
  2088. expr = parsePostfixExpression();
  2089. } else if (match('++') || match('--')) {
  2090. startToken = lookahead;
  2091. token = lex();
  2092. expr = parseUnaryExpression();
  2093. // 11.4.4, 11.4.5
  2094. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2095. throwErrorTolerant({}, Messages.StrictLHSPrefix);
  2096. }
  2097. if (!isLeftHandSide(expr)) {
  2098. throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
  2099. }
  2100. expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr);
  2101. } else if (match('+') || match('-') || match('~') || match('!')) {
  2102. startToken = lookahead;
  2103. token = lex();
  2104. expr = parseUnaryExpression();
  2105. expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr);
  2106. } else if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) {
  2107. startToken = lookahead;
  2108. token = lex();
  2109. expr = parseUnaryExpression();
  2110. expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr);
  2111. if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) {
  2112. throwErrorTolerant({}, Messages.StrictDelete);
  2113. }
  2114. } else {
  2115. expr = parsePostfixExpression();
  2116. }
  2117. return expr;
  2118. }
  2119. function binaryPrecedence(token, allowIn) {
  2120. var prec = 0;
  2121. if (token.type !== Token.Punctuator && token.type !== Token.Keyword) {
  2122. return 0;
  2123. }
  2124. switch (token.value) {
  2125. case '||':
  2126. prec = 1;
  2127. break;
  2128. case '&&':
  2129. prec = 2;
  2130. break;
  2131. case '|':
  2132. prec = 3;
  2133. break;
  2134. case '^':
  2135. prec = 4;
  2136. break;
  2137. case '&':
  2138. prec = 5;
  2139. break;
  2140. case '==':
  2141. case '!=':
  2142. case '===':
  2143. case '!==':
  2144. prec = 6;
  2145. break;
  2146. case '<':
  2147. case '>':
  2148. case '<=':
  2149. case '>=':
  2150. case 'instanceof':
  2151. prec = 7;
  2152. break;
  2153. case 'in':
  2154. prec = allowIn ? 7 : 0;
  2155. break;
  2156. case '<<':
  2157. case '>>':
  2158. case '>>>':
  2159. prec = 8;
  2160. break;
  2161. case '+':
  2162. case '-':
  2163. prec = 9;
  2164. break;
  2165. case '*':
  2166. case '/':
  2167. case '%':
  2168. prec = 11;
  2169. break;
  2170. default:
  2171. break;
  2172. }
  2173. return prec;
  2174. }
  2175. // 11.5 Multiplicative Operators
  2176. // 11.6 Additive Operators
  2177. // 11.7 Bitwise Shift Operators
  2178. // 11.8 Relational Operators
  2179. // 11.9 Equality Operators
  2180. // 11.10 Binary Bitwise Operators
  2181. // 11.11 Binary Logical Operators
  2182. function parseBinaryExpression() {
  2183. var marker, markers, expr, token, prec, stack, right, operator, left, i;
  2184. marker = lookahead;
  2185. left = parseUnaryExpression();
  2186. if (left === PlaceHolders.ArrowParameterPlaceHolder) {
  2187. return left;
  2188. }
  2189. token = lookahead;
  2190. prec = binaryPrecedence(token, state.allowIn);
  2191. if (prec === 0) {
  2192. return left;
  2193. }
  2194. token.prec = prec;
  2195. lex();
  2196. markers = [marker, lookahead];
  2197. right = parseUnaryExpression();
  2198. stack = [left, token, right];
  2199. while ((prec = binaryPrecedence(lookahead, state.allowIn)) > 0) {
  2200. // Reduce: make a binary expression from the three topmost entries.
  2201. while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) {
  2202. right = stack.pop();
  2203. operator = stack.pop().value;
  2204. left = stack.pop();
  2205. markers.pop();
  2206. expr = new WrappingNode(markers[markers.length - 1]).finishBinaryExpression(operator, left, right);
  2207. stack.push(expr);
  2208. }
  2209. // Shift.
  2210. token = lex();
  2211. token.prec = prec;
  2212. stack.push(token);
  2213. markers.push(lookahead);
  2214. expr = parseUnaryExpression();
  2215. stack.push(expr);
  2216. }
  2217. // Final reduce to clean-up the stack.
  2218. i = stack.length - 1;
  2219. expr = stack[i];
  2220. markers.pop();
  2221. while (i > 1) {
  2222. expr = new WrappingNode(markers.pop()).finishBinaryExpression(stack[i - 1].value, stack[i - 2], expr);
  2223. i -= 2;
  2224. }
  2225. return expr;
  2226. }
  2227. // 11.12 Conditional Operator
  2228. function parseConditionalExpression() {
  2229. var expr, previousAllowIn, consequent, alternate, startToken;
  2230. startToken = lookahead;
  2231. expr = parseBinaryExpression();
  2232. if (expr === PlaceHolders.ArrowParameterPlaceHolder) {
  2233. return expr;
  2234. }
  2235. if (match('?')) {
  2236. lex();
  2237. previousAllowIn = state.allowIn;
  2238. state.allowIn = true;
  2239. consequent = parseAssignmentExpression();
  2240. state.allowIn = previousAllowIn;
  2241. expect(':');
  2242. alternate = parseAssignmentExpression();
  2243. expr = new WrappingNode(startToken).finishConditionalExpression(expr, consequent, alternate);
  2244. }
  2245. return expr;
  2246. }
  2247. // [ES6] 14.2 Arrow Function
  2248. function parseConciseBody() {
  2249. if (match('{')) {
  2250. return parseFunctionSourceElements();
  2251. }
  2252. return parseAssignmentExpression();
  2253. }
  2254. function reinterpretAsCoverFormalsList(expressions) {
  2255. var i, len, param, params, defaults, defaultCount, options, rest;
  2256. params = [];
  2257. defaults = [];
  2258. defaultCount = 0;
  2259. rest = null;
  2260. options = {
  2261. paramSet: {}
  2262. };
  2263. for (i = 0, len = expressions.length; i < len; i += 1) {
  2264. param = expressions[i];
  2265. if (param.type === Syntax.Identifier) {
  2266. params.push(param);
  2267. defaults.push(null);
  2268. validateParam(options, param, param.name);
  2269. } else if (param.type === Syntax.AssignmentExpression) {
  2270. params.push(param.left);
  2271. defaults.push(param.right);
  2272. ++defaultCount;
  2273. validateParam(options, param.left, param.left.name);
  2274. } else {
  2275. return null;
  2276. }
  2277. }
  2278. if (options.message === Messages.StrictParamDupe) {
  2279. throwError(
  2280. strict ? options.stricted : options.firstRestricted,
  2281. options.message
  2282. );
  2283. }
  2284. if (defaultCount === 0) {
  2285. defaults = [];
  2286. }
  2287. return {
  2288. params: params,
  2289. defaults: defaults,
  2290. rest: rest,
  2291. stricted: options.stricted,
  2292. firstRestricted: options.firstRestricted,
  2293. message: options.message
  2294. };
  2295. }
  2296. function parseArrowFunctionExpression(options, node) {
  2297. var previousStrict, body;
  2298. expect('=>');
  2299. previousStrict = strict;
  2300. body = parseConciseBody();
  2301. if (strict && options.firstRestricted) {
  2302. throwError(options.firstRestricted, options.message);
  2303. }
  2304. if (strict && options.stricted) {
  2305. throwErrorTolerant(options.stricted, options.message);
  2306. }
  2307. strict = previousStrict;
  2308. return node.finishArrowFunctionExpression(options.params, options.defaults, body, body.type !== Syntax.BlockStatement);
  2309. }
  2310. // 11.13 Assignment Operators
  2311. function parseAssignmentExpression() {
  2312. var oldParenthesisCount, token, expr, right, list, startToken;
  2313. oldParenthesisCount = state.parenthesisCount;
  2314. startToken = lookahead;
  2315. token = lookahead;
  2316. expr = parseConditionalExpression();
  2317. if (expr === PlaceHolders.ArrowParameterPlaceHolder || match('=>')) {
  2318. if (state.parenthesisCount === oldParenthesisCount ||
  2319. state.parenthesisCount === (oldParenthesisCount + 1)) {
  2320. if (expr.type === Syntax.Identifier) {
  2321. list = reinterpretAsCoverFormalsList([ expr ]);
  2322. } else if (expr.type === Syntax.AssignmentExpression) {
  2323. list = reinterpretAsCoverFormalsList([ expr ]);
  2324. } else if (expr.type === Syntax.SequenceExpression) {
  2325. list = reinterpretAsCoverFormalsList(expr.expressions);
  2326. } else if (expr === PlaceHolders.ArrowParameterPlaceHolder) {
  2327. list = reinterpretAsCoverFormalsList([]);
  2328. }
  2329. if (list) {
  2330. return parseArrowFunctionExpression(list, new WrappingNode(startToken));
  2331. }
  2332. }
  2333. }
  2334. if (matchAssign()) {
  2335. // LeftHandSideExpression
  2336. if (!isLeftHandSide(expr)) {
  2337. throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
  2338. }
  2339. // 11.13.1
  2340. if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
  2341. throwErrorTolerant(token, Messages.StrictLHSAssignment);
  2342. }
  2343. token = lex();
  2344. right = parseAssignmentExpression();
  2345. expr = new WrappingNode(startToken).finishAssignmentExpression(token.value, expr, right);
  2346. }
  2347. return expr;
  2348. }
  2349. // 11.14 Comma Operator
  2350. function parseExpression() {
  2351. var expr, startToken = lookahead, expressions;
  2352. expr = parseAssignmentExpression();
  2353. if (match(',')) {
  2354. expressions = [expr];
  2355. while (index < length) {
  2356. if (!match(',')) {
  2357. break;
  2358. }
  2359. lex();
  2360. expressions.push(parseAssignmentExpression());
  2361. }
  2362. expr = new WrappingNode(startToken).finishSequenceExpression(expressions);
  2363. }
  2364. return expr;
  2365. }
  2366. // 12.1 Block
  2367. function parseStatementList() {
  2368. var list = [],
  2369. statement;
  2370. while (index < length) {
  2371. if (match('}')) {
  2372. break;
  2373. }
  2374. statement = parseSourceElement();
  2375. if (typeof statement === 'undefined') {
  2376. break;
  2377. }
  2378. list.push(statement);
  2379. }
  2380. return list;
  2381. }
  2382. function parseBlock() {
  2383. var block, node = new Node();
  2384. expect('{');
  2385. block = parseStatementList();
  2386. expect('}');
  2387. return node.finishBlockStatement(block);
  2388. }
  2389. // 12.2 Variable Statement
  2390. function parseVariableIdentifier() {
  2391. var token, node = new Node();
  2392. token = lex();
  2393. if (token.type !== Token.Identifier) {
  2394. throwUnexpected(token);
  2395. }
  2396. return node.finishIdentifier(token.value);
  2397. }
  2398. function parseVariableDeclaration(kind) {
  2399. var init = null, id, node = new Node();
  2400. id = parseVariableIdentifier();
  2401. // 12.2.1
  2402. if (strict && isRestrictedWord(id.name)) {
  2403. throwErrorTolerant({}, Messages.StrictVarName);
  2404. }
  2405. if (kind === 'const') {
  2406. expect('=');
  2407. init = parseAssignmentExpression();
  2408. } else if (match('=')) {
  2409. lex();
  2410. init = parseAssignmentExpression();
  2411. }
  2412. return node.finishVariableDeclarator(id, init);
  2413. }
  2414. function parseVariableDeclarationList(kind) {
  2415. var list = [];
  2416. do {
  2417. list.push(parseVariableDeclaration(kind));
  2418. if (!match(',')) {
  2419. break;
  2420. }
  2421. lex();
  2422. } while (index < length);
  2423. return list;
  2424. }
  2425. function parseVariableStatement(node) {
  2426. var declarations;
  2427. expectKeyword('var');
  2428. declarations = parseVariableDeclarationList();
  2429. consumeSemicolon();
  2430. return node.finishVariableDeclaration(declarations, 'var');
  2431. }
  2432. // kind may be `const` or `let`
  2433. // Both are experimental and not in the specification yet.
  2434. // see http://wiki.ecmascript.org/doku.php?id=harmony:const
  2435. // and http://wiki.ecmascript.org/doku.php?id=harmony:let
  2436. function parseConstLetDeclaration(kind) {
  2437. var declarations, node = new Node();
  2438. expectKeyword(kind);
  2439. declarations = parseVariableDeclarationList(kind);
  2440. consumeSemicolon();
  2441. return node.finishVariableDeclaration(declarations, kind);
  2442. }
  2443. // 12.3 Empty Statement
  2444. function parseEmptyStatement() {
  2445. var node = new Node();
  2446. expect(';');
  2447. return node.finishEmptyStatement();
  2448. }
  2449. // 12.4 Expression Statement
  2450. function parseExpressionStatement(node) {
  2451. var expr = parseExpression();
  2452. consumeSemicolon();
  2453. return node.finishExpressionStatement(expr);
  2454. }
  2455. // 12.5 If statement
  2456. function parseIfStatement(node) {
  2457. var test, consequent, alternate;
  2458. expectKeyword('if');
  2459. expect('(');
  2460. test = parseExpression();
  2461. expect(')');
  2462. consequent = parseStatement();
  2463. if (matchKeyword('else')) {
  2464. lex();
  2465. alternate = parseStatement();
  2466. } else {
  2467. alternate = null;
  2468. }
  2469. return node.finishIfStatement(test, consequent, alternate);
  2470. }
  2471. // 12.6 Iteration Statements
  2472. function parseDoWhileStatement(node) {
  2473. var body, test, oldInIteration;
  2474. expectKeyword('do');
  2475. oldInIteration = state.inIteration;
  2476. state.inIteration = true;
  2477. body = parseStatement();
  2478. state.inIteration = oldInIteration;
  2479. expectKeyword('while');
  2480. expect('(');
  2481. test = parseExpression();
  2482. expect(')');
  2483. if (match(';')) {
  2484. lex();
  2485. }
  2486. return node.finishDoWhileStatement(body, test);
  2487. }
  2488. function parseWhileStatement(node) {
  2489. var test, body, oldInIteration;
  2490. expectKeyword('while');
  2491. expect('(');
  2492. test = parseExpression();
  2493. expect(')');
  2494. oldInIteration = state.inIteration;
  2495. state.inIteration = true;
  2496. body = parseStatement();
  2497. state.inIteration = oldInIteration;
  2498. return node.finishWhileStatement(test, body);
  2499. }
  2500. function parseForVariableDeclaration() {
  2501. var token, declarations, node = new Node();
  2502. token = lex();
  2503. declarations = parseVariableDeclarationList();
  2504. return node.finishVariableDeclaration(declarations, token.value);
  2505. }
  2506. function parseForStatement(node) {
  2507. var init, test, update, left, right, body, oldInIteration;
  2508. init = test = update = null;
  2509. expectKeyword('for');
  2510. expect('(');
  2511. if (match(';')) {
  2512. lex();
  2513. } else {
  2514. if (matchKeyword('var') || matchKeyword('let')) {
  2515. state.allowIn = false;
  2516. init = parseForVariableDeclaration();
  2517. state.allowIn = true;
  2518. if (init.declarations.length === 1 && matchKeyword('in')) {
  2519. lex();
  2520. left = init;
  2521. right = parseExpression();
  2522. init = null;
  2523. }
  2524. } else {
  2525. state.allowIn = false;
  2526. init = parseExpression();
  2527. state.allowIn = true;
  2528. if (matchKeyword('in')) {
  2529. // LeftHandSideExpression
  2530. if (!isLeftHandSide(init)) {
  2531. throwErrorTolerant({}, Messages.InvalidLHSInForIn);
  2532. }
  2533. lex();
  2534. left = init;
  2535. right = parseExpression();
  2536. init = null;
  2537. }
  2538. }
  2539. if (typeof left === 'undefined') {
  2540. expect(';');
  2541. }
  2542. }
  2543. if (typeof left === 'undefined') {
  2544. if (!match(';')) {
  2545. test = parseExpression();
  2546. }
  2547. expect(';');
  2548. if (!match(')')) {
  2549. update = parseExpression();
  2550. }
  2551. }
  2552. expect(')');
  2553. oldInIteration = state.inIteration;
  2554. state.inIteration = true;
  2555. body = parseStatement();
  2556. state.inIteration = oldInIteration;
  2557. return (typeof left === 'undefined') ?
  2558. node.finishForStatement(init, test, update, body) :
  2559. node.finishForInStatement(left, right, body);
  2560. }
  2561. // 12.7 The continue statement
  2562. function parseContinueStatement(node) {
  2563. var label = null, key;
  2564. expectKeyword('continue');
  2565. // Optimize the most common form: 'continue;'.
  2566. if (source.charCodeAt(index) === 0x3B) {
  2567. lex();
  2568. if (!state.inIteration) {
  2569. throwError({}, Messages.IllegalContinue);
  2570. }
  2571. return node.finishContinueStatement(null);
  2572. }
  2573. if (peekLineTerminator()) {
  2574. if (!state.inIteration) {
  2575. throwError({}, Messages.IllegalContinue);
  2576. }
  2577. return node.finishContinueStatement(null);
  2578. }
  2579. if (lookahead.type === Token.Identifier) {
  2580. label = parseVariableIdentifier();
  2581. key = '$' + label.name;
  2582. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  2583. throwError({}, Messages.UnknownLabel, label.name);
  2584. }
  2585. }
  2586. consumeSemicolon();
  2587. if (label === null && !state.inIteration) {
  2588. throwError({}, Messages.IllegalContinue);
  2589. }
  2590. return node.finishContinueStatement(label);
  2591. }
  2592. // 12.8 The break statement
  2593. function parseBreakStatement(node) {
  2594. var label = null, key;
  2595. expectKeyword('break');
  2596. // Catch the very common case first: immediately a semicolon (U+003B).
  2597. if (source.charCodeAt(index) === 0x3B) {
  2598. lex();
  2599. if (!(state.inIteration || state.inSwitch)) {
  2600. throwError({}, Messages.IllegalBreak);
  2601. }
  2602. return node.finishBreakStatement(null);
  2603. }
  2604. if (peekLineTerminator()) {
  2605. if (!(state.inIteration || state.inSwitch)) {
  2606. throwError({}, Messages.IllegalBreak);
  2607. }
  2608. return node.finishBreakStatement(null);
  2609. }
  2610. if (lookahead.type === Token.Identifier) {
  2611. label = parseVariableIdentifier();
  2612. key = '$' + label.name;
  2613. if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  2614. throwError({}, Messages.UnknownLabel, label.name);
  2615. }
  2616. }
  2617. consumeSemicolon();
  2618. if (label === null && !(state.inIteration || state.inSwitch)) {
  2619. throwError({}, Messages.IllegalBreak);
  2620. }
  2621. return node.finishBreakStatement(label);
  2622. }
  2623. // 12.9 The return statement
  2624. function parseReturnStatement(node) {
  2625. var argument = null;
  2626. expectKeyword('return');
  2627. if (!state.inFunctionBody) {
  2628. throwErrorTolerant({}, Messages.IllegalReturn);
  2629. }
  2630. // 'return' followed by a space and an identifier is very common.
  2631. if (source.charCodeAt(index) === 0x20) {
  2632. if (isIdentifierStart(source.charCodeAt(index + 1))) {
  2633. argument = parseExpression();
  2634. consumeSemicolon();
  2635. return node.finishReturnStatement(argument);
  2636. }
  2637. }
  2638. if (peekLineTerminator()) {
  2639. return node.finishReturnStatement(null);
  2640. }
  2641. if (!match(';')) {
  2642. if (!match('}') && lookahead.type !== Token.EOF) {
  2643. argument = parseExpression();
  2644. }
  2645. }
  2646. consumeSemicolon();
  2647. return node.finishReturnStatement(argument);
  2648. }
  2649. // 12.10 The with statement
  2650. function parseWithStatement(node) {
  2651. var object, body;
  2652. if (strict) {
  2653. // TODO(ikarienator): Should we update the test cases instead?
  2654. skipComment();
  2655. throwErrorTolerant({}, Messages.StrictModeWith);
  2656. }
  2657. expectKeyword('with');
  2658. expect('(');
  2659. object = parseExpression();
  2660. expect(')');
  2661. body = parseStatement();
  2662. return node.finishWithStatement(object, body);
  2663. }
  2664. // 12.10 The swith statement
  2665. function parseSwitchCase() {
  2666. var test, consequent = [], statement, node = new Node();
  2667. if (matchKeyword('default')) {
  2668. lex();
  2669. test = null;
  2670. } else {
  2671. expectKeyword('case');
  2672. test = parseExpression();
  2673. }
  2674. expect(':');
  2675. while (index < length) {
  2676. if (match('}') || matchKeyword('default') || matchKeyword('case')) {
  2677. break;
  2678. }
  2679. statement = parseStatement();
  2680. consequent.push(statement);
  2681. }
  2682. return node.finishSwitchCase(test, consequent);
  2683. }
  2684. function parseSwitchStatement(node) {
  2685. var discriminant, cases, clause, oldInSwitch, defaultFound;
  2686. expectKeyword('switch');
  2687. expect('(');
  2688. discriminant = parseExpression();
  2689. expect(')');
  2690. expect('{');
  2691. cases = [];
  2692. if (match('}')) {
  2693. lex();
  2694. return node.finishSwitchStatement(discriminant, cases);
  2695. }
  2696. oldInSwitch = state.inSwitch;
  2697. state.inSwitch = true;
  2698. defaultFound = false;
  2699. while (index < length) {
  2700. if (match('}')) {
  2701. break;
  2702. }
  2703. clause = parseSwitchCase();
  2704. if (clause.test === null) {
  2705. if (defaultFound) {
  2706. throwError({}, Messages.MultipleDefaultsInSwitch);
  2707. }
  2708. defaultFound = true;
  2709. }
  2710. cases.push(clause);
  2711. }
  2712. state.inSwitch = oldInSwitch;
  2713. expect('}');
  2714. return node.finishSwitchStatement(discriminant, cases);
  2715. }
  2716. // 12.13 The throw statement
  2717. function parseThrowStatement(node) {
  2718. var argument;
  2719. expectKeyword('throw');
  2720. if (peekLineTerminator()) {
  2721. throwError({}, Messages.NewlineAfterThrow);
  2722. }
  2723. argument = parseExpression();
  2724. consumeSemicolon();
  2725. return node.finishThrowStatement(argument);
  2726. }
  2727. // 12.14 The try statement
  2728. function parseCatchClause() {
  2729. var param, body, node = new Node();
  2730. expectKeyword('catch');
  2731. expect('(');
  2732. if (match(')')) {
  2733. throwUnexpected(lookahead);
  2734. }
  2735. param = parseVariableIdentifier();
  2736. // 12.14.1
  2737. if (strict && isRestrictedWord(param.name)) {
  2738. throwErrorTolerant({}, Messages.StrictCatchVariable);
  2739. }
  2740. expect(')');
  2741. body = parseBlock();
  2742. return node.finishCatchClause(param, body);
  2743. }
  2744. function parseTryStatement(node) {
  2745. var block, handlers = [], finalizer = null;
  2746. expectKeyword('try');
  2747. block = parseBlock();
  2748. if (matchKeyword('catch')) {
  2749. handlers.push(parseCatchClause());
  2750. }
  2751. if (matchKeyword('finally')) {
  2752. lex();
  2753. finalizer = parseBlock();
  2754. }
  2755. if (handlers.length === 0 && !finalizer) {
  2756. throwError({}, Messages.NoCatchOrFinally);
  2757. }
  2758. return node.finishTryStatement(block, [], handlers, finalizer);
  2759. }
  2760. // 12.15 The debugger statement
  2761. function parseDebuggerStatement(node) {
  2762. expectKeyword('debugger');
  2763. consumeSemicolon();
  2764. return node.finishDebuggerStatement();
  2765. }
  2766. // 12 Statements
  2767. function parseStatement() {
  2768. var type = lookahead.type,
  2769. expr,
  2770. labeledBody,
  2771. key,
  2772. node;
  2773. if (type === Token.EOF) {
  2774. throwUnexpected(lookahead);
  2775. }
  2776. if (type === Token.Punctuator && lookahead.value === '{') {
  2777. return parseBlock();
  2778. }
  2779. node = new Node();
  2780. if (type === Token.Punctuator) {
  2781. switch (lookahead.value) {
  2782. case ';':
  2783. return parseEmptyStatement(node);
  2784. case '(':
  2785. return parseExpressionStatement(node);
  2786. default:
  2787. break;
  2788. }
  2789. } else if (type === Token.Keyword) {
  2790. switch (lookahead.value) {
  2791. case 'break':
  2792. return parseBreakStatement(node);
  2793. case 'continue':
  2794. return parseContinueStatement(node);
  2795. case 'debugger':
  2796. return parseDebuggerStatement(node);
  2797. case 'do':
  2798. return parseDoWhileStatement(node);
  2799. case 'for':
  2800. return parseForStatement(node);
  2801. case 'function':
  2802. return parseFunctionDeclaration(node);
  2803. case 'if':
  2804. return parseIfStatement(node);
  2805. case 'return':
  2806. return parseReturnStatement(node);
  2807. case 'switch':
  2808. return parseSwitchStatement(node);
  2809. case 'throw':
  2810. return parseThrowStatement(node);
  2811. case 'try':
  2812. return parseTryStatement(node);
  2813. case 'var':
  2814. return parseVariableStatement(node);
  2815. case 'while':
  2816. return parseWhileStatement(node);
  2817. case 'with':
  2818. return parseWithStatement(node);
  2819. default:
  2820. break;
  2821. }
  2822. }
  2823. expr = parseExpression();
  2824. // 12.12 Labelled Statements
  2825. if ((expr.type === Syntax.Identifier) && match(':')) {
  2826. lex();
  2827. key = '$' + expr.name;
  2828. if (Object.prototype.hasOwnProperty.call(state.labelSet, key)) {
  2829. throwError({}, Messages.Redeclaration, 'Label', expr.name);
  2830. }
  2831. state.labelSet[key] = true;
  2832. labeledBody = parseStatement();
  2833. delete state.labelSet[key];
  2834. return node.finishLabeledStatement(expr, labeledBody);
  2835. }
  2836. consumeSemicolon();
  2837. return node.finishExpressionStatement(expr);
  2838. }
  2839. // 13 Function Definition
  2840. function parseFunctionSourceElements() {
  2841. var sourceElement, sourceElements = [], token, directive, firstRestricted,
  2842. oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody, oldParenthesisCount,
  2843. node = new Node();
  2844. expect('{');
  2845. while (index < length) {
  2846. if (lookahead.type !== Token.StringLiteral) {
  2847. break;
  2848. }
  2849. token = lookahead;
  2850. sourceElement = parseSourceElement();
  2851. sourceElements.push(sourceElement);
  2852. if (sourceElement.expression.type !== Syntax.Literal) {
  2853. // this is not directive
  2854. break;
  2855. }
  2856. directive = source.slice(token.start + 1, token.end - 1);
  2857. if (directive === 'use strict') {
  2858. strict = true;
  2859. if (firstRestricted) {
  2860. throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  2861. }
  2862. } else {
  2863. if (!firstRestricted && token.octal) {
  2864. firstRestricted = token;
  2865. }
  2866. }
  2867. }
  2868. oldLabelSet = state.labelSet;
  2869. oldInIteration = state.inIteration;
  2870. oldInSwitch = state.inSwitch;
  2871. oldInFunctionBody = state.inFunctionBody;
  2872. oldParenthesisCount = state.parenthesizedCount;
  2873. state.labelSet = {};
  2874. state.inIteration = false;
  2875. state.inSwitch = false;
  2876. state.inFunctionBody = true;
  2877. state.parenthesizedCount = 0;
  2878. while (index < length) {
  2879. if (match('}')) {
  2880. break;
  2881. }
  2882. sourceElement = parseSourceElement();
  2883. if (typeof sourceElement === 'undefined') {
  2884. break;
  2885. }
  2886. sourceElements.push(sourceElement);
  2887. }
  2888. expect('}');
  2889. state.labelSet = oldLabelSet;
  2890. state.inIteration = oldInIteration;
  2891. state.inSwitch = oldInSwitch;
  2892. state.inFunctionBody = oldInFunctionBody;
  2893. state.parenthesizedCount = oldParenthesisCount;
  2894. return node.finishBlockStatement(sourceElements);
  2895. }
  2896. function validateParam(options, param, name) {
  2897. var key = '$' + name;
  2898. if (strict) {
  2899. if (isRestrictedWord(name)) {
  2900. options.stricted = param;
  2901. options.message = Messages.StrictParamName;
  2902. }
  2903. if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) {
  2904. options.stricted = param;
  2905. options.message = Messages.StrictParamDupe;
  2906. }
  2907. } else if (!options.firstRestricted) {
  2908. if (isRestrictedWord(name)) {
  2909. options.firstRestricted = param;
  2910. options.message = Messages.StrictParamName;
  2911. } else if (isStrictModeReservedWord(name)) {
  2912. options.firstRestricted = param;
  2913. options.message = Messages.StrictReservedWord;
  2914. } else if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) {
  2915. options.firstRestricted = param;
  2916. options.message = Messages.StrictParamDupe;
  2917. }
  2918. }
  2919. options.paramSet[key] = true;
  2920. }
  2921. function parseParam(options) {
  2922. var token, param, def;
  2923. token = lookahead;
  2924. param = parseVariableIdentifier();
  2925. validateParam(options, token, token.value);
  2926. if (match('=')) {
  2927. lex();
  2928. def = parseAssignmentExpression();
  2929. ++options.defaultCount;
  2930. }
  2931. options.params.push(param);
  2932. options.defaults.push(def);
  2933. return !match(')');
  2934. }
  2935. function parseParams(firstRestricted) {
  2936. var options;
  2937. options = {
  2938. params: [],
  2939. defaultCount: 0,
  2940. defaults: [],
  2941. firstRestricted: firstRestricted
  2942. };
  2943. expect('(');
  2944. if (!match(')')) {
  2945. options.paramSet = {};
  2946. while (index < length) {
  2947. if (!parseParam(options)) {
  2948. break;
  2949. }
  2950. expect(',');
  2951. }
  2952. }
  2953. expect(')');
  2954. if (options.defaultCount === 0) {
  2955. options.defaults = [];
  2956. }
  2957. return {
  2958. params: options.params,
  2959. defaults: options.defaults,
  2960. stricted: options.stricted,
  2961. firstRestricted: options.firstRestricted,
  2962. message: options.message
  2963. };
  2964. }
  2965. function parseFunctionDeclaration() {
  2966. var id, params = [], defaults = [], body, token, stricted, tmp, firstRestricted, message, previousStrict, node = new Node();
  2967. expectKeyword('function');
  2968. token = lookahead;
  2969. id = parseVariableIdentifier();
  2970. if (strict) {
  2971. if (isRestrictedWord(token.value)) {
  2972. throwErrorTolerant(token, Messages.StrictFunctionName);
  2973. }
  2974. } else {
  2975. if (isRestrictedWord(token.value)) {
  2976. firstRestricted = token;
  2977. message = Messages.StrictFunctionName;
  2978. } else if (isStrictModeReservedWord(token.value)) {
  2979. firstRestricted = token;
  2980. message = Messages.StrictReservedWord;
  2981. }
  2982. }
  2983. tmp = parseParams(firstRestricted);
  2984. params = tmp.params;
  2985. defaults = tmp.defaults;
  2986. stricted = tmp.stricted;
  2987. firstRestricted = tmp.firstRestricted;
  2988. if (tmp.message) {
  2989. message = tmp.message;
  2990. }
  2991. previousStrict = strict;
  2992. body = parseFunctionSourceElements();
  2993. if (strict && firstRestricted) {
  2994. throwError(firstRestricted, message);
  2995. }
  2996. if (strict && stricted) {
  2997. throwErrorTolerant(stricted, message);
  2998. }
  2999. strict = previousStrict;
  3000. return node.finishFunctionDeclaration(id, params, defaults, body);
  3001. }
  3002. function parseFunctionExpression() {
  3003. var token, id = null, stricted, firstRestricted, message, tmp,
  3004. params = [], defaults = [], body, previousStrict, node = new Node();
  3005. expectKeyword('function');
  3006. if (!match('(')) {
  3007. token = lookahead;
  3008. id = parseVariableIdentifier();
  3009. if (strict) {
  3010. if (isRestrictedWord(token.value)) {
  3011. throwErrorTolerant(token, Messages.StrictFunctionName);
  3012. }
  3013. } else {
  3014. if (isRestrictedWord(token.value)) {
  3015. firstRestricted = token;
  3016. message = Messages.StrictFunctionName;
  3017. } else if (isStrictModeReservedWord(token.value)) {
  3018. firstRestricted = token;
  3019. message = Messages.StrictReservedWord;
  3020. }
  3021. }
  3022. }
  3023. tmp = parseParams(firstRestricted);
  3024. params = tmp.params;
  3025. defaults = tmp.defaults;
  3026. stricted = tmp.stricted;
  3027. firstRestricted = tmp.firstRestricted;
  3028. if (tmp.message) {
  3029. message = tmp.message;
  3030. }
  3031. previousStrict = strict;
  3032. body = parseFunctionSourceElements();
  3033. if (strict && firstRestricted) {
  3034. throwError(firstRestricted, message);
  3035. }
  3036. if (strict && stricted) {
  3037. throwErrorTolerant(stricted, message);
  3038. }
  3039. strict = previousStrict;
  3040. return node.finishFunctionExpression(id, params, defaults, body);
  3041. }
  3042. // 14 Program
  3043. function parseSourceElement() {
  3044. if (lookahead.type === Token.Keyword) {
  3045. switch (lookahead.value) {
  3046. case 'const':
  3047. case 'let':
  3048. return parseConstLetDeclaration(lookahead.value);
  3049. case 'function':
  3050. return parseFunctionDeclaration();
  3051. default:
  3052. return parseStatement();
  3053. }
  3054. }
  3055. if (lookahead.type !== Token.EOF) {
  3056. return parseStatement();
  3057. }
  3058. }
  3059. function parseSourceElements() {
  3060. var sourceElement, sourceElements = [], token, directive, firstRestricted;
  3061. while (index < length) {
  3062. token = lookahead;
  3063. if (token.type !== Token.StringLiteral) {
  3064. break;
  3065. }
  3066. sourceElement = parseSourceElement();
  3067. sourceElements.push(sourceElement);
  3068. if (sourceElement.expression.type !== Syntax.Literal) {
  3069. // this is not directive
  3070. break;
  3071. }
  3072. directive = source.slice(token.start + 1, token.end - 1);
  3073. if (directive === 'use strict') {
  3074. strict = true;
  3075. if (firstRestricted) {
  3076. throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  3077. }
  3078. } else {
  3079. if (!firstRestricted && token.octal) {
  3080. firstRestricted = token;
  3081. }
  3082. }
  3083. }
  3084. while (index < length) {
  3085. sourceElement = parseSourceElement();
  3086. /* istanbul ignore if */
  3087. if (typeof sourceElement === 'undefined') {
  3088. break;
  3089. }
  3090. sourceElements.push(sourceElement);
  3091. }
  3092. return sourceElements;
  3093. }
  3094. function parseProgram() {
  3095. var body, node;
  3096. skipComment();
  3097. peek();
  3098. node = new Node();
  3099. strict = false;
  3100. body = parseSourceElements();
  3101. return node.finishProgram(body);
  3102. }
  3103. function filterTokenLocation() {
  3104. var i, entry, token, tokens = [];
  3105. for (i = 0; i < extra.tokens.length; ++i) {
  3106. entry = extra.tokens[i];
  3107. token = {
  3108. type: entry.type,
  3109. value: entry.value
  3110. };
  3111. if (extra.range) {
  3112. token.range = entry.range;
  3113. }
  3114. if (extra.loc) {
  3115. token.loc = entry.loc;
  3116. }
  3117. tokens.push(token);
  3118. }
  3119. extra.tokens = tokens;
  3120. }
  3121. function tokenize(code, options) {
  3122. var toString,
  3123. tokens;
  3124. toString = String;
  3125. if (typeof code !== 'string' && !(code instanceof String)) {
  3126. code = toString(code);
  3127. }
  3128. source = code;
  3129. index = 0;
  3130. lineNumber = (source.length > 0) ? 1 : 0;
  3131. lineStart = 0;
  3132. length = source.length;
  3133. lookahead = null;
  3134. state = {
  3135. allowIn: true,
  3136. labelSet: {},
  3137. inFunctionBody: false,
  3138. inIteration: false,
  3139. inSwitch: false,
  3140. lastCommentStart: -1
  3141. };
  3142. extra = {};
  3143. // Options matching.
  3144. options = options || {};
  3145. // Of course we collect tokens here.
  3146. options.tokens = true;
  3147. extra.tokens = [];
  3148. extra.tokenize = true;
  3149. // The following two fields are necessary to compute the Regex tokens.
  3150. extra.openParenToken = -1;
  3151. extra.openCurlyToken = -1;
  3152. extra.range = (typeof options.range === 'boolean') && options.range;
  3153. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  3154. if (typeof options.comment === 'boolean' && options.comment) {
  3155. extra.comments = [];
  3156. }
  3157. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  3158. extra.errors = [];
  3159. }
  3160. try {
  3161. peek();
  3162. if (lookahead.type === Token.EOF) {
  3163. return extra.tokens;
  3164. }
  3165. lex();
  3166. while (lookahead.type !== Token.EOF) {
  3167. try {
  3168. lex();
  3169. } catch (lexError) {
  3170. if (extra.errors) {
  3171. extra.errors.push(lexError);
  3172. // We have to break on the first error
  3173. // to avoid infinite loops.
  3174. break;
  3175. } else {
  3176. throw lexError;
  3177. }
  3178. }
  3179. }
  3180. filterTokenLocation();
  3181. tokens = extra.tokens;
  3182. if (typeof extra.comments !== 'undefined') {
  3183. tokens.comments = extra.comments;
  3184. }
  3185. if (typeof extra.errors !== 'undefined') {
  3186. tokens.errors = extra.errors;
  3187. }
  3188. } catch (e) {
  3189. throw e;
  3190. } finally {
  3191. extra = {};
  3192. }
  3193. return tokens;
  3194. }
  3195. function parse(code, options) {
  3196. var program, toString;
  3197. toString = String;
  3198. if (typeof code !== 'string' && !(code instanceof String)) {
  3199. code = toString(code);
  3200. }
  3201. source = code;
  3202. index = 0;
  3203. lineNumber = (source.length > 0) ? 1 : 0;
  3204. lineStart = 0;
  3205. length = source.length;
  3206. lookahead = null;
  3207. state = {
  3208. allowIn: true,
  3209. labelSet: {},
  3210. parenthesisCount: 0,
  3211. inFunctionBody: false,
  3212. inIteration: false,
  3213. inSwitch: false,
  3214. lastCommentStart: -1
  3215. };
  3216. extra = {};
  3217. if (typeof options !== 'undefined') {
  3218. extra.range = (typeof options.range === 'boolean') && options.range;
  3219. extra.loc = (typeof options.loc === 'boolean') && options.loc;
  3220. extra.attachComment = (typeof options.attachComment === 'boolean') && options.attachComment;
  3221. if (extra.loc && options.source !== null && options.source !== undefined) {
  3222. extra.source = toString(options.source);
  3223. }
  3224. if (typeof options.tokens === 'boolean' && options.tokens) {
  3225. extra.tokens = [];
  3226. }
  3227. if (typeof options.comment === 'boolean' && options.comment) {
  3228. extra.comments = [];
  3229. }
  3230. if (typeof options.tolerant === 'boolean' && options.tolerant) {
  3231. extra.errors = [];
  3232. }
  3233. if (extra.attachComment) {
  3234. extra.range = true;
  3235. extra.comments = [];
  3236. extra.bottomRightStack = [];
  3237. extra.trailingComments = [];
  3238. extra.leadingComments = [];
  3239. }
  3240. }
  3241. try {
  3242. program = parseProgram();
  3243. if (typeof extra.comments !== 'undefined') {
  3244. program.comments = extra.comments;
  3245. }
  3246. if (typeof extra.tokens !== 'undefined') {
  3247. filterTokenLocation();
  3248. program.tokens = extra.tokens;
  3249. }
  3250. if (typeof extra.errors !== 'undefined') {
  3251. program.errors = extra.errors;
  3252. }
  3253. } catch (e) {
  3254. throw e;
  3255. } finally {
  3256. extra = {};
  3257. }
  3258. return program;
  3259. }
  3260. // Sync with *.json manifests.
  3261. exports.version = '2.0.0-dev';
  3262. exports.tokenize = tokenize;
  3263. exports.parse = parse;
  3264. // Deep copy.
  3265. /* istanbul ignore next */
  3266. exports.Syntax = (function () {
  3267. var name, types = {};
  3268. if (typeof Object.create === 'function') {
  3269. types = Object.create(null);
  3270. }
  3271. for (name in Syntax) {
  3272. if (Syntax.hasOwnProperty(name)) {
  3273. types[name] = Syntax[name];
  3274. }
  3275. }
  3276. if (typeof Object.freeze === 'function') {
  3277. Object.freeze(types);
  3278. }
  3279. return types;
  3280. }());
  3281. }));
  3282. /* vim: set sw=4 ts=4 et tw=80 : */