JsonParser.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Jint.Native.Object;
  6. using Jint.Parser;
  7. using Jint.Parser.Ast;
  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 == 'r'
  66. || ch == 'u'
  67. || ch == 'e'
  68. || ch == 'f'
  69. || ch == 'a'
  70. || ch == 'l'
  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. throw new JavaScriptException(_engine.SyntaxError, string.Format("Expected hexadecimal digit:{0}", _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 = code.ToString(),
  131. LineNumber = _lineNumber,
  132. LineStart = _lineStart,
  133. Range = new[] {start, _index}
  134. };
  135. }
  136. throw new JavaScriptException(_engine.SyntaxError, 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. throw new Exception(Messages.UnexpectedToken);
  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. throw new Exception(Messages.UnexpectedToken);
  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. else
  227. {
  228. throw new JavaScriptException(_engine.SyntaxError, string.Format(Messages.UnexpectedToken, s));
  229. }
  230. }
  231. private Token ScanNullLiteral()
  232. {
  233. int start = _index;
  234. string s = "";
  235. while (IsNullChar(_source.CharCodeAt(_index)))
  236. {
  237. s += _source.CharCodeAt(_index++).ToString();
  238. }
  239. if (s == Null.Text)
  240. {
  241. return new Token
  242. {
  243. Type = Tokens.NullLiteral,
  244. Value = Null.Instance,
  245. LineNumber = _lineNumber,
  246. LineStart = _lineStart,
  247. Range = new[] { start, _index }
  248. };
  249. }
  250. else
  251. {
  252. throw new JavaScriptException(_engine.SyntaxError, string.Format(Messages.UnexpectedToken, s));
  253. }
  254. }
  255. private Token ScanStringLiteral()
  256. {
  257. var sb = new System.Text.StringBuilder();
  258. char quote = _source.CharCodeAt(_index);
  259. int start = _index;
  260. ++_index;
  261. while (_index < _length)
  262. {
  263. char ch = _source.CharCodeAt(_index++);
  264. if (ch == quote)
  265. {
  266. quote = char.MinValue;
  267. break;
  268. }
  269. if (ch <= 31)
  270. {
  271. throw new JavaScriptException(_engine.SyntaxError, string.Format("Invalid character '{0}', position:{1}, string:{2}", ch, _index, _source));
  272. }
  273. if (ch == '\\')
  274. {
  275. ch = _source.CharCodeAt(_index++);
  276. if (ch > 0 || !IsLineTerminator(ch))
  277. {
  278. switch (ch)
  279. {
  280. case 'n':
  281. sb.Append('\n');
  282. break;
  283. case 'r':
  284. sb.Append('\r');
  285. break;
  286. case 't':
  287. sb.Append('\t');
  288. break;
  289. case 'u':
  290. case 'x':
  291. int restore = _index;
  292. char unescaped = ScanHexEscape(ch);
  293. if (unescaped > 0)
  294. {
  295. sb.Append(unescaped.ToString());
  296. }
  297. else
  298. {
  299. _index = restore;
  300. sb.Append(ch.ToString());
  301. }
  302. break;
  303. case 'b':
  304. sb.Append("\b");
  305. break;
  306. case 'f':
  307. sb.Append("\f");
  308. break;
  309. case 'v':
  310. sb.Append("\x0B");
  311. break;
  312. default:
  313. if (IsOctalDigit(ch))
  314. {
  315. int code = "01234567".IndexOf(ch);
  316. if (_index < _length && IsOctalDigit(_source.CharCodeAt(_index)))
  317. {
  318. code = code * 8 + "01234567".IndexOf(_source.CharCodeAt(_index++));
  319. // 3 digits are only allowed when string starts
  320. // with 0, 1, 2, 3
  321. if ("0123".IndexOf(ch) >= 0 &&
  322. _index < _length &&
  323. IsOctalDigit(_source.CharCodeAt(_index)))
  324. {
  325. code = code * 8 + "01234567".IndexOf(_source.CharCodeAt(_index++));
  326. }
  327. }
  328. sb.Append(((char)code).ToString());
  329. }
  330. else
  331. {
  332. sb.Append(ch.ToString());
  333. }
  334. break;
  335. }
  336. }
  337. else
  338. {
  339. ++_lineNumber;
  340. if (ch == '\r' && _source.CharCodeAt(_index) == '\n')
  341. {
  342. ++_index;
  343. }
  344. }
  345. }
  346. else if (IsLineTerminator(ch))
  347. {
  348. break;
  349. }
  350. else
  351. {
  352. sb.Append(ch.ToString());
  353. }
  354. }
  355. if (quote != 0)
  356. {
  357. throw new JavaScriptException(_engine.SyntaxError, string.Format(Messages.UnexpectedToken, _source));
  358. }
  359. return new Token
  360. {
  361. Type = Tokens.String,
  362. Value = sb.ToString(),
  363. LineNumber = _lineNumber,
  364. LineStart = _lineStart,
  365. Range = new[] {start, _index}
  366. };
  367. }
  368. private Token Advance()
  369. {
  370. SkipWhiteSpace();
  371. if (_index >= _length)
  372. {
  373. return new Token
  374. {
  375. Type = Tokens.EOF,
  376. LineNumber = _lineNumber,
  377. LineStart = _lineStart,
  378. Range = new[] {_index, _index}
  379. };
  380. }
  381. char ch = _source.CharCodeAt(_index);
  382. // Very common: ( and ) and ;
  383. if (ch == 40 || ch == 41 || ch == 58)
  384. {
  385. return ScanPunctuator();
  386. }
  387. // String literal starts with double quote (#34).
  388. // Single quote (#39) are not allowed in JSON.
  389. if (ch == 34)
  390. {
  391. return ScanStringLiteral();
  392. }
  393. // Dot (.) char #46 can also start a floating-point number, hence the need
  394. // to check the next character.
  395. if (ch == 46)
  396. {
  397. if (IsDecimalDigit(_source.CharCodeAt(_index + 1)))
  398. {
  399. return ScanNumericLiteral();
  400. }
  401. return ScanPunctuator();
  402. }
  403. if (ch == '-') // Negative Number
  404. {
  405. if (IsDecimalDigit(_source.CharCodeAt(_index + 1)))
  406. {
  407. return ScanNumericLiteral();
  408. }
  409. return ScanPunctuator();
  410. }
  411. if (IsDecimalDigit(ch))
  412. {
  413. return ScanNumericLiteral();
  414. }
  415. if (ch == 't' || ch == 'f')
  416. {
  417. return ScanBooleanLiteral();
  418. }
  419. if (ch == 'n')
  420. {
  421. return ScanNullLiteral();
  422. }
  423. return ScanPunctuator();
  424. }
  425. private Token CollectToken()
  426. {
  427. _location = new Location
  428. {
  429. Start = new Position
  430. {
  431. Line = _lineNumber,
  432. Column = _index - _lineStart
  433. }
  434. };
  435. Token token = Advance();
  436. _location.End = new Position
  437. {
  438. Line = _lineNumber,
  439. Column = _index - _lineStart
  440. };
  441. if (token.Type != Tokens.EOF)
  442. {
  443. var range = new[] {token.Range[0], token.Range[1]};
  444. string value = _source.Slice(token.Range[0], token.Range[1]);
  445. _extra.Tokens.Add(new Token
  446. {
  447. Type = token.Type,
  448. Value = value,
  449. Range = range,
  450. });
  451. }
  452. return token;
  453. }
  454. private Token Lex()
  455. {
  456. Token token = _lookahead;
  457. _index = token.Range[1];
  458. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  459. _lineStart = token.LineStart;
  460. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  461. _index = token.Range[1];
  462. _lineNumber = token.LineNumber.HasValue ? token.LineNumber.Value : 0;
  463. _lineStart = token.LineStart;
  464. return token;
  465. }
  466. private void Peek()
  467. {
  468. int pos = _index;
  469. int line = _lineNumber;
  470. int start = _lineStart;
  471. _lookahead = (_extra.Tokens != null) ? CollectToken() : Advance();
  472. _index = pos;
  473. _lineNumber = line;
  474. _lineStart = start;
  475. }
  476. private void MarkStart()
  477. {
  478. if (_extra.Loc.HasValue)
  479. {
  480. _state.MarkerStack.Push(_index - _lineStart);
  481. _state.MarkerStack.Push(_lineNumber);
  482. }
  483. if (_extra.Range != null)
  484. {
  485. _state.MarkerStack.Push(_index);
  486. }
  487. }
  488. private T MarkEnd<T>(T node) where T : SyntaxNode
  489. {
  490. if (_extra.Range != null)
  491. {
  492. node.Range = new[] {_state.MarkerStack.Pop(), _index};
  493. }
  494. if (_extra.Loc.HasValue)
  495. {
  496. node.Location = new Location
  497. {
  498. Start = new Position
  499. {
  500. Line = _state.MarkerStack.Pop(),
  501. Column = _state.MarkerStack.Pop()
  502. },
  503. End = new Position
  504. {
  505. Line = _lineNumber,
  506. Column = _index - _lineStart
  507. }
  508. };
  509. PostProcess(node);
  510. }
  511. return node;
  512. }
  513. public T MarkEndIf<T>(T node) where T : SyntaxNode
  514. {
  515. if (node.Range != null || node.Location != null)
  516. {
  517. if (_extra.Loc.HasValue)
  518. {
  519. _state.MarkerStack.Pop();
  520. _state.MarkerStack.Pop();
  521. }
  522. if (_extra.Range != null)
  523. {
  524. _state.MarkerStack.Pop();
  525. }
  526. }
  527. else
  528. {
  529. MarkEnd(node);
  530. }
  531. return node;
  532. }
  533. public SyntaxNode PostProcess(SyntaxNode node)
  534. {
  535. if (_extra.Source != null)
  536. {
  537. node.Location.Source = _extra.Source;
  538. }
  539. return node;
  540. }
  541. public ObjectInstance CreateArrayInstance(IEnumerable<JsValue> values)
  542. {
  543. var jsArray = _engine.Array.Construct(Arguments.Empty);
  544. _engine.Array.PrototypeObject.Push(jsArray, values.ToArray());
  545. return jsArray;
  546. }
  547. // Throw an exception
  548. private void ThrowError(Token token, string messageFormat, params object[] arguments)
  549. {
  550. ParserException exception;
  551. string msg = System.String.Format(messageFormat, arguments);
  552. if (token.LineNumber.HasValue)
  553. {
  554. exception = new ParserException("Line " + token.LineNumber + ": " + msg)
  555. {
  556. Index = token.Range[0],
  557. LineNumber = token.LineNumber.Value,
  558. Column = token.Range[0] - _lineStart + 1
  559. };
  560. }
  561. else
  562. {
  563. exception = new ParserException("Line " + _lineNumber + ": " + msg)
  564. {
  565. Index = _index,
  566. LineNumber = _lineNumber,
  567. Column = _index - _lineStart + 1
  568. };
  569. }
  570. exception.Description = msg;
  571. throw exception;
  572. }
  573. // Throw an exception because of the token.
  574. private void ThrowUnexpected(Token token)
  575. {
  576. if (token.Type == Tokens.EOF)
  577. {
  578. ThrowError(token, Messages.UnexpectedEOS);
  579. }
  580. if (token.Type == Tokens.Number)
  581. {
  582. ThrowError(token, Messages.UnexpectedNumber);
  583. }
  584. if (token.Type == Tokens.String)
  585. {
  586. ThrowError(token, Messages.UnexpectedString);
  587. }
  588. // BooleanLiteral, NullLiteral, or Punctuator.
  589. ThrowError(token, Messages.UnexpectedToken, token.Value as string);
  590. }
  591. // Expect the next token to match the specified punctuator.
  592. // If not, an exception will be thrown.
  593. private void Expect(string value)
  594. {
  595. Token token = Lex();
  596. if (token.Type != Tokens.Punctuator || !value.Equals(token.Value))
  597. {
  598. ThrowUnexpected(token);
  599. }
  600. }
  601. // Return true if the next token matches the specified punctuator.
  602. private bool Match(string value)
  603. {
  604. return _lookahead.Type == Tokens.Punctuator && value.Equals(_lookahead.Value);
  605. }
  606. private ObjectInstance ParseJsonArray()
  607. {
  608. var elements = new List<JsValue>();
  609. Expect("[");
  610. while (!Match("]"))
  611. {
  612. if (Match(","))
  613. {
  614. Lex();
  615. elements.Add(Null.Instance);
  616. }
  617. else
  618. {
  619. elements.Add(ParseJsonValue());
  620. if (!Match("]"))
  621. {
  622. Expect(",");
  623. }
  624. }
  625. }
  626. Expect("]");
  627. return CreateArrayInstance(elements);
  628. }
  629. public ObjectInstance ParseJsonObject()
  630. {
  631. Expect("{");
  632. var obj = _engine.Object.Construct(Arguments.Empty);
  633. while (!Match("}"))
  634. {
  635. Tokens type = _lookahead.Type;
  636. if (type != Tokens.String)
  637. {
  638. ThrowUnexpected(Lex());
  639. }
  640. var name = Lex().Value.ToString();
  641. if (PropertyNameContainsInvalidChar0To31(name))
  642. {
  643. throw new JavaScriptException(_engine.SyntaxError, string.Format("Invalid character in property name '{0}'", name));
  644. }
  645. Expect(":");
  646. var value = ParseJsonValue();
  647. obj.FastAddProperty(name, value, true, true, true);
  648. if (!Match("}"))
  649. {
  650. Expect(",");
  651. }
  652. }
  653. Expect("}");
  654. return obj;
  655. }
  656. private bool PropertyNameContainsInvalidChar0To31(string s)
  657. {
  658. const int max = 31;
  659. for (var i = 0; i < s.Length; i++)
  660. {
  661. var val = (int)s[i];
  662. if (val <= max)
  663. return true;
  664. }
  665. return false;
  666. }
  667. /// <summary>
  668. /// Optimization.
  669. /// By calling Lex().Value for each type, we parse the token twice.
  670. /// It was already parsed by the peek() method.
  671. /// _lookahead.Value already contain the value.
  672. /// </summary>
  673. /// <returns></returns>
  674. private JsValue ParseJsonValue()
  675. {
  676. Tokens type = _lookahead.Type;
  677. MarkStart();
  678. switch (type)
  679. {
  680. case Tokens.NullLiteral:
  681. var v = Lex().Value;
  682. return Null.Instance;
  683. case Tokens.BooleanLiteral:
  684. return new JsValue((bool)Lex().Value);
  685. case Tokens.String:
  686. return new JsValue((string)Lex().Value);
  687. case Tokens.Number:
  688. return new JsValue((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 (!System.String.IsNullOrEmpty(options.Source))
  732. {
  733. _extra.Source = options.Source;
  734. }
  735. if (options.Tokens)
  736. {
  737. _extra.Tokens = new List<Token>();
  738. }
  739. }
  740. try
  741. {
  742. MarkStart();
  743. Peek();
  744. JsValue jsv = ParseJsonValue();
  745. Peek();
  746. Tokens type = _lookahead.Type;
  747. object value = _lookahead.Value;
  748. if(_lookahead.Type != Tokens.EOF)
  749. {
  750. throw new JavaScriptException(_engine.SyntaxError, string.Format("Unexpected {0} {1}", _lookahead.Type, _lookahead.Value));
  751. }
  752. return jsv;
  753. }
  754. finally
  755. {
  756. _extra = new Extra();
  757. }
  758. }
  759. private class Extra
  760. {
  761. public int? Loc;
  762. public int[] Range;
  763. public string Source;
  764. public List<Token> Tokens;
  765. }
  766. private enum Tokens
  767. {
  768. NullLiteral,
  769. BooleanLiteral,
  770. String,
  771. Number,
  772. Punctuator,
  773. EOF,
  774. };
  775. class Token
  776. {
  777. public Tokens Type;
  778. public object Value;
  779. public int[] Range;
  780. public int? LineNumber;
  781. public int LineStart;
  782. }
  783. static class Messages
  784. {
  785. public const string UnexpectedToken = "Unexpected token {0}";
  786. public const string UnexpectedNumber = "Unexpected number";
  787. public const string UnexpectedString = "Unexpected string";
  788. public const string UnexpectedEOS = "Unexpected end of input";
  789. };
  790. }
  791. }