JavascriptParser.cs 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using Jint.Parser.Ast;
  7. namespace Jint.Parser
  8. {
  9. public class JavaScriptParser
  10. {
  11. private static readonly object[] Keywords = new object[]
  12. {
  13. "if", "in", "do", "var", "for", "new", "try", "let",
  14. "this", "else", "case", "void", "with", "enum",
  15. "while", "break", "catch", "throw", "const", "yield",
  16. "class", "super", "return", "typeof", "delete",
  17. "switch", "export", "import", "default", "finally", "extends",
  18. "function", "continue", "debugger", "instanceof"
  19. };
  20. private static readonly object[] StrictModeReservedWords = new object[] {
  21. "implements",
  22. "interface",
  23. "package",
  24. "private",
  25. "protected",
  26. "public",
  27. "static",
  28. "yield",
  29. "let"
  30. };
  31. private static readonly object[] FutureReservedWords = new object[]
  32. {
  33. "class",
  34. "enum",
  35. "export",
  36. "extends",
  37. "import",
  38. "super"
  39. };
  40. private Extra _extra;
  41. private int _index; // position in the stream
  42. private int _length; // length of the stream
  43. private int _lineNumber;
  44. private int _lineStart;
  45. private Location _location;
  46. private Token _lookahead;
  47. private string _source;
  48. private State _state;
  49. private bool _strict;
  50. private readonly Stack<IVariableScope> _variableScopes = new Stack<IVariableScope>();
  51. private readonly Stack<IFunctionScope> _functionScopes = new Stack<IFunctionScope>();
  52. private static bool IsDecimalDigit(char ch)
  53. {
  54. return (ch >= '0' && ch <= '9');
  55. }
  56. private static bool IsHexDigit(char ch)
  57. {
  58. return
  59. ch >= '0' && ch <= '9' ||
  60. ch >= 'a' && ch <= 'f' ||
  61. ch >= 'A' && ch <= 'F'
  62. ;
  63. }
  64. private static bool IsOctalDigit(char ch)
  65. {
  66. return ch >= '0' && ch <= '7';
  67. }
  68. // 7.2 White Space
  69. private static bool IsWhiteSpace(char ch)
  70. {
  71. return (ch == 32) || // space
  72. (ch == 9) || // tab
  73. (ch == 0xB) ||
  74. (ch == 0xC) ||
  75. (ch == 0xA0) ||
  76. (ch >= 0x1680 && (
  77. ch == 0x1680 ||
  78. ch == 0x180E ||
  79. (ch >= 0x2000 && ch <= 0x200A) ||
  80. ch == 0x202F ||
  81. ch == 0x205F ||
  82. ch == 0x3000 ||
  83. ch == 0xFEFF));
  84. }
  85. // 7.3 Line Terminators
  86. private static bool IsLineTerminator(char ch)
  87. {
  88. return (ch == 10) || (ch == 13) || (ch == 0x2028) || (ch == 0x2029);
  89. }
  90. // 7.6 Identifier Names and Identifiers
  91. private static bool IsIdentifierStart(char ch)
  92. {
  93. return (ch == '$') || (ch == '_') ||
  94. (ch >= 'A' && ch <= 'Z') ||
  95. (ch >= 'a' && ch <= 'z') ||
  96. (ch == '\\') ||
  97. ((ch >= 0x80) && Regexes.NonAsciiIdentifierStart.IsMatch(ch.ToString()));
  98. }
  99. private static bool IsIdentifierPart(char ch)
  100. {
  101. return (ch == '$') || (ch == '_') ||
  102. (ch >= 'A' && ch <= 'Z') ||
  103. (ch >= 'a' && ch <= 'z') ||
  104. (ch >= '0' && ch <= '9') ||
  105. (ch == '\\') ||
  106. ((ch >= 0x80) && Regexes.NonAsciiIdentifierPart.IsMatch(ch.ToString()));
  107. }
  108. // 7.6.1.2 Future Reserved Words
  109. private static bool IsFutureReservedWord(string id)
  110. {
  111. return Array.IndexOf(FutureReservedWords, id) >= 0;
  112. }
  113. private static bool IsStrictModeReservedWord(string id)
  114. {
  115. return Array.IndexOf(StrictModeReservedWords, id) >= 0;
  116. }
  117. private static bool IsRestrictedWord(string id)
  118. {
  119. return "eval".Equals(id) || "arguments".Equals(id);
  120. }
  121. // 7.6.1.1 Keywords
  122. private bool IsKeyword(string id)
  123. {
  124. if (_strict && IsStrictModeReservedWord(id))
  125. {
  126. return true;
  127. }
  128. // 'const' is specialized as Keyword in V8.
  129. // 'yield' and 'let' are for compatiblity with SpiderMonkey and ES.next.
  130. // Some others are from future reserved words.
  131. return Array.IndexOf(Keywords, id) >= 0;
  132. }
  133. // 7.4 Comments
  134. private void AddComment(string type, string value, int start, int end, Location location)
  135. {
  136. // Because the way the actual token is scanned, often the comments
  137. // (if any) are skipped twice during the lexical analysis.
  138. // Thus, we need to skip adding a comment if the comment array already
  139. // handled it.
  140. if (_state.LastCommentStart >= start)
  141. {
  142. return;
  143. }
  144. _state.LastCommentStart = start;
  145. var comment = new Comment
  146. {
  147. Type = type,
  148. Value = value
  149. };
  150. if (_extra.Range != null)
  151. {
  152. comment.Range = new[] {start, end};
  153. }
  154. if (_extra.Loc.HasValue)
  155. {
  156. comment.Location = location;
  157. }
  158. _extra.Comments.Add(comment);
  159. }
  160. private void SkipSingleLineComment()
  161. {
  162. //var start, loc, ch, comment;
  163. int start = _index - 2;
  164. _location = new Location
  165. {
  166. Start = new Position
  167. {
  168. Line = _lineNumber,
  169. Column = _index - _lineStart - 2
  170. }
  171. };
  172. while (_index < _length)
  173. {
  174. char ch = _source.CharCodeAt(_index);
  175. ++_index;
  176. if (IsLineTerminator(ch))
  177. {
  178. if (_extra.Comments != null)
  179. {
  180. var comment = _source.Slice(start + 2, _index - 1);
  181. _location.End = new Position
  182. {
  183. Line = _lineNumber,
  184. Column = _index - _lineStart - 1
  185. };
  186. AddComment("Line", comment, start, _index - 1, _location);
  187. }
  188. if (ch == 13 && _source.CharCodeAt(_index) == 10)
  189. {
  190. ++_index;
  191. }
  192. ++_lineNumber;
  193. _lineStart = _index;
  194. return;
  195. }
  196. }
  197. if (_extra.Comments != null)
  198. {
  199. var comment = _source.Slice(start + 2, _index);
  200. _location.End = new Position
  201. {
  202. Line = _lineNumber,
  203. Column = _index - _lineStart
  204. };
  205. AddComment("Line", comment, start, _index, _location);
  206. }
  207. }
  208. private void SkipMultiLineComment()
  209. {
  210. //var start, loc, ch, comment;
  211. int start = 0;
  212. if (_extra.Comments != null)
  213. {
  214. start = _index - 2;
  215. _location = new Location
  216. {
  217. Start = new Position
  218. {
  219. Line = _lineNumber,
  220. Column = _index - _lineStart - 2
  221. }
  222. };
  223. }
  224. while (_index < _length)
  225. {
  226. char ch = _source.CharCodeAt(_index);
  227. if (IsLineTerminator(ch))
  228. {
  229. if (ch == 13 && _source.CharCodeAt(_index + 1) == 10)
  230. {
  231. ++_index;
  232. }
  233. ++_lineNumber;
  234. ++_index;
  235. _lineStart = _index;
  236. if (_index >= _length)
  237. {
  238. throw new Exception(Messages.UnexpectedToken);
  239. }
  240. }
  241. else if (ch == 42)
  242. {
  243. // Block comment ends with '*/' (char #42, char #47).
  244. if (_source.CharCodeAt(_index + 1) == 47)
  245. {
  246. ++_index;
  247. ++_index;
  248. if (_extra.Comments != null)
  249. {
  250. string comment = _source.Slice(start + 2, _index - 2);
  251. _location.End = new Position
  252. {
  253. Line = _lineNumber,
  254. Column = _index - _lineStart
  255. };
  256. AddComment("Block", comment, start, _index, _location);
  257. }
  258. return;
  259. }
  260. ++_index;
  261. }
  262. else
  263. {
  264. ++_index;
  265. }
  266. }
  267. throw new Exception(Messages.UnexpectedToken);
  268. }
  269. private void SkipComment()
  270. {
  271. while (_index < _length)
  272. {
  273. char ch = _source.CharCodeAt(_index);
  274. if (IsWhiteSpace(ch))
  275. {
  276. ++_index;
  277. }
  278. else if (IsLineTerminator(ch))
  279. {
  280. ++_index;
  281. if (ch == 13 && _source.CharCodeAt(_index) == 10)
  282. {
  283. ++_index;
  284. }
  285. ++_lineNumber;
  286. _lineStart = _index;
  287. }
  288. else if (ch == 47)
  289. {
  290. // 47 is '/'
  291. ch = _source.CharCodeAt(_index + 1);
  292. if (ch == 47)
  293. {
  294. ++_index;
  295. ++_index;
  296. SkipSingleLineComment();
  297. }
  298. else if (ch == 42)
  299. {
  300. // 42 is '*'
  301. ++_index;
  302. ++_index;
  303. SkipMultiLineComment();
  304. }
  305. else
  306. {
  307. break;
  308. }
  309. }
  310. else
  311. {
  312. break;
  313. }
  314. }
  315. }
  316. private char ScanHexEscape(char prefix)
  317. {
  318. int code = char.MinValue;
  319. int len = (prefix == 'u') ? 4 : 2;
  320. for (int i = 0; i < len; ++i)
  321. {
  322. if (_index < _length && IsHexDigit(_source.CharCodeAt(_index)))
  323. {
  324. char ch = _source.CharCodeAt(_index++);
  325. code = code*16 +
  326. "0123456789abcdef".IndexOf(ch.ToString(),
  327. StringComparison.OrdinalIgnoreCase);
  328. }
  329. else
  330. {
  331. return char.MinValue;
  332. }
  333. }
  334. return (char) code;
  335. }
  336. private string GetEscapedIdentifier()
  337. {
  338. char ch = _source.CharCodeAt(_index++);
  339. string id = ch.ToString();
  340. // '\u' (char #92, char #117) denotes an escaped character.
  341. if (ch == 92)
  342. {
  343. if (_source.CharCodeAt(_index) != 117)
  344. {
  345. throw new Exception(Messages.UnexpectedToken);
  346. }
  347. ++_index;
  348. ch = ScanHexEscape('u');
  349. if (ch == char.MinValue || ch == '\\' || !IsIdentifierStart(ch))
  350. {
  351. throw new Exception(Messages.UnexpectedToken);
  352. }
  353. id = ch.ToString();
  354. }
  355. while (_index < _length)
  356. {
  357. ch = _source.CharCodeAt(_index);
  358. if (!IsIdentifierPart(ch))
  359. {
  360. break;
  361. }
  362. ++_index;
  363. id += ch.ToString();
  364. // '\u' (char #92, char #117) denotes an escaped character.
  365. if (ch == 92)
  366. {
  367. id = id.Substring(0, id.Length - 1);
  368. if (_source.CharCodeAt(_index) != 117)
  369. {
  370. throw new Exception(Messages.UnexpectedToken);
  371. }
  372. ++_index;
  373. ch = ScanHexEscape('u');
  374. if (ch == char.MinValue || ch == '\\' || !IsIdentifierPart(ch))
  375. {
  376. throw new Exception(Messages.UnexpectedToken);
  377. }
  378. id += ch.ToString();
  379. }
  380. }
  381. return id;
  382. }
  383. private string GetIdentifier()
  384. {
  385. int start = _index++;
  386. while (_index < _length)
  387. {
  388. char ch = _source.CharCodeAt(_index);
  389. if (ch == 92)
  390. {
  391. // Blackslash (char #92) marks Unicode escape sequence.
  392. _index = start;
  393. return GetEscapedIdentifier();
  394. }
  395. if (IsIdentifierPart(ch))
  396. {
  397. ++_index;
  398. }
  399. else
  400. {
  401. break;
  402. }
  403. }
  404. return _source.Slice(start, _index);
  405. }
  406. private Token ScanIdentifier()
  407. {
  408. int start = _index;
  409. Tokens type;
  410. // Backslash (char #92) starts an escaped character.
  411. string id = (_source.CharCodeAt(_index) == 92) ? GetEscapedIdentifier() : GetIdentifier();
  412. // There is no keyword or literal with only one character.
  413. // Thus, it must be an identifier.
  414. if (id.Length == 1)
  415. {
  416. type = Tokens.Identifier;
  417. }
  418. else if (IsKeyword(id))
  419. {
  420. type = Tokens.Keyword;
  421. }
  422. else if ("null".Equals(id))
  423. {
  424. type = Tokens.NullLiteral;
  425. }
  426. else if ("true".Equals(id) || "false".Equals(id))
  427. {
  428. type = Tokens.BooleanLiteral;
  429. }
  430. else
  431. {
  432. type = Tokens.Identifier;
  433. }
  434. return new Token
  435. {
  436. Type = type,
  437. Value = id,
  438. LineNumber = _lineNumber,
  439. LineStart = _lineStart,
  440. Range = new[] {start, _index}
  441. };
  442. }
  443. // 7.7 Punctuators
  444. private Token ScanPunctuator()
  445. {
  446. int start = _index;
  447. char code = _source.CharCodeAt(_index);
  448. char ch1 = _source.CharCodeAt(_index);
  449. switch ((int) code)
  450. {
  451. // Check for most common single-character punctuators.
  452. case 46: // . dot
  453. case 40: // ( open bracket
  454. case 41: // ) close bracket
  455. case 59: // ; semicolon
  456. case 44: // , comma
  457. case 123: // { open curly brace
  458. case 125: // } close curly brace
  459. case 91: // [
  460. case 93: // ]
  461. case 58: // :
  462. case 63: // ?
  463. case 126: // ~
  464. ++_index;
  465. return new Token
  466. {
  467. Type = Tokens.Punctuator,
  468. Value = code.ToString(),
  469. LineNumber = _lineNumber,
  470. LineStart = _lineStart,
  471. Range = new[] {start, _index}
  472. };
  473. default:
  474. char code2 = _source.CharCodeAt(_index + 1);
  475. // '=' (char #61) marks an assignment or comparison operator.
  476. if (code2 == 61)
  477. {
  478. switch ((int) code)
  479. {
  480. case 37: // %
  481. case 38: // &
  482. case 42: // *:
  483. case 43: // +
  484. case 45: // -
  485. case 47: // /
  486. case 60: // <
  487. case 62: // >
  488. case 94: // ^
  489. case 124: // |
  490. _index += 2;
  491. return new Token
  492. {
  493. Type = Tokens.Punctuator,
  494. Value =
  495. code.ToString() +
  496. code2.ToString(),
  497. LineNumber = _lineNumber,
  498. LineStart = _lineStart,
  499. Range = new[] {start, _index}
  500. };
  501. case 33: // !
  502. case 61: // =
  503. _index += 2;
  504. // !== and ===
  505. if (_source.CharCodeAt(_index) == 61)
  506. {
  507. ++_index;
  508. }
  509. return new Token
  510. {
  511. Type = Tokens.Punctuator,
  512. Value = _source.Slice(start, _index),
  513. LineNumber = _lineNumber,
  514. LineStart = _lineStart,
  515. Range = new[] {start, _index}
  516. };
  517. }
  518. }
  519. break;
  520. }
  521. // Peek more characters.
  522. char ch2 = _source.CharCodeAt(_index + 1);
  523. char ch3 = _source.CharCodeAt(_index + 2);
  524. char ch4 = _source.CharCodeAt(_index + 3);
  525. // 4-character punctuator: >>>=
  526. if (ch1 == '>' && ch2 == '>' && ch3 == '>')
  527. {
  528. if (ch4 == '=')
  529. {
  530. _index += 4;
  531. return new Token
  532. {
  533. Type = Tokens.Punctuator,
  534. Value = ">>>=",
  535. LineNumber = _lineNumber,
  536. LineStart = _lineStart,
  537. Range = new[] {start, _index}
  538. };
  539. }
  540. }
  541. // 3-character punctuators: == !== >>> <<= >>=
  542. if (ch1 == '>' && ch2 == '>' && ch3 == '>')
  543. {
  544. _index += 3;
  545. return new Token
  546. {
  547. Type = Tokens.Punctuator,
  548. Value = ">>>",
  549. LineNumber = _lineNumber,
  550. LineStart = _lineStart,
  551. Range = new[] {start, _index}
  552. };
  553. }
  554. if (ch1 == '<' && ch2 == '<' && ch3 == '=')
  555. {
  556. _index += 3;
  557. return new Token
  558. {
  559. Type = Tokens.Punctuator,
  560. Value = "<<=",
  561. LineNumber = _lineNumber,
  562. LineStart = _lineStart,
  563. Range = new[] {start, _index}
  564. };
  565. }
  566. if (ch1 == '>' && ch2 == '>' && ch3 == '=')
  567. {
  568. _index += 3;
  569. return new Token
  570. {
  571. Type = Tokens.Punctuator,
  572. Value = ">>=",
  573. LineNumber = _lineNumber,
  574. LineStart = _lineStart,
  575. Range = new[] {start, _index}
  576. };
  577. }
  578. // Other 2-character punctuators: ++ -- << >> && ||
  579. if (ch1 == ch2 && ("+-<>&|".IndexOf(ch1) >= 0))
  580. {
  581. _index += 2;
  582. return new Token
  583. {
  584. Type = Tokens.Punctuator,
  585. Value = ch1.ToString() + ch2.ToString(),
  586. LineNumber = _lineNumber,
  587. LineStart = _lineStart,
  588. Range = new[] {start, _index}
  589. };
  590. }
  591. if ("<>=!+-*%&|^/".IndexOf(ch1) >= 0)
  592. {
  593. ++_index;
  594. return new Token
  595. {
  596. Type = Tokens.Punctuator,
  597. Value = ch1.ToString(),
  598. LineNumber = _lineNumber,
  599. LineStart = _lineStart,
  600. Range = new[] {start, _index}
  601. };
  602. }
  603. throw new Exception(Messages.UnexpectedToken);
  604. }
  605. // 7.8.3 Numeric Literals
  606. private Token ScanHexLiteral(int start)
  607. {
  608. string number = "";
  609. while (_index < _length)
  610. {
  611. if (!IsHexDigit(_source.CharCodeAt(_index)))
  612. {
  613. break;
  614. }
  615. number += _source.CharCodeAt(_index++).ToString();
  616. }
  617. if (number.Length == 0)
  618. {
  619. throw new Exception(Messages.UnexpectedToken);
  620. }
  621. if (IsIdentifierStart(_source.CharCodeAt(_index)))
  622. {
  623. throw new Exception(Messages.UnexpectedToken);
  624. }
  625. return new Token
  626. {
  627. Type = Tokens.NumericLiteral,
  628. Value = Convert.ToInt32(number, 16),
  629. LineNumber = _lineNumber,
  630. LineStart = _lineStart,
  631. Range = new[] {start, _index}
  632. };
  633. }
  634. private Token ScanOctalLiteral(int start)
  635. {
  636. string number = "0" + _source.CharCodeAt(_index++);
  637. while (_index < _length)
  638. {
  639. if (!IsOctalDigit(_source.CharCodeAt(_index)))
  640. {
  641. break;
  642. }
  643. number += _source.CharCodeAt(_index++).ToString();
  644. }
  645. if (IsIdentifierStart(_source.CharCodeAt(_index)) || IsDecimalDigit(_source.CharCodeAt(_index)))
  646. {
  647. throw new Exception(Messages.UnexpectedToken);
  648. }
  649. return new Token
  650. {
  651. Type = Tokens.NumericLiteral,
  652. Value = Convert.ToInt32(number, 8),
  653. Octal = true,
  654. LineNumber = _lineNumber,
  655. LineStart = _lineStart,
  656. Range = new[] {start, _index}
  657. };
  658. }
  659. private Token ScanNumericLiteral()
  660. {
  661. char ch = _source.CharCodeAt(_index);
  662. int start = _index;
  663. string number = "";
  664. if (ch != '.')
  665. {
  666. number = _source.CharCodeAt(_index++).ToString();
  667. ch = _source.CharCodeAt(_index);
  668. // Hex number starts with '0x'.
  669. // Octal number starts with '0'.
  670. if (number == "0")
  671. {
  672. if (ch == 'x' || ch == 'X')
  673. {
  674. ++_index;
  675. return ScanHexLiteral(start);
  676. }
  677. if (IsOctalDigit(ch))
  678. {
  679. return ScanOctalLiteral(start);
  680. }
  681. // decimal number starts with '0' such as '09' is illegal.
  682. if (ch > 0 && IsDecimalDigit(ch))
  683. {
  684. throw new Exception(Messages.UnexpectedToken);
  685. }
  686. }
  687. while (IsDecimalDigit(_source.CharCodeAt(_index)))
  688. {
  689. number += _source.CharCodeAt(_index++).ToString();
  690. }
  691. ch = _source.CharCodeAt(_index);
  692. }
  693. if (ch == '.')
  694. {
  695. number += _source.CharCodeAt(_index++).ToString();
  696. while (IsDecimalDigit(_source.CharCodeAt(_index)))
  697. {
  698. number += _source.CharCodeAt(_index++).ToString();
  699. }
  700. ch = _source.CharCodeAt(_index);
  701. }
  702. if (ch == 'e' || ch == 'E')
  703. {
  704. number += _source.CharCodeAt(_index++).ToString();
  705. ch = _source.CharCodeAt(_index);
  706. if (ch == '+' || ch == '-')
  707. {
  708. number += _source.CharCodeAt(_index++).ToString();
  709. }
  710. if (IsDecimalDigit(_source.CharCodeAt(_index)))
  711. {
  712. while (IsDecimalDigit(_source.CharCodeAt(_index)))
  713. {
  714. number += _source.CharCodeAt(_index++).ToString();
  715. }
  716. }
  717. else
  718. {
  719. throw new Exception(Messages.UnexpectedToken);
  720. }
  721. }
  722. if (IsIdentifierStart(_source.CharCodeAt(_index)))
  723. {
  724. throw new Exception(Messages.UnexpectedToken);
  725. }
  726. return new Token
  727. {
  728. Type = Tokens.NumericLiteral,
  729. Value = Double.Parse(number, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent),
  730. LineNumber = _lineNumber,
  731. LineStart = _lineStart,
  732. Range = new[] {start, _index}
  733. };
  734. }
  735. // 7.8.4 String Literals
  736. private Token ScanStringLiteral()
  737. {
  738. string str = "";
  739. bool octal = false;
  740. char quote = _source.CharCodeAt(_index);
  741. int start = _index;
  742. ++_index;
  743. while (_index < _length)
  744. {
  745. char ch = _source.CharCodeAt(_index++);
  746. if (ch == quote)
  747. {
  748. quote = char.MinValue;
  749. break;
  750. }
  751. if (ch == '\\')
  752. {
  753. ch = _source.CharCodeAt(_index++);
  754. if (ch > 0 || !IsLineTerminator(ch))
  755. {
  756. switch (ch)
  757. {
  758. case 'n':
  759. str += '\n';
  760. break;
  761. case 'r':
  762. str += '\r';
  763. break;
  764. case 't':
  765. str += '\t';
  766. break;
  767. case 'u':
  768. case 'x':
  769. int restore = _index;
  770. char unescaped = ScanHexEscape(ch);
  771. if (unescaped > 0)
  772. {
  773. str += unescaped.ToString();
  774. }
  775. else
  776. {
  777. _index = restore;
  778. str += ch.ToString();
  779. }
  780. break;
  781. case 'b':
  782. str += "\b";
  783. break;
  784. case 'f':
  785. str += "\f";
  786. break;
  787. case 'v':
  788. str += "\x0B";
  789. break;
  790. default:
  791. if (IsOctalDigit(ch))
  792. {
  793. int code = "01234567".IndexOf(ch);
  794. // \0 is not octal escape sequence
  795. if (code != 0)
  796. {
  797. octal = true;
  798. }
  799. if (_index < _length && IsOctalDigit(_source.CharCodeAt(_index)))
  800. {
  801. octal = true;
  802. code = code * 8 + "01234567".IndexOf(_source.CharCodeAt(_index++));
  803. // 3 digits are only allowed when string starts
  804. // with 0, 1, 2, 3
  805. if ("0123".IndexOf(ch) >= 0 &&
  806. _index < _length &&
  807. IsOctalDigit(_source.CharCodeAt(_index)))
  808. {
  809. code = code * 8 + "01234567".IndexOf(_source.CharCodeAt(_index++));
  810. }
  811. }
  812. str += ((char)code).ToString();
  813. }
  814. else
  815. {
  816. str += ch.ToString();
  817. }
  818. break;
  819. }
  820. }
  821. else
  822. {
  823. ++_lineNumber;
  824. if (ch == '\r' && _source.CharCodeAt(_index) == '\n')
  825. {
  826. ++_index;
  827. }
  828. }
  829. }
  830. else if (IsLineTerminator(ch))
  831. {
  832. break;
  833. }
  834. else
  835. {
  836. str += ch.ToString();
  837. }
  838. }
  839. if (quote != 0)
  840. {
  841. throw new Exception(Messages.UnexpectedToken);
  842. }
  843. return new Token
  844. {
  845. Type = Tokens.StringLiteral,
  846. Value = str,
  847. Octal = octal,
  848. LineNumber = _lineNumber,
  849. LineStart = _lineStart,
  850. Range = new[] {start, _index}
  851. };
  852. }
  853. private Token ScanRegExp()
  854. {
  855. Regex value;
  856. bool classMarker = false;
  857. bool terminated = false;
  858. SkipComment();
  859. int start = _index;
  860. char ch;
  861. string str = _source.CharCodeAt(_index++).ToString();
  862. while (_index < _length)
  863. {
  864. ch = _source.CharCodeAt(_index++);
  865. str += ch.ToString();
  866. if (classMarker)
  867. {
  868. if (ch == ']')
  869. {
  870. classMarker = false;
  871. }
  872. }
  873. else
  874. {
  875. if (ch == '\\')
  876. {
  877. ch = _source.CharCodeAt(_index++);
  878. // ECMA-262 7.8.5
  879. if (IsLineTerminator(ch))
  880. {
  881. throw new Exception(Messages.UnterminatedRegExp);
  882. }
  883. str += ch.ToString();
  884. }
  885. else if (ch == '/')
  886. {
  887. terminated = true;
  888. break;
  889. }
  890. else if (ch == '[')
  891. {
  892. classMarker = true;
  893. }
  894. else if (IsLineTerminator(ch))
  895. {
  896. throw new Exception(Messages.UnterminatedRegExp);
  897. }
  898. }
  899. }
  900. if (!terminated)
  901. {
  902. throw new Exception(Messages.UnterminatedRegExp);
  903. }
  904. // Exclude leading and trailing slash.
  905. string pattern = str.Substring(1, str.Length - 2);
  906. string flags = "";
  907. while (_index < _length)
  908. {
  909. ch = _source.CharCodeAt(_index);
  910. if (!IsIdentifierPart(ch))
  911. {
  912. break;
  913. }
  914. ++_index;
  915. if (ch == '\\' && _index < _length)
  916. {
  917. ch = _source.CharCodeAt(_index);
  918. if (ch == 'u')
  919. {
  920. ++_index;
  921. int restore = _index;
  922. ch = ScanHexEscape('u');
  923. if (ch > 0)
  924. {
  925. flags += ch.ToString();
  926. for (str += "\\u"; restore < _index; ++restore)
  927. {
  928. str += _source.CharCodeAt(restore).ToString();
  929. }
  930. }
  931. else
  932. {
  933. _index = restore;
  934. flags += "u";
  935. str += "\\u";
  936. }
  937. }
  938. else
  939. {
  940. str += "\\";
  941. }
  942. }
  943. else
  944. {
  945. flags += ch.ToString();
  946. str += ch.ToString();
  947. }
  948. }
  949. try
  950. {
  951. var options = RegexOptions.ECMAScript;
  952. if (flags.Contains("g"))
  953. {
  954. // todo: implement
  955. }
  956. if (flags.Contains("i"))
  957. {
  958. options |= RegexOptions.IgnoreCase;
  959. }
  960. if (flags.Contains("m"))
  961. {
  962. options |= RegexOptions.Multiline;
  963. }
  964. value = new Regex(pattern, options);
  965. }
  966. catch (Exception e)
  967. {
  968. throw new Exception(Messages.InvalidRegExp, e);
  969. }
  970. Peek();
  971. return new Token
  972. {
  973. Literal = str,
  974. Value = value,
  975. Range = new[] {start, _index}
  976. };
  977. }
  978. private Token CollectRegex()
  979. {
  980. SkipComment();
  981. int pos = _index;
  982. var loc = new Location
  983. {
  984. Start = new Position
  985. {
  986. Line = _lineNumber,
  987. Column = _index - _lineStart
  988. }
  989. };
  990. Token regex = ScanRegExp();
  991. loc.End = new Position
  992. {
  993. Line = _lineNumber,
  994. Column = _index - _lineStart
  995. };
  996. // Pop the previous token, which is likely '/' or '/='
  997. if (_extra.Tokens != null)
  998. {
  999. Token token = _extra.Tokens[_extra.Tokens.Count - 1];
  1000. if (token.Range[0] == pos && token.Type == Tokens.Punctuator)
  1001. {
  1002. if ("/".Equals(token.Value) || "/=".Equals(token.Value))
  1003. {
  1004. _extra.Tokens.RemoveAt(_extra.Tokens.Count - 1);
  1005. }
  1006. }
  1007. _extra.Tokens.Add(new Token
  1008. {
  1009. Type = Tokens.RegularExpression,
  1010. Value = regex.Literal,
  1011. Range = new[] { pos, _index },
  1012. Location = loc
  1013. });
  1014. }
  1015. return regex;
  1016. }
  1017. private bool IsIdentifierName(Token token)
  1018. {
  1019. return token.Type == Tokens.Identifier ||
  1020. token.Type == Tokens.Keyword ||
  1021. token.Type == Tokens.BooleanLiteral ||
  1022. token.Type == Tokens.NullLiteral;
  1023. }
  1024. private Token Advance()
  1025. {
  1026. SkipComment();
  1027. if (_index >= _length)
  1028. {
  1029. return new Token
  1030. {
  1031. Type = Tokens.EOF,
  1032. LineNumber = _lineNumber,
  1033. LineStart = _lineStart,
  1034. Range = new[] {_index, _index}
  1035. };
  1036. }
  1037. char ch = _source.CharCodeAt(_index);
  1038. // Very common: ( and ) and ;
  1039. if (ch == 40 || ch == 41 || ch == 58)
  1040. {
  1041. return ScanPunctuator();
  1042. }
  1043. // String literal starts with single quote (#39) or double quote (#34).
  1044. if (ch == 39 || ch == 34)
  1045. {
  1046. return ScanStringLiteral();
  1047. }
  1048. if (IsIdentifierStart(ch))
  1049. {
  1050. return ScanIdentifier();
  1051. }
  1052. // Dot (.) char #46 can also start a floating-point number, hence the need
  1053. // to check the next character.
  1054. if (ch == 46)
  1055. {
  1056. if (IsDecimalDigit(_source.CharCodeAt(_index + 1)))
  1057. {
  1058. return ScanNumericLiteral();
  1059. }
  1060. return ScanPunctuator();
  1061. }
  1062. if (IsDecimalDigit(ch))
  1063. {
  1064. return ScanNumericLiteral();
  1065. }
  1066. return ScanPunctuator();
  1067. }
  1068. private Token CollectToken()
  1069. {
  1070. SkipComment();
  1071. _location = new Location
  1072. {
  1073. Start = new Position
  1074. {
  1075. Line = _lineNumber,
  1076. Column = _index - _lineStart
  1077. }
  1078. };
  1079. Token token = Advance();
  1080. _location.End = new Position
  1081. {
  1082. Line = _lineNumber,
  1083. Column = _index - _lineStart
  1084. };
  1085. if (token.Type != Tokens.EOF)
  1086. {
  1087. var range = new[] {token.Range[0], token.Range[1]};
  1088. string value = _source.Slice(token.Range[0], token.Range[1]);
  1089. _extra.Tokens.Add(new Token
  1090. {
  1091. Type = token.Type,
  1092. Value = value,
  1093. Range = range,
  1094. Location = _location
  1095. });
  1096. }
  1097. return token;
  1098. }
  1099. private Token Lex()
  1100. {
  1101. Token token = _lookahead;
  1102. _index = token.Range[1];
  1103. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  1104. _lineStart = token.LineStart;
  1105. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  1106. _index = token.Range[1];
  1107. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  1108. _lineStart = token.LineStart;
  1109. return token;
  1110. }
  1111. private void Peek()
  1112. {
  1113. int pos = _index;
  1114. int line = _lineNumber;
  1115. int start = _lineStart;
  1116. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  1117. _index = pos;
  1118. _lineNumber = line;
  1119. _lineStart = start;
  1120. }
  1121. private void MarkStart()
  1122. {
  1123. if (_extra.Loc.HasValue)
  1124. {
  1125. _state.MarkerStack.Push(_index - _lineStart);
  1126. _state.MarkerStack.Push(_lineNumber);
  1127. }
  1128. if (_extra.Range != null)
  1129. {
  1130. _state.MarkerStack.Push(_index);
  1131. }
  1132. }
  1133. private T MarkEnd<T>(T node) where T : SyntaxNode
  1134. {
  1135. if (_extra.Range != null)
  1136. {
  1137. node.Range = new[] {_state.MarkerStack.Pop(), _index};
  1138. }
  1139. if (_extra.Loc.HasValue)
  1140. {
  1141. node.Location = new Location
  1142. {
  1143. Start = new Position
  1144. {
  1145. Line = _state.MarkerStack.Pop(),
  1146. Column = _state.MarkerStack.Pop()
  1147. },
  1148. End = new Position
  1149. {
  1150. Line = _lineNumber,
  1151. Column = _index - _lineStart
  1152. }
  1153. };
  1154. PostProcess(node);
  1155. }
  1156. return node;
  1157. }
  1158. public T MarkEndIf<T>(T node) where T : SyntaxNode
  1159. {
  1160. if (node.Range != null || node.Location != null)
  1161. {
  1162. if (_extra.Loc.HasValue)
  1163. {
  1164. _state.MarkerStack.Pop();
  1165. _state.MarkerStack.Pop();
  1166. }
  1167. if (_extra.Range != null)
  1168. {
  1169. _state.MarkerStack.Pop();
  1170. }
  1171. }
  1172. else
  1173. {
  1174. MarkEnd(node);
  1175. }
  1176. return node;
  1177. }
  1178. public SyntaxNode PostProcess(SyntaxNode node)
  1179. {
  1180. if (_extra.Source != null)
  1181. {
  1182. node.Location.Source = _extra.Source;
  1183. }
  1184. return node;
  1185. }
  1186. public ArrayExpression CreateArrayExpression(IEnumerable<Expression> elements)
  1187. {
  1188. return new ArrayExpression
  1189. {
  1190. Type = SyntaxNodes.ArrayExpression,
  1191. Elements = elements
  1192. };
  1193. }
  1194. public AssignmentExpression CreateAssignmentExpression(string op, Expression left, Expression right)
  1195. {
  1196. return new AssignmentExpression
  1197. {
  1198. Type = SyntaxNodes.AssignmentExpression,
  1199. Operator = op,
  1200. Left = left,
  1201. Right = right
  1202. };
  1203. }
  1204. public BinaryExpression CreateBinaryExpression(string op, Expression left, Expression right)
  1205. {
  1206. return (op == "||" || op == "&&")
  1207. ? new LogicalExpression
  1208. {
  1209. Type = SyntaxNodes.LogicalExpression,
  1210. Operator = op,
  1211. Left = left,
  1212. Right = right
  1213. }
  1214. : new BinaryExpression
  1215. {
  1216. Type = SyntaxNodes.BinaryExpression,
  1217. Operator = op,
  1218. Left = left,
  1219. Right = right
  1220. };
  1221. }
  1222. public BlockStatement CreateBlockStatement(IEnumerable<Statement> body)
  1223. {
  1224. return new BlockStatement
  1225. {
  1226. Type = SyntaxNodes.BlockStatement,
  1227. Body = body
  1228. };
  1229. }
  1230. public BreakStatement CreateBreakStatement(Identifier label)
  1231. {
  1232. return new BreakStatement
  1233. {
  1234. Type = SyntaxNodes.BreakStatement,
  1235. Label = label
  1236. };
  1237. }
  1238. public CallExpression CreateCallExpression(Expression callee, IEnumerable<Expression> args)
  1239. {
  1240. return new CallExpression
  1241. {
  1242. Type = SyntaxNodes.CallExpression,
  1243. Callee = callee,
  1244. Arguments = args
  1245. };
  1246. }
  1247. public CatchClause CreateCatchClause(Identifier param, BlockStatement body)
  1248. {
  1249. return new CatchClause
  1250. {
  1251. Type = SyntaxNodes.CatchClause,
  1252. Param = param,
  1253. Body = body
  1254. };
  1255. }
  1256. public ConditionalExpression CreateConditionalExpression(Expression test, Expression consequent,
  1257. Expression alternate)
  1258. {
  1259. return new ConditionalExpression
  1260. {
  1261. Type = SyntaxNodes.ConditionalExpression,
  1262. Test = test,
  1263. Consequent = consequent,
  1264. Alternate = alternate
  1265. };
  1266. }
  1267. public ContinueStatement CreateContinueStatement(Identifier label)
  1268. {
  1269. return new ContinueStatement
  1270. {
  1271. Type = SyntaxNodes.ContinueStatement,
  1272. Label = label
  1273. };
  1274. }
  1275. public DebuggerStatement CreateDebuggerStatement()
  1276. {
  1277. return new DebuggerStatement
  1278. {
  1279. Type = SyntaxNodes.DebuggerStatement
  1280. };
  1281. }
  1282. public DoWhileStatement CreateDoWhileStatement(Statement body, Expression test)
  1283. {
  1284. return new DoWhileStatement
  1285. {
  1286. Type = SyntaxNodes.DoWhileStatement,
  1287. Body = body,
  1288. Test = test
  1289. };
  1290. }
  1291. public EmptyStatement CreateEmptyStatement()
  1292. {
  1293. return new EmptyStatement
  1294. {
  1295. Type = SyntaxNodes.EmptyStatement
  1296. };
  1297. }
  1298. public ExpressionStatement CreateExpressionStatement(Expression expression)
  1299. {
  1300. return new ExpressionStatement
  1301. {
  1302. Type = SyntaxNodes.ExpressionStatement,
  1303. Expression = expression
  1304. };
  1305. }
  1306. public ForStatement CreateForStatement(SyntaxNode init, Expression test, Expression update, Statement body)
  1307. {
  1308. return new ForStatement
  1309. {
  1310. Type = SyntaxNodes.ForStatement,
  1311. Init = init,
  1312. Test = test,
  1313. Update = update,
  1314. Body = body
  1315. };
  1316. }
  1317. public ForInStatement CreateForInStatement(SyntaxNode left, Expression right, Statement body)
  1318. {
  1319. return new ForInStatement
  1320. {
  1321. Type = SyntaxNodes.ForInStatement,
  1322. Left = left,
  1323. Right = right,
  1324. Body = body,
  1325. Each = false
  1326. };
  1327. }
  1328. public FunctionDeclaration CreateFunctionDeclaration(Identifier id, IEnumerable<Identifier> parameters,
  1329. IEnumerable<Expression> defaults, Statement body, bool strict)
  1330. {
  1331. var functionDeclaration = new FunctionDeclaration
  1332. {
  1333. Type = SyntaxNodes.FunctionDeclaration,
  1334. Id = id,
  1335. Parameters = parameters,
  1336. Defaults = defaults,
  1337. Body = body,
  1338. Strict = strict,
  1339. Rest = null,
  1340. Generator = false,
  1341. Expression = false,
  1342. VariableDeclarations = LeaveVariableScope(),
  1343. FunctionDeclarations = LeaveFunctionScope()
  1344. };
  1345. _functionScopes.Peek().FunctionDeclarations.Add(functionDeclaration);
  1346. return functionDeclaration;
  1347. }
  1348. public FunctionExpression CreateFunctionExpression(Identifier id, IEnumerable<Identifier> parameters,
  1349. IEnumerable<Expression> defaults, Statement body, bool strict)
  1350. {
  1351. return new FunctionExpression
  1352. {
  1353. Type = SyntaxNodes.FunctionExpression,
  1354. Id = id,
  1355. Parameters = parameters,
  1356. Defaults = defaults,
  1357. Body = body,
  1358. Strict = strict,
  1359. Rest = null,
  1360. Generator = false,
  1361. Expression = false,
  1362. VariableDeclarations = LeaveVariableScope(),
  1363. FunctionDeclarations = LeaveFunctionScope()
  1364. };
  1365. }
  1366. public Identifier CreateIdentifier(string name)
  1367. {
  1368. return new Identifier
  1369. {
  1370. Type = SyntaxNodes.Identifier,
  1371. Name = name
  1372. };
  1373. }
  1374. public IfStatement CreateIfStatement(Expression test, Statement consequent, Statement alternate)
  1375. {
  1376. return new IfStatement
  1377. {
  1378. Type = SyntaxNodes.IfStatement,
  1379. Test = test,
  1380. Consequent = consequent,
  1381. Alternate = alternate
  1382. };
  1383. }
  1384. public LabeledStatement CreateLabeledStatement(Identifier label, Statement body)
  1385. {
  1386. return new LabeledStatement
  1387. {
  1388. Type = SyntaxNodes.LabeledStatement,
  1389. Label = label,
  1390. Body = body
  1391. };
  1392. }
  1393. public Literal CreateLiteral(Token token)
  1394. {
  1395. return new Literal
  1396. {
  1397. Type = SyntaxNodes.Literal,
  1398. Value = token.Value,
  1399. Raw = _source.Slice(token.Range[0], token.Range[1])
  1400. };
  1401. }
  1402. public MemberExpression CreateMemberExpression(char accessor, Expression obj, Expression property)
  1403. {
  1404. return new MemberExpression
  1405. {
  1406. Type = SyntaxNodes.MemberExpression,
  1407. Computed = accessor == '[',
  1408. Object = obj,
  1409. Property = property
  1410. };
  1411. }
  1412. public NewExpression CreateNewExpression(Expression callee, IEnumerable<Expression> args)
  1413. {
  1414. return new NewExpression
  1415. {
  1416. Type = SyntaxNodes.NewExpression,
  1417. Callee = callee,
  1418. Arguments = args
  1419. };
  1420. }
  1421. public ObjectExpression CreateObjectExpression(IEnumerable<Property> properties)
  1422. {
  1423. return new ObjectExpression
  1424. {
  1425. Type = SyntaxNodes.ObjectExpression,
  1426. Properties = properties
  1427. };
  1428. }
  1429. public UpdateExpression CreatePostfixExpression(string op, Expression argument)
  1430. {
  1431. return new UpdateExpression
  1432. {
  1433. Type = SyntaxNodes.UpdateExpression,
  1434. Operator = op,
  1435. Argument = argument,
  1436. Prefix = false
  1437. };
  1438. }
  1439. public Program CreateProgram(ICollection<Statement> body, bool strict)
  1440. {
  1441. return new Program
  1442. {
  1443. Type = SyntaxNodes.Program,
  1444. Body = body,
  1445. Strict = strict,
  1446. VariableDeclarations = LeaveVariableScope(),
  1447. FunctionDeclarations = LeaveFunctionScope()
  1448. };
  1449. }
  1450. public Property CreateProperty(PropertyKind kind, IPropertyKeyExpression key, Expression value)
  1451. {
  1452. return new Property
  1453. {
  1454. Type = SyntaxNodes.Property,
  1455. Key = key,
  1456. Value = value,
  1457. Kind = kind
  1458. };
  1459. }
  1460. public ReturnStatement CreateReturnStatement(Expression argument)
  1461. {
  1462. return new ReturnStatement
  1463. {
  1464. Type = SyntaxNodes.ReturnStatement,
  1465. Argument = argument
  1466. };
  1467. }
  1468. public SequenceExpression CreateSequenceExpression(IList<Expression> expressions)
  1469. {
  1470. return new SequenceExpression
  1471. {
  1472. Type = SyntaxNodes.SequenceExpression,
  1473. Expressions = expressions
  1474. };
  1475. }
  1476. public SwitchCase CreateSwitchCase(Expression test, IEnumerable<Statement> consequent)
  1477. {
  1478. return new SwitchCase
  1479. {
  1480. Type = SyntaxNodes.SwitchCase,
  1481. Test = test,
  1482. Consequent = consequent
  1483. };
  1484. }
  1485. public SwitchStatement CreateSwitchStatement(Expression discriminant, IEnumerable<SwitchCase> cases)
  1486. {
  1487. return new SwitchStatement
  1488. {
  1489. Type = SyntaxNodes.SwitchStatement,
  1490. Discriminant = discriminant,
  1491. Cases = cases
  1492. };
  1493. }
  1494. public ThisExpression CreateThisExpression()
  1495. {
  1496. return new ThisExpression
  1497. {
  1498. Type = SyntaxNodes.ThisExpression
  1499. };
  1500. }
  1501. public ThrowStatement CreateThrowStatement(Expression argument)
  1502. {
  1503. return new ThrowStatement
  1504. {
  1505. Type = SyntaxNodes.ThrowStatement,
  1506. Argument = argument
  1507. };
  1508. }
  1509. public TryStatement CreateTryStatement(Statement block, IEnumerable<Statement> guardedHandlers,
  1510. IEnumerable<CatchClause> handlers, Statement finalizer)
  1511. {
  1512. return new TryStatement
  1513. {
  1514. Type = SyntaxNodes.TryStatement,
  1515. Block = block,
  1516. GuardedHandlers = guardedHandlers,
  1517. Handlers = handlers,
  1518. Finalizer = finalizer
  1519. };
  1520. }
  1521. public UnaryExpression CreateUnaryExpression(string op, Expression argument)
  1522. {
  1523. if (op == "++" || op == "--")
  1524. {
  1525. return new UpdateExpression
  1526. {
  1527. Type = SyntaxNodes.UpdateExpression,
  1528. Operator = op,
  1529. Argument = argument,
  1530. Prefix = true
  1531. };
  1532. }
  1533. return new UnaryExpression
  1534. {
  1535. Type = SyntaxNodes.UnaryExpression,
  1536. Operator = op,
  1537. Argument = argument,
  1538. Prefix = true
  1539. };
  1540. }
  1541. public VariableDeclaration CreateVariableDeclaration(IEnumerable<VariableDeclarator> declarations, string kind)
  1542. {
  1543. var variableDeclaration = new VariableDeclaration
  1544. {
  1545. Type = SyntaxNodes.VariableDeclaration,
  1546. Declarations = declarations,
  1547. Kind = kind
  1548. };
  1549. _variableScopes.Peek().VariableDeclarations.Add(variableDeclaration);
  1550. return variableDeclaration;
  1551. }
  1552. public VariableDeclarator CreateVariableDeclarator(Identifier id, Expression init)
  1553. {
  1554. return new VariableDeclarator
  1555. {
  1556. Type = SyntaxNodes.VariableDeclarator,
  1557. Id = id,
  1558. Init = init
  1559. };
  1560. }
  1561. public WhileStatement CreateWhileStatement(Expression test, Statement body)
  1562. {
  1563. return new WhileStatement
  1564. {
  1565. Type = SyntaxNodes.WhileStatement,
  1566. Test = test,
  1567. Body = body
  1568. };
  1569. }
  1570. public WithStatement CreateWithStatement(Expression obj, Statement body)
  1571. {
  1572. return new WithStatement
  1573. {
  1574. Type = SyntaxNodes.WithStatement,
  1575. Object = obj,
  1576. Body = body
  1577. };
  1578. }
  1579. // Return true if there is a line terminator before the next token.
  1580. private bool PeekLineTerminator()
  1581. {
  1582. int pos = _index;
  1583. int line = _lineNumber;
  1584. int start = _lineStart;
  1585. SkipComment();
  1586. bool found = _lineNumber != line;
  1587. _index = pos;
  1588. _lineNumber = line;
  1589. _lineStart = start;
  1590. return found;
  1591. }
  1592. // Throw an exception
  1593. private void ThrowError(Token token, string messageFormat, params object[] arguments)
  1594. {
  1595. ParserError error;
  1596. string msg = String.Format(messageFormat, arguments);
  1597. if (token.LineNumber.HasValue)
  1598. {
  1599. error = new ParserError("Line " + token.LineNumber + ": " + msg)
  1600. {
  1601. Index = token.Range[0],
  1602. LineNumber = token.LineNumber.Value,
  1603. Column = token.Range[0] - _lineStart + 1
  1604. };
  1605. }
  1606. else
  1607. {
  1608. error = new ParserError("Line " + _lineNumber + ": " + msg)
  1609. {
  1610. Index = _index,
  1611. LineNumber = _lineNumber,
  1612. Column = _index - _lineStart + 1
  1613. };
  1614. }
  1615. error.Description = msg;
  1616. throw error;
  1617. }
  1618. private void ThrowErrorTolerant(Token token, string messageFormat, params object[] arguments)
  1619. {
  1620. try
  1621. {
  1622. ThrowError(token, messageFormat, arguments);
  1623. }
  1624. catch (Exception e)
  1625. {
  1626. if (_extra.Errors != null)
  1627. {
  1628. _extra.Errors.Add(new ParserError(e.Message));
  1629. }
  1630. else
  1631. {
  1632. throw;
  1633. }
  1634. }
  1635. }
  1636. // Throw an exception because of the token.
  1637. private void ThrowUnexpected(Token token)
  1638. {
  1639. if (token.Type == Tokens.EOF)
  1640. {
  1641. ThrowError(token, Messages.UnexpectedEOS);
  1642. }
  1643. if (token.Type == Tokens.NumericLiteral)
  1644. {
  1645. ThrowError(token, Messages.UnexpectedNumber);
  1646. }
  1647. if (token.Type == Tokens.StringLiteral)
  1648. {
  1649. ThrowError(token, Messages.UnexpectedString);
  1650. }
  1651. if (token.Type == Tokens.Identifier)
  1652. {
  1653. ThrowError(token, Messages.UnexpectedIdentifier);
  1654. }
  1655. if (token.Type == Tokens.Keyword)
  1656. {
  1657. if (IsFutureReservedWord(token.Value as string))
  1658. {
  1659. ThrowError(token, Messages.UnexpectedReserved);
  1660. }
  1661. else if (_strict && IsStrictModeReservedWord(token.Value as string))
  1662. {
  1663. ThrowErrorTolerant(token, Messages.StrictReservedWord);
  1664. return;
  1665. }
  1666. ThrowError(token, Messages.UnexpectedToken, token.Value as string);
  1667. }
  1668. // BooleanLiteral, NullLiteral, or Punctuator.
  1669. ThrowError(token, Messages.UnexpectedToken, token.Value as string);
  1670. }
  1671. // Expect the next token to match the specified punctuator.
  1672. // If not, an exception will be thrown.
  1673. private void Expect(string value)
  1674. {
  1675. Token token = Lex();
  1676. if (token.Type != Tokens.Punctuator || !value.Equals(token.Value))
  1677. {
  1678. ThrowUnexpected(token);
  1679. }
  1680. }
  1681. // Expect the next token to match the specified keyword.
  1682. // If not, an exception will be thrown.
  1683. private void ExpectKeyword(string keyword)
  1684. {
  1685. Token token = Lex();
  1686. if (token.Type != Tokens.Keyword || !keyword.Equals(token.Value))
  1687. {
  1688. ThrowUnexpected(token);
  1689. }
  1690. }
  1691. // Return true if the next token matches the specified punctuator.
  1692. private bool Match(string value)
  1693. {
  1694. return _lookahead.Type == Tokens.Punctuator && value.Equals(_lookahead.Value);
  1695. }
  1696. // Return true if the next token matches the specified keyword
  1697. private bool MatchKeyword(object keyword)
  1698. {
  1699. return _lookahead.Type == Tokens.Keyword && keyword.Equals(_lookahead.Value);
  1700. }
  1701. // Return true if the next token is an assignment operator
  1702. private bool MatchAssign()
  1703. {
  1704. if (_lookahead.Type != Tokens.Punctuator)
  1705. {
  1706. return false;
  1707. }
  1708. var op = _lookahead.Value as string;
  1709. return op == "=" ||
  1710. op == "*=" ||
  1711. op == "/=" ||
  1712. op == "%=" ||
  1713. op == "+=" ||
  1714. op == "-=" ||
  1715. op == "<<=" ||
  1716. op == ">>=" ||
  1717. op == ">>>=" ||
  1718. op == "&=" ||
  1719. op == "^=" ||
  1720. op == "|=";
  1721. }
  1722. private void ConsumeSemicolon()
  1723. {
  1724. // Catch the very common case first: immediately a semicolon (char #59).
  1725. if (_source.CharCodeAt(_index) == 59)
  1726. {
  1727. Lex();
  1728. return;
  1729. }
  1730. int line = _lineNumber;
  1731. SkipComment();
  1732. if (_lineNumber != line)
  1733. {
  1734. return;
  1735. }
  1736. if (Match(";"))
  1737. {
  1738. Lex();
  1739. return;
  1740. }
  1741. if (_lookahead.Type != Tokens.EOF && !Match("}"))
  1742. {
  1743. ThrowUnexpected(_lookahead);
  1744. }
  1745. }
  1746. // Return true if provided expression is LeftHandSideExpression
  1747. private bool isLeftHandSide(Expression expr)
  1748. {
  1749. return expr.Type == SyntaxNodes.Identifier || expr.Type == SyntaxNodes.MemberExpression;
  1750. }
  1751. // 11.1.4 Array Initialiser
  1752. private ArrayExpression ParseArrayInitialiser()
  1753. {
  1754. var elements = new List<Expression>();
  1755. Expect("[");
  1756. while (!Match("]"))
  1757. {
  1758. if (Match(","))
  1759. {
  1760. Lex();
  1761. elements.Add(null);
  1762. }
  1763. else
  1764. {
  1765. elements.Add(ParseAssignmentExpression());
  1766. if (!Match("]"))
  1767. {
  1768. Expect(",");
  1769. }
  1770. }
  1771. }
  1772. Expect("]");
  1773. return CreateArrayExpression(elements);
  1774. }
  1775. // 11.1.5 Object Initialiser
  1776. private FunctionExpression ParsePropertyFunction(Identifier[] parameters, Token first = null)
  1777. {
  1778. EnterVariableScope();
  1779. EnterFunctionScope();
  1780. bool previousStrict = _strict;
  1781. SkipComment();
  1782. MarkStart();
  1783. Statement body = ParseFunctionSourceElements();
  1784. if (first != null && _strict && IsRestrictedWord(parameters[0].Name))
  1785. {
  1786. ThrowErrorTolerant(first, Messages.StrictParamName);
  1787. }
  1788. bool functionStrict = _strict;
  1789. _strict = previousStrict;
  1790. return MarkEnd(CreateFunctionExpression(null, parameters, new Expression[0], body, functionStrict));
  1791. }
  1792. private IPropertyKeyExpression ParseObjectPropertyKey()
  1793. {
  1794. SkipComment();
  1795. MarkStart();
  1796. Token token = Lex();
  1797. // Note: This function is called only from parseObjectProperty(), where
  1798. // EOF and Punctuator tokens are already filtered out.
  1799. if (token.Type == Tokens.StringLiteral || token.Type == Tokens.NumericLiteral)
  1800. {
  1801. if (_strict && token.Octal)
  1802. {
  1803. ThrowErrorTolerant(token, Messages.StrictOctalLiteral);
  1804. }
  1805. return MarkEnd(CreateLiteral(token));
  1806. }
  1807. return MarkEnd(CreateIdentifier((string) token.Value));
  1808. }
  1809. private Property ParseObjectProperty()
  1810. {
  1811. Expression value;
  1812. Token token = _lookahead;
  1813. SkipComment();
  1814. MarkStart();
  1815. if (token.Type == Tokens.Identifier)
  1816. {
  1817. IPropertyKeyExpression id = ParseObjectPropertyKey();
  1818. // Property Assignment: Getter and Setter.
  1819. if ("get".Equals(token.Value) && !Match(":"))
  1820. {
  1821. var key = ParseObjectPropertyKey();
  1822. Expect("(");
  1823. Expect(")");
  1824. value = ParsePropertyFunction(new Identifier[0]);
  1825. return MarkEnd(CreateProperty(PropertyKind.Get, key, value));
  1826. }
  1827. if ("set".Equals(token.Value) && !Match(":"))
  1828. {
  1829. var key = ParseObjectPropertyKey();
  1830. Expect("(");
  1831. token = _lookahead;
  1832. if (token.Type != Tokens.Identifier)
  1833. {
  1834. Expect(")");
  1835. ThrowErrorTolerant(token, Messages.UnexpectedToken, (string) token.Value);
  1836. value = ParsePropertyFunction(new Identifier[0]);
  1837. }
  1838. else
  1839. {
  1840. var param = new[] {ParseVariableIdentifier()};
  1841. Expect(")");
  1842. value = ParsePropertyFunction(param, token);
  1843. }
  1844. return MarkEnd(CreateProperty(PropertyKind.Set, key, value));
  1845. }
  1846. Expect(":");
  1847. value = ParseAssignmentExpression();
  1848. return MarkEnd(CreateProperty(PropertyKind.Data, id, value));
  1849. }
  1850. if (token.Type == Tokens.EOF || token.Type == Tokens.Punctuator)
  1851. {
  1852. ThrowUnexpected(token);
  1853. return null; // can't be reached
  1854. }
  1855. else
  1856. {
  1857. IPropertyKeyExpression key = ParseObjectPropertyKey();
  1858. Expect(":");
  1859. value = ParseAssignmentExpression();
  1860. return MarkEnd(CreateProperty(PropertyKind.Data, key, value));
  1861. }
  1862. }
  1863. private ObjectExpression ParseObjectInitialiser()
  1864. {
  1865. var properties = new List<Property>();
  1866. var map = new Dictionary<string, PropertyKind>();
  1867. Expect("{");
  1868. while (!Match("}"))
  1869. {
  1870. Property property = ParseObjectProperty();
  1871. string name = property.Key.GetKey();
  1872. PropertyKind kind = property.Kind;
  1873. string key = "$" + name;
  1874. if (map.ContainsKey(key))
  1875. {
  1876. if (map[key] == PropertyKind.Data)
  1877. {
  1878. if (_strict && kind == PropertyKind.Data)
  1879. {
  1880. ThrowErrorTolerant(Token.Empty, Messages.StrictDuplicateProperty);
  1881. }
  1882. else if (kind != PropertyKind.Data)
  1883. {
  1884. ThrowErrorTolerant(Token.Empty, Messages.AccessorDataProperty);
  1885. }
  1886. }
  1887. else
  1888. {
  1889. if (kind == PropertyKind.Data)
  1890. {
  1891. ThrowErrorTolerant(Token.Empty, Messages.AccessorDataProperty);
  1892. }
  1893. else if ((map[key] & kind) == map[key])
  1894. {
  1895. ThrowErrorTolerant(Token.Empty, Messages.AccessorGetSet);
  1896. }
  1897. }
  1898. map[key] |= kind;
  1899. }
  1900. else
  1901. {
  1902. map[key] = kind;
  1903. }
  1904. properties.Add(property);
  1905. if (!Match("}"))
  1906. {
  1907. Expect(",");
  1908. }
  1909. }
  1910. Expect("}");
  1911. return CreateObjectExpression(properties);
  1912. }
  1913. // 11.1.6 The Grouping Operator
  1914. private Expression ParseGroupExpression()
  1915. {
  1916. Expect("(");
  1917. Expression expr = ParseExpression();
  1918. Expect(")");
  1919. return expr;
  1920. }
  1921. // 11.1 Primary Expressions
  1922. private Expression ParsePrimaryExpression()
  1923. {
  1924. Expression expr = null;
  1925. if (Match("("))
  1926. {
  1927. return ParseGroupExpression();
  1928. }
  1929. Tokens type = _lookahead.Type;
  1930. MarkStart();
  1931. if (type == Tokens.Identifier)
  1932. {
  1933. expr = CreateIdentifier((string) Lex().Value);
  1934. }
  1935. else if (type == Tokens.StringLiteral || type == Tokens.NumericLiteral)
  1936. {
  1937. if (_strict && _lookahead.Octal)
  1938. {
  1939. ThrowErrorTolerant(_lookahead, Messages.StrictOctalLiteral);
  1940. }
  1941. expr = CreateLiteral(Lex());
  1942. }
  1943. else if (type == Tokens.Keyword)
  1944. {
  1945. if (MatchKeyword("this"))
  1946. {
  1947. Lex();
  1948. expr = CreateThisExpression();
  1949. }
  1950. else if (MatchKeyword("function"))
  1951. {
  1952. expr = ParseFunctionExpression();
  1953. }
  1954. }
  1955. else if (type == Tokens.BooleanLiteral)
  1956. {
  1957. Token token = Lex();
  1958. token.Value = ("true".Equals(token.Value));
  1959. expr = CreateLiteral(token);
  1960. }
  1961. else if (type == Tokens.NullLiteral)
  1962. {
  1963. Token token = Lex();
  1964. token.Value = null;
  1965. expr = CreateLiteral(token);
  1966. }
  1967. else if (Match("["))
  1968. {
  1969. expr = ParseArrayInitialiser();
  1970. }
  1971. else if (Match("{"))
  1972. {
  1973. expr = ParseObjectInitialiser();
  1974. }
  1975. else if (Match("/") || Match("/="))
  1976. {
  1977. expr = CreateLiteral(_extra.Tokens != null ? CollectRegex() : ScanRegExp());
  1978. }
  1979. if (expr != null)
  1980. {
  1981. return MarkEnd(expr);
  1982. }
  1983. ThrowUnexpected(Lex());
  1984. return null; // can't be reached
  1985. }
  1986. // 11.2 Left-Hand-Side Expressions
  1987. private IEnumerable<Expression> ParseArguments()
  1988. {
  1989. var args = new List<Expression>();
  1990. Expect("(");
  1991. if (!Match(")"))
  1992. {
  1993. while (_index < _length)
  1994. {
  1995. args.Add(ParseAssignmentExpression());
  1996. if (Match(")"))
  1997. {
  1998. break;
  1999. }
  2000. Expect(",");
  2001. }
  2002. }
  2003. Expect(")");
  2004. return args;
  2005. }
  2006. private Identifier ParseNonComputedProperty()
  2007. {
  2008. MarkStart();
  2009. Token token = Lex();
  2010. if (!IsIdentifierName(token))
  2011. {
  2012. ThrowUnexpected(token);
  2013. }
  2014. return MarkEnd(CreateIdentifier((string) token.Value));
  2015. }
  2016. private Identifier ParseNonComputedMember()
  2017. {
  2018. Expect(".");
  2019. return ParseNonComputedProperty();
  2020. }
  2021. private Expression ParseComputedMember()
  2022. {
  2023. Expect("[");
  2024. Expression expr = ParseExpression();
  2025. Expect("]");
  2026. return expr;
  2027. }
  2028. private NewExpression ParseNewExpression()
  2029. {
  2030. MarkStart();
  2031. ExpectKeyword("new");
  2032. Expression callee = ParseLeftHandSideExpression();
  2033. IEnumerable<Expression> args = Match("(") ? ParseArguments() : new AssignmentExpression[0];
  2034. return MarkEnd(CreateNewExpression(callee, args));
  2035. }
  2036. private Expression ParseLeftHandSideExpressionAllowCall()
  2037. {
  2038. LocationMarker marker = CreateLocationMarker();
  2039. Expression expr = MatchKeyword("new") ? ParseNewExpression() : ParsePrimaryExpression();
  2040. while (Match(".") || Match("[") || Match("("))
  2041. {
  2042. if (Match("("))
  2043. {
  2044. IEnumerable<Expression> args = ParseArguments();
  2045. expr = CreateCallExpression(expr, args);
  2046. }
  2047. else if (Match("["))
  2048. {
  2049. Expression property = ParseComputedMember();
  2050. expr = CreateMemberExpression('[', expr, property);
  2051. }
  2052. else
  2053. {
  2054. Identifier property = ParseNonComputedMember();
  2055. expr = CreateMemberExpression('.', expr, property);
  2056. }
  2057. if (marker != null)
  2058. {
  2059. marker.End(_index, _lineNumber, _lineStart);
  2060. marker.Apply(expr, _extra, PostProcess);
  2061. }
  2062. }
  2063. return expr;
  2064. }
  2065. private Expression ParseLeftHandSideExpression()
  2066. {
  2067. LocationMarker marker = CreateLocationMarker();
  2068. Expression expr = MatchKeyword("new") ? ParseNewExpression() : ParsePrimaryExpression();
  2069. while (Match(".") || Match("["))
  2070. {
  2071. if (Match("["))
  2072. {
  2073. Expression property = ParseComputedMember();
  2074. expr = CreateMemberExpression('[', expr, property);
  2075. }
  2076. else
  2077. {
  2078. Identifier property = ParseNonComputedMember();
  2079. expr = CreateMemberExpression('.', expr, property);
  2080. }
  2081. if (marker != null)
  2082. {
  2083. marker.End(_index, _lineNumber, _lineStart);
  2084. marker.Apply(expr, _extra, PostProcess);
  2085. }
  2086. }
  2087. return expr;
  2088. }
  2089. // 11.3 Postfix Expressions
  2090. private Expression ParsePostfixExpression()
  2091. {
  2092. MarkStart();
  2093. Expression expr = ParseLeftHandSideExpressionAllowCall();
  2094. if (_lookahead.Type == Tokens.Punctuator)
  2095. {
  2096. if ((Match("++") || Match("--")) && !PeekLineTerminator())
  2097. {
  2098. // 11.3.1, 11.3.2
  2099. if (_strict && expr.Type == SyntaxNodes.Identifier && IsRestrictedWord(((Identifier) expr).Name))
  2100. {
  2101. ThrowErrorTolerant(Token.Empty, Messages.StrictLHSPostfix);
  2102. }
  2103. if (!isLeftHandSide(expr))
  2104. {
  2105. ThrowError(Token.Empty, Messages.InvalidLHSInAssignment);
  2106. }
  2107. Token token = Lex();
  2108. expr = CreatePostfixExpression((string) token.Value, expr);
  2109. }
  2110. }
  2111. return MarkEndIf(expr);
  2112. }
  2113. // 11.4 Unary Operators
  2114. private Expression ParseUnaryExpression()
  2115. {
  2116. Expression expr;
  2117. MarkStart();
  2118. if (_lookahead.Type != Tokens.Punctuator && _lookahead.Type != Tokens.Keyword)
  2119. {
  2120. expr = ParsePostfixExpression();
  2121. }
  2122. else if (Match("++") || Match("--"))
  2123. {
  2124. Token token = Lex();
  2125. expr = ParseUnaryExpression();
  2126. // 11.4.4, 11.4.5
  2127. if (_strict && expr.Type == SyntaxNodes.Identifier && IsRestrictedWord(((Identifier) expr).Name))
  2128. {
  2129. ThrowErrorTolerant(Token.Empty, Messages.StrictLHSPrefix);
  2130. }
  2131. if (!isLeftHandSide(expr))
  2132. {
  2133. ThrowError(Token.Empty, Messages.InvalidLHSInAssignment);
  2134. }
  2135. expr = CreateUnaryExpression((string) token.Value, expr);
  2136. }
  2137. else if (Match("+") || Match("-") || Match("~") || Match("!"))
  2138. {
  2139. Token token = Lex();
  2140. expr = ParseUnaryExpression();
  2141. expr = CreateUnaryExpression((string) token.Value, expr);
  2142. }
  2143. else if (MatchKeyword("delete") || MatchKeyword("void") || MatchKeyword("typeof"))
  2144. {
  2145. Token token = Lex();
  2146. expr = ParseUnaryExpression();
  2147. UnaryExpression unaryExpr = CreateUnaryExpression((string) token.Value, expr);
  2148. if (_strict && unaryExpr.Operator == "delete" && unaryExpr.Argument.Type == SyntaxNodes.Identifier)
  2149. {
  2150. ThrowErrorTolerant(Token.Empty, Messages.StrictDelete);
  2151. }
  2152. expr = unaryExpr;
  2153. }
  2154. else
  2155. {
  2156. expr = ParsePostfixExpression();
  2157. }
  2158. return MarkEndIf(expr);
  2159. }
  2160. private int binaryPrecedence(Token token, bool allowIn)
  2161. {
  2162. int prec = 0;
  2163. if (token.Type != Tokens.Punctuator && token.Type != Tokens.Keyword)
  2164. {
  2165. return 0;
  2166. }
  2167. switch ((string) token.Value)
  2168. {
  2169. case "||":
  2170. prec = 1;
  2171. break;
  2172. case "&&":
  2173. prec = 2;
  2174. break;
  2175. case "|":
  2176. prec = 3;
  2177. break;
  2178. case "^":
  2179. prec = 4;
  2180. break;
  2181. case "&":
  2182. prec = 5;
  2183. break;
  2184. case "==":
  2185. case "!=":
  2186. case "===":
  2187. case "!==":
  2188. prec = 6;
  2189. break;
  2190. case "<":
  2191. case ">":
  2192. case "<=":
  2193. case ">=":
  2194. case "instanceof":
  2195. prec = 7;
  2196. break;
  2197. case "in":
  2198. prec = allowIn ? 7 : 0;
  2199. break;
  2200. case "<<":
  2201. case ">>":
  2202. case ">>>":
  2203. prec = 8;
  2204. break;
  2205. case "+":
  2206. case "-":
  2207. prec = 9;
  2208. break;
  2209. case "*":
  2210. case "/":
  2211. case "%":
  2212. prec = 11;
  2213. break;
  2214. }
  2215. return prec;
  2216. }
  2217. // 11.5 Multiplicative Operators
  2218. // 11.6 Additive Operators
  2219. // 11.7 Bitwise Shift Operators
  2220. // 11.8 Relational Operators
  2221. // 11.9 Equality Operators
  2222. // 11.10 Binary Bitwise Operators
  2223. // 11.11 Binary Logical Operators
  2224. private Expression ParseBinaryExpression()
  2225. {
  2226. Expression expr;
  2227. bool previousAllowIn = _state.AllowIn;
  2228. _state.AllowIn = true;
  2229. LocationMarker marker = CreateLocationMarker();
  2230. Expression left = ParseUnaryExpression();
  2231. Token token = _lookahead;
  2232. int prec = binaryPrecedence(token, previousAllowIn);
  2233. if (prec == 0)
  2234. {
  2235. return left;
  2236. }
  2237. token.Precedence = prec;
  2238. Lex();
  2239. var markers = new Stack<LocationMarker>( new [] {marker, CreateLocationMarker()});
  2240. Expression right = ParseUnaryExpression();
  2241. var stack = new Stack<object>( new object[] {left, token, right});
  2242. while ((prec = binaryPrecedence(_lookahead, previousAllowIn)) > 0)
  2243. {
  2244. // Reduce: make a binary expression from the three topmost entries.
  2245. while ((stack.Count > 2) && (prec <= ((Token) stack.ElementAt(stack.Count - 2)).Precedence))
  2246. {
  2247. right = (Expression) stack.Pop();
  2248. var op = (string) ((Token) stack.Pop()).Value;
  2249. left = (Expression) stack.Pop();
  2250. expr = CreateBinaryExpression(op, left, right);
  2251. markers.Pop();
  2252. marker = markers.Pop();
  2253. if (marker != null)
  2254. {
  2255. marker.End(_index, _lineNumber, _lineStart);
  2256. marker.Apply(expr, _extra, PostProcess);
  2257. }
  2258. stack.Push(expr);
  2259. markers.Push(marker);
  2260. }
  2261. // Shift.
  2262. token = Lex();
  2263. token.Precedence = prec;
  2264. stack.Push(token);
  2265. markers.Push(CreateLocationMarker());
  2266. expr = ParseUnaryExpression();
  2267. stack.Push(expr);
  2268. }
  2269. _state.AllowIn = previousAllowIn;
  2270. // Final reduce to clean-up the stack.
  2271. int i = stack.Count - 1;
  2272. expr = (Expression) stack.ElementAt(i);
  2273. markers.Pop();
  2274. while (i > 1)
  2275. {
  2276. expr = CreateBinaryExpression((string) ((Token) stack.ElementAt(i - 1)).Value, expr, (Expression) stack.ElementAt(i - 2));
  2277. i -= 2;
  2278. marker = markers.Pop();
  2279. if (marker != null)
  2280. {
  2281. marker.End(_index, _lineNumber, _lineStart);
  2282. marker.Apply(expr, _extra, PostProcess);
  2283. }
  2284. }
  2285. return expr;
  2286. }
  2287. // 11.12 Conditional Operator
  2288. private Expression ParseConditionalExpression()
  2289. {
  2290. MarkStart();
  2291. Expression expr = ParseBinaryExpression();
  2292. if (Match("?"))
  2293. {
  2294. Lex();
  2295. bool previousAllowIn = _state.AllowIn;
  2296. _state.AllowIn = true;
  2297. Expression consequent = ParseAssignmentExpression();
  2298. _state.AllowIn = previousAllowIn;
  2299. Expect(":");
  2300. Expression alternate = ParseAssignmentExpression();
  2301. expr = MarkEnd(CreateConditionalExpression(expr, consequent, alternate));
  2302. }
  2303. else
  2304. {
  2305. MarkEnd(new SyntaxNode());
  2306. }
  2307. return expr;
  2308. }
  2309. // 11.13 Assignment Operators
  2310. private Expression ParseAssignmentExpression()
  2311. {
  2312. Expression left;
  2313. Token token = _lookahead;
  2314. MarkStart();
  2315. Expression expr = left = ParseConditionalExpression();
  2316. if (MatchAssign())
  2317. {
  2318. // LeftHandSideExpression
  2319. if (!isLeftHandSide(left))
  2320. {
  2321. ThrowError(Token.Empty, Messages.InvalidLHSInAssignment);
  2322. }
  2323. // 11.13.1
  2324. if (_strict && left.Type == SyntaxNodes.Identifier && IsRestrictedWord(((Identifier) left).Name))
  2325. {
  2326. ThrowErrorTolerant(token, Messages.StrictLHSAssignment);
  2327. }
  2328. token = Lex();
  2329. Expression right = ParseAssignmentExpression();
  2330. expr = CreateAssignmentExpression((string) token.Value, left, right);
  2331. }
  2332. return MarkEndIf(expr);
  2333. }
  2334. // 11.14 Comma Operator
  2335. private Expression ParseExpression()
  2336. {
  2337. MarkStart();
  2338. Expression expr = ParseAssignmentExpression();
  2339. if (Match(","))
  2340. {
  2341. expr = CreateSequenceExpression(new List<Expression> {expr});
  2342. while (_index < _length)
  2343. {
  2344. if (!Match(","))
  2345. {
  2346. break;
  2347. }
  2348. Lex();
  2349. ((SequenceExpression) expr).Expressions.Add(ParseAssignmentExpression());
  2350. }
  2351. }
  2352. return MarkEndIf(expr);
  2353. }
  2354. // 12.1 Block
  2355. private IEnumerable<Statement> ParseStatementList()
  2356. {
  2357. var list = new List<Statement>();
  2358. while (_index < _length)
  2359. {
  2360. if (Match("}"))
  2361. {
  2362. break;
  2363. }
  2364. Statement statement = ParseSourceElement();
  2365. if (statement == null)
  2366. {
  2367. break;
  2368. }
  2369. list.Add(statement);
  2370. }
  2371. return list;
  2372. }
  2373. private BlockStatement ParseBlock()
  2374. {
  2375. SkipComment();
  2376. MarkStart();
  2377. Expect("{");
  2378. IEnumerable<Statement> block = ParseStatementList();
  2379. Expect("}");
  2380. return MarkEnd(CreateBlockStatement(block));
  2381. }
  2382. // 12.2 Variable Statement
  2383. private Identifier ParseVariableIdentifier()
  2384. {
  2385. SkipComment();
  2386. MarkStart();
  2387. Token token = Lex();
  2388. if (token.Type != Tokens.Identifier)
  2389. {
  2390. ThrowUnexpected(token);
  2391. }
  2392. return MarkEnd(CreateIdentifier((string) token.Value));
  2393. }
  2394. private VariableDeclarator ParseVariableDeclaration(string kind)
  2395. {
  2396. Expression init = null;
  2397. SkipComment();
  2398. MarkStart();
  2399. Identifier id = ParseVariableIdentifier();
  2400. // 12.2.1
  2401. if (_strict && IsRestrictedWord(id.Name))
  2402. {
  2403. ThrowErrorTolerant(Token.Empty, Messages.StrictVarName);
  2404. }
  2405. if ("const".Equals(kind))
  2406. {
  2407. Expect("=");
  2408. init = ParseAssignmentExpression();
  2409. }
  2410. else if (Match("="))
  2411. {
  2412. Lex();
  2413. init = ParseAssignmentExpression();
  2414. }
  2415. return MarkEnd(CreateVariableDeclarator(id, init));
  2416. }
  2417. private IEnumerable<VariableDeclarator> ParseVariableDeclarationList(string kind)
  2418. {
  2419. var list = new List<VariableDeclarator>();
  2420. do
  2421. {
  2422. list.Add(ParseVariableDeclaration(kind));
  2423. if (!Match(","))
  2424. {
  2425. break;
  2426. }
  2427. Lex();
  2428. } while (_index < _length);
  2429. return list;
  2430. }
  2431. private VariableDeclaration ParseVariableStatement()
  2432. {
  2433. ExpectKeyword("var");
  2434. IEnumerable<VariableDeclarator> declarations = ParseVariableDeclarationList(null);
  2435. ConsumeSemicolon();
  2436. return CreateVariableDeclaration(declarations, "var");
  2437. }
  2438. // kind may be `const` or `let`
  2439. // Both are experimental and not in the specification yet.
  2440. // see http://wiki.ecmascript.org/doku.php?id=harmony:const
  2441. // and http://wiki.ecmascript.org/doku.php?id=harmony:let
  2442. private VariableDeclaration ParseConstLetDeclaration(string kind)
  2443. {
  2444. SkipComment();
  2445. MarkStart();
  2446. ExpectKeyword(kind);
  2447. IEnumerable<VariableDeclarator> declarations = ParseVariableDeclarationList(kind);
  2448. ConsumeSemicolon();
  2449. return MarkEnd(CreateVariableDeclaration(declarations, kind));
  2450. }
  2451. // 12.3 Empty Statement
  2452. private EmptyStatement ParseEmptyStatement()
  2453. {
  2454. Expect(";");
  2455. return CreateEmptyStatement();
  2456. }
  2457. // 12.4 Expression Statement
  2458. private ExpressionStatement ParseExpressionStatement()
  2459. {
  2460. Expression expr = ParseExpression();
  2461. ConsumeSemicolon();
  2462. return CreateExpressionStatement(expr);
  2463. }
  2464. // 12.5 If statement
  2465. private IfStatement ParseIfStatement()
  2466. {
  2467. Statement alternate;
  2468. ExpectKeyword("if");
  2469. Expect("(");
  2470. Expression test = ParseExpression();
  2471. Expect(")");
  2472. Statement consequent = ParseStatement();
  2473. if (MatchKeyword("else"))
  2474. {
  2475. Lex();
  2476. alternate = ParseStatement();
  2477. }
  2478. else
  2479. {
  2480. alternate = null;
  2481. }
  2482. return CreateIfStatement(test, consequent, alternate);
  2483. }
  2484. // 12.6 Iteration Statements
  2485. private DoWhileStatement ParseDoWhileStatement()
  2486. {
  2487. ExpectKeyword("do");
  2488. bool oldInIteration = _state.InIteration;
  2489. _state.InIteration = true;
  2490. Statement body = ParseStatement();
  2491. _state.InIteration = oldInIteration;
  2492. ExpectKeyword("while");
  2493. Expect("(");
  2494. Expression test = ParseExpression();
  2495. Expect(")");
  2496. if (Match(";"))
  2497. {
  2498. Lex();
  2499. }
  2500. return CreateDoWhileStatement(body, test);
  2501. }
  2502. private WhileStatement ParseWhileStatement()
  2503. {
  2504. ExpectKeyword("while");
  2505. Expect("(");
  2506. Expression test = ParseExpression();
  2507. Expect(")");
  2508. bool oldInIteration = _state.InIteration;
  2509. _state.InIteration = true;
  2510. Statement body = ParseStatement();
  2511. _state.InIteration = oldInIteration;
  2512. return CreateWhileStatement(test, body);
  2513. }
  2514. private VariableDeclaration ParseForVariableDeclaration()
  2515. {
  2516. MarkStart();
  2517. Token token = Lex();
  2518. IEnumerable<VariableDeclarator> declarations = ParseVariableDeclarationList(null);
  2519. return MarkEnd(CreateVariableDeclaration(declarations, (string) token.Value));
  2520. }
  2521. private Statement ParseForStatement()
  2522. {
  2523. SyntaxNode init = null, left = null;
  2524. Expression right = null;
  2525. Expression test = null, update = null;
  2526. ExpectKeyword("for");
  2527. Expect("(");
  2528. if (Match(";"))
  2529. {
  2530. Lex();
  2531. }
  2532. else
  2533. {
  2534. if (MatchKeyword("var") || MatchKeyword("let"))
  2535. {
  2536. _state.AllowIn = false;
  2537. init = ParseForVariableDeclaration();
  2538. _state.AllowIn = true;
  2539. if (init.As<VariableDeclaration>().Declarations.Count() == 1 && MatchKeyword("in"))
  2540. {
  2541. Lex();
  2542. left = init;
  2543. right = ParseExpression();
  2544. init = null;
  2545. }
  2546. }
  2547. else
  2548. {
  2549. _state.AllowIn = false;
  2550. init = ParseExpression();
  2551. _state.AllowIn = true;
  2552. if (MatchKeyword("in"))
  2553. {
  2554. // LeftHandSideExpression
  2555. if (!isLeftHandSide((Expression) init))
  2556. {
  2557. ThrowError(Token.Empty, Messages.InvalidLHSInForIn);
  2558. }
  2559. Lex();
  2560. left = init;
  2561. right = ParseExpression();
  2562. init = null;
  2563. }
  2564. }
  2565. if (left == null)
  2566. {
  2567. Expect(";");
  2568. }
  2569. }
  2570. if (left == null)
  2571. {
  2572. if (!Match(";"))
  2573. {
  2574. test = ParseExpression();
  2575. }
  2576. Expect(";");
  2577. if (!Match(")"))
  2578. {
  2579. update = ParseExpression();
  2580. }
  2581. }
  2582. Expect(")");
  2583. bool oldInIteration = _state.InIteration;
  2584. _state.InIteration = true;
  2585. Statement body = ParseStatement();
  2586. _state.InIteration = oldInIteration;
  2587. return (left == null)
  2588. ? (Statement) CreateForStatement(init, test, update, body)
  2589. : CreateForInStatement(left, right, body);
  2590. }
  2591. // 12.7 The continue statement
  2592. private Statement ParseContinueStatement()
  2593. {
  2594. Identifier label = null;
  2595. ExpectKeyword("continue");
  2596. // Optimize the most common form: 'continue;'.
  2597. if (_source.CharCodeAt(_index) == 59)
  2598. {
  2599. Lex();
  2600. if (!_state.InIteration)
  2601. {
  2602. ThrowError(Token.Empty, Messages.IllegalContinue);
  2603. }
  2604. return CreateContinueStatement(null);
  2605. }
  2606. if (PeekLineTerminator())
  2607. {
  2608. if (!_state.InIteration)
  2609. {
  2610. ThrowError(Token.Empty, Messages.IllegalContinue);
  2611. }
  2612. return CreateContinueStatement(null);
  2613. }
  2614. if (_lookahead.Type == Tokens.Identifier)
  2615. {
  2616. label = ParseVariableIdentifier();
  2617. string key = "$" + label.Name;
  2618. if (!_state.LabelSet.Contains(key))
  2619. {
  2620. ThrowError(Token.Empty, Messages.UnknownLabel, label.Name);
  2621. }
  2622. }
  2623. ConsumeSemicolon();
  2624. if (label == null && !_state.InIteration)
  2625. {
  2626. ThrowError(Token.Empty, Messages.IllegalContinue);
  2627. }
  2628. return CreateContinueStatement(label);
  2629. }
  2630. // 12.8 The break statement
  2631. private BreakStatement ParseBreakStatement()
  2632. {
  2633. Identifier label = null;
  2634. ExpectKeyword("break");
  2635. // Catch the very common case first: immediately a semicolon (char #59).
  2636. if (_source.CharCodeAt(_index) == 59)
  2637. {
  2638. Lex();
  2639. if (!(_state.InIteration || _state.InSwitch))
  2640. {
  2641. ThrowError(Token.Empty, Messages.IllegalBreak);
  2642. }
  2643. return CreateBreakStatement(null);
  2644. }
  2645. if (PeekLineTerminator())
  2646. {
  2647. if (!(_state.InIteration || _state.InSwitch))
  2648. {
  2649. ThrowError(Token.Empty, Messages.IllegalBreak);
  2650. }
  2651. return CreateBreakStatement(null);
  2652. }
  2653. if (_lookahead.Type == Tokens.Identifier)
  2654. {
  2655. label = ParseVariableIdentifier();
  2656. string key = "$" + label.Name;
  2657. if (!_state.LabelSet.Contains(key))
  2658. {
  2659. ThrowError(Token.Empty, Messages.UnknownLabel, label.Name);
  2660. }
  2661. }
  2662. ConsumeSemicolon();
  2663. if (label == null && !(_state.InIteration || _state.InSwitch))
  2664. {
  2665. ThrowError(Token.Empty, Messages.IllegalBreak);
  2666. }
  2667. return CreateBreakStatement(label);
  2668. }
  2669. // 12.9 The return statement
  2670. private ReturnStatement ParseReturnStatement()
  2671. {
  2672. Expression argument = null;
  2673. ExpectKeyword("return");
  2674. if (!_state.InFunctionBody)
  2675. {
  2676. ThrowErrorTolerant(Token.Empty, Messages.IllegalReturn);
  2677. }
  2678. // 'return' followed by a space and an identifier is very common.
  2679. if (_source.CharCodeAt(_index) == 32)
  2680. {
  2681. if (IsIdentifierStart(_source.CharCodeAt(_index + 1)))
  2682. {
  2683. argument = ParseExpression();
  2684. ConsumeSemicolon();
  2685. return CreateReturnStatement(argument);
  2686. }
  2687. }
  2688. if (PeekLineTerminator())
  2689. {
  2690. return CreateReturnStatement(null);
  2691. }
  2692. if (!Match(";"))
  2693. {
  2694. if (!Match("}") && _lookahead.Type != Tokens.EOF)
  2695. {
  2696. argument = ParseExpression();
  2697. }
  2698. }
  2699. ConsumeSemicolon();
  2700. return CreateReturnStatement(argument);
  2701. }
  2702. // 12.10 The with statement
  2703. private WithStatement ParseWithStatement()
  2704. {
  2705. if (_strict)
  2706. {
  2707. ThrowErrorTolerant(Token.Empty, Messages.StrictModeWith);
  2708. }
  2709. ExpectKeyword("with");
  2710. Expect("(");
  2711. Expression obj = ParseExpression();
  2712. Expect(")");
  2713. Statement body = ParseStatement();
  2714. return CreateWithStatement(obj, body);
  2715. }
  2716. // 12.10 The swith statement
  2717. private SwitchCase ParseSwitchCase()
  2718. {
  2719. Expression test;
  2720. var consequent = new List<Statement>();
  2721. SkipComment();
  2722. MarkStart();
  2723. if (MatchKeyword("default"))
  2724. {
  2725. Lex();
  2726. test = null;
  2727. }
  2728. else
  2729. {
  2730. ExpectKeyword("case");
  2731. test = ParseExpression();
  2732. }
  2733. Expect(":");
  2734. while (_index < _length)
  2735. {
  2736. if (Match("}") || MatchKeyword("default") || MatchKeyword("case"))
  2737. {
  2738. break;
  2739. }
  2740. Statement statement = ParseStatement();
  2741. consequent.Add(statement);
  2742. }
  2743. return MarkEnd(CreateSwitchCase(test, consequent));
  2744. }
  2745. private SwitchStatement ParseSwitchStatement()
  2746. {
  2747. ExpectKeyword("switch");
  2748. Expect("(");
  2749. Expression discriminant = ParseExpression();
  2750. Expect(")");
  2751. Expect("{");
  2752. var cases = new List<SwitchCase>();
  2753. if (Match("}"))
  2754. {
  2755. Lex();
  2756. return CreateSwitchStatement(discriminant, cases);
  2757. }
  2758. bool oldInSwitch = _state.InSwitch;
  2759. _state.InSwitch = true;
  2760. bool defaultFound = false;
  2761. while (_index < _length)
  2762. {
  2763. if (Match("}"))
  2764. {
  2765. break;
  2766. }
  2767. SwitchCase clause = ParseSwitchCase();
  2768. if (clause.Test == null)
  2769. {
  2770. if (defaultFound)
  2771. {
  2772. ThrowError(Token.Empty, Messages.MultipleDefaultsInSwitch);
  2773. }
  2774. defaultFound = true;
  2775. }
  2776. cases.Add(clause);
  2777. }
  2778. _state.InSwitch = oldInSwitch;
  2779. Expect("}");
  2780. return CreateSwitchStatement(discriminant, cases);
  2781. }
  2782. // 12.13 The throw statement
  2783. private ThrowStatement ParseThrowStatement()
  2784. {
  2785. ExpectKeyword("throw");
  2786. if (PeekLineTerminator())
  2787. {
  2788. ThrowError(Token.Empty, Messages.NewlineAfterThrow);
  2789. }
  2790. Expression argument = ParseExpression();
  2791. ConsumeSemicolon();
  2792. return CreateThrowStatement(argument);
  2793. }
  2794. // 12.14 The try statement
  2795. private CatchClause ParseCatchClause()
  2796. {
  2797. SkipComment();
  2798. MarkStart();
  2799. ExpectKeyword("catch");
  2800. Expect("(");
  2801. if (Match(")"))
  2802. {
  2803. ThrowUnexpected(_lookahead);
  2804. }
  2805. Identifier param = ParseVariableIdentifier();
  2806. // 12.14.1
  2807. if (_strict && IsRestrictedWord(param.Name))
  2808. {
  2809. ThrowErrorTolerant(Token.Empty, Messages.StrictCatchVariable);
  2810. }
  2811. Expect(")");
  2812. BlockStatement body = ParseBlock();
  2813. return MarkEnd(CreateCatchClause(param, body));
  2814. }
  2815. private TryStatement ParseTryStatement()
  2816. {
  2817. var handlers = new List<CatchClause>();
  2818. Statement finalizer = null;
  2819. ExpectKeyword("try");
  2820. BlockStatement block = ParseBlock();
  2821. if (MatchKeyword("catch"))
  2822. {
  2823. handlers.Add(ParseCatchClause());
  2824. }
  2825. if (MatchKeyword("finally"))
  2826. {
  2827. Lex();
  2828. finalizer = ParseBlock();
  2829. }
  2830. if (handlers.Count == 0 && finalizer == null)
  2831. {
  2832. ThrowError(Token.Empty, Messages.NoCatchOrFinally);
  2833. }
  2834. return CreateTryStatement(block, new Statement[0], handlers, finalizer);
  2835. }
  2836. // 12.15 The debugger statement
  2837. private DebuggerStatement ParseDebuggerStatement()
  2838. {
  2839. ExpectKeyword("debugger");
  2840. ConsumeSemicolon();
  2841. return CreateDebuggerStatement();
  2842. }
  2843. // 12 Statements
  2844. private Statement ParseStatement()
  2845. {
  2846. Tokens type = _lookahead.Type;
  2847. if (type == Tokens.EOF)
  2848. {
  2849. ThrowUnexpected(_lookahead);
  2850. }
  2851. SkipComment();
  2852. MarkStart();
  2853. if (type == Tokens.Punctuator)
  2854. {
  2855. switch ((string) _lookahead.Value)
  2856. {
  2857. case ";":
  2858. return MarkEnd(ParseEmptyStatement());
  2859. case "{":
  2860. return MarkEnd(ParseBlock());
  2861. case "(":
  2862. return MarkEnd(ParseExpressionStatement());
  2863. }
  2864. }
  2865. if (type == Tokens.Keyword)
  2866. {
  2867. switch ((string) _lookahead.Value)
  2868. {
  2869. case "break":
  2870. return MarkEnd(ParseBreakStatement());
  2871. case "continue":
  2872. return MarkEnd(ParseContinueStatement());
  2873. case "debugger":
  2874. return MarkEnd(ParseDebuggerStatement());
  2875. case "do":
  2876. return MarkEnd(ParseDoWhileStatement());
  2877. case "for":
  2878. return MarkEnd(ParseForStatement());
  2879. case "function":
  2880. return MarkEnd(ParseFunctionDeclaration());
  2881. case "if":
  2882. return MarkEnd(ParseIfStatement());
  2883. case "return":
  2884. return MarkEnd(ParseReturnStatement());
  2885. case "switch":
  2886. return MarkEnd(ParseSwitchStatement());
  2887. case "throw":
  2888. return MarkEnd(ParseThrowStatement());
  2889. case "try":
  2890. return MarkEnd(ParseTryStatement());
  2891. case "var":
  2892. return MarkEnd(ParseVariableStatement());
  2893. case "while":
  2894. return MarkEnd(ParseWhileStatement());
  2895. case "with":
  2896. return MarkEnd(ParseWithStatement());
  2897. }
  2898. }
  2899. Expression expr = ParseExpression();
  2900. // 12.12 Labelled Statements
  2901. if ((expr.Type == SyntaxNodes.Identifier) && Match(":"))
  2902. {
  2903. Lex();
  2904. string key = "$" + ((Identifier) expr).Name;
  2905. if (_state.LabelSet.Contains(key))
  2906. {
  2907. ThrowError(Token.Empty, Messages.Redeclaration, "Label", ((Identifier) expr).Name);
  2908. }
  2909. _state.LabelSet.Add(key);
  2910. Statement labeledBody = ParseStatement();
  2911. _state.LabelSet.Remove(key);
  2912. return MarkEnd(CreateLabeledStatement((Identifier) expr, labeledBody));
  2913. }
  2914. ConsumeSemicolon();
  2915. return MarkEnd(CreateExpressionStatement(expr));
  2916. }
  2917. // 13 Function Definition
  2918. private Statement ParseFunctionSourceElements()
  2919. {
  2920. Token firstRestricted = Token.Empty;
  2921. var sourceElements = new List<Statement>();
  2922. SkipComment();
  2923. MarkStart();
  2924. Expect("{");
  2925. while (_index < _length)
  2926. {
  2927. if (_lookahead.Type != Tokens.StringLiteral)
  2928. {
  2929. break;
  2930. }
  2931. Token token = _lookahead;
  2932. Statement sourceElement = ParseSourceElement();
  2933. sourceElements.Add(sourceElement);
  2934. if (((ExpressionStatement) sourceElement).Expression.Type != SyntaxNodes.Literal)
  2935. {
  2936. // this is not directive
  2937. break;
  2938. }
  2939. string directive = _source.Slice(token.Range[0] + 1, token.Range[1] - 1);
  2940. if (directive == "use strict")
  2941. {
  2942. _strict = true;
  2943. if (firstRestricted != Token.Empty)
  2944. {
  2945. ThrowErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  2946. }
  2947. }
  2948. else
  2949. {
  2950. if (firstRestricted == Token.Empty && token.Octal)
  2951. {
  2952. firstRestricted = token;
  2953. }
  2954. }
  2955. }
  2956. HashSet<string> oldLabelSet = _state.LabelSet;
  2957. bool oldInIteration = _state.InIteration;
  2958. bool oldInSwitch = _state.InSwitch;
  2959. bool oldInFunctionBody = _state.InFunctionBody;
  2960. _state.LabelSet = new HashSet<string>();
  2961. _state.InIteration = false;
  2962. _state.InSwitch = false;
  2963. _state.InFunctionBody = true;
  2964. while (_index < _length)
  2965. {
  2966. if (Match("}"))
  2967. {
  2968. break;
  2969. }
  2970. Statement sourceElement = ParseSourceElement();
  2971. if (sourceElement == null)
  2972. {
  2973. break;
  2974. }
  2975. sourceElements.Add(sourceElement);
  2976. }
  2977. Expect("}");
  2978. _state.LabelSet = oldLabelSet;
  2979. _state.InIteration = oldInIteration;
  2980. _state.InSwitch = oldInSwitch;
  2981. _state.InFunctionBody = oldInFunctionBody;
  2982. return MarkEnd(CreateBlockStatement(sourceElements));
  2983. }
  2984. private ParsedParameters ParseParams(Token firstRestricted)
  2985. {
  2986. string message = null;
  2987. Token stricted = Token.Empty;
  2988. var parameters = new List<Identifier>();
  2989. Expect("(");
  2990. if (!Match(")"))
  2991. {
  2992. var paramSet = new HashSet<string>();
  2993. while (_index < _length)
  2994. {
  2995. Token token = _lookahead;
  2996. Identifier param = ParseVariableIdentifier();
  2997. string key = '$' + (string) token.Value;
  2998. if (_strict)
  2999. {
  3000. if (IsRestrictedWord((string) token.Value))
  3001. {
  3002. stricted = token;
  3003. message = Messages.StrictParamName;
  3004. }
  3005. if (paramSet.Contains(key))
  3006. {
  3007. stricted = token;
  3008. message = Messages.StrictParamDupe;
  3009. }
  3010. }
  3011. else if (firstRestricted == Token.Empty)
  3012. {
  3013. if (IsRestrictedWord((string) token.Value))
  3014. {
  3015. firstRestricted = token;
  3016. message = Messages.StrictParamName;
  3017. }
  3018. else if (IsStrictModeReservedWord((string) token.Value))
  3019. {
  3020. firstRestricted = token;
  3021. message = Messages.StrictReservedWord;
  3022. }
  3023. else if (paramSet.Contains(key))
  3024. {
  3025. firstRestricted = token;
  3026. message = Messages.StrictParamDupe;
  3027. }
  3028. }
  3029. parameters.Add(param);
  3030. paramSet.Add(key);
  3031. if (Match(")"))
  3032. {
  3033. break;
  3034. }
  3035. Expect(",");
  3036. }
  3037. }
  3038. Expect(")");
  3039. return new ParsedParameters
  3040. {
  3041. Parameters = parameters,
  3042. Stricted = stricted,
  3043. FirstRestricted = firstRestricted,
  3044. Message = message
  3045. };
  3046. }
  3047. private Statement ParseFunctionDeclaration()
  3048. {
  3049. EnterVariableScope();
  3050. EnterFunctionScope();
  3051. Token firstRestricted = Token.Empty;
  3052. string message = null;
  3053. SkipComment();
  3054. MarkStart();
  3055. ExpectKeyword("function");
  3056. Token token = _lookahead;
  3057. Identifier id = ParseVariableIdentifier();
  3058. if (_strict)
  3059. {
  3060. if (IsRestrictedWord((string) token.Value))
  3061. {
  3062. ThrowErrorTolerant(token, Messages.StrictFunctionName);
  3063. }
  3064. }
  3065. else
  3066. {
  3067. if (IsRestrictedWord((string) token.Value))
  3068. {
  3069. firstRestricted = token;
  3070. message = Messages.StrictFunctionName;
  3071. }
  3072. else if (IsStrictModeReservedWord((string) token.Value))
  3073. {
  3074. firstRestricted = token;
  3075. message = Messages.StrictReservedWord;
  3076. }
  3077. }
  3078. ParsedParameters tmp = ParseParams(firstRestricted);
  3079. IEnumerable<Identifier> parameters = tmp.Parameters;
  3080. Token stricted = tmp.Stricted;
  3081. firstRestricted = tmp.FirstRestricted;
  3082. if (tmp.Message != null)
  3083. {
  3084. message = tmp.Message;
  3085. }
  3086. bool previousStrict = _strict;
  3087. Statement body = ParseFunctionSourceElements();
  3088. if (_strict && firstRestricted != Token.Empty)
  3089. {
  3090. ThrowError(firstRestricted, message);
  3091. }
  3092. if (_strict && stricted != Token.Empty)
  3093. {
  3094. ThrowErrorTolerant(stricted, message);
  3095. }
  3096. bool functionStrict = _strict;
  3097. _strict = previousStrict;
  3098. return MarkEnd(CreateFunctionDeclaration(id, parameters, new Expression[0], body, functionStrict));
  3099. }
  3100. private void EnterVariableScope()
  3101. {
  3102. _variableScopes.Push(new VariableScope());
  3103. }
  3104. private IList<VariableDeclaration> LeaveVariableScope()
  3105. {
  3106. return _variableScopes.Pop().VariableDeclarations;
  3107. }
  3108. private void EnterFunctionScope()
  3109. {
  3110. _functionScopes.Push(new FunctionScope());
  3111. }
  3112. private IList<FunctionDeclaration> LeaveFunctionScope()
  3113. {
  3114. return _functionScopes.Pop().FunctionDeclarations;
  3115. }
  3116. private FunctionExpression ParseFunctionExpression()
  3117. {
  3118. EnterVariableScope();
  3119. EnterFunctionScope();
  3120. Token firstRestricted = Token.Empty;
  3121. string message = null;
  3122. Identifier id = null;
  3123. MarkStart();
  3124. ExpectKeyword("function");
  3125. if (!Match("("))
  3126. {
  3127. Token token = _lookahead;
  3128. id = ParseVariableIdentifier();
  3129. if (_strict)
  3130. {
  3131. if (IsRestrictedWord((string) token.Value))
  3132. {
  3133. ThrowErrorTolerant(token, Messages.StrictFunctionName);
  3134. }
  3135. }
  3136. else
  3137. {
  3138. if (IsRestrictedWord((string) token.Value))
  3139. {
  3140. firstRestricted = token;
  3141. message = Messages.StrictFunctionName;
  3142. }
  3143. else if (IsStrictModeReservedWord((string) token.Value))
  3144. {
  3145. firstRestricted = token;
  3146. message = Messages.StrictReservedWord;
  3147. }
  3148. }
  3149. }
  3150. ParsedParameters tmp = ParseParams(firstRestricted);
  3151. IEnumerable<Identifier> parameters = tmp.Parameters;
  3152. Token stricted = tmp.Stricted;
  3153. firstRestricted = tmp.FirstRestricted;
  3154. if (tmp.Message != null)
  3155. {
  3156. message = tmp.Message;
  3157. }
  3158. bool previousStrict = _strict;
  3159. Statement body = ParseFunctionSourceElements();
  3160. if (_strict && firstRestricted != Token.Empty)
  3161. {
  3162. ThrowError(firstRestricted, message);
  3163. }
  3164. if (_strict && stricted != Token.Empty)
  3165. {
  3166. ThrowErrorTolerant(stricted, message);
  3167. }
  3168. bool functionStrict = _strict;
  3169. _strict = previousStrict;
  3170. return MarkEnd(CreateFunctionExpression(id, parameters, new Expression[0], body, functionStrict));
  3171. }
  3172. // 14 Program
  3173. private Statement ParseSourceElement()
  3174. {
  3175. if (_lookahead.Type == Tokens.Keyword)
  3176. {
  3177. switch ((string) _lookahead.Value)
  3178. {
  3179. case "const":
  3180. case "let":
  3181. return ParseConstLetDeclaration((string) _lookahead.Value);
  3182. case "function":
  3183. return ParseFunctionDeclaration();
  3184. default:
  3185. return ParseStatement();
  3186. }
  3187. }
  3188. if (_lookahead.Type != Tokens.EOF)
  3189. {
  3190. return ParseStatement();
  3191. }
  3192. return null;
  3193. }
  3194. private ICollection<Statement> ParseSourceElements()
  3195. {
  3196. var sourceElements = new List<Statement>();
  3197. Token firstRestricted = Token.Empty;
  3198. Statement sourceElement;
  3199. while (_index < _length)
  3200. {
  3201. Token token = _lookahead;
  3202. if (token.Type != Tokens.StringLiteral)
  3203. {
  3204. break;
  3205. }
  3206. sourceElement = ParseSourceElement();
  3207. sourceElements.Add(sourceElement);
  3208. if (((ExpressionStatement) sourceElement).Expression.Type != SyntaxNodes.Literal)
  3209. {
  3210. // this is not directive
  3211. break;
  3212. }
  3213. string directive = _source.Slice(token.Range[0] + 1, token.Range[1] - 1);
  3214. if (directive == "use strict")
  3215. {
  3216. _strict = true;
  3217. if (firstRestricted != Token.Empty)
  3218. {
  3219. ThrowErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
  3220. }
  3221. }
  3222. else
  3223. {
  3224. if (firstRestricted == Token.Empty && token.Octal)
  3225. {
  3226. firstRestricted = token;
  3227. }
  3228. }
  3229. }
  3230. while (_index < _length)
  3231. {
  3232. sourceElement = ParseSourceElement();
  3233. if (sourceElement == null)
  3234. {
  3235. break;
  3236. }
  3237. sourceElements.Add(sourceElement);
  3238. }
  3239. return sourceElements;
  3240. }
  3241. private Program ParseProgram()
  3242. {
  3243. EnterVariableScope();
  3244. EnterFunctionScope();
  3245. SkipComment();
  3246. MarkStart();
  3247. _strict = false;
  3248. Peek();
  3249. ICollection<Statement> body = ParseSourceElements();
  3250. return MarkEnd(CreateProgram(body, _strict));
  3251. }
  3252. private LocationMarker CreateLocationMarker()
  3253. {
  3254. if (!_extra.Loc.HasValue && _extra.Range.Length == 0)
  3255. {
  3256. return null;
  3257. }
  3258. SkipComment();
  3259. return new LocationMarker(_index, _lineNumber, _lineStart);
  3260. }
  3261. public Program Parse(string code)
  3262. {
  3263. return Parse(code, null);
  3264. }
  3265. public Program Parse(string code, ParserOptions options)
  3266. {
  3267. Program program;
  3268. _source = code;
  3269. _index = 0;
  3270. _lineNumber = (_source.Length > 0) ? 1 : 0;
  3271. _lineStart = 0;
  3272. _length = _source.Length;
  3273. _lookahead = null;
  3274. _state = new State
  3275. {
  3276. AllowIn = true,
  3277. LabelSet = new HashSet<string>(),
  3278. InFunctionBody = false,
  3279. InIteration = false,
  3280. InSwitch = false,
  3281. LastCommentStart = -1,
  3282. MarkerStack = new Stack<int>()
  3283. };
  3284. _extra = new Extra
  3285. {
  3286. Range = new int[0],
  3287. Loc = 0,
  3288. };
  3289. if (options != null)
  3290. {
  3291. if (!String.IsNullOrEmpty(options.Source))
  3292. {
  3293. _extra.Source = options.Source;
  3294. }
  3295. if (options.Tokens)
  3296. {
  3297. _extra.Tokens = new List<Token>();
  3298. }
  3299. if (options.Comment)
  3300. {
  3301. _extra.Comments = new List<Comment>();
  3302. }
  3303. if (options.Tolerant)
  3304. {
  3305. _extra.Errors = new List<ParserError>();
  3306. }
  3307. }
  3308. try
  3309. {
  3310. program = ParseProgram();
  3311. if (_extra.Comments != null)
  3312. {
  3313. program.Comments = _extra.Comments;
  3314. }
  3315. if (_extra.Tokens != null)
  3316. {
  3317. program.Tokens = _extra.Tokens;
  3318. }
  3319. if (_extra.Errors != null)
  3320. {
  3321. program.Errors = _extra.Errors;
  3322. }
  3323. }
  3324. finally
  3325. {
  3326. _extra = new Extra();
  3327. }
  3328. return program;
  3329. }
  3330. private class Extra
  3331. {
  3332. public int? Loc;
  3333. public int[] Range;
  3334. public string Source;
  3335. public List<Comment> Comments;
  3336. public List<Token> Tokens;
  3337. public List<ParserError> Errors;
  3338. }
  3339. private class LocationMarker
  3340. {
  3341. private readonly int[] _marker;
  3342. public LocationMarker(int index, int lineNumber, int lineStart)
  3343. {
  3344. _marker = new[] {index, lineNumber, index - lineStart, 0, 0, 0};
  3345. }
  3346. public void End(int index, int lineNumber, int lineStart)
  3347. {
  3348. _marker[3] = index;
  3349. _marker[4] = lineNumber;
  3350. _marker[5] = index - lineStart;
  3351. }
  3352. public void Apply(SyntaxNode node, Extra extra, Func<SyntaxNode, SyntaxNode> postProcess)
  3353. {
  3354. if (extra.Range.Length > 0)
  3355. {
  3356. node.Range = new[] {_marker[0], _marker[3]};
  3357. }
  3358. if (extra.Loc.HasValue)
  3359. {
  3360. node.Location = new Location
  3361. {
  3362. Start = new Position
  3363. {
  3364. Line = _marker[1],
  3365. Column = _marker[2]
  3366. },
  3367. End = new Position
  3368. {
  3369. Line = _marker[4],
  3370. Column = _marker[5]
  3371. }
  3372. };
  3373. }
  3374. node = postProcess(node);
  3375. }
  3376. };
  3377. private struct ParsedParameters
  3378. {
  3379. public Token FirstRestricted;
  3380. public string Message;
  3381. public IEnumerable<Identifier> Parameters;
  3382. public Token Stricted;
  3383. }
  3384. private static class Regexes
  3385. {
  3386. public static readonly Regex NonAsciiIdentifierStart = new Regex("[\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]");
  3387. public static readonly Regex NonAsciiIdentifierPart = new Regex("[\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]");
  3388. };
  3389. }
  3390. }