JsonParser.cs 27 KB

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