JavascriptParser.cs 128 KB

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