JsonParser.cs 27 KB

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