pegjs/examples/json.pegjs

120 lines
2.5 KiB
Plaintext
Raw Normal View History

2010-03-22 12:18:58 +01:00
/* JSON parser based on the grammar described at http://json.org/. */
/* ===== Syntactical Elements ===== */
start
= _ object:object { return object; }
2010-03-22 12:18:58 +01:00
object
= "{" _ "}" _ { return {}; }
/ "{" _ members:members "}" _ { return members; }
2010-03-22 12:18:58 +01:00
members
= head:pair tail:("," _ pair)* {
var result = {};
result[head[0]] = head[1];
for (var i = 0; i < tail.length; i++) {
result[tail[i][2][0]] = tail[i][2][1];
}
return result;
}
2010-03-22 12:18:58 +01:00
pair
= name:string ":" _ value:value { return [name, value]; }
2010-03-22 12:18:58 +01:00
array
= "[" _ "]" _ { return []; }
/ "[" _ elements:elements "]" _ { return elements; }
2010-03-22 12:18:58 +01:00
elements
= head:value tail:("," _ value)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][2]);
}
return result;
}
2010-03-22 12:18:58 +01:00
value
= string
2010-03-22 12:18:58 +01:00
/ number
/ object
/ array
/ "true" _ { return true; }
/ "false" _ { return false; }
/ "null" _ { return null; }
2010-03-22 12:18:58 +01:00
/* ===== Lexical Elements ===== */
string "string"
= '"' '"' _ { return ""; }
/ '"' chars:chars '"' _ { return chars; }
2010-03-22 12:18:58 +01:00
chars
= chars:char+ { return chars.join(""); }
2010-03-22 12:18:58 +01:00
char
// In the original JSON grammar: "any-Unicode-character-except-"-or-\-or-control-character"
= [^"\\\0-\x1F\x7f]
2010-03-22 12:18:58 +01:00
/ '\\"' { return '"'; }
/ "\\\\" { return "\\"; }
/ "\\/" { return "/"; }
/ "\\b" { return "\b"; }
/ "\\f" { return "\f"; }
/ "\\n" { return "\n"; }
/ "\\r" { return "\r"; }
/ "\\t" { return "\t"; }
/ "\\u" digits:$(hexDigit hexDigit hexDigit hexDigit) {
return String.fromCharCode(parseInt("0x" + digits));
2010-03-22 12:18:58 +01:00
}
number "number"
= parts:$(int frac exp) _ { return parseFloat(parts); }
/ parts:$(int frac) _ { return parseFloat(parts); }
/ parts:$(int exp) _ { return parseFloat(parts); }
/ parts:$(int) _ { return parseFloat(parts); }
2010-03-22 12:18:58 +01:00
int
= digit19 digits
/ digit
/ "-" digit19 digits
/ "-" digit
2010-03-22 12:18:58 +01:00
frac
= "." digits
2010-03-22 12:18:58 +01:00
exp
= e digits
2010-03-22 12:18:58 +01:00
digits
= digit+
2010-03-22 12:18:58 +01:00
e
= [eE] [+-]?
2010-03-22 12:18:58 +01:00
/*
* The following rules are not present in the original JSON gramar, but they are
* assumed to exist implicitly.
*
* FIXME: Define them according to ECMA-262, 5th ed.
2010-03-22 12:18:58 +01:00
*/
digit
= [0-9]
2010-03-22 12:18:58 +01:00
digit19
= [1-9]
2010-03-22 12:18:58 +01:00
hexDigit
= [0-9a-fA-F]
2010-03-22 12:18:58 +01:00
/* ===== Whitespace ===== */
_ "whitespace"
= whitespace*
2010-03-22 12:18:58 +01:00
// Whitespace is undefined in the original JSON grammar, so I assume a simple
// conventional definition consistent with ECMA-262, 5th ed.
whitespace
= [ \t\n\r]