UNPKG

json-ometajs

Version:
225 lines (196 loc) 9.56 kB
var ometajs_ = require("ometajs"); var AbstractGrammar = ometajs_.grammars.AbstractGrammar; var BSJSParser = ometajs_.grammars.BSJSParser; var BSJSIdentity = ometajs_.grammars.BSJSIdentity; var BSJSTranslator = ometajs_.grammars.BSJSTranslator; var JSON = function JSON(source, opts) { AbstractGrammar.call(this, source, opts); }; JSON.grammarName = "JSON"; JSON.match = AbstractGrammar.match; JSON.matchAll = AbstractGrammar.matchAll; exports.JSON = JSON; require("util").inherits(JSON, AbstractGrammar); JSON.prototype["root"] = function $root() { var a; return this._rule("value", false, [], null, this["value"]) && (a = this._getIntermediate(), true) && this._rule("end", false, [], null, this["end"]) && this._exec(a); }; JSON.prototype["value"] = function $value() { var v; return this._optional(function() { return this._rule("spaces", true, [], null, this["spaces"]); }) && (this._atomic(function() { return this._rule("object", false, [], null, this["object"]); }) || this._atomic(function() { return this._rule("array", false, [], null, this["array"]); }) || this._atomic(function() { return this._rule("string", false, [], null, this["string"]); }) || this._atomic(function() { return this._rule("number", false, [], null, this["number"]); }) || this._atomic(function() { return this._rule("boolean", false, [], null, this["boolean"]); })) && (v = this._getIntermediate(), true) && this._optional(function() { return this._rule("spaces", true, [], null, this["spaces"]); }) && this._exec(v); }; JSON.prototype["object"] = function $object() { return this._atomic(function() { var l, r; return this._rule("token", true, [ "{" ], null, this["token"]) && this._rule("pair", false, [], null, this["pair"]) && (l = this._getIntermediate(), true) && this._any(function() { return this._atomic(function() { return this._rule("token", true, [ "," ], null, this["token"]) && this._rule("pair", false, [], null, this["pair"]); }); }) && (r = this._getIntermediate(), true) && this._rule("token", true, [ "}" ], null, this["token"]) && this._exec([ l ].concat(r).reduce(function(acc, e) { acc[e[0]] = e[1]; return acc; }, {})); }) || this._atomic(function() { return this._rule("token", true, [ "{" ], null, this["token"]) && this._rule("token", true, [ "}" ], null, this["token"]) && this._exec(new Object); }); }; JSON.prototype["pair"] = function $pair() { var s, a; return this._optional(function() { return this._rule("spaces", true, [], null, this["spaces"]); }) && this._rule("string", false, [], null, this["string"]) && (s = this._getIntermediate(), true) && this._optional(function() { return this._rule("spaces", true, [], null, this["spaces"]); }) && this._rule("token", true, [ ":" ], null, this["token"]) && this._rule("value", false, [], null, this["value"]) && (a = this._getIntermediate(), true) && this._exec([ s, a ]); }; JSON.prototype["array"] = function $array() { return this._atomic(function() { var l, r; return this._rule("token", true, [ "[" ], null, this["token"]) && this._rule("value", false, [], null, this["value"]) && (l = this._getIntermediate(), true) && this._any(function() { return this._atomic(function() { return this._rule("token", true, [ "," ], null, this["token"]) && this._rule("value", false, [], null, this["value"]); }); }) && (r = this._getIntermediate(), true) && this._rule("token", true, [ "]" ], null, this["token"]) && this._exec(function() { return r ? [ l ].concat(r) : [ l ]; }.call(this)); }) || this._atomic(function() { return this._rule("token", true, [ "[" ], null, this["token"]) && this._optional(function() { return this._rule("spaces", true, [], null, this["spaces"]); }) && this._rule("token", true, [ "]" ], null, this["token"]) && this._exec([]); }); }; JSON.prototype["string"] = function $string() { var s; return this._rule("token", true, [ '"' ], null, this["token"]) && this._any(function() { return this._atomic(function() { return !this._atomic(function() { return this._match('"'); }, true) && this._rule("character", false, [], null, this["character"]); }); }) && (s = this._getIntermediate(), true) && this._rule("token", true, [ '"' ], null, this["token"]) && this._exec(s.join("")); }; JSON.prototype["character"] = function $character() { return this._atomic(function() { return this._rule("escape", false, [], null, this["escape"]); }) || this._atomic(function() { return this._list(function() { return !this._atomic(function() { return this._seq(/^([\t\n\r\f\v])/); }, true) && this._rule("char", false, [], null, this["char"]); }, true); }); }; JSON.prototype["escape"] = function $escape() { var v; return this._match("\\") && (this._match('"') || this._match("'") || this._match("\\") || this._match("/") || this._atomic(function() { return this._match("b") && this._exec("\b"); }) || this._atomic(function() { return this._match("f") && this._exec("\f"); }) || this._atomic(function() { return this._match("n") && this._exec("\n"); }) || this._atomic(function() { return this._match("r") && this._exec("\r"); }) || this._atomic(function() { return this._match("t") && this._exec(" "); }) || this._atomic(function() { var s; return this._match("u") && this._list(function() { return this._rule("hex", false, [], null, this["hex"]) && this._rule("hex", false, [], null, this["hex"]) && this._rule("hex", false, [], null, this["hex"]) && this._rule("hex", false, [], null, this["hex"]); }, true) && (s = this._getIntermediate(), true) && this._exec(String.fromCharCode(parseInt(s, 16))); }) || this._atomic(function() { return this._exec(this._getError()); })) && (v = this._getIntermediate(), true) && this._exec(v); }; JSON.prototype["hex"] = function $hex() { return this._seq(/^([0-9A-Fa-f])/); }; JSON.prototype["number"] = function $number() { return this._atomic(function() { return this._rule("float", false, [], null, this["float"]); }) || this._atomic(function() { return this._rule("int", false, [], null, this["int"]); }); }; JSON.prototype["float"] = function $float() { var n; return this._list(function() { return this._optional(function() { return this._rule("sign", false, [], null, this["sign"]); }) && this._many(function() { return this._atomic(function() { return this._rule("digit", false, [], null, this["digit"]); }); }) && (this._atomic(function() { return this._match(".") && this._any(function() { return this._atomic(function() { return this._rule("digit", false, [], null, this["digit"]); }); }) && this._optional(function() { return this._rule("exp", false, [], null, this["exp"]); }); }) || this._atomic(function() { return this._rule("exp", false, [], null, this["exp"]); })); }, true) && (n = this._getIntermediate(), true) && this._exec(parseFloat(n)); }; JSON.prototype["int"] = function $int() { return this._atomic(function() { var n; return this._list(function() { return this._optional(function() { return this._rule("sign", false, [], null, this["sign"]); }) && this._seq(/^([1-9])/) && this._any(function() { return this._atomic(function() { return this._rule("digit", false, [], null, this["digit"]); }); }); }, true) && (n = this._getIntermediate(), true) && this._exec(parseInt(n, 10)); }) || this._match("0"); }; JSON.prototype["sign"] = function $sign() { return this._seq(/^(\+|-)/); }; JSON.prototype["exp"] = function $exp() { return this._seq(/^([Ee])/) && this._optional(function() { return this._rule("sign", false, [], null, this["sign"]); }) && this._many(function() { return this._atomic(function() { return this._rule("digit", false, [], null, this["digit"]); }); }); }; JSON.prototype["boolean"] = function $boolean() { return this._atomic(function() { return this._rule("token", true, [ "true" ], null, this["token"]) && this._exec(true); }) || this._atomic(function() { return this._rule("token", true, [ "false" ], null, this["token"]) && this._exec(false); }) || this._atomic(function() { return this._rule("token", true, [ "null" ], null, this["token"]) && this._exec(null); }); }; JSON.prototype["spaces"] = function $spaces() { return this._seq(/^([ \t\f\r\n\v]+)/); }; JSON.prototype["token"] = function $token() { var t; return this._optional(function() { return this._rule("spaces", true, [], null, this["spaces"]); }) && this._seq(/^(true|false|null|:|"|'|\{|\}|\[|\]|,)/) && (t = this._getIntermediate(), true) && this._exec([ t, t ]); }; JSON.parse = function parse(s) { return this.matchAll(s, "root"); }; module.exports = JSON;