Skip to content

Commit

Permalink
[Json] Update the parser so we parse field names without quotes. This…
Browse files Browse the repository at this point in the history
… is allowable javascript, but not necessarily 'good form' json
  • Loading branch information
Whiteknight committed Apr 6, 2012
1 parent b43ea6c commit b893463
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/include/Ascii.winxed
Expand Up @@ -134,3 +134,9 @@ inline codepoint_is_digit(int c) return int
{
return ((c >= ASCII_0 && c <= ASCII_9)) // '0' <= c <= '9'
}

inline codepoint_is_alpha(int c) return int
{
return ((c >= ASCII_A && c <= ASCII_Z) ||
(c >= ASCII_a && c <= ASCII_z));
}
31 changes: 31 additions & 0 deletions src/unstable/json/Parser.winxed
Expand Up @@ -64,6 +64,22 @@ namespace Rosella.Json.Parser
if (c == ASCII_CLOSE_CURLY)
break;
}
else if (codepoint_is_alpha(c)) {
unshift_int(s, c);
string str = __parse_alphanumeric(json, s, b, len);
eat_whitespace(s, b);
c = get_next(s, b);
if (c != ASCII_COLON)
Rosella.Error.error("Bad object literal syntax at position %d", current_position(len, s, b));
var v = __parse_value(json, s, b, len);
h[str] = v;
eat_whitespace(s, b);
c = get_next(s, b);
if (c == ASCII_COMMA)
continue;
if (c == ASCII_CLOSE_CURLY)
break;
}
else if (c == ASCII_CLOSE_CURLY)
return h;
Rosella.Error.error("Unknown character in object literal '%s' at position %d", chr(c), current_position(len, s, b));
Expand Down Expand Up @@ -113,6 +129,21 @@ namespace Rosella.Json.Parser
return sb;
}

function __parse_alphanumeric(string xml, var s, var b, int len)
{
var sb = new 'StringBuilder';
while(have_more_chars(s, b)) {
int c = get_next(s, b);
if (!codepoint_is_alphanumeric(c)) {
unshift_int(s, c);
break;
}
push(sb, chr(c));
}
string result = string(sb);
return result;
}

function __parse_number(int c, string json, var s, var b, int len)
{
int have_e = false;
Expand Down

0 comments on commit b893463

Please sign in to comment.