JsonParser.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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.ThrowArgumentException(Messages.UnexpectedToken);
  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.ThrowArgumentException(Messages.UnexpectedToken);
  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. _location = new Location
  425. {
  426. Start = new Position
  427. {
  428. Line = _lineNumber,
  429. Column = _index - _lineStart
  430. }
  431. };
  432. Token token = Advance();
  433. _location.End = new Position
  434. {
  435. Line = _lineNumber,
  436. Column = _index - _lineStart
  437. };
  438. if (token.Type != Tokens.EOF)
  439. {
  440. var range = new[] {token.Range[0], token.Range[1]};
  441. string value = _source.Slice(token.Range[0], token.Range[1]);
  442. _extra.Tokens.Add(new Token
  443. {
  444. Type = token.Type,
  445. Value = value,
  446. Range = range,
  447. });
  448. }
  449. return token;
  450. }
  451. private Token Lex()
  452. {
  453. Token token = _lookahead;
  454. _index = token.Range[1];
  455. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  456. _lineStart = token.LineStart;
  457. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  458. _index = token.Range[1];
  459. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  460. _lineStart = token.LineStart;
  461. return token;
  462. }
  463. private void Peek()
  464. {
  465. int pos = _index;
  466. int line = _lineNumber;
  467. int start = _lineStart;
  468. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  469. _index = pos;
  470. _lineNumber = line;
  471. _lineStart = start;
  472. }
  473. private void MarkStart()
  474. {
  475. if (_extra.Loc.HasValue)
  476. {
  477. _state.MarkerStack.Push(_index - _lineStart);
  478. _state.MarkerStack.Push(_lineNumber);
  479. }
  480. if (_extra.Range != null)
  481. {
  482. _state.MarkerStack.Push(_index);
  483. }
  484. }
  485. private T MarkEnd<T>(T node) where T : INode
  486. {
  487. if (_extra.Range != null)
  488. {
  489. node.Range = new int[] {_state.MarkerStack.Pop(), _index};
  490. }
  491. if (_extra.Loc.HasValue)
  492. {
  493. node.Location = new Location
  494. {
  495. Start = new Position
  496. {
  497. Line = _state.MarkerStack.Pop(),
  498. Column = _state.MarkerStack.Pop()
  499. },
  500. End = new Position
  501. {
  502. Line = _lineNumber,
  503. Column = _index - _lineStart
  504. }
  505. };
  506. PostProcess(node);
  507. }
  508. return node;
  509. }
  510. public T MarkEndIf<T>(T node) where T : INode
  511. {
  512. if (node.Range != null || node.Location != null)
  513. {
  514. if (_extra.Loc.HasValue)
  515. {
  516. _state.MarkerStack.Pop();
  517. _state.MarkerStack.Pop();
  518. }
  519. if (_extra.Range != null)
  520. {
  521. _state.MarkerStack.Pop();
  522. }
  523. }
  524. else
  525. {
  526. MarkEnd(node);
  527. }
  528. return node;
  529. }
  530. public INode PostProcess(INode node)
  531. {
  532. //if (_extra.Source != null)
  533. //{
  534. // node.Location.Source = _extra.Source;
  535. //}
  536. return node;
  537. }
  538. public ObjectInstance CreateArrayInstance(IEnumerable<JsValue> values)
  539. {
  540. var jsValues = values.ToArray();
  541. var jsArray = _engine.Array.Construct(jsValues.Length);
  542. _engine.Array.PrototypeObject.Push(jsArray, jsValues);
  543. return jsArray;
  544. }
  545. // Throw an exception
  546. private void ThrowError(Token token, string messageFormat, params object[] arguments)
  547. {
  548. ParserException exception;
  549. string msg = System.String.Format(messageFormat, arguments);
  550. if (token.LineNumber.HasValue)
  551. {
  552. exception = new ParserException("Line " + token.LineNumber + ": " + msg)
  553. {
  554. Index = token.Range[0],
  555. LineNumber = token.LineNumber.Value,
  556. Column = token.Range[0] - _lineStart + 1
  557. };
  558. }
  559. else
  560. {
  561. exception = new ParserException("Line " + _lineNumber + ": " + msg)
  562. {
  563. Index = _index,
  564. LineNumber = _lineNumber,
  565. Column = _index - _lineStart + 1
  566. };
  567. }
  568. exception.Description = msg;
  569. throw exception;
  570. }
  571. // Throw an exception because of the token.
  572. private void ThrowUnexpected(Token token)
  573. {
  574. if (token.Type == Tokens.EOF)
  575. {
  576. ThrowError(token, Messages.UnexpectedEOS);
  577. }
  578. if (token.Type == Tokens.Number)
  579. {
  580. ThrowError(token, Messages.UnexpectedNumber);
  581. }
  582. if (token.Type == Tokens.String)
  583. {
  584. ThrowError(token, Messages.UnexpectedString);
  585. }
  586. // BooleanLiteral, NullLiteral, or Punctuator.
  587. ThrowError(token, Messages.UnexpectedToken, token.Value as string);
  588. }
  589. // Expect the next token to match the specified punctuator.
  590. // If not, an exception will be thrown.
  591. private void Expect(string value)
  592. {
  593. Token token = Lex();
  594. if (token.Type != Tokens.Punctuator || !value.Equals(token.Value))
  595. {
  596. ThrowUnexpected(token);
  597. }
  598. }
  599. // Return true if the next token matches the specified punctuator.
  600. private bool Match(string value)
  601. {
  602. return _lookahead.Type == Tokens.Punctuator && value.Equals(_lookahead.Value);
  603. }
  604. private ObjectInstance ParseJsonArray()
  605. {
  606. var elements = new List<JsValue>();
  607. Expect("[");
  608. while (!Match("]"))
  609. {
  610. if (Match(","))
  611. {
  612. Lex();
  613. elements.Add(Null.Instance);
  614. }
  615. else
  616. {
  617. elements.Add(ParseJsonValue());
  618. if (!Match("]"))
  619. {
  620. Expect(",");
  621. }
  622. }
  623. }
  624. Expect("]");
  625. return CreateArrayInstance(elements);
  626. }
  627. public ObjectInstance ParseJsonObject()
  628. {
  629. Expect("{");
  630. var obj = _engine.Object.Construct(Arguments.Empty);
  631. while (!Match("}"))
  632. {
  633. Tokens type = _lookahead.Type;
  634. if (type != Tokens.String)
  635. {
  636. ThrowUnexpected(Lex());
  637. }
  638. var name = Lex().Value.ToString();
  639. if (PropertyNameContainsInvalidChar0To31(name))
  640. {
  641. ExceptionHelper.ThrowSyntaxError(_engine, $"Invalid character in property name '{name}'");
  642. }
  643. Expect(":");
  644. var value = ParseJsonValue();
  645. obj.FastAddProperty(name, value, true, true, true);
  646. if (!Match("}"))
  647. {
  648. Expect(",");
  649. }
  650. }
  651. Expect("}");
  652. return obj;
  653. }
  654. private static bool PropertyNameContainsInvalidChar0To31(string s)
  655. {
  656. const int max = 31;
  657. for (var i = 0; i < s.Length; i++)
  658. {
  659. var val = (int)s[i];
  660. if (val <= max)
  661. return true;
  662. }
  663. return false;
  664. }
  665. /// <summary>
  666. /// Optimization.
  667. /// By calling Lex().Value for each type, we parse the token twice.
  668. /// It was already parsed by the peek() method.
  669. /// _lookahead.Value already contain the value.
  670. /// </summary>
  671. /// <returns></returns>
  672. private JsValue ParseJsonValue()
  673. {
  674. Tokens type = _lookahead.Type;
  675. MarkStart();
  676. switch (type)
  677. {
  678. case Tokens.NullLiteral:
  679. var v = Lex().Value;
  680. return Null.Instance;
  681. case Tokens.BooleanLiteral:
  682. // implicit conversion operator goes through caching
  683. return (bool) Lex().Value ? JsBoolean.True : JsBoolean.False;
  684. case Tokens.String:
  685. // implicit conversion operator goes through caching
  686. return (string) Lex().Value;
  687. case Tokens.Number:
  688. return (double) Lex().Value;
  689. }
  690. if (Match("["))
  691. {
  692. return ParseJsonArray();
  693. }
  694. if (Match("{"))
  695. {
  696. return ParseJsonObject();
  697. }
  698. ThrowUnexpected(Lex());
  699. // can't be reached
  700. return Null.Instance;
  701. }
  702. public JsValue Parse(string code)
  703. {
  704. return Parse(code, null);
  705. }
  706. public JsValue Parse(string code, ParserOptions options)
  707. {
  708. _source = code;
  709. _index = 0;
  710. _lineNumber = (_source.Length > 0) ? 1 : 0;
  711. _lineStart = 0;
  712. _length = _source.Length;
  713. _lookahead = null;
  714. _state = new State
  715. {
  716. AllowIn = true,
  717. LabelSet = new HashSet<string>(),
  718. InFunctionBody = false,
  719. InIteration = false,
  720. InSwitch = false,
  721. LastCommentStart = -1,
  722. MarkerStack = new Stack<int>()
  723. };
  724. _extra = new Extra
  725. {
  726. Range = new int[0],
  727. Loc = 0,
  728. };
  729. if (options != null)
  730. {
  731. if (options.Tokens)
  732. {
  733. _extra.Tokens = new List<Token>();
  734. }
  735. }
  736. try
  737. {
  738. MarkStart();
  739. Peek();
  740. JsValue jsv = ParseJsonValue();
  741. Peek();
  742. Tokens type = _lookahead.Type;
  743. object value = _lookahead.Value;
  744. if(_lookahead.Type != Tokens.EOF)
  745. {
  746. ExceptionHelper.ThrowSyntaxError(_engine, $"Unexpected {_lookahead.Type} {_lookahead.Value}");
  747. }
  748. return jsv;
  749. }
  750. finally
  751. {
  752. _extra = new Extra();
  753. }
  754. }
  755. private class Extra
  756. {
  757. public int? Loc;
  758. public int[] Range;
  759. public List<Token> Tokens;
  760. }
  761. private enum Tokens
  762. {
  763. NullLiteral,
  764. BooleanLiteral,
  765. String,
  766. Number,
  767. Punctuator,
  768. EOF,
  769. };
  770. class Token
  771. {
  772. public Tokens Type;
  773. public object Value;
  774. public int[] Range;
  775. public int? LineNumber;
  776. public int LineStart;
  777. }
  778. static class Messages
  779. {
  780. public const string UnexpectedToken = "Unexpected token {0}";
  781. public const string UnexpectedNumber = "Unexpected number";
  782. public const string UnexpectedString = "Unexpected string";
  783. public const string UnexpectedEOS = "Unexpected end of input";
  784. };
  785. struct State
  786. {
  787. public int LastCommentStart;
  788. public bool AllowIn;
  789. public HashSet<string> LabelSet;
  790. public bool InFunctionBody;
  791. public bool InIteration;
  792. public bool InSwitch;
  793. public Stack<int> MarkerStack;
  794. }
  795. }
  796. }