UNPKG

@gram-data/gram-stringify

Version:
182 lines (148 loc) 5.66 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var gramAst = require('@gram-data/gram-ast'); var isEmpty = function isEmpty(r) { return r.size === 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; case 'string': return "`" + l.value + "`"; case 'tagged': return l.tag + "`" + l.value + "`"; case 'measurement': return "" + l.value + l.unit; default: return assertNever(l); } }; var toStringValue = function toStringValue(v) { if (gramAst.isGramLiteralArray(v)) { return "[" + v.map(function (l) { return toStringLiteral(l); }).join(',') + "]"; } else if (gramAst.isGramLiteral(v)) { return toStringLiteral(v); } else { return objectToString(v); } }; var propertyToString = function propertyToString(property) { return property.name + ":" + toStringValue(property.value); }; var recordToString = function recordToString(record) { var fields = Array.from(record, function (_ref, i) { var k = _ref[0], v = _ref[1]; return "" + (i > 0 ? ',' : '') + k + ":" + toStringValue(v); }); return "{" + fields.join('') + "}"; }; var arrayToString = function arrayToString(xs) { return "[" + xs.map(stringify).join(',') + "]"; }; var objectToString = function objectToString(o) { var fields = Object.entries(o).map(function (_ref2, i) { var name = _ref2[0], value = _ref2[1]; return "" + (i > 0 ? ',' : '') + name + ":" + stringify(value); }); return "{" + fields.join('') + "}"; }; var elementContentToString = function elementContentToString(ast) { var idString = ast.id || ''; var labelsString = ast.labels && ast.labels.length > 0 ? ':' + ast.labels.join(':') : ''; var recordString = ast.record && !isEmpty(ast.record) ? recordToString(ast.record) : ''; return "" + idString + labelsString + (((idString.length > 0 || labelsString.length > 0) && recordString.length) > 0 ? ' ' : '') + recordString; }; var nodeToString = function nodeToString(ast) { return "(" + elementContentToString(ast) + ")"; }; var edgeToString = function edgeToString(ast) { 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]); var rightNode = gramAst.isGramNode(ast.children[1]) ? nodeToString(ast.children[1]) : edgeToString(ast.children[1]); var content = elementContentToString(ast); var boxedContent = content.length > 0 ? "[" + content + "]" : ''; return "" + leftNode + left + boxedContent + right + rightNode; }; var pathCompositionToString = function pathCompositionToString(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]) : ''; var relation = ast.kind === 'left' ? '<--' : ast.kind === 'right' ? '-->' : ast.kind === 'either' ? '--' : lhs.length > 0 && rhs.length > 0 ? ',' : ''; var content = elementContentToString(ast); 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) : hasAttributes(ast) ? pathCompositionToString(ast) : pairToString(ast)) : ''; return pathExpression; }; var stringify = function stringify(ast) { if (Array.isArray(ast)) { if (ast.length > 0) { var element = ast[0]; if (gramAst.isGramPath(element)) { return ast.map(stringify).join(' '); } else { return arrayToString(ast); } } else return '[]'; } else if (ast.type !== undefined) { switch (ast.type) { case 'path': return pathToString(ast); case 'seq': return stringify(ast.children); case 'property': return propertyToString(ast); default: if (gramAst.isGramLiteral(ast)) { return toStringLiteral(ast); } return objectToString(ast); } } else if (typeof ast === 'object') { if (gramAst.isGramRecord(ast)) { return recordToString(ast); } else { return objectToString(ast); } } throw new Error("Can't stringify <" + ast + ">"); }; // import {VFile} from 'vfile' var stringifyCompiler = function stringifyCompiler(element) { if (gramAst.isGramPath(element)) { return stringify(element); } if (gramAst.isGramSeq(element)) { return stringify(element); } else { throw new Error("Don't know how to stringify \"" + element.type + "\""); } }; var gramStringifyPlugin = function gramStringifyPlugin() { this.Compiler = stringifyCompiler; }; exports.gramStringifyPlugin = gramStringifyPlugin; exports.stringify = stringify; exports.toGram = stringify; //# sourceMappingURL=gram-stringify.cjs.development.js.map