JavascriptParser.cs 131 KB

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