JsonParser.cs 26 KB

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