JavascriptParser.cs 131 KB

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