-
-
Save dasdachs/c795e5dcc2ac3547099fc675a4e2d623 to your computer and use it in GitHub Desktop.
json_pegjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doc = doc:(object / array / ws / n) { return doc } | |
// Doc | |
line = key:key ws? seperator ws? value:(prim / array / object) ws? lineEnd? n? ws? { return {key, value} } | |
key = txt:txt { return txt } | |
// Primitives | |
prim "primitive value" = val:(null / bool / num / txt) { return val } | |
null = "null" { return null } | |
bool "boolean" = bool:("true" / "false") { return Boolean(bool) } | |
num "number" = num:(minusPlus? [0-9]+ decimal? [0-9]*) { | |
const parsedNum = num.reduce((acc, curr) => { | |
const str = Array.isArray(curr) ? curr.join("") : curr && curr.length ? curr : ""; | |
return acc + str; | |
}, "") | |
return parseFloat(parsedNum); | |
} | |
txt "text" = dobleQuote chars:char* dobleQuote { return chars.join("") } | |
// Compund type | |
array = arrayStart n? ws? objects:object+ n? ws? arrayEnd { return objects } | |
object = objectStart n? ws? lines:line+ objectEnd { return lines } | |
// Syntax | |
arrayStart = "[" | |
arrayEnd = "]" | |
objectStart = "{" | |
objectEnd = "}" | |
lineEnd = "," | |
decimal = "." | |
dobleQuote = '"' | |
seperator = ":" | |
// Common | |
minusPlus = "-" / "+" | |
ws "whitespace" = [ \t\r]+ | |
n "newline" = "\n" | |
// String | |
char | |
= unescaped | |
/ escape | |
sequence:( | |
'"' | |
/ "\\" | |
/ "/" | |
/ "b" { return "\b"; } | |
/ "f" { return "\f"; } | |
/ "n" { return "\n"; } | |
/ "r" { return "\r"; } | |
/ "t" { return "\t"; } | |
/ "u" digits:$(HEXDIG HEXDIG HEXDIG HEXDIG) { | |
return String.fromCharCode(parseInt(digits, 16)); | |
} | |
) | |
{ return sequence; } | |
escape | |
= "\\" | |
quotation_mark | |
= '"' | |
unescaped | |
= [^\0-\x1F\x22\x5C] | |
HEXDIG = [0-9a-f]i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment