UNPKG

nisp

Version:

A language that for easily build cross-language language

113 lines (112 loc) 3.25 kB
"use strict"; exports.__esModule = true; var extended_1 = require("../parser/extended"); var fill = function (input, length, space) { if (space === void 0) { space = 4; } return new Array(length * space + 1).join(' ') + input; }; var comments = function (nodes) { var ret = []; if (!nodes) { return ret; } for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) { var item = nodes_1[_i]; if (item.type === Type.comment) { ret.push(item); } } return ret; }; var Type; (function (Type) { Type[Type["blank"] = 0] = "blank"; Type[Type["comment"] = 1] = "comment"; Type[Type["package"] = 2] = "package"; Type[Type["value"] = 3] = "value"; Type[Type["nisp"] = 4] = "nisp"; })(Type || (Type = {})); var Context = (function () { function Context() { this.root = null; this.indent = 0; } return Context; }()); var table = (_a = { start: function (node) { var context = new Context(); context.root = node; return table.router(node, context); }, router: function (node, context) { if (!node) { return node; } var handler = table[node.type]; if (handler) { return handler(node, context); } return node; } }, _a[Type.value] = function (node) { return node.value + ''; }, _a[Type.blank] = function () { return ''; }, _a[Type.comment] = function (node) { return node.value; }, _a[Type.package] = function (node, context) { var ret = []; var list = comments(node.left); list.push(node.value); list = list.concat(comments(node.right)); ret.push(table.router(list.shift(), context), '\n'); for (var _i = 0, list_1 = list; _i < list_1.length; _i++) { var item = list_1[_i]; ret.push(fill(table.router(item, context), context.indent), '\n'); } ret.pop(); return ret.join(''); }, _a[Type.nisp] = function (node, context) { var ret = []; var list = comments(node.left); list = list.concat(node.value); list = list.concat(comments(node.right)); ret.push('('); if (list.length) { ++context.indent; var top_1 = list[0]; if (top_1.type !== Type.nisp && top_1.type !== Type.comment) { ret.push(table.router(top_1, context)); list.shift(); } if (list.length) { ret.push('\n'); } for (var _i = 0, list_2 = list; _i < list_2.length; _i++) { var item = list_2[_i]; ret.push(fill(table.router(item, context), context.indent), '\n'); } --context.indent; } if (list.length) { ret.push(fill(')', context.indent)); } else { ret.push(')'); } return ret.join(''); }, _a); function default_1(code) { var ast = extended_1.parse(code); return table.start(ast); } exports["default"] = default_1; ; var _a;