JavascriptParser.cs 127 KB

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