From 33ab1d84cdfa0406b73a59b56a9b27062d9ccacf Mon Sep 17 00:00:00 2001 From: Philipp Klose Date: Thu, 9 Dec 2010 12:23:24 +0100 Subject: [PATCH] works with hxcpp --- hxjson2/JSONDecoder.hx | 70 ++++++++++++++++++++-------------------- hxjson2/JSONToken.hx | 2 +- hxjson2/JSONTokenizer.hx | 24 +++++++------- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/hxjson2/JSONDecoder.hx b/hxjson2/JSONDecoder.hx index ddf7e3b..de370fd 100644 --- a/hxjson2/JSONDecoder.hx +++ b/hxjson2/JSONDecoder.hx @@ -41,19 +41,19 @@ package hxjson2 ; enum JSONTokenType { - UNKNOWN; - COMMA; - LEFT_BRACE; - RIGHT_BRACE; - LEFT_BRACKET; - RIGHT_BRACKET; - COLON; - TRUE; - FALSE; - NULL; - STRING; - NUMBER; - NAN; + tUNKNOWN; + tCOMMA; + tLEFT_BRACE; + tRIGHT_BRACE; + tLEFT_BRACKET; + tRIGHT_BRACKET; + tCOLON; + tTRUE; + tFALSE; + tNULL; + tSTRING; + tNUMBER; + tNAN; } class JSONDecoder { @@ -114,15 +114,15 @@ class JSONDecoder { // past the opening [ nextToken(); // check to see if we have an empty array - if ( token.type == RIGHT_BRACKET ) { + if ( token.type == tRIGHT_BRACKET ) { // we're done reading the array, so return it return a; } else { - if (!strict && token.type == JSONTokenType.COMMA) { + if (!strict && token.type == JSONTokenType.tCOMMA) { nextToken(); // check to see if we're reached the end of the array - if ( token.type == JSONTokenType.RIGHT_BRACKET ){ + if ( token.type == JSONTokenType.tRIGHT_BRACKET ){ return a; } else { @@ -137,17 +137,17 @@ class JSONDecoder { a.push ( parseValue() ); // after the value there should be a ] or a , nextToken(); - if ( token.type == RIGHT_BRACKET ) { + if ( token.type == tRIGHT_BRACKET ) { // we're done reading the array, so return it return a; - } else if ( token.type == COMMA ) { + } else if ( token.type == tCOMMA ) { // move past the comma and read another value nextToken(); // Allow arrays to have a comma after the last element // if the decoder is not in strict mode if ( !strict ){ // Reached ",]" as the end of the array, so return it - if ( token.type == JSONTokenType.RIGHT_BRACKET ){ + if ( token.type == JSONTokenType.tRIGHT_BRACKET ){ return a; } } @@ -171,17 +171,17 @@ class JSONDecoder { // grab the next token from the tokenizer nextToken(); // check to see if we have an empty object - if ( token.type == RIGHT_BRACE ) { + if ( token.type == tRIGHT_BRACE ) { // we're done reading the object, so return it return o; } // in non-strict mode an empty object is also a comma // followed by a right bracket else { - if ( !strict && token.type == JSONTokenType.COMMA ) { + if ( !strict && token.type == JSONTokenType.tCOMMA ) { // move past the comma nextToken(); // check to see if we're reached the end of the object - if ( token.type == JSONTokenType.RIGHT_BRACE ) { + if ( token.type == JSONTokenType.tRIGHT_BRACE ) { return o; } else { @@ -192,23 +192,23 @@ class JSONDecoder { // deal with members of the object, and use an "infinite" // loop because we could have any amount of members while ( true ) { - if ( token.type == STRING ) { + if ( token.type == tSTRING ) { // the string value we read is the key for the object key = Std.string(token.value); // move past the string to see what's next nextToken(); // after the string there should be a : - if ( token.type == COLON ) { + if ( token.type == tCOLON ) { // move past the : and read/assign a value for the key nextToken(); Reflect.setField(o,key,parseValue()); // move past the value to see what's next nextToken(); // after the value there's either a } or a , - if ( token.type == RIGHT_BRACE ) { + if ( token.type == tRIGHT_BRACE ) { // // we're done reading the object, so return it return o; - } else if ( token.type == COMMA ) { + } else if ( token.type == tCOMMA ) { // skip past the comma and read another member nextToken(); @@ -216,7 +216,7 @@ class JSONDecoder { // if the decoder is not in strict mode if ( !strict ){ // Reached ",}" as the end of the object, so return it - if ( token.type == JSONTokenType.RIGHT_BRACE ) { + if ( token.type == JSONTokenType.tRIGHT_BRACE ) { return o; } } @@ -241,21 +241,21 @@ class JSONDecoder { if ( token == null ) tokenizer.parseError( "Unexpected end of input" ); switch ( token.type ) { - case LEFT_BRACE: + case tLEFT_BRACE: return parseObject(); - case LEFT_BRACKET: + case tLEFT_BRACKET: return parseArray(); - case STRING: + case tSTRING: return token.value; - case NUMBER: + case tNUMBER: return token.value; - case TRUE: + case tTRUE: return true; - case FALSE: + case tFALSE: return false; - case NULL: + case tNULL: return null; - case NAN: + case tNAN: if (!strict) return token.value; else diff --git a/hxjson2/JSONToken.hx b/hxjson2/JSONToken.hx index 856d389..d270e62 100644 --- a/hxjson2/JSONToken.hx +++ b/hxjson2/JSONToken.hx @@ -56,7 +56,7 @@ class JSONToken { * @param value The value of the token */ public function new(?type:JSONTokenType,?value:Dynamic = null) { - this.type = type==null?UNKNOWN:type; + this.type = type==null?tUNKNOWN:type; this.value = value; } } diff --git a/hxjson2/JSONTokenizer.hx b/hxjson2/JSONTokenizer.hx index f92d4ee..f531cb0 100644 --- a/hxjson2/JSONTokenizer.hx +++ b/hxjson2/JSONTokenizer.hx @@ -87,33 +87,33 @@ class JSONTokenizer { // examine the new character and see what we have... switch ( ch ) { case '{': - token.type = LEFT_BRACE; + token.type = tLEFT_BRACE; token.value = '{'; nextChar(); case '}': - token.type = RIGHT_BRACE; + token.type = tRIGHT_BRACE; token.value = '}'; nextChar(); case '[': - token.type = LEFT_BRACKET; + token.type = tLEFT_BRACKET; token.value = '['; nextChar(); case ']': - token.type = RIGHT_BRACKET; + token.type = tRIGHT_BRACKET; token.value = ']'; nextChar(); case ',': - token.type = COMMA; + token.type = tCOMMA; token.value = ','; nextChar(); case ':': - token.type = COLON; + token.type = tCOLON; token.value = ':'; nextChar(); case 't': // attempt to read true var possibleTrue:String = "t" + nextChar() + nextChar() + nextChar(); if ( possibleTrue == "true" ) { - token.type = TRUE; + token.type = tTRUE; token.value = true; nextChar(); } else { @@ -122,7 +122,7 @@ class JSONTokenizer { case 'f': // attempt to read false var possibleFalse:String = "f" + nextChar() + nextChar() + nextChar() + nextChar(); if ( possibleFalse == "false" ) { - token.type = FALSE; + token.type = tFALSE; token.value = false; nextChar(); } else { @@ -131,7 +131,7 @@ class JSONTokenizer { case 'n': // attempt to read null var possibleNull:String = "n" + nextChar() + nextChar() + nextChar(); if ( possibleNull == "null" ) { - token.type = NULL; + token.type = tNULL; token.value = null; nextChar(); } else { @@ -140,7 +140,7 @@ class JSONTokenizer { case 'N': //attempt to read NAN var possibleNAN:String = 'N' + nextChar() + nextChar(); if (possibleNAN == "NAN" || possibleNAN == "NaN") { - token.type = NAN; + token.type = tNAN; token.value = Math.NaN; nextChar(); } @@ -252,7 +252,7 @@ class JSONTokenizer { nextChar(); // the token for the string we'll try to read var token:JSONToken = new JSONToken(); - token.type = STRING; + token.type = tSTRING; // attach to the string to the token so we can return it token.value = string; return token; @@ -384,7 +384,7 @@ class JSONTokenizer { if ( Math.isFinite( num ) && !Math.isNaN( num ) ) { // the token for the number we'll try to read var token:JSONToken = new JSONToken(); - token.type = NUMBER; + token.type = tNUMBER; token.value = num; return token; } else {