UNPKG

@gram-data/gram-stringify

Version:
3 lines (2 loc) 2.58 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("@gram-data/gram-ast"),e=function(r){switch(r.type){case"integer":case"boolean":case"octal":case"hexadecimal":case"decimal":return r.value;case"string":return"`"+r.value+"`";case"tagged":return r.tag+"`"+r.value+"`";case"measurement":return""+r.value+r.unit;default:return function(r){throw new Error("Unexpected object: "+r)}(r)}},n=function(n){return r.isGramLiteralArray(n)?"["+n.map((function(r){return e(r)})).join(",")+"]":r.isGramLiteral(n)?e(n):a(n)},t=function(r){return"{"+Array.from(r,(function(r,e){return(e>0?",":"")+r[0]+":"+n(r[1])})).join("")+"}"},i=function(r){return"["+r.map(u).join(",")+"]"},a=function(r){return"{"+Object.entries(r).map((function(r,e){return(e>0?",":"")+r[0]+":"+u(r[1])})).join("")+"}"},c=function(r){var e=r.id||"",n=r.labels&&r.labels.length>0?":"+r.labels.join(":"):"",i=r.record&&0!==r.record.size?t(r.record):"";return""+e+n+(((e.length>0||n.length>0)&&i.length)>0?" ":"")+i},l=function(r){return"("+c(r)+")"},o=function(e){var n;return e?""+(r.isGramEmptyPath(e)?"":r.isGramNode(e)?l(e):r.isGramEdge(e)?function e(n){var t="left"===n.kind?"<-":"-",i="right"===n.kind?"->":"-",a=r.isGramNode(n.children[0])?l(n.children[0]):e(n.children[0]),o=r.isGramNode(n.children[1])?l(n.children[1]):e(n.children[1]),u=c(n);return""+a+t+(u.length>0?"["+u+"]":"")+i+o}(e):(n=e).id||n.labels||n.record?function(r){var e=r.children&&r.children.length>0?o(r.children[0]):"",n=r.children&&r.children.length>1?o(r.children[1]):"",t="left"===r.kind?"<--":"right"===r.kind?"--\x3e":"either"===r.kind?"--":e.length>0&&n.length>0?",":"";return"["+c(r)+(t.length>0?" ":"")+t+(e.length>0?" ":"")+e+(n.length>0?" ":"")+n+"]"}(e):function(r){var e=r.children&&r.children.length>0?o(r.children[0]):"",n=r.children&&r.children.length>1?o(r.children[1]):"";return e+","+(n.length>0?" ":"")+n}(e)):""},u=function c(l){var u;if(Array.isArray(l))return l.length>0?r.isGramPath(l[0])?l.map(c).join(" "):i(l):"[]";if(void 0!==l.type)switch(l.type){case"path":return o(l);case"seq":return c(l.children);case"property":return(u=l).name+":"+n(u.value);default:return r.isGramLiteral(l)?e(l):a(l)}else if("object"==typeof l)return r.isGramRecord(l)?t(l):a(l);throw new Error("Can't stringify <"+l+">")},h=function(e){if(r.isGramPath(e))return u(e);if(r.isGramSeq(e))return u(e);throw new Error("Don't know how to stringify \""+e.type+'"')};exports.gramStringifyPlugin=function(){this.Compiler=h},exports.stringify=u,exports.toGram=u; //# sourceMappingURL=gram-stringify.cjs.production.min.js.map