JavascriptParser.cs 131 KB

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