JavascriptParser.cs 130 KB

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