/src/com/google/maps/extras/arcgislink/json/JSONTokenizer.as

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 654 lines · 423 code · 73 blank · 158 comment · 68 complexity · 69c26a5c7131bd3f8b72722bbd018947 MD5 · raw file

  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. //http://code.google.com/p/as3corelib/
  28. //package com.adobe.serialization.json
  29. package com.google.maps.extras.arcgislink.json {
  30. public class JSONTokenizer {
  31. /**
  32. * Flag indicating if the tokenizer should only recognize
  33. * standard JSON tokens. Setting to <code>false</code> allows
  34. * tokens such as NaN and allows numbers to be formatted as
  35. * hex, etc.
  36. */
  37. private var strict:Boolean;
  38. /** The object that will get parsed from the JSON string */
  39. private var obj:Object;
  40. /** The JSON string to be parsed */
  41. private var jsonString:String;
  42. /** The current parsing location in the JSON string */
  43. private var loc:int;
  44. /** The current character in the JSON string during parsing */
  45. private var ch:String;
  46. /**
  47. * Constructs a new JSONDecoder to parse a JSON string
  48. * into a native object.
  49. *
  50. * @param s The JSON string to be converted
  51. * into a native object
  52. */
  53. public function JSONTokenizer( s:String, strict:Boolean )
  54. {
  55. jsonString = s;
  56. this.strict = strict;
  57. loc = 0;
  58. // prime the pump by getting the first character
  59. nextChar();
  60. }
  61. /**
  62. * Gets the next token in the input sting and advances
  63. * the character to the next character after the token
  64. */
  65. public function getNextToken():JSONToken
  66. {
  67. var token:JSONToken = new JSONToken();
  68. // skip any whitespace / comments since the last
  69. // token was read
  70. skipIgnored();
  71. // examine the new character and see what we have...
  72. switch ( ch )
  73. {
  74. case '{':
  75. token.type = JSONTokenType.LEFT_BRACE;
  76. token.value = '{';
  77. nextChar();
  78. break
  79. case '}':
  80. token.type = JSONTokenType.RIGHT_BRACE;
  81. token.value = '}';
  82. nextChar();
  83. break
  84. case '[':
  85. token.type = JSONTokenType.LEFT_BRACKET;
  86. token.value = '[';
  87. nextChar();
  88. break
  89. case ']':
  90. token.type = JSONTokenType.RIGHT_BRACKET;
  91. token.value = ']';
  92. nextChar();
  93. break
  94. case ',':
  95. token.type = JSONTokenType.COMMA;
  96. token.value = ',';
  97. nextChar();
  98. break
  99. case ':':
  100. token.type = JSONTokenType.COLON;
  101. token.value = ':';
  102. nextChar();
  103. break;
  104. case 't': // attempt to read true
  105. var possibleTrue:String = "t" + nextChar() + nextChar() + nextChar();
  106. if ( possibleTrue == "true" )
  107. {
  108. token.type = JSONTokenType.TRUE;
  109. token.value = true;
  110. nextChar();
  111. }
  112. else
  113. {
  114. parseError( "Expecting 'true' but found " + possibleTrue );
  115. }
  116. break;
  117. case 'f': // attempt to read false
  118. var possibleFalse:String = "f" + nextChar() + nextChar() + nextChar() + nextChar();
  119. if ( possibleFalse == "false" )
  120. {
  121. token.type = JSONTokenType.FALSE;
  122. token.value = false;
  123. nextChar();
  124. }
  125. else
  126. {
  127. parseError( "Expecting 'false' but found " + possibleFalse );
  128. }
  129. break;
  130. case 'n': // attempt to read null
  131. var possibleNull:String = "n" + nextChar() + nextChar() + nextChar();
  132. if ( possibleNull == "null" )
  133. {
  134. token.type = JSONTokenType.NULL;
  135. token.value = null;
  136. nextChar();
  137. }
  138. else
  139. {
  140. parseError( "Expecting 'null' but found " + possibleNull );
  141. }
  142. break;
  143. case 'N': // attempt to read NaN
  144. var possibleNaN:String = "N" + nextChar() + nextChar();
  145. if ( possibleNaN == "NaN" )
  146. {
  147. token.type = JSONTokenType.NAN;
  148. token.value = NaN;
  149. nextChar();
  150. }
  151. else
  152. {
  153. parseError( "Expecting 'NaN' but found " + possibleNaN );
  154. }
  155. break;
  156. case '"': // the start of a string
  157. token = readString();
  158. break;
  159. default:
  160. // see if we can read a number
  161. if ( isDigit( ch ) || ch == '-' )
  162. {
  163. token = readNumber();
  164. }
  165. else if ( ch == '' )
  166. {
  167. // check for reading past the end of the string
  168. return null;
  169. }
  170. else
  171. {
  172. // not sure what was in the input string - it's not
  173. // anything we expected
  174. parseError( "Unexpected " + ch + " encountered" );
  175. }
  176. }
  177. return token;
  178. }
  179. /**
  180. * Attempts to read a string from the input string. Places
  181. * the character location at the first character after the
  182. * string. It is assumed that ch is " before this method is called.
  183. *
  184. * @return the JSONToken with the string value if a string could
  185. * be read. Throws an error otherwise.
  186. */
  187. private function readString():JSONToken
  188. {
  189. // the string to store the string we'll try to read
  190. var string:String = "";
  191. // advance past the first "
  192. nextChar();
  193. while ( ch != '"' && ch != '' )
  194. {
  195. // unescape the escape sequences in the string
  196. if ( ch == '\\' )
  197. {
  198. // get the next character so we know what
  199. // to unescape
  200. nextChar();
  201. switch ( ch )
  202. {
  203. case '"': // quotation mark
  204. string += '"';
  205. break;
  206. case '/': // solidus
  207. string += "/";
  208. break;
  209. case '\\': // reverse solidus
  210. string += '\\';
  211. break;
  212. case 'b': // bell
  213. string += '\b';
  214. break;
  215. case 'f': // form feed
  216. string += '\f';
  217. break;
  218. case 'n': // newline
  219. string += '\n';
  220. break;
  221. case 'r': // carriage return
  222. string += '\r';
  223. break;
  224. case 't': // horizontal tab
  225. string += '\t'
  226. break;
  227. case 'u':
  228. // convert a unicode escape sequence
  229. // to it's character value - expecting
  230. // 4 hex digits
  231. // save the characters as a string we'll convert to an int
  232. var hexValue:String = "";
  233. // try to find 4 hex characters
  234. for ( var i:int = 0; i < 4; i++ )
  235. {
  236. // get the next character and determine
  237. // if it's a valid hex digit or not
  238. if ( !isHexDigit( nextChar() ) )
  239. {
  240. parseError( " Excepted a hex digit, but found: " + ch );
  241. }
  242. // valid, add it to the value
  243. hexValue += ch;
  244. }
  245. // convert hexValue to an integer, and use that
  246. // integrer value to create a character to add
  247. // to our string.
  248. string += String.fromCharCode( parseInt( hexValue, 16 ) );
  249. break;
  250. default:
  251. // couldn't unescape the sequence, so just
  252. // pass it through
  253. string += '\\' + ch;
  254. }
  255. }
  256. else
  257. {
  258. // didn't have to unescape, so add the character to the string
  259. string += ch;
  260. }
  261. // move to the next character
  262. nextChar();
  263. }
  264. // we read past the end of the string without closing it, which
  265. // is a parse error
  266. if ( ch == '' )
  267. {
  268. parseError( "Unterminated string literal" );
  269. }
  270. // move past the closing " in the input string
  271. nextChar();
  272. // the token for the string we've just read
  273. var token:JSONToken = new JSONToken();
  274. token.type = JSONTokenType.STRING;
  275. // attach to the string to the token so we can return it
  276. token.value = string;
  277. return token;
  278. }
  279. /**
  280. * Attempts to read a number from the input string. Places
  281. * the character location at the first character after the
  282. * number.
  283. *
  284. * @return The JSONToken with the number value if a number could
  285. * be read. Throws an error otherwise.
  286. */
  287. private function readNumber():JSONToken
  288. {
  289. // the string to accumulate the number characters
  290. // into that we'll convert to a number at the end
  291. var input:String = "";
  292. // check for a negative number
  293. if ( ch == '-' )
  294. {
  295. input += '-';
  296. nextChar();
  297. }
  298. // the number must start with a digit
  299. if ( !isDigit( ch ) )
  300. {
  301. parseError( "Expecting a digit" );
  302. }
  303. // 0 can only be the first digit if it
  304. // is followed by a decimal point
  305. if ( ch == '0' )
  306. {
  307. input += ch;
  308. nextChar();
  309. // make sure no other digits come after 0
  310. if ( isDigit( ch ) )
  311. {
  312. parseError( "A digit cannot immediately follow 0" );
  313. }
  314. // unless we have 0x which starts a hex number, but this
  315. // doesn't match JSON spec so check for not strict mode.
  316. else if ( !strict && ch == 'x' )
  317. {
  318. // include the x in the input
  319. input += ch;
  320. nextChar();
  321. // need at least one hex digit after 0x to
  322. // be valid
  323. if ( isHexDigit( ch ) )
  324. {
  325. input += ch;
  326. nextChar();
  327. }
  328. else
  329. {
  330. parseError( "Number in hex format require at least one hex digit after \"0x\"" );
  331. }
  332. // consume all of the hex values
  333. while ( isHexDigit( ch ) )
  334. {
  335. input += ch;
  336. nextChar();
  337. }
  338. }
  339. }
  340. else
  341. {
  342. // read numbers while we can
  343. while ( isDigit( ch ) )
  344. {
  345. input += ch;
  346. nextChar();
  347. }
  348. }
  349. // check for a decimal value
  350. if ( ch == '.' )
  351. {
  352. input += '.';
  353. nextChar();
  354. // after the decimal there has to be a digit
  355. if ( !isDigit( ch ) )
  356. {
  357. parseError( "Expecting a digit" );
  358. }
  359. // read more numbers to get the decimal value
  360. while ( isDigit( ch ) )
  361. {
  362. input += ch;
  363. nextChar();
  364. }
  365. }
  366. // check for scientific notation
  367. if ( ch == 'e' || ch == 'E' )
  368. {
  369. input += "e"
  370. nextChar();
  371. // check for sign
  372. if ( ch == '+' || ch == '-' )
  373. {
  374. input += ch;
  375. nextChar();
  376. }
  377. // require at least one number for the exponent
  378. // in this case
  379. if ( !isDigit( ch ) )
  380. {
  381. parseError( "Scientific notation number needs exponent value" );
  382. }
  383. // read in the exponent
  384. while ( isDigit( ch ) )
  385. {
  386. input += ch;
  387. nextChar();
  388. }
  389. }
  390. // convert the string to a number value
  391. var num:Number = Number( input );
  392. if ( isFinite( num ) && !isNaN( num ) )
  393. {
  394. // the token for the number that we've read
  395. var token:JSONToken = new JSONToken();
  396. token.type = JSONTokenType.NUMBER;
  397. token.value = num;
  398. return token;
  399. }
  400. else
  401. {
  402. parseError( "Number " + num + " is not valid!" );
  403. }
  404. return null;
  405. }
  406. /**
  407. * Reads the next character in the input
  408. * string and advances the character location.
  409. *
  410. * @return The next character in the input string, or
  411. * null if we've read past the end.
  412. */
  413. private function nextChar():String
  414. {
  415. return ch = jsonString.charAt( loc++ );
  416. }
  417. /**
  418. * Advances the character location past any
  419. * sort of white space and comments
  420. */
  421. private function skipIgnored():void
  422. {
  423. var originalLoc:int;
  424. // keep trying to skip whitespace and comments as long
  425. // as we keep advancing past the original location
  426. do
  427. {
  428. originalLoc = loc;
  429. skipWhite();
  430. skipComments();
  431. }
  432. while ( originalLoc != loc );
  433. }
  434. /**
  435. * Skips comments in the input string, either
  436. * single-line or multi-line. Advances the character
  437. * to the first position after the end of the comment.
  438. */
  439. private function skipComments():void
  440. {
  441. if ( ch == '/' )
  442. {
  443. // Advance past the first / to find out what type of comment
  444. nextChar();
  445. switch ( ch )
  446. {
  447. case '/': // single-line comment, read through end of line
  448. // Loop over the characters until we find
  449. // a newline or until there's no more characters left
  450. do
  451. {
  452. nextChar();
  453. }
  454. while ( ch != '\n' && ch != '' )
  455. // move past the \n
  456. nextChar();
  457. break;
  458. case '*': // multi-line comment, read until closing */
  459. // move past the opening *
  460. nextChar();
  461. // try to find a trailing */
  462. while ( true )
  463. {
  464. if ( ch == '*' )
  465. {
  466. // check to see if we have a closing /
  467. nextChar();
  468. if ( ch == '/')
  469. {
  470. // move past the end of the closing */
  471. nextChar();
  472. break;
  473. }
  474. }
  475. else
  476. {
  477. // move along, looking if the next character is a *
  478. nextChar();
  479. }
  480. // when we're here we've read past the end of
  481. // the string without finding a closing */, so error
  482. if ( ch == '' )
  483. {
  484. parseError( "Multi-line comment not closed" );
  485. }
  486. }
  487. break;
  488. // Can't match a comment after a /, so it's a parsing error
  489. default:
  490. parseError( "Unexpected " + ch + " encountered (expecting '/' or '*' )" );
  491. }
  492. }
  493. }
  494. /**
  495. * Skip any whitespace in the input string and advances
  496. * the character to the first character after any possible
  497. * whitespace.
  498. */
  499. private function skipWhite():void
  500. {
  501. // As long as there are spaces in the input
  502. // stream, advance the current location pointer
  503. // past them
  504. while ( isWhiteSpace( ch ) )
  505. {
  506. nextChar();
  507. }
  508. }
  509. /**
  510. * Determines if a character is whitespace or not.
  511. *
  512. * @return True if the character passed in is a whitespace
  513. * character
  514. */
  515. private function isWhiteSpace( ch:String ):Boolean
  516. {
  517. return ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
  518. }
  519. /**
  520. * Determines if a character is a digit [0-9].
  521. *
  522. * @return True if the character passed in is a digit
  523. */
  524. private function isDigit( ch:String ):Boolean
  525. {
  526. return ( ch >= '0' && ch <= '9' );
  527. }
  528. /**
  529. * Determines if a character is a digit [0-9].
  530. *
  531. * @return True if the character passed in is a digit
  532. */
  533. private function isHexDigit( ch:String ):Boolean
  534. {
  535. // get the uppercase value of ch so we only have
  536. // to compare the value between 'A' and 'F'
  537. var uc:String = ch.toUpperCase();
  538. // a hex digit is a digit of A-F, inclusive ( using
  539. // our uppercase constraint )
  540. return ( isDigit( ch ) || ( uc >= 'A' && uc <= 'F' ) );
  541. }
  542. /**
  543. * Raises a parsing error with a specified message, tacking
  544. * on the error location and the original string.
  545. *
  546. * @param message The message indicating why the error occurred
  547. */
  548. public function parseError( message:String ):void
  549. {
  550. throw new JSONParseError( message, loc, jsonString );
  551. }
  552. }
  553. }