JsonParser.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Esprima;
  5. using Esprima.Ast;
  6. using Jint.Native.Object;
  7. using Jint.Runtime;
  8. namespace Jint.Native.Json
  9. {
  10. public class JsonParser
  11. {
  12. private readonly Engine _engine;
  13. public JsonParser(Engine engine)
  14. {
  15. _engine = engine;
  16. }
  17. private Extra _extra;
  18. private int _index; // position in the stream
  19. private int _length; // length of the stream
  20. private int _lineNumber;
  21. private int _lineStart;
  22. private Location _location;
  23. private Token _lookahead;
  24. private string _source;
  25. private State _state;
  26. private static bool IsDecimalDigit(char ch)
  27. {
  28. return (ch >= '0' && ch <= '9');
  29. }
  30. private static bool IsHexDigit(char ch)
  31. {
  32. return
  33. ch >= '0' && ch <= '9' ||
  34. ch >= 'a' && ch <= 'f' ||
  35. ch >= 'A' && ch <= 'F'
  36. ;
  37. }
  38. private static bool IsOctalDigit(char ch)
  39. {
  40. return ch >= '0' && ch <= '7';
  41. }
  42. private static bool IsWhiteSpace(char ch)
  43. {
  44. return (ch == ' ') ||
  45. (ch == '\t') ||
  46. (ch == '\n') ||
  47. (ch == '\r');
  48. }
  49. private static bool IsLineTerminator(char ch)
  50. {
  51. return (ch == 10) || (ch == 13) || (ch == 0x2028) || (ch == 0x2029);
  52. }
  53. private static bool IsNullChar(char ch)
  54. {
  55. return ch == 'n'
  56. || ch == 'u'
  57. || ch == 'l'
  58. || ch == 'l'
  59. ;
  60. }
  61. private static bool IsTrueOrFalseChar(char ch)
  62. {
  63. return ch == 't'
  64. || ch == 'f'
  65. || ch == 'r'
  66. || ch == 'a'
  67. || ch == 'u'
  68. || ch == 'l'
  69. || ch == 'e'
  70. || ch == 's'
  71. ;
  72. }
  73. private char ScanHexEscape(char prefix)
  74. {
  75. int code = char.MinValue;
  76. int len = (prefix == 'u') ? 4 : 2;
  77. for (int i = 0; i < len; ++i)
  78. {
  79. if (_index < _length && IsHexDigit(_source.CharCodeAt(_index)))
  80. {
  81. char ch = _source.CharCodeAt(_index++);
  82. code = code * 16 + "0123456789abcdef".IndexOf(ch.ToString(), StringComparison.OrdinalIgnoreCase);
  83. }
  84. else
  85. {
  86. ExceptionHelper.ThrowSyntaxError(_engine, $"Expected hexadecimal digit:{_source}");
  87. }
  88. }
  89. return (char)code;
  90. }
  91. private void SkipWhiteSpace()
  92. {
  93. while (_index < _length)
  94. {
  95. char ch = _source.CharCodeAt(_index);
  96. if (IsWhiteSpace(ch))
  97. {
  98. ++_index;
  99. }
  100. else
  101. {
  102. break;
  103. }
  104. }
  105. }
  106. private Token ScanPunctuator()
  107. {
  108. int start = _index;
  109. char code = _source.CharCodeAt(_index);
  110. switch ((int) code)
  111. {
  112. // Check for most common single-character punctuators.
  113. case 46: // . dot
  114. case 40: // ( open bracket
  115. case 41: // ) close bracket
  116. case 59: // ; semicolon
  117. case 44: // , comma
  118. case 123: // { open curly brace
  119. case 125: // } close curly brace
  120. case 91: // [
  121. case 93: // ]
  122. case 58: // :
  123. case 63: // ?
  124. case 126: // ~
  125. ++_index;
  126. return new Token
  127. {
  128. Type = Tokens.Punctuator,
  129. Value = TypeConverter.ToString(code),
  130. LineNumber = _lineNumber,
  131. LineStart = _lineStart,
  132. Range = new[] {start, _index}
  133. };
  134. }
  135. return ExceptionHelper.ThrowSyntaxError<Token>(_engine, string.Format(Messages.UnexpectedToken, code));
  136. }
  137. private Token ScanNumericLiteral()
  138. {
  139. char ch = _source.CharCodeAt(_index);
  140. int start = _index;
  141. string number = "";
  142. // Number start with a -
  143. if (ch == '-')
  144. {
  145. number += _source.CharCodeAt(_index++).ToString();
  146. ch = _source.CharCodeAt(_index);
  147. }
  148. if (ch != '.')
  149. {
  150. number += _source.CharCodeAt(_index++).ToString();
  151. ch = _source.CharCodeAt(_index);
  152. // Hex number starts with '0x'.
  153. // Octal number starts with '0'.
  154. if (number == "0")
  155. {
  156. // decimal number starts with '0' such as '09' is illegal.
  157. if (ch > 0 && IsDecimalDigit(ch))
  158. {
  159. ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, ch));
  160. }
  161. }
  162. while (IsDecimalDigit(_source.CharCodeAt(_index)))
  163. {
  164. number += _source.CharCodeAt(_index++).ToString();
  165. }
  166. ch = _source.CharCodeAt(_index);
  167. }
  168. if (ch == '.')
  169. {
  170. number += _source.CharCodeAt(_index++).ToString();
  171. while (IsDecimalDigit(_source.CharCodeAt(_index)))
  172. {
  173. number += _source.CharCodeAt(_index++).ToString();
  174. }
  175. ch = _source.CharCodeAt(_index);
  176. }
  177. if (ch == 'e' || ch == 'E')
  178. {
  179. number += _source.CharCodeAt(_index++).ToString();
  180. ch = _source.CharCodeAt(_index);
  181. if (ch == '+' || ch == '-')
  182. {
  183. number += _source.CharCodeAt(_index++).ToString();
  184. }
  185. if (IsDecimalDigit(_source.CharCodeAt(_index)))
  186. {
  187. while (IsDecimalDigit(_source.CharCodeAt(_index)))
  188. {
  189. number += _source.CharCodeAt(_index++).ToString();
  190. }
  191. }
  192. else
  193. {
  194. ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, _source.CharCodeAt(_index)));
  195. }
  196. }
  197. return new Token
  198. {
  199. Type = Tokens.Number,
  200. Value = Double.Parse(number, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture),
  201. LineNumber = _lineNumber,
  202. LineStart = _lineStart,
  203. Range = new[] {start, _index}
  204. };
  205. }
  206. private Token ScanBooleanLiteral()
  207. {
  208. int start = _index;
  209. string s = "";
  210. while (IsTrueOrFalseChar(_source.CharCodeAt(_index)))
  211. {
  212. s += _source.CharCodeAt(_index++).ToString();
  213. }
  214. if (s == "true" || s == "false")
  215. {
  216. return new Token
  217. {
  218. Type = Tokens.BooleanLiteral,
  219. Value = s == "true",
  220. LineNumber = _lineNumber,
  221. LineStart = _lineStart,
  222. Range = new[] { start, _index }
  223. };
  224. }
  225. return ExceptionHelper.ThrowSyntaxError<Token>(_engine, string.Format(Messages.UnexpectedToken, s));
  226. }
  227. private Token ScanNullLiteral()
  228. {
  229. int start = _index;
  230. string s = "";
  231. while (IsNullChar(_source.CharCodeAt(_index)))
  232. {
  233. s += _source.CharCodeAt(_index++).ToString();
  234. }
  235. if (s == Null.Text)
  236. {
  237. return new Token
  238. {
  239. Type = Tokens.NullLiteral,
  240. Value = Null.Instance,
  241. LineNumber = _lineNumber,
  242. LineStart = _lineStart,
  243. Range = new[] { start, _index }
  244. };
  245. }
  246. return ExceptionHelper.ThrowSyntaxError<Token>(_engine, string.Format(Messages.UnexpectedToken, s));
  247. }
  248. private Token ScanStringLiteral()
  249. {
  250. var sb = new System.Text.StringBuilder();
  251. char quote = _source.CharCodeAt(_index);
  252. int start = _index;
  253. ++_index;
  254. while (_index < _length)
  255. {
  256. char ch = _source.CharCodeAt(_index++);
  257. if (ch == quote)
  258. {
  259. quote = char.MinValue;
  260. break;
  261. }
  262. if (ch <= 31)
  263. {
  264. ExceptionHelper.ThrowSyntaxError(_engine, $"Invalid character '{ch}', position:{_index}, string:{_source}");
  265. }
  266. if (ch == '\\')
  267. {
  268. ch = _source.CharCodeAt(_index++);
  269. if (ch > 0 || !IsLineTerminator(ch))
  270. {
  271. switch (ch)
  272. {
  273. case 'n':
  274. sb.Append('\n');
  275. break;
  276. case 'r':
  277. sb.Append('\r');
  278. break;
  279. case 't':
  280. sb.Append('\t');
  281. break;
  282. case 'u':
  283. case 'x':
  284. int restore = _index;
  285. char unescaped = ScanHexEscape(ch);
  286. if (unescaped > 0)
  287. {
  288. sb.Append(unescaped.ToString());
  289. }
  290. else
  291. {
  292. _index = restore;
  293. sb.Append(ch.ToString());
  294. }
  295. break;
  296. case 'b':
  297. sb.Append("\b");
  298. break;
  299. case 'f':
  300. sb.Append("\f");
  301. break;
  302. case 'v':
  303. sb.Append("\x0B");
  304. break;
  305. default:
  306. if (IsOctalDigit(ch))
  307. {
  308. int code = "01234567".IndexOf(ch);
  309. if (_index < _length && IsOctalDigit(_source.CharCodeAt(_index)))
  310. {
  311. code = code * 8 + "01234567".IndexOf(_source.CharCodeAt(_index++));
  312. // 3 digits are only allowed when string starts
  313. // with 0, 1, 2, 3
  314. if ("0123".IndexOf(ch) >= 0 &&
  315. _index < _length &&
  316. IsOctalDigit(_source.CharCodeAt(_index)))
  317. {
  318. code = code * 8 + "01234567".IndexOf(_source.CharCodeAt(_index++));
  319. }
  320. }
  321. sb.Append(((char)code).ToString());
  322. }
  323. else
  324. {
  325. sb.Append(ch.ToString());
  326. }
  327. break;
  328. }
  329. }
  330. else
  331. {
  332. ++_lineNumber;
  333. if (ch == '\r' && _source.CharCodeAt(_index) == '\n')
  334. {
  335. ++_index;
  336. }
  337. }
  338. }
  339. else if (IsLineTerminator(ch))
  340. {
  341. break;
  342. }
  343. else
  344. {
  345. sb.Append(ch.ToString());
  346. }
  347. }
  348. if (quote != 0)
  349. {
  350. ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, _source));
  351. }
  352. return new Token
  353. {
  354. Type = Tokens.String,
  355. Value = sb.ToString(),
  356. LineNumber = _lineNumber,
  357. LineStart = _lineStart,
  358. Range = new[] {start, _index}
  359. };
  360. }
  361. private Token Advance()
  362. {
  363. SkipWhiteSpace();
  364. if (_index >= _length)
  365. {
  366. return new Token
  367. {
  368. Type = Tokens.EOF,
  369. LineNumber = _lineNumber,
  370. LineStart = _lineStart,
  371. Range = new[] {_index, _index}
  372. };
  373. }
  374. char ch = _source.CharCodeAt(_index);
  375. // Very common: ( and ) and ;
  376. if (ch == 40 || ch == 41 || ch == 58)
  377. {
  378. return ScanPunctuator();
  379. }
  380. // String literal starts with double quote (#34).
  381. // Single quote (#39) are not allowed in JSON.
  382. if (ch == 34)
  383. {
  384. return ScanStringLiteral();
  385. }
  386. // Dot (.) char #46 can also start a floating-point number, hence the need
  387. // to check the next character.
  388. if (ch == 46)
  389. {
  390. if (IsDecimalDigit(_source.CharCodeAt(_index + 1)))
  391. {
  392. return ScanNumericLiteral();
  393. }
  394. return ScanPunctuator();
  395. }
  396. if (ch == '-') // Negative Number
  397. {
  398. if (IsDecimalDigit(_source.CharCodeAt(_index + 1)))
  399. {
  400. return ScanNumericLiteral();
  401. }
  402. return ScanPunctuator();
  403. }
  404. if (IsDecimalDigit(ch))
  405. {
  406. return ScanNumericLiteral();
  407. }
  408. if (ch == 't' || ch == 'f')
  409. {
  410. return ScanBooleanLiteral();
  411. }
  412. if (ch == 'n')
  413. {
  414. return ScanNullLiteral();
  415. }
  416. return ScanPunctuator();
  417. }
  418. private Token CollectToken()
  419. {
  420. var start = new Position(
  421. line: _lineNumber,
  422. column: _index - _lineStart);
  423. Token token = Advance();
  424. var end = new Position(
  425. line: _lineNumber,
  426. column: _index - _lineStart);
  427. _location = new Location(start, end, _source);
  428. if (token.Type != Tokens.EOF)
  429. {
  430. var range = new[] {token.Range[0], token.Range[1]};
  431. string value = _source.Slice(token.Range[0], token.Range[1]);
  432. _extra.Tokens.Add(new Token
  433. {
  434. Type = token.Type,
  435. Value = value,
  436. Range = range,
  437. });
  438. }
  439. return token;
  440. }
  441. private Token Lex()
  442. {
  443. Token token = _lookahead;
  444. _index = token.Range[1];
  445. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  446. _lineStart = token.LineStart;
  447. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  448. _index = token.Range[1];
  449. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  450. _lineStart = token.LineStart;
  451. return token;
  452. }
  453. private void Peek()
  454. {
  455. int pos = _index;
  456. int line = _lineNumber;
  457. int start = _lineStart;
  458. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  459. _index = pos;
  460. _lineNumber = line;
  461. _lineStart = start;
  462. }
  463. private void MarkStart()
  464. {
  465. if (_extra.Loc.HasValue)
  466. {
  467. _state.MarkerStack.Push(_index - _lineStart);
  468. _state.MarkerStack.Push(_lineNumber);
  469. }
  470. if (_extra.Range != null)
  471. {
  472. _state.MarkerStack.Push(_index);
  473. }
  474. }
  475. private T MarkEnd<T>(T node) where T : Node
  476. {
  477. if (_extra.Range != null)
  478. {
  479. node.Range = new Esprima.Ast.Range(_state.MarkerStack.Pop(), _index);
  480. }
  481. if (_extra.Loc.HasValue)
  482. {
  483. node.Location = new Location(
  484. start: new Position(
  485. line: _state.MarkerStack.Pop(),
  486. column: _state.MarkerStack.Pop()),
  487. end: new Position(
  488. line: _lineNumber,
  489. column: _index - _lineStart),
  490. source: _source);
  491. PostProcess(node);
  492. }
  493. return node;
  494. }
  495. public T MarkEndIf<T>(T node) where T : Node
  496. {
  497. if (node.Range != default || node.Location != default)
  498. {
  499. if (_extra.Loc.HasValue)
  500. {
  501. _state.MarkerStack.Pop();
  502. _state.MarkerStack.Pop();
  503. }
  504. if (_extra.Range != null)
  505. {
  506. _state.MarkerStack.Pop();
  507. }
  508. }
  509. else
  510. {
  511. MarkEnd(node);
  512. }
  513. return node;
  514. }
  515. public Node PostProcess(Node node)
  516. {
  517. //if (_extra.Source != null)
  518. //{
  519. // node.Location.Source = _extra.Source;
  520. //}
  521. return node;
  522. }
  523. // Throw an exception
  524. private void ThrowError(Token token, string messageFormat, params object[] arguments)
  525. {
  526. string msg = System.String.Format(messageFormat, arguments);
  527. int lineNumber = token.LineNumber ?? _lineNumber;
  528. var error = new ParseError(
  529. description: msg,
  530. source: _source,
  531. index: token.Range[0],
  532. position: new Position(token.LineNumber ?? _lineNumber, token.Range[0] - _lineStart + 1));
  533. var exception = new ParserException("Line " + lineNumber + ": " + msg, error);
  534. throw exception;
  535. }
  536. // Throw an exception because of the token.
  537. private void ThrowUnexpected(Token token)
  538. {
  539. if (token.Type == Tokens.EOF)
  540. {
  541. ThrowError(token, Messages.UnexpectedEOS);
  542. }
  543. if (token.Type == Tokens.Number)
  544. {
  545. ThrowError(token, Messages.UnexpectedNumber);
  546. }
  547. if (token.Type == Tokens.String)
  548. {
  549. ThrowError(token, Messages.UnexpectedString);
  550. }
  551. // BooleanLiteral, NullLiteral, or Punctuator.
  552. ThrowError(token, Messages.UnexpectedToken, token.Value as string);
  553. }
  554. // Expect the next token to match the specified punctuator.
  555. // If not, an exception will be thrown.
  556. private void Expect(string value)
  557. {
  558. Token token = Lex();
  559. if (token.Type != Tokens.Punctuator || !value.Equals(token.Value))
  560. {
  561. ThrowUnexpected(token);
  562. }
  563. }
  564. // Return true if the next token matches the specified punctuator.
  565. private bool Match(string value)
  566. {
  567. return _lookahead.Type == Tokens.Punctuator && value.Equals(_lookahead.Value);
  568. }
  569. private ObjectInstance ParseJsonArray()
  570. {
  571. var elements = new List<JsValue>();
  572. Expect("[");
  573. while (!Match("]"))
  574. {
  575. if (Match(","))
  576. {
  577. Lex();
  578. elements.Add(Null.Instance);
  579. }
  580. else
  581. {
  582. elements.Add(ParseJsonValue());
  583. if (!Match("]"))
  584. {
  585. Expect(",");
  586. }
  587. }
  588. }
  589. Expect("]");
  590. return _engine.Array.ConstructFast(elements);
  591. }
  592. public ObjectInstance ParseJsonObject()
  593. {
  594. Expect("{");
  595. var obj = _engine.Object.Construct(Arguments.Empty);
  596. while (!Match("}"))
  597. {
  598. Tokens type = _lookahead.Type;
  599. if (type != Tokens.String)
  600. {
  601. ThrowUnexpected(Lex());
  602. }
  603. var name = Lex().Value.ToString();
  604. if (PropertyNameContainsInvalidCharacters(name))
  605. {
  606. ExceptionHelper.ThrowSyntaxError(_engine, $"Invalid character in property name '{name}'");
  607. }
  608. Expect(":");
  609. var value = ParseJsonValue();
  610. obj.FastAddProperty(name, value, true, true, true);
  611. if (!Match("}"))
  612. {
  613. Expect(",");
  614. }
  615. }
  616. Expect("}");
  617. return obj;
  618. }
  619. private static bool PropertyNameContainsInvalidCharacters(string propertyName)
  620. {
  621. const char max = (char) 31;
  622. foreach (var c in propertyName)
  623. {
  624. if (c != '\t' && c <= max)
  625. return true;
  626. }
  627. return false;
  628. }
  629. /// <summary>
  630. /// Optimization.
  631. /// By calling Lex().Value for each type, we parse the token twice.
  632. /// It was already parsed by the peek() method.
  633. /// _lookahead.Value already contain the value.
  634. /// </summary>
  635. /// <returns></returns>
  636. private JsValue ParseJsonValue()
  637. {
  638. Tokens type = _lookahead.Type;
  639. MarkStart();
  640. switch (type)
  641. {
  642. case Tokens.NullLiteral:
  643. var v = Lex().Value;
  644. return Null.Instance;
  645. case Tokens.BooleanLiteral:
  646. // implicit conversion operator goes through caching
  647. return (bool) Lex().Value ? JsBoolean.True : JsBoolean.False;
  648. case Tokens.String:
  649. // implicit conversion operator goes through caching
  650. return new JsString((string) Lex().Value);
  651. case Tokens.Number:
  652. return (double) Lex().Value;
  653. }
  654. if (Match("["))
  655. {
  656. return ParseJsonArray();
  657. }
  658. if (Match("{"))
  659. {
  660. return ParseJsonObject();
  661. }
  662. ThrowUnexpected(Lex());
  663. // can't be reached
  664. return Null.Instance;
  665. }
  666. public JsValue Parse(string code)
  667. {
  668. return Parse(code, null);
  669. }
  670. public JsValue Parse(string code, ParserOptions options)
  671. {
  672. _source = code;
  673. _index = 0;
  674. _lineNumber = 1;
  675. _lineStart = 0;
  676. _length = _source.Length;
  677. _lookahead = null;
  678. _state = new State
  679. {
  680. AllowIn = true,
  681. LabelSet = new HashSet<string>(),
  682. InFunctionBody = false,
  683. InIteration = false,
  684. InSwitch = false,
  685. LastCommentStart = -1,
  686. MarkerStack = new Stack<int>()
  687. };
  688. _extra = new Extra
  689. {
  690. Range = new int[0],
  691. Loc = 0,
  692. };
  693. if (options != null)
  694. {
  695. if (options.Tokens)
  696. {
  697. _extra.Tokens = new List<Token>();
  698. }
  699. }
  700. try
  701. {
  702. MarkStart();
  703. Peek();
  704. JsValue jsv = ParseJsonValue();
  705. Peek();
  706. if(_lookahead.Type != Tokens.EOF)
  707. {
  708. ExceptionHelper.ThrowSyntaxError(_engine, $"Unexpected {_lookahead.Type} {_lookahead.Value}");
  709. }
  710. return jsv;
  711. }
  712. finally
  713. {
  714. _extra = new Extra();
  715. }
  716. }
  717. private class Extra
  718. {
  719. public int? Loc;
  720. public int[] Range;
  721. public List<Token> Tokens;
  722. }
  723. private enum Tokens
  724. {
  725. NullLiteral,
  726. BooleanLiteral,
  727. String,
  728. Number,
  729. Punctuator,
  730. EOF,
  731. };
  732. class Token
  733. {
  734. public Tokens Type;
  735. public object Value;
  736. public int[] Range;
  737. public int? LineNumber;
  738. public int LineStart;
  739. }
  740. static class Messages
  741. {
  742. public const string UnexpectedToken = "Unexpected token '{0}'";
  743. public const string UnexpectedNumber = "Unexpected number";
  744. public const string UnexpectedString = "Unexpected string";
  745. public const string UnexpectedEOS = "Unexpected end of input";
  746. };
  747. struct State
  748. {
  749. public int LastCommentStart;
  750. public bool AllowIn;
  751. public HashSet<string> LabelSet;
  752. public bool InFunctionBody;
  753. public bool InIteration;
  754. public bool InSwitch;
  755. public Stack<int> MarkerStack;
  756. }
  757. }
  758. }