@gram-data/gram-stringify
Advanced tools
Comparing version 0.2.9 to 0.2.10
@@ -11,5 +11,13 @@ 'use strict'; | ||
function assertNever(x) { | ||
throw new Error('Unexpected object: ' + x); | ||
} | ||
var toStringLiteral = function toStringLiteral(l) { | ||
switch (l.type) { | ||
case 'integer': | ||
case 'boolean': | ||
case 'octal': | ||
case 'hexadecimal': | ||
case 'decimal': | ||
return l.value; | ||
@@ -23,4 +31,7 @@ | ||
case 'measurement': | ||
return "" + l.value + l.unit; | ||
default: | ||
return "<ERROR, can't stringify literals of type " + l.type + ">"; | ||
return assertNever(l); | ||
} | ||
@@ -48,2 +59,11 @@ }; | ||
var recordMapToString = function recordMapToString(record) { | ||
var fields = Object.entries(record).map(function (_ref, i) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
return "" + (i > 0 ? ',' : '') + name + ":" + toStringValue(value); | ||
}); | ||
return "{" + fields.join('') + "}"; | ||
}; | ||
var elementContentToString = function elementContentToString(ast) { | ||
@@ -61,4 +81,4 @@ var idString = ast.id || ''; | ||
var edgeToString = function edgeToString(ast) { | ||
var left = ast.relation === 'left' ? '<-' : '-'; | ||
var right = ast.relation === 'right' ? '->' : '-'; | ||
var left = ast.kind === 'left' ? '<-' : '-'; | ||
var right = ast.kind === 'right' ? '->' : '-'; | ||
var leftNode = gramAst.isGramNode(ast.children[0]) ? nodeToString(ast.children[0]) : edgeToString(ast.children[0]); | ||
@@ -74,3 +94,3 @@ var rightNode = gramAst.isGramNode(ast.children[1]) ? nodeToString(ast.children[1]) : edgeToString(ast.children[1]); | ||
var rhs = ast.children && ast.children.length > 1 ? pathToString(ast.children[1]) : ''; | ||
var relation = ast.relation === 'left' ? '<--' : ast.relation === 'right' ? '-->' : ast.relation === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; | ||
var relation = ast.kind === 'left' ? '<--' : ast.kind === 'right' ? '-->' : ast.kind === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; | ||
var content = elementContentToString(ast); | ||
@@ -80,4 +100,14 @@ return "[" + content + (relation.length > 0 ? ' ' : '') + relation + (lhs.length > 0 ? ' ' : '') + lhs + (rhs.length > 0 ? ' ' : '') + rhs + "]"; | ||
var pairToString = function pairToString(ast) { | ||
var lhs = ast.children && ast.children.length > 0 ? pathToString(ast.children[0]) : ''; | ||
var rhs = ast.children && ast.children.length > 1 ? pathToString(ast.children[1]) : ''; | ||
return lhs + "," + (rhs.length > 0 ? ' ' : '') + rhs; | ||
}; | ||
var hasAttributes = function hasAttributes(p) { | ||
return p.id || p.labels || p.record; | ||
}; | ||
var pathToString = function pathToString(ast) { | ||
var pathExpression = ast ? "" + (gramAst.isGramEmptyPath(ast) ? '' : gramAst.isGramNode(ast) ? nodeToString(ast) : gramAst.isGramEdge(ast) ? edgeToString(ast) : pathCompositionToString(ast)) : ''; | ||
var pathExpression = ast ? "" + (gramAst.isGramEmptyPath(ast) ? '' : gramAst.isGramNode(ast) ? nodeToString(ast) : gramAst.isGramEdge(ast) ? edgeToString(ast) : hasAttributes(ast) ? pathCompositionToString(ast) : pairToString(ast)) : ''; | ||
return pathExpression; | ||
@@ -87,19 +117,17 @@ }; | ||
var stringify = function stringify(ast) { | ||
var tokens = []; | ||
if (Array.isArray(ast)) { | ||
return ast.map(stringify).join(' '); | ||
} else if (ast.type !== undefined) { | ||
switch (ast.type) { | ||
case 'path': | ||
return pathToString(ast); | ||
switch (ast.type) { | ||
case 'seq': | ||
var paths = ast.children; | ||
return paths.map(function (path) { | ||
return stringify(path); | ||
}).join('\n'); | ||
case 'path': | ||
return pathToString(ast); | ||
default: | ||
console.error("Impossible:", typeof ast); | ||
case 'seq': | ||
return stringify(ast.children); | ||
} | ||
} else if (typeof ast === 'object') { | ||
return recordMapToString(ast); | ||
} | ||
return tokens.join(''); | ||
throw new Error("Can't stringify <" + ast + ">"); | ||
}; | ||
@@ -110,6 +138,10 @@ | ||
var stringifyCompiler = function stringifyCompiler(element) { | ||
if (gramAst.isGramPathlike(element)) { | ||
if (gramAst.isGramPath(element)) { | ||
return stringify(element); | ||
} | ||
if (gramAst.isGramSeq(element)) { | ||
return stringify(element); | ||
} else { | ||
return "Don't know how to stringify \"" + element.type + "\" nodes"; | ||
throw new Error("Don't know how to stringify \"" + element.type + "\""); | ||
} | ||
@@ -116,0 +148,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@gram-data/gram-ast"),r=function(e){switch(e.type){case"integer":return e.value;case"string":return"`"+e.value+"`";case"tagged":return e.tag+"`"+e.value+"`";default:return"<ERROR, can't stringify literals of type "+e.type+">"}},n=function(t){return"{"+t.map((function(t,i){return(i>0?",":"")+t.name+":"+(e.isGramLiteralArray(l=t.value)?"["+l.map((function(e){return r(e)})).join(",")+"]":e.isLiteral(l)?r(l):n(l));var l})).join("")+"}"},t=function(e){var r=e.id||"",t=e.labels&&e.labels.length>0?":"+e.labels.join(":"):"",i=e.record&&0!==Object.keys(e.record).length?n(e.record):"";return""+r+t+(((r.length>0||t.length>0)&&i.length)>0?" ":"")+i},i=function(e){return"("+t(e)+")"},l=function(r){return r?""+(e.isGramEmptyPath(r)?"":e.isGramNode(r)?i(r):e.isGramEdge(r)?function r(n){var l="left"===n.relation?"<-":"-",a="right"===n.relation?"->":"-",o=e.isGramNode(n.children[0])?i(n.children[0]):r(n.children[0]),c=e.isGramNode(n.children[1])?i(n.children[1]):r(n.children[1]),u=t(n);return""+o+l+(u.length>0?"["+u+"]":"")+a+c}(r):function(e){var r=e.children&&e.children.length>0?l(e.children[0]):"",n=e.children&&e.children.length>1?l(e.children[1]):"",i="left"===e.relation?"<--":"right"===e.relation?"--\x3e":"either"===e.relation?"--":r.length>0&&n.length>0?",":"";return"["+t(e)+(i.length>0?" ":"")+i+(r.length>0?" ":"")+r+(n.length>0?" ":"")+n+"]"}(r)):""},a=function e(r){switch(r.type){case"seq":return r.children.map((function(r){return e(r)})).join("\n");case"path":return l(r);default:console.error("Impossible:",typeof r)}return[].join("")},o=function(r){return e.isGramPathlike(r)?a(r):"Don't know how to stringify \""+r.type+'" nodes'};exports.gramStringifyPlugin=function(){this.Compiler=o},exports.stringify=a,exports.toGram=a; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@gram-data/gram-ast"),r=function(e){switch(e.type){case"integer":case"boolean":case"octal":case"hexadecimal":case"decimal":return e.value;case"string":return"`"+e.value+"`";case"tagged":return e.tag+"`"+e.value+"`";case"measurement":return""+e.value+e.unit;default:return function(e){throw new Error("Unexpected object: "+e)}(e)}},n=function(n){return e.isGramLiteralArray(n)?"["+n.map((function(e){return r(e)})).join(",")+"]":e.isLiteral(n)?r(n):t(n)},t=function(e){return"{"+e.map((function(e,r){return(r>0?",":"")+e.name+":"+n(e.value)})).join("")+"}"},i=function(e){var r=e.id||"",n=e.labels&&e.labels.length>0?":"+e.labels.join(":"):"",i=e.record&&0!==Object.keys(e.record).length?t(e.record):"";return""+r+n+(((r.length>0||n.length>0)&&i.length)>0?" ":"")+i},a=function(e){return"("+i(e)+")"},l=function(r){var n;return r?""+(e.isGramEmptyPath(r)?"":e.isGramNode(r)?a(r):e.isGramEdge(r)?function r(n){var t="left"===n.kind?"<-":"-",l="right"===n.kind?"->":"-",c=e.isGramNode(n.children[0])?a(n.children[0]):r(n.children[0]),o=e.isGramNode(n.children[1])?a(n.children[1]):r(n.children[1]),u=i(n);return""+c+t+(u.length>0?"["+u+"]":"")+l+o}(r):(n=r).id||n.labels||n.record?function(e){var r=e.children&&e.children.length>0?l(e.children[0]):"",n=e.children&&e.children.length>1?l(e.children[1]):"",t="left"===e.kind?"<--":"right"===e.kind?"--\x3e":"either"===e.kind?"--":r.length>0&&n.length>0?",":"";return"["+i(e)+(t.length>0?" ":"")+t+(r.length>0?" ":"")+r+(n.length>0?" ":"")+n+"]"}(r):function(e){var r=e.children&&e.children.length>0?l(e.children[0]):"",n=e.children&&e.children.length>1?l(e.children[1]):"";return r+","+(n.length>0?" ":"")+n}(r)):""},c=function e(r){if(Array.isArray(r))return r.map(e).join(" ");if(void 0!==r.type)switch(r.type){case"path":return l(r);case"seq":return e(r.children)}else if("object"==typeof r)return"{"+Object.entries(r).map((function(e,r){return(r>0?",":"")+e[0]+":"+n(e[1])})).join("")+"}";throw new Error("Can't stringify <"+r+">")},o=function(r){if(e.isGramPath(r))return c(r);if(e.isGramSeq(r))return c(r);throw new Error("Don't know how to stringify \""+r.type+'"')};exports.gramStringifyPlugin=function(){this.Compiler=o},exports.stringify=c,exports.toGram=c; | ||
//# sourceMappingURL=gram-stringify.cjs.production.min.js.map |
@@ -1,3 +0,3 @@ | ||
import { GramPathlike, GramPathSeq } from '@gram-data/gram-ast'; | ||
export declare const stringify: (ast: GramPathlike | GramPathSeq) => string; | ||
import { GramPath, GramSeq, GramPropertyMap } from '@gram-data/gram-ast'; | ||
export declare const stringify: (ast: GramPath | GramSeq | GramPath[] | GramPropertyMap) => string; | ||
export default stringify; |
@@ -1,2 +0,2 @@ | ||
import { isGramEmptyPath, isGramNode, isGramEdge, isGramLiteralArray, isLiteral, isGramPathlike } from '@gram-data/gram-ast'; | ||
import { isGramEmptyPath, isGramNode, isGramEdge, isGramLiteralArray, isLiteral, isGramPath, isGramSeq } from '@gram-data/gram-ast'; | ||
@@ -7,5 +7,13 @@ var isEmpty = function isEmpty(o) { | ||
function assertNever(x) { | ||
throw new Error('Unexpected object: ' + x); | ||
} | ||
var toStringLiteral = function toStringLiteral(l) { | ||
switch (l.type) { | ||
case 'integer': | ||
case 'boolean': | ||
case 'octal': | ||
case 'hexadecimal': | ||
case 'decimal': | ||
return l.value; | ||
@@ -19,4 +27,7 @@ | ||
case 'measurement': | ||
return "" + l.value + l.unit; | ||
default: | ||
return "<ERROR, can't stringify literals of type " + l.type + ">"; | ||
return assertNever(l); | ||
} | ||
@@ -44,2 +55,11 @@ }; | ||
var recordMapToString = function recordMapToString(record) { | ||
var fields = Object.entries(record).map(function (_ref, i) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
return "" + (i > 0 ? ',' : '') + name + ":" + toStringValue(value); | ||
}); | ||
return "{" + fields.join('') + "}"; | ||
}; | ||
var elementContentToString = function elementContentToString(ast) { | ||
@@ -57,4 +77,4 @@ var idString = ast.id || ''; | ||
var edgeToString = function edgeToString(ast) { | ||
var left = ast.relation === 'left' ? '<-' : '-'; | ||
var right = ast.relation === 'right' ? '->' : '-'; | ||
var left = ast.kind === 'left' ? '<-' : '-'; | ||
var right = ast.kind === 'right' ? '->' : '-'; | ||
var leftNode = isGramNode(ast.children[0]) ? nodeToString(ast.children[0]) : edgeToString(ast.children[0]); | ||
@@ -70,3 +90,3 @@ var rightNode = isGramNode(ast.children[1]) ? nodeToString(ast.children[1]) : edgeToString(ast.children[1]); | ||
var rhs = ast.children && ast.children.length > 1 ? pathToString(ast.children[1]) : ''; | ||
var relation = ast.relation === 'left' ? '<--' : ast.relation === 'right' ? '-->' : ast.relation === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; | ||
var relation = ast.kind === 'left' ? '<--' : ast.kind === 'right' ? '-->' : ast.kind === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; | ||
var content = elementContentToString(ast); | ||
@@ -76,4 +96,14 @@ return "[" + content + (relation.length > 0 ? ' ' : '') + relation + (lhs.length > 0 ? ' ' : '') + lhs + (rhs.length > 0 ? ' ' : '') + rhs + "]"; | ||
var pairToString = function pairToString(ast) { | ||
var lhs = ast.children && ast.children.length > 0 ? pathToString(ast.children[0]) : ''; | ||
var rhs = ast.children && ast.children.length > 1 ? pathToString(ast.children[1]) : ''; | ||
return lhs + "," + (rhs.length > 0 ? ' ' : '') + rhs; | ||
}; | ||
var hasAttributes = function hasAttributes(p) { | ||
return p.id || p.labels || p.record; | ||
}; | ||
var pathToString = function pathToString(ast) { | ||
var pathExpression = ast ? "" + (isGramEmptyPath(ast) ? '' : isGramNode(ast) ? nodeToString(ast) : isGramEdge(ast) ? edgeToString(ast) : pathCompositionToString(ast)) : ''; | ||
var pathExpression = ast ? "" + (isGramEmptyPath(ast) ? '' : isGramNode(ast) ? nodeToString(ast) : isGramEdge(ast) ? edgeToString(ast) : hasAttributes(ast) ? pathCompositionToString(ast) : pairToString(ast)) : ''; | ||
return pathExpression; | ||
@@ -83,19 +113,17 @@ }; | ||
var stringify = function stringify(ast) { | ||
var tokens = []; | ||
if (Array.isArray(ast)) { | ||
return ast.map(stringify).join(' '); | ||
} else if (ast.type !== undefined) { | ||
switch (ast.type) { | ||
case 'path': | ||
return pathToString(ast); | ||
switch (ast.type) { | ||
case 'seq': | ||
var paths = ast.children; | ||
return paths.map(function (path) { | ||
return stringify(path); | ||
}).join('\n'); | ||
case 'path': | ||
return pathToString(ast); | ||
default: | ||
console.error("Impossible:", typeof ast); | ||
case 'seq': | ||
return stringify(ast.children); | ||
} | ||
} else if (typeof ast === 'object') { | ||
return recordMapToString(ast); | ||
} | ||
return tokens.join(''); | ||
throw new Error("Can't stringify <" + ast + ">"); | ||
}; | ||
@@ -106,6 +134,10 @@ | ||
var stringifyCompiler = function stringifyCompiler(element) { | ||
if (isGramPathlike(element)) { | ||
if (isGramPath(element)) { | ||
return stringify(element); | ||
} | ||
if (isGramSeq(element)) { | ||
return stringify(element); | ||
} else { | ||
return "Don't know how to stringify \"" + element.type + "\" nodes"; | ||
throw new Error("Don't know how to stringify \"" + element.type + "\""); | ||
} | ||
@@ -112,0 +144,0 @@ }; |
@@ -10,3 +10,2 @@ (function (global, factory) { | ||
* | ||
* These AST elements | ||
* | ||
@@ -21,2 +20,11 @@ * References: | ||
/** | ||
* Type guard for GramSeq. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramSeq = function isGramSeq(o) { | ||
return !!o.type && o.type === 'seq'; | ||
}; | ||
/** | ||
* Type guard for a Path. | ||
@@ -27,2 +35,3 @@ * | ||
var isGramPath = function isGramPath(o) { | ||
@@ -46,3 +55,3 @@ return !!o.type && o.type === 'path'; | ||
var isGramEmptyPath = function isGramEmptyPath(o) { | ||
return isGramPath(o) && o.children === undefined && o.id === EMPTY_PATH_ID; | ||
return isGramPath(o) && o.id === EMPTY_PATH_ID; | ||
}; | ||
@@ -68,2 +77,4 @@ /** | ||
* | ||
* An edge is a path composed of two nodes. | ||
* | ||
* @param o any object | ||
@@ -74,21 +85,7 @@ */ | ||
var isGramEdge = function isGramEdge(o) { | ||
return isGramPath(o) && o.relation !== undefined && o.relation !== 'pair' && o.children !== undefined && o.children.every(function (child) { | ||
return isGramPath(o) && o.kind !== undefined && o.kind !== 'pair' && o.children !== undefined && o.children.every(function (child) { | ||
return isGramNode(child); | ||
}); | ||
}; | ||
/** | ||
* Type guard for GramPathSequence. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramPathSequence = function isGramPathSequence(o) { | ||
return !!o.type && o.type === 'seq'; | ||
}; | ||
var isGramPathlike = function isGramPathlike(o) { | ||
return isGramPath(o) || isGramEmptyPath(o) || isGramNode(o) || isGramEdge(o) || isGramPathSequence(o); | ||
}; | ||
var isGramLiteralArray = function isGramLiteralArray(v) { | ||
@@ -112,5 +109,13 @@ return Array.isArray(v) && isLiteral(v[0]); | ||
function assertNever(x) { | ||
throw new Error('Unexpected object: ' + x); | ||
} | ||
var toStringLiteral = function toStringLiteral(l) { | ||
switch (l.type) { | ||
case 'integer': | ||
case 'boolean': | ||
case 'octal': | ||
case 'hexadecimal': | ||
case 'decimal': | ||
return l.value; | ||
@@ -124,4 +129,7 @@ | ||
case 'measurement': | ||
return "" + l.value + l.unit; | ||
default: | ||
return "<ERROR, can't stringify literals of type " + l.type + ">"; | ||
return assertNever(l); | ||
} | ||
@@ -149,2 +157,11 @@ }; | ||
var recordMapToString = function recordMapToString(record) { | ||
var fields = Object.entries(record).map(function (_ref, i) { | ||
var name = _ref[0], | ||
value = _ref[1]; | ||
return "" + (i > 0 ? ',' : '') + name + ":" + toStringValue(value); | ||
}); | ||
return "{" + fields.join('') + "}"; | ||
}; | ||
var elementContentToString = function elementContentToString(ast) { | ||
@@ -162,4 +179,4 @@ var idString = ast.id || ''; | ||
var edgeToString = function edgeToString(ast) { | ||
var left = ast.relation === 'left' ? '<-' : '-'; | ||
var right = ast.relation === 'right' ? '->' : '-'; | ||
var left = ast.kind === 'left' ? '<-' : '-'; | ||
var right = ast.kind === 'right' ? '->' : '-'; | ||
var leftNode = isGramNode(ast.children[0]) ? nodeToString(ast.children[0]) : edgeToString(ast.children[0]); | ||
@@ -175,3 +192,3 @@ var rightNode = isGramNode(ast.children[1]) ? nodeToString(ast.children[1]) : edgeToString(ast.children[1]); | ||
var rhs = ast.children && ast.children.length > 1 ? pathToString(ast.children[1]) : ''; | ||
var relation = ast.relation === 'left' ? '<--' : ast.relation === 'right' ? '-->' : ast.relation === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; | ||
var relation = ast.kind === 'left' ? '<--' : ast.kind === 'right' ? '-->' : ast.kind === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; | ||
var content = elementContentToString(ast); | ||
@@ -181,4 +198,14 @@ return "[" + content + (relation.length > 0 ? ' ' : '') + relation + (lhs.length > 0 ? ' ' : '') + lhs + (rhs.length > 0 ? ' ' : '') + rhs + "]"; | ||
var pairToString = function pairToString(ast) { | ||
var lhs = ast.children && ast.children.length > 0 ? pathToString(ast.children[0]) : ''; | ||
var rhs = ast.children && ast.children.length > 1 ? pathToString(ast.children[1]) : ''; | ||
return lhs + "," + (rhs.length > 0 ? ' ' : '') + rhs; | ||
}; | ||
var hasAttributes = function hasAttributes(p) { | ||
return p.id || p.labels || p.record; | ||
}; | ||
var pathToString = function pathToString(ast) { | ||
var pathExpression = ast ? "" + (isGramEmptyPath(ast) ? '' : isGramNode(ast) ? nodeToString(ast) : isGramEdge(ast) ? edgeToString(ast) : pathCompositionToString(ast)) : ''; | ||
var pathExpression = ast ? "" + (isGramEmptyPath(ast) ? '' : isGramNode(ast) ? nodeToString(ast) : isGramEdge(ast) ? edgeToString(ast) : hasAttributes(ast) ? pathCompositionToString(ast) : pairToString(ast)) : ''; | ||
return pathExpression; | ||
@@ -188,19 +215,17 @@ }; | ||
var stringify = function stringify(ast) { | ||
var tokens = []; | ||
if (Array.isArray(ast)) { | ||
return ast.map(stringify).join(' '); | ||
} else if (ast.type !== undefined) { | ||
switch (ast.type) { | ||
case 'path': | ||
return pathToString(ast); | ||
switch (ast.type) { | ||
case 'seq': | ||
var paths = ast.children; | ||
return paths.map(function (path) { | ||
return stringify(path); | ||
}).join('\n'); | ||
case 'path': | ||
return pathToString(ast); | ||
default: | ||
console.error("Impossible:", typeof ast); | ||
case 'seq': | ||
return stringify(ast.children); | ||
} | ||
} else if (typeof ast === 'object') { | ||
return recordMapToString(ast); | ||
} | ||
return tokens.join(''); | ||
throw new Error("Can't stringify <" + ast + ">"); | ||
}; | ||
@@ -211,6 +236,10 @@ | ||
var stringifyCompiler = function stringifyCompiler(element) { | ||
if (isGramPathlike(element)) { | ||
if (isGramPath(element)) { | ||
return stringify(element); | ||
} | ||
if (isGramSeq(element)) { | ||
return stringify(element); | ||
} else { | ||
return "Don't know how to stringify \"" + element.type + "\" nodes"; | ||
throw new Error("Don't know how to stringify \"" + element.type + "\""); | ||
} | ||
@@ -217,0 +246,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(((e=e||self).gram=e.gram||{},e.gram.stringify={}))}(this,(function(e){"use strict";var n=function(e){return!!e.type&&"path"===e.type},t=function(e){return n(e)&&void 0===e.children&&"ø"===e.id},r=function(e){return n(e)&&e.children&&0===e.children.length&&"ø"!==e.id},i=function(e){return n(e)&&void 0!==e.relation&&"pair"!==e.relation&&void 0!==e.children&&e.children.every((function(e){return r(e)}))},o=function(e){return!!e.type&&!!e.value&&"property"!==e.type},l=function(e){switch(e.type){case"integer":return e.value;case"string":return"`"+e.value+"`";case"tagged":return e.tag+"`"+e.value+"`";default:return"<ERROR, can't stringify literals of type "+e.type+">"}},u=function(e){return"{"+e.map((function(e,n){return(n>0?",":"")+e.name+":"+(function(e){return Array.isArray(e)&&o(e[0])}(t=e.value)?"["+t.map((function(e){return l(e)})).join(",")+"]":o(t)?l(t):u(t));var t})).join("")+"}"},c=function(e){var n=e.id||"",t=e.labels&&e.labels.length>0?":"+e.labels.join(":"):"",r=e.record&&0!==Object.keys(e.record).length?u(e.record):"";return""+n+t+(((n.length>0||t.length>0)&&r.length)>0?" ":"")+r},a=function(e){return"("+c(e)+")"},f=function(e){return e?""+(t(e)?"":r(e)?a(e):i(e)?function e(n){var t="left"===n.relation?"<-":"-",i="right"===n.relation?"->":"-",o=r(n.children[0])?a(n.children[0]):e(n.children[0]),l=r(n.children[1])?a(n.children[1]):e(n.children[1]),u=c(n);return""+o+t+(u.length>0?"["+u+"]":"")+i+l}(e):function(e){var n=e.children&&e.children.length>0?f(e.children[0]):"",t=e.children&&e.children.length>1?f(e.children[1]):"",r="left"===e.relation?"<--":"right"===e.relation?"--\x3e":"either"===e.relation?"--":n.length>0&&t.length>0?",":"";return"["+c(e)+(r.length>0?" ":"")+r+(n.length>0?" ":"")+n+(t.length>0?" ":"")+t+"]"}(e)):""},h=function e(n){switch(n.type){case"seq":return n.children.map((function(n){return e(n)})).join("\n");case"path":return f(n);default:console.error("Impossible:",typeof n)}return[].join("")},d=function(e){return n(o=e)||t(o)||r(o)||i(o)||function(e){return!!e.type&&"seq"===e.type}(o)?h(e):"Don't know how to stringify \""+e.type+'" nodes';var o};e.gramStringifyPlugin=function(){this.Compiler=d},e.stringify=h,e.toGram=h,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(((e=e||self).gram=e.gram||{},e.gram.stringify={}))}(this,(function(e){"use strict";var n=function(e){return!!e.type&&"path"===e.type},r=function(e){return n(e)&&e.children&&0===e.children.length&&"ø"!==e.id},t=function(e){return!!e.type&&!!e.value&&"property"!==e.type},i=function(e){switch(e.type){case"integer":case"boolean":case"octal":case"hexadecimal":case"decimal":return e.value;case"string":return"`"+e.value+"`";case"tagged":return e.tag+"`"+e.value+"`";case"measurement":return""+e.value+e.unit;default:return function(e){throw new Error("Unexpected object: "+e)}(e)}},c=function(e){return function(e){return Array.isArray(e)&&t(e[0])}(e)?"["+e.map((function(e){return i(e)})).join(",")+"]":t(e)?i(e):l(e)},l=function(e){return"{"+e.map((function(e,n){return(n>0?",":"")+e.name+":"+c(e.value)})).join("")+"}"},u=function(e){var n=e.id||"",r=e.labels&&e.labels.length>0?":"+e.labels.join(":"):"",t=e.record&&0!==Object.keys(e.record).length?l(e.record):"";return""+n+r+(((n.length>0||r.length>0)&&t.length)>0?" ":"")+t},o=function(e){return"("+u(e)+")"},a=function(e){var t,i;return e?""+(n(i=e)&&"ø"===i.id?"":r(e)?o(e):function(e){return n(e)&&void 0!==e.kind&&"pair"!==e.kind&&void 0!==e.children&&e.children.every((function(e){return r(e)}))}(e)?function e(n){var t="left"===n.kind?"<-":"-",i="right"===n.kind?"->":"-",c=r(n.children[0])?o(n.children[0]):e(n.children[0]),l=r(n.children[1])?o(n.children[1]):e(n.children[1]),a=u(n);return""+c+t+(a.length>0?"["+a+"]":"")+i+l}(e):(t=e).id||t.labels||t.record?function(e){var n=e.children&&e.children.length>0?a(e.children[0]):"",r=e.children&&e.children.length>1?a(e.children[1]):"",t="left"===e.kind?"<--":"right"===e.kind?"--\x3e":"either"===e.kind?"--":n.length>0&&r.length>0?",":"";return"["+u(e)+(t.length>0?" ":"")+t+(n.length>0?" ":"")+n+(r.length>0?" ":"")+r+"]"}(e):function(e){var n=e.children&&e.children.length>0?a(e.children[0]):"",r=e.children&&e.children.length>1?a(e.children[1]):"";return n+","+(r.length>0?" ":"")+r}(e)):""},d=function e(n){if(Array.isArray(n))return n.map(e).join(" ");if(void 0!==n.type)switch(n.type){case"path":return a(n);case"seq":return e(n.children)}else if("object"==typeof n)return"{"+Object.entries(n).map((function(e,n){return(n>0?",":"")+e[0]+":"+c(e[1])})).join("")+"}";throw new Error("Can't stringify <"+n+">")},h=function(e){if(n(e))return d(e);if((r=e).type&&"seq"===r.type)return d(e);throw new Error("Don't know how to stringify \""+e.type+'"');var r};e.gramStringifyPlugin=function(){this.Compiler=h},e.stringify=d,e.toGram=d,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=gram-stringify.umd.production.min.js.map |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
"license": "MIT", | ||
@@ -52,3 +52,4 @@ "repository": { | ||
"devDependencies": { | ||
"@gram-data/gram-builder": "^0.2.9", | ||
"@gram-data/gram-builder": "^0.2.10", | ||
"@gram-data/gram-ops": "^0.2.10", | ||
"@types/jest": "^26.0.15", | ||
@@ -70,6 +71,6 @@ "@types/unist": "^2.0.3", | ||
"dependencies": { | ||
"@gram-data/gram-ast": "^0.2.9", | ||
"@gram-data/gram-ast": "^0.2.10", | ||
"unified": "^9.2.0" | ||
}, | ||
"gitHead": "1ff6bdb1c799a29a95540314bdb5b2a881971774", | ||
"gitHead": "5ab0f6f900db922abaf69906f62d6cf23e12215e", | ||
"publishConfig": { | ||
@@ -76,0 +77,0 @@ "access": "public" |
@@ -5,3 +5,3 @@ import { CompilerFunction, Plugin } from 'unified'; | ||
import { isGramPathlike } from '@gram-data/gram-ast'; | ||
import { isGramPath, isGramSeq } from '@gram-data/gram-ast'; | ||
@@ -11,6 +11,9 @@ import stringify from './gram-stringify'; | ||
const stringifyCompiler: CompilerFunction = (element: UnistNode) => { | ||
if (isGramPathlike(element)) { | ||
if (isGramPath(element)) { | ||
return stringify(element); | ||
} | ||
if (isGramSeq(element)) { | ||
return stringify(element); | ||
} else { | ||
return `Don't know how to stringify "${element.type}" nodes`; | ||
throw new Error(`Don't know how to stringify "${element.type}"`); | ||
} | ||
@@ -17,0 +20,0 @@ }; |
@@ -5,3 +5,2 @@ import { | ||
GramRecord, | ||
GramLiteral, | ||
GramPath, | ||
@@ -13,6 +12,7 @@ GramRecordValue, | ||
isGramEdge, | ||
GramPathlike, | ||
GramPathSeq, | ||
GramSeq, | ||
isGramEmptyPath, | ||
isGramLiteralArray, | ||
GramPropertyMap, | ||
GramLiteral, | ||
} from '@gram-data/gram-ast'; | ||
@@ -22,5 +22,12 @@ | ||
function assertNever(x: never): never { | ||
throw new Error('Unexpected object: ' + x); | ||
} | ||
const toStringLiteral = (l: GramLiteral): string => { | ||
switch (l.type) { | ||
case 'integer': | ||
case 'boolean': | ||
case 'octal': | ||
case 'hexadecimal': | ||
case 'decimal': | ||
return l.value; | ||
@@ -31,4 +38,6 @@ case 'string': | ||
return `${l.tag}\`${l.value}\``; | ||
case 'measurement': | ||
return `${l.value}${l.unit}`; | ||
default: | ||
return `<ERROR, can't stringify literals of type ${l.type}>`; | ||
return assertNever(l); | ||
} | ||
@@ -55,3 +64,11 @@ }; | ||
const elementContentToString = (ast: GramPathlike): string => { | ||
const recordMapToString = (record: GramPropertyMap): string => { | ||
const fields = Object.entries(record).map( | ||
([name, value], i: number) => | ||
`${i > 0 ? ',' : ''}${name}:${toStringValue(value)}` | ||
); | ||
return `{${fields.join('')}}`; | ||
}; | ||
const elementContentToString = (ast: GramPath): string => { | ||
const idString = ast.id || ''; | ||
@@ -74,4 +91,4 @@ const labelsString = | ||
const edgeToString = (ast: GramEdge): string => { | ||
const left = ast.relation === 'left' ? '<-' : '-'; | ||
const right = ast.relation === 'right' ? '->' : '-'; | ||
const left = ast.kind === 'left' ? '<-' : '-'; | ||
const right = ast.kind === 'right' ? '->' : '-'; | ||
@@ -100,7 +117,7 @@ const leftNode = isGramNode(ast.children[0]) | ||
const relation = | ||
ast.relation === 'left' | ||
ast.kind === 'left' | ||
? '<--' | ||
: ast.relation === 'right' | ||
: ast.kind === 'right' | ||
? '-->' | ||
: ast.relation === 'either' | ||
: ast.kind === 'either' | ||
? '--' | ||
@@ -116,2 +133,17 @@ : lhs.length > 0 && rhs.length > 0 | ||
}; | ||
const pairToString = (ast: GramPath): string => { | ||
const lhs = | ||
ast.children && ast.children.length > 0 | ||
? pathToString(ast.children[0]) | ||
: ''; | ||
const rhs = | ||
ast.children && ast.children.length > 1 | ||
? pathToString(ast.children[1]) | ||
: ''; | ||
return `${lhs},${rhs.length > 0 ? ' ' : ''}${rhs}`; | ||
}; | ||
const hasAttributes = (p: GramPath) => p.id || p.labels || p.record; | ||
const pathToString = (ast?: GramPath): string => { | ||
@@ -126,3 +158,5 @@ const pathExpression = ast | ||
? edgeToString(ast) | ||
: pathCompositionToString(ast) | ||
: hasAttributes(ast) | ||
? pathCompositionToString(ast) | ||
: pairToString(ast) | ||
}` | ||
@@ -133,17 +167,21 @@ : ''; | ||
export const stringify = (ast: GramPathlike | GramPathSeq): string => { | ||
const tokens: Array<string> = []; | ||
switch (ast.type) { | ||
case 'seq': | ||
const paths = ast.children as GramPath[]; | ||
return paths.map((path: GramPath) => stringify(path)).join('\n'); | ||
case 'path': | ||
return pathToString(ast as GramPath); | ||
default: | ||
console.error(`Impossible:`, typeof ast); | ||
export const stringify = ( | ||
ast: GramPath | GramSeq | GramPath[] | GramPropertyMap | ||
): string => { | ||
if (Array.isArray(ast)) { | ||
return ast.map(stringify).join(' '); | ||
} else if (ast.type !== undefined) { | ||
switch (ast.type) { | ||
case 'path': | ||
return pathToString(ast as GramPath); | ||
case 'seq': | ||
return stringify(ast.children as GramPath[]); | ||
} | ||
} else if (typeof ast === 'object') { | ||
return recordMapToString(ast); | ||
} | ||
return tokens.join(''); | ||
throw new Error(`Can't stringify <${ast}>`); | ||
}; | ||
export default stringify; |
import gramStringifyPlugin from './gram-stringify-plugin'; | ||
import toGram, {stringify} from './gram-stringify'; | ||
import toGram, { stringify } from './gram-stringify'; | ||
export { stringify, toGram, gramStringifyPlugin }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86534
662
16
Updated@gram-data/gram-ast@^0.2.10