Lexer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. #region Header
  2. /**
  3. * Lexer.cs
  4. * JSON lexer implementation based on a finite state machine.
  5. *
  6. * The authors disclaim copyright to this source code. For more details, see
  7. * the COPYING file included with this distribution.
  8. **/
  9. #endregion
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Text;
  14. using Best.HTTP.Shared.PlatformSupport.Text;
  15. namespace Best.HTTP.JSON.LitJson
  16. {
  17. internal sealed class FsmContext
  18. {
  19. public bool Return;
  20. public int NextState;
  21. public Lexer L;
  22. public int StateStack;
  23. }
  24. internal sealed class Lexer
  25. {
  26. #region Fields
  27. private delegate bool StateHandler (FsmContext ctx);
  28. private static readonly int[] fsm_return_table;
  29. private static readonly StateHandler[] fsm_handler_table;
  30. private bool allow_comments;
  31. private bool allow_single_quoted_strings;
  32. private bool end_of_input;
  33. private FsmContext fsm_context;
  34. private int input_buffer;
  35. private int input_char;
  36. private TextReader reader;
  37. private int state;
  38. private StringBuilder string_buffer;
  39. private string string_value;
  40. private int token;
  41. private int unichar;
  42. #endregion
  43. #region Properties
  44. public bool AllowComments {
  45. get { return allow_comments; }
  46. set { allow_comments = value; }
  47. }
  48. public bool AllowSingleQuotedStrings {
  49. get { return allow_single_quoted_strings; }
  50. set { allow_single_quoted_strings = value; }
  51. }
  52. public bool EndOfInput {
  53. get { return end_of_input; }
  54. }
  55. public int Token {
  56. get { return token; }
  57. }
  58. public string StringValue {
  59. get { return string_value; }
  60. }
  61. #endregion
  62. #region Constructors
  63. static Lexer ()
  64. {
  65. PopulateFsmTables (out fsm_handler_table, out fsm_return_table);
  66. }
  67. public Lexer (TextReader reader)
  68. {
  69. allow_comments = true;
  70. allow_single_quoted_strings = true;
  71. input_buffer = 0;
  72. string_buffer = StringBuilderPool.Get(128); // new StringBuilder (128);
  73. state = 1;
  74. end_of_input = false;
  75. this.reader = reader;
  76. fsm_context = new FsmContext ();
  77. fsm_context.L = this;
  78. }
  79. #endregion
  80. #region Static Methods
  81. private static int HexValue (int digit)
  82. {
  83. switch (digit) {
  84. case 'a':
  85. case 'A':
  86. return 10;
  87. case 'b':
  88. case 'B':
  89. return 11;
  90. case 'c':
  91. case 'C':
  92. return 12;
  93. case 'd':
  94. case 'D':
  95. return 13;
  96. case 'e':
  97. case 'E':
  98. return 14;
  99. case 'f':
  100. case 'F':
  101. return 15;
  102. default:
  103. return digit - '0';
  104. }
  105. }
  106. private static void PopulateFsmTables (out StateHandler[] fsm_handler_table, out int[] fsm_return_table)
  107. {
  108. // See section A.1. of the manual for details of the finite
  109. // state machine.
  110. fsm_handler_table = new StateHandler[28] {
  111. State1,
  112. State2,
  113. State3,
  114. State4,
  115. State5,
  116. State6,
  117. State7,
  118. State8,
  119. State9,
  120. State10,
  121. State11,
  122. State12,
  123. State13,
  124. State14,
  125. State15,
  126. State16,
  127. State17,
  128. State18,
  129. State19,
  130. State20,
  131. State21,
  132. State22,
  133. State23,
  134. State24,
  135. State25,
  136. State26,
  137. State27,
  138. State28
  139. };
  140. fsm_return_table = new int[28] {
  141. (int) ParserToken.Char,
  142. 0,
  143. (int) ParserToken.Number,
  144. (int) ParserToken.Number,
  145. 0,
  146. (int) ParserToken.Number,
  147. 0,
  148. (int) ParserToken.Number,
  149. 0,
  150. 0,
  151. (int) ParserToken.True,
  152. 0,
  153. 0,
  154. 0,
  155. (int) ParserToken.False,
  156. 0,
  157. 0,
  158. (int) ParserToken.Null,
  159. (int) ParserToken.CharSeq,
  160. (int) ParserToken.Char,
  161. 0,
  162. 0,
  163. (int) ParserToken.CharSeq,
  164. (int) ParserToken.Char,
  165. 0,
  166. 0,
  167. 0,
  168. 0
  169. };
  170. }
  171. private static char ProcessEscChar (int esc_char)
  172. {
  173. switch (esc_char) {
  174. case '"':
  175. case '\'':
  176. case '\\':
  177. case '/':
  178. return Convert.ToChar (esc_char);
  179. case 'n':
  180. return '\n';
  181. case 't':
  182. return '\t';
  183. case 'r':
  184. return '\r';
  185. case 'b':
  186. return '\b';
  187. case 'f':
  188. return '\f';
  189. default:
  190. // Unreachable
  191. return '?';
  192. }
  193. }
  194. private static bool State1 (FsmContext ctx)
  195. {
  196. while (ctx.L.GetChar ()) {
  197. if (ctx.L.input_char == ' ' ||
  198. ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r')
  199. continue;
  200. if (ctx.L.input_char >= '1' && ctx.L.input_char <= '9') {
  201. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  202. ctx.NextState = 3;
  203. return true;
  204. }
  205. switch (ctx.L.input_char) {
  206. case '"':
  207. ctx.NextState = 19;
  208. ctx.Return = true;
  209. return true;
  210. case ',':
  211. case ':':
  212. case '[':
  213. case ']':
  214. case '{':
  215. case '}':
  216. ctx.NextState = 1;
  217. ctx.Return = true;
  218. return true;
  219. case '-':
  220. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  221. ctx.NextState = 2;
  222. return true;
  223. case '0':
  224. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  225. ctx.NextState = 4;
  226. return true;
  227. case 'f':
  228. ctx.NextState = 12;
  229. return true;
  230. case 'n':
  231. ctx.NextState = 16;
  232. return true;
  233. case 't':
  234. ctx.NextState = 9;
  235. return true;
  236. case '\'':
  237. if (! ctx.L.allow_single_quoted_strings)
  238. return false;
  239. ctx.L.input_char = '"';
  240. ctx.NextState = 23;
  241. ctx.Return = true;
  242. return true;
  243. case '/':
  244. if (! ctx.L.allow_comments)
  245. return false;
  246. ctx.NextState = 25;
  247. return true;
  248. default:
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. private static bool State2 (FsmContext ctx)
  255. {
  256. ctx.L.GetChar ();
  257. if (ctx.L.input_char >= '1' && ctx.L.input_char<= '9') {
  258. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  259. ctx.NextState = 3;
  260. return true;
  261. }
  262. switch (ctx.L.input_char) {
  263. case '0':
  264. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  265. ctx.NextState = 4;
  266. return true;
  267. default:
  268. return false;
  269. }
  270. }
  271. private static bool State3 (FsmContext ctx)
  272. {
  273. while (ctx.L.GetChar ()) {
  274. if (ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
  275. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  276. continue;
  277. }
  278. if (ctx.L.input_char == ' ' ||
  279. ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') {
  280. ctx.Return = true;
  281. ctx.NextState = 1;
  282. return true;
  283. }
  284. switch (ctx.L.input_char) {
  285. case ',':
  286. case ']':
  287. case '}':
  288. ctx.L.UngetChar ();
  289. ctx.Return = true;
  290. ctx.NextState = 1;
  291. return true;
  292. case '.':
  293. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  294. ctx.NextState = 5;
  295. return true;
  296. case 'e':
  297. case 'E':
  298. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  299. ctx.NextState = 7;
  300. return true;
  301. default:
  302. return false;
  303. }
  304. }
  305. return true;
  306. }
  307. private static bool State4 (FsmContext ctx)
  308. {
  309. ctx.L.GetChar ();
  310. if (ctx.L.input_char == ' ' ||
  311. ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') {
  312. ctx.Return = true;
  313. ctx.NextState = 1;
  314. return true;
  315. }
  316. switch (ctx.L.input_char) {
  317. case ',':
  318. case ']':
  319. case '}':
  320. ctx.L.UngetChar ();
  321. ctx.Return = true;
  322. ctx.NextState = 1;
  323. return true;
  324. case '.':
  325. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  326. ctx.NextState = 5;
  327. return true;
  328. case 'e':
  329. case 'E':
  330. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  331. ctx.NextState = 7;
  332. return true;
  333. default:
  334. return false;
  335. }
  336. }
  337. private static bool State5 (FsmContext ctx)
  338. {
  339. ctx.L.GetChar ();
  340. if (ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
  341. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  342. ctx.NextState = 6;
  343. return true;
  344. }
  345. return false;
  346. }
  347. private static bool State6 (FsmContext ctx)
  348. {
  349. while (ctx.L.GetChar ()) {
  350. if (ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
  351. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  352. continue;
  353. }
  354. if (ctx.L.input_char == ' ' ||
  355. ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') {
  356. ctx.Return = true;
  357. ctx.NextState = 1;
  358. return true;
  359. }
  360. switch (ctx.L.input_char) {
  361. case ',':
  362. case ']':
  363. case '}':
  364. ctx.L.UngetChar ();
  365. ctx.Return = true;
  366. ctx.NextState = 1;
  367. return true;
  368. case 'e':
  369. case 'E':
  370. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  371. ctx.NextState = 7;
  372. return true;
  373. default:
  374. return false;
  375. }
  376. }
  377. return true;
  378. }
  379. private static bool State7 (FsmContext ctx)
  380. {
  381. ctx.L.GetChar ();
  382. if (ctx.L.input_char >= '0' && ctx.L.input_char<= '9') {
  383. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  384. ctx.NextState = 8;
  385. return true;
  386. }
  387. switch (ctx.L.input_char) {
  388. case '+':
  389. case '-':
  390. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  391. ctx.NextState = 8;
  392. return true;
  393. default:
  394. return false;
  395. }
  396. }
  397. private static bool State8 (FsmContext ctx)
  398. {
  399. while (ctx.L.GetChar ()) {
  400. if (ctx.L.input_char >= '0' && ctx.L.input_char<= '9') {
  401. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  402. continue;
  403. }
  404. if (ctx.L.input_char == ' ' ||
  405. ctx.L.input_char >= '\t' && ctx.L.input_char<= '\r') {
  406. ctx.Return = true;
  407. ctx.NextState = 1;
  408. return true;
  409. }
  410. switch (ctx.L.input_char) {
  411. case ',':
  412. case ']':
  413. case '}':
  414. ctx.L.UngetChar ();
  415. ctx.Return = true;
  416. ctx.NextState = 1;
  417. return true;
  418. default:
  419. return false;
  420. }
  421. }
  422. return true;
  423. }
  424. private static bool State9 (FsmContext ctx)
  425. {
  426. ctx.L.GetChar ();
  427. switch (ctx.L.input_char) {
  428. case 'r':
  429. ctx.NextState = 10;
  430. return true;
  431. default:
  432. return false;
  433. }
  434. }
  435. private static bool State10 (FsmContext ctx)
  436. {
  437. ctx.L.GetChar ();
  438. switch (ctx.L.input_char) {
  439. case 'u':
  440. ctx.NextState = 11;
  441. return true;
  442. default:
  443. return false;
  444. }
  445. }
  446. private static bool State11 (FsmContext ctx)
  447. {
  448. ctx.L.GetChar ();
  449. switch (ctx.L.input_char) {
  450. case 'e':
  451. ctx.Return = true;
  452. ctx.NextState = 1;
  453. return true;
  454. default:
  455. return false;
  456. }
  457. }
  458. private static bool State12 (FsmContext ctx)
  459. {
  460. ctx.L.GetChar ();
  461. switch (ctx.L.input_char) {
  462. case 'a':
  463. ctx.NextState = 13;
  464. return true;
  465. default:
  466. return false;
  467. }
  468. }
  469. private static bool State13 (FsmContext ctx)
  470. {
  471. ctx.L.GetChar ();
  472. switch (ctx.L.input_char) {
  473. case 'l':
  474. ctx.NextState = 14;
  475. return true;
  476. default:
  477. return false;
  478. }
  479. }
  480. private static bool State14 (FsmContext ctx)
  481. {
  482. ctx.L.GetChar ();
  483. switch (ctx.L.input_char) {
  484. case 's':
  485. ctx.NextState = 15;
  486. return true;
  487. default:
  488. return false;
  489. }
  490. }
  491. private static bool State15 (FsmContext ctx)
  492. {
  493. ctx.L.GetChar ();
  494. switch (ctx.L.input_char) {
  495. case 'e':
  496. ctx.Return = true;
  497. ctx.NextState = 1;
  498. return true;
  499. default:
  500. return false;
  501. }
  502. }
  503. private static bool State16 (FsmContext ctx)
  504. {
  505. ctx.L.GetChar ();
  506. switch (ctx.L.input_char) {
  507. case 'u':
  508. ctx.NextState = 17;
  509. return true;
  510. default:
  511. return false;
  512. }
  513. }
  514. private static bool State17 (FsmContext ctx)
  515. {
  516. ctx.L.GetChar ();
  517. switch (ctx.L.input_char) {
  518. case 'l':
  519. ctx.NextState = 18;
  520. return true;
  521. default:
  522. return false;
  523. }
  524. }
  525. private static bool State18 (FsmContext ctx)
  526. {
  527. ctx.L.GetChar ();
  528. switch (ctx.L.input_char) {
  529. case 'l':
  530. ctx.Return = true;
  531. ctx.NextState = 1;
  532. return true;
  533. default:
  534. return false;
  535. }
  536. }
  537. private static bool State19 (FsmContext ctx)
  538. {
  539. while (ctx.L.GetChar ()) {
  540. switch (ctx.L.input_char) {
  541. case '"':
  542. ctx.L.UngetChar ();
  543. ctx.Return = true;
  544. ctx.NextState = 20;
  545. return true;
  546. case '\\':
  547. ctx.StateStack = 19;
  548. ctx.NextState = 21;
  549. return true;
  550. default:
  551. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  552. continue;
  553. }
  554. }
  555. return true;
  556. }
  557. private static bool State20 (FsmContext ctx)
  558. {
  559. ctx.L.GetChar ();
  560. switch (ctx.L.input_char) {
  561. case '"':
  562. ctx.Return = true;
  563. ctx.NextState = 1;
  564. return true;
  565. default:
  566. return false;
  567. }
  568. }
  569. private static bool State21 (FsmContext ctx)
  570. {
  571. ctx.L.GetChar ();
  572. switch (ctx.L.input_char) {
  573. case 'u':
  574. ctx.NextState = 22;
  575. return true;
  576. case '"':
  577. case '\'':
  578. case '/':
  579. case '\\':
  580. case 'b':
  581. case 'f':
  582. case 'n':
  583. case 'r':
  584. case 't':
  585. ctx.L.string_buffer.Append (
  586. ProcessEscChar (ctx.L.input_char));
  587. ctx.NextState = ctx.StateStack;
  588. return true;
  589. default:
  590. return false;
  591. }
  592. }
  593. private static bool State22 (FsmContext ctx)
  594. {
  595. int counter = 0;
  596. int mult = 4096;
  597. ctx.L.unichar = 0;
  598. while (ctx.L.GetChar ()) {
  599. if (ctx.L.input_char >= '0' && ctx.L.input_char <= '9' ||
  600. ctx.L.input_char >= 'A' && ctx.L.input_char <= 'F' ||
  601. ctx.L.input_char >= 'a' && ctx.L.input_char <= 'f') {
  602. ctx.L.unichar += HexValue (ctx.L.input_char) * mult;
  603. counter++;
  604. mult /= 16;
  605. if (counter == 4) {
  606. ctx.L.string_buffer.Append (
  607. Convert.ToChar (ctx.L.unichar));
  608. ctx.NextState = ctx.StateStack;
  609. return true;
  610. }
  611. continue;
  612. }
  613. return false;
  614. }
  615. return true;
  616. }
  617. private static bool State23 (FsmContext ctx)
  618. {
  619. while (ctx.L.GetChar ()) {
  620. switch (ctx.L.input_char) {
  621. case '\'':
  622. ctx.L.UngetChar ();
  623. ctx.Return = true;
  624. ctx.NextState = 24;
  625. return true;
  626. case '\\':
  627. ctx.StateStack = 23;
  628. ctx.NextState = 21;
  629. return true;
  630. default:
  631. ctx.L.string_buffer.Append ((char) ctx.L.input_char);
  632. continue;
  633. }
  634. }
  635. return true;
  636. }
  637. private static bool State24 (FsmContext ctx)
  638. {
  639. ctx.L.GetChar ();
  640. switch (ctx.L.input_char) {
  641. case '\'':
  642. ctx.L.input_char = '"';
  643. ctx.Return = true;
  644. ctx.NextState = 1;
  645. return true;
  646. default:
  647. return false;
  648. }
  649. }
  650. private static bool State25 (FsmContext ctx)
  651. {
  652. ctx.L.GetChar ();
  653. switch (ctx.L.input_char) {
  654. case '*':
  655. ctx.NextState = 27;
  656. return true;
  657. case '/':
  658. ctx.NextState = 26;
  659. return true;
  660. default:
  661. return false;
  662. }
  663. }
  664. private static bool State26 (FsmContext ctx)
  665. {
  666. while (ctx.L.GetChar ()) {
  667. if (ctx.L.input_char == '\n') {
  668. ctx.NextState = 1;
  669. return true;
  670. }
  671. }
  672. return true;
  673. }
  674. private static bool State27 (FsmContext ctx)
  675. {
  676. while (ctx.L.GetChar ()) {
  677. if (ctx.L.input_char == '*') {
  678. ctx.NextState = 28;
  679. return true;
  680. }
  681. }
  682. return true;
  683. }
  684. private static bool State28 (FsmContext ctx)
  685. {
  686. while (ctx.L.GetChar ()) {
  687. if (ctx.L.input_char == '*')
  688. continue;
  689. if (ctx.L.input_char == '/') {
  690. ctx.NextState = 1;
  691. return true;
  692. }
  693. ctx.NextState = 27;
  694. return true;
  695. }
  696. return true;
  697. }
  698. #endregion
  699. private bool GetChar ()
  700. {
  701. if ((input_char = NextChar ()) != -1)
  702. return true;
  703. end_of_input = true;
  704. return false;
  705. }
  706. private int NextChar ()
  707. {
  708. if (input_buffer != 0) {
  709. int tmp = input_buffer;
  710. input_buffer = 0;
  711. return tmp;
  712. }
  713. return reader.Read ();
  714. }
  715. public bool NextToken ()
  716. {
  717. StateHandler handler;
  718. fsm_context.Return = false;
  719. while (true) {
  720. handler = fsm_handler_table[state - 1];
  721. if (! handler (fsm_context))
  722. throw new JsonException (input_char);
  723. if (end_of_input)
  724. return false;
  725. if (fsm_context.Return) {
  726. string_value = string_buffer.ToString ();
  727. string_buffer.Remove (0, string_buffer.Length);
  728. token = fsm_return_table[state - 1];
  729. if (token == (int) ParserToken.Char)
  730. token = input_char;
  731. state = fsm_context.NextState;
  732. return true;
  733. }
  734. state = fsm_context.NextState;
  735. }
  736. }
  737. private void UngetChar ()
  738. {
  739. input_buffer = input_char;
  740. }
  741. public void Clear()
  742. {
  743. StringBuilderPool.Release(this.string_buffer);
  744. }
  745. }
  746. }