Skip to content

Commit

Permalink
works with hxcpp
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHippo committed Dec 9, 2010
1 parent 70e02a6 commit 33ab1d8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
70 changes: 35 additions & 35 deletions hxjson2/JSONDecoder.hx
Expand Up @@ -41,19 +41,19 @@
package hxjson2 ; package hxjson2 ;


enum JSONTokenType { enum JSONTokenType {
UNKNOWN; tUNKNOWN;
COMMA; tCOMMA;
LEFT_BRACE; tLEFT_BRACE;
RIGHT_BRACE; tRIGHT_BRACE;
LEFT_BRACKET; tLEFT_BRACKET;
RIGHT_BRACKET; tRIGHT_BRACKET;
COLON; tCOLON;
TRUE; tTRUE;
FALSE; tFALSE;
NULL; tNULL;
STRING; tSTRING;
NUMBER; tNUMBER;
NAN; tNAN;
} }


class JSONDecoder { class JSONDecoder {
Expand Down Expand Up @@ -114,15 +114,15 @@ class JSONDecoder {
// past the opening [ // past the opening [
nextToken(); nextToken();
// check to see if we have an empty array // 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 // we're done reading the array, so return it
return a; return a;
} }
else { else {
if (!strict && token.type == JSONTokenType.COMMA) { if (!strict && token.type == JSONTokenType.tCOMMA) {
nextToken(); nextToken();
// check to see if we're reached the end of the array // 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; return a;
} }
else { else {
Expand All @@ -137,17 +137,17 @@ class JSONDecoder {
a.push ( parseValue() ); a.push ( parseValue() );
// after the value there should be a ] or a , // after the value there should be a ] or a ,
nextToken(); nextToken();
if ( token.type == RIGHT_BRACKET ) { if ( token.type == tRIGHT_BRACKET ) {
// we're done reading the array, so return it // we're done reading the array, so return it
return a; return a;
} else if ( token.type == COMMA ) { } else if ( token.type == tCOMMA ) {
// move past the comma and read another value // move past the comma and read another value
nextToken(); nextToken();
// Allow arrays to have a comma after the last element // Allow arrays to have a comma after the last element
// if the decoder is not in strict mode // if the decoder is not in strict mode
if ( !strict ){ if ( !strict ){
// Reached ",]" as the end of the array, so return it // Reached ",]" as the end of the array, so return it
if ( token.type == JSONTokenType.RIGHT_BRACKET ){ if ( token.type == JSONTokenType.tRIGHT_BRACKET ){
return a; return a;
} }
} }
Expand All @@ -171,17 +171,17 @@ class JSONDecoder {
// grab the next token from the tokenizer // grab the next token from the tokenizer
nextToken(); nextToken();
// check to see if we have an empty object // 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 // we're done reading the object, so return it
return o; return o;
} // in non-strict mode an empty object is also a comma } // in non-strict mode an empty object is also a comma
// followed by a right bracket // followed by a right bracket
else { else {
if ( !strict && token.type == JSONTokenType.COMMA ) { if ( !strict && token.type == JSONTokenType.tCOMMA ) {
// move past the comma // move past the comma
nextToken(); nextToken();
// check to see if we're reached the end of the object // 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; return o;
} }
else { else {
Expand All @@ -192,31 +192,31 @@ class JSONDecoder {
// deal with members of the object, and use an "infinite" // deal with members of the object, and use an "infinite"
// loop because we could have any amount of members // loop because we could have any amount of members
while ( true ) { while ( true ) {
if ( token.type == STRING ) { if ( token.type == tSTRING ) {
// the string value we read is the key for the object // the string value we read is the key for the object
key = Std.string(token.value); key = Std.string(token.value);
// move past the string to see what's next // move past the string to see what's next
nextToken(); nextToken();
// after the string there should be a : // 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 // move past the : and read/assign a value for the key
nextToken(); nextToken();
Reflect.setField(o,key,parseValue()); Reflect.setField(o,key,parseValue());
// move past the value to see what's next // move past the value to see what's next
nextToken(); nextToken();
// after the value there's either a } or a , // 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 // // we're done reading the object, so return it
return o; return o;
} else if ( token.type == COMMA ) { } else if ( token.type == tCOMMA ) {
// skip past the comma and read another member // skip past the comma and read another member
nextToken(); nextToken();


// Allow objects to have a comma after the last member // Allow objects to have a comma after the last member
// if the decoder is not in strict mode // if the decoder is not in strict mode
if ( !strict ){ if ( !strict ){
// Reached ",}" as the end of the object, so return it // Reached ",}" as the end of the object, so return it
if ( token.type == JSONTokenType.RIGHT_BRACE ) { if ( token.type == JSONTokenType.tRIGHT_BRACE ) {
return o; return o;
} }
} }
Expand All @@ -241,21 +241,21 @@ class JSONDecoder {
if ( token == null ) if ( token == null )
tokenizer.parseError( "Unexpected end of input" ); tokenizer.parseError( "Unexpected end of input" );
switch ( token.type ) { switch ( token.type ) {
case LEFT_BRACE: case tLEFT_BRACE:
return parseObject(); return parseObject();
case LEFT_BRACKET: case tLEFT_BRACKET:
return parseArray(); return parseArray();
case STRING: case tSTRING:
return token.value; return token.value;
case NUMBER: case tNUMBER:
return token.value; return token.value;
case TRUE: case tTRUE:
return true; return true;
case FALSE: case tFALSE:
return false; return false;
case NULL: case tNULL:
return null; return null;
case NAN: case tNAN:
if (!strict) if (!strict)
return token.value; return token.value;
else else
Expand Down
2 changes: 1 addition & 1 deletion hxjson2/JSONToken.hx
Expand Up @@ -56,7 +56,7 @@ class JSONToken {
* @param value The value of the token * @param value The value of the token
*/ */
public function new(?type:JSONTokenType,?value:Dynamic = null) { public function new(?type:JSONTokenType,?value:Dynamic = null) {
this.type = type==null?UNKNOWN:type; this.type = type==null?tUNKNOWN:type;
this.value = value; this.value = value;
} }
} }
24 changes: 12 additions & 12 deletions hxjson2/JSONTokenizer.hx
Expand Up @@ -87,33 +87,33 @@ class JSONTokenizer {
// examine the new character and see what we have... // examine the new character and see what we have...
switch ( ch ) { switch ( ch ) {
case '{': case '{':
token.type = LEFT_BRACE; token.type = tLEFT_BRACE;
token.value = '{'; token.value = '{';
nextChar(); nextChar();
case '}': case '}':
token.type = RIGHT_BRACE; token.type = tRIGHT_BRACE;
token.value = '}'; token.value = '}';
nextChar(); nextChar();
case '[': case '[':
token.type = LEFT_BRACKET; token.type = tLEFT_BRACKET;
token.value = '['; token.value = '[';
nextChar(); nextChar();
case ']': case ']':
token.type = RIGHT_BRACKET; token.type = tRIGHT_BRACKET;
token.value = ']'; token.value = ']';
nextChar(); nextChar();
case ',': case ',':
token.type = COMMA; token.type = tCOMMA;
token.value = ','; token.value = ',';
nextChar(); nextChar();
case ':': case ':':
token.type = COLON; token.type = tCOLON;
token.value = ':'; token.value = ':';
nextChar(); nextChar();
case 't': // attempt to read true case 't': // attempt to read true
var possibleTrue:String = "t" + nextChar() + nextChar() + nextChar(); var possibleTrue:String = "t" + nextChar() + nextChar() + nextChar();
if ( possibleTrue == "true" ) { if ( possibleTrue == "true" ) {
token.type = TRUE; token.type = tTRUE;
token.value = true; token.value = true;
nextChar(); nextChar();
} else { } else {
Expand All @@ -122,7 +122,7 @@ class JSONTokenizer {
case 'f': // attempt to read false case 'f': // attempt to read false
var possibleFalse:String = "f" + nextChar() + nextChar() + nextChar() + nextChar(); var possibleFalse:String = "f" + nextChar() + nextChar() + nextChar() + nextChar();
if ( possibleFalse == "false" ) { if ( possibleFalse == "false" ) {
token.type = FALSE; token.type = tFALSE;
token.value = false; token.value = false;
nextChar(); nextChar();
} else { } else {
Expand All @@ -131,7 +131,7 @@ class JSONTokenizer {
case 'n': // attempt to read null case 'n': // attempt to read null
var possibleNull:String = "n" + nextChar() + nextChar() + nextChar(); var possibleNull:String = "n" + nextChar() + nextChar() + nextChar();
if ( possibleNull == "null" ) { if ( possibleNull == "null" ) {
token.type = NULL; token.type = tNULL;
token.value = null; token.value = null;
nextChar(); nextChar();
} else { } else {
Expand All @@ -140,7 +140,7 @@ class JSONTokenizer {
case 'N': //attempt to read NAN case 'N': //attempt to read NAN
var possibleNAN:String = 'N' + nextChar() + nextChar(); var possibleNAN:String = 'N' + nextChar() + nextChar();
if (possibleNAN == "NAN" || possibleNAN == "NaN") { if (possibleNAN == "NAN" || possibleNAN == "NaN") {
token.type = NAN; token.type = tNAN;
token.value = Math.NaN; token.value = Math.NaN;
nextChar(); nextChar();
} }
Expand Down Expand Up @@ -252,7 +252,7 @@ class JSONTokenizer {
nextChar(); nextChar();
// the token for the string we'll try to read // the token for the string we'll try to read
var token:JSONToken = new JSONToken(); var token:JSONToken = new JSONToken();
token.type = STRING; token.type = tSTRING;
// attach to the string to the token so we can return it // attach to the string to the token so we can return it
token.value = string; token.value = string;
return token; return token;
Expand Down Expand Up @@ -384,7 +384,7 @@ class JSONTokenizer {
if ( Math.isFinite( num ) && !Math.isNaN( num ) ) { if ( Math.isFinite( num ) && !Math.isNaN( num ) ) {
// the token for the number we'll try to read // the token for the number we'll try to read
var token:JSONToken = new JSONToken(); var token:JSONToken = new JSONToken();
token.type = NUMBER; token.type = tNUMBER;
token.value = num; token.value = num;
return token; return token;
} else { } else {
Expand Down

0 comments on commit 33ab1d8

Please sign in to comment.