JavascriptParser.cs 128 KB

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