UNPKG

prettier-plugin-asciidoc

Version:

Format AsciiDoc files with prettier 📖

170 lines (169 loc) • 6.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cstToAst = void 0; const parse_to_cst_1 = require("./parse-to-cst"); const shared_token_1 = require("./lexer/shared-token"); const attribute_list_tokens_1 = require("./lexer/attribute-list-tokens"); const string_tokens_1 = require("./lexer/string-tokens"); class CSTVisitor { visit(node) { return this[node.name](node); } [parse_to_cst_1.Rules.Adoc](node) { return { type: "adoc", location: this.location(node), children: this.childNodes(node).map((child) => this.visit(child)), }; } [parse_to_cst_1.Rules.Headline](node) { var _a, _b; const headline = (_b = (_a = this.getToken(node, shared_token_1.Headline)) === null || _a === void 0 ? void 0 : _a.image) !== null && _b !== void 0 ? _b : ""; return { type: "headline", level: headline.replace("\n", "").length - 1, location: this.location(node), text: this.TextRule(this.getNode(node, parse_to_cst_1.Rules.Text)), }; } [parse_to_cst_1.Rules.EmptyLine](node) { return { type: "emptyLine", location: this.location(node), }; } [parse_to_cst_1.Rules.AttributeList](node) { return { type: "attributeList", location: this.location(node), attributes: this.childNodes(node).map((child) => this.visit(child)), }; } [parse_to_cst_1.Rules.AttributeBlock](node) { return { type: "attributeBlock", location: this.location(node), list: this.AttributeList(this.getNode(node, parse_to_cst_1.Rules.AttributeList)), }; } [parse_to_cst_1.Rules.AttributeEntry](node) { var _a; const valueNode = this.getNode(node, parse_to_cst_1.Rules.AttributeValue); return { type: "attributeNode", location: this.location(node), key: (_a = this.getToken(node, attribute_list_tokens_1.AttributeInlineText)) === null || _a === void 0 ? void 0 : _a.image, value: this.AttributeValue(valueNode), }; } [parse_to_cst_1.Rules.AttributeEntryShorthand](node) { var _a, _b; const key = (_b = (_a = (this.getToken(node, attribute_list_tokens_1.AttributeIdShorthand) && "id")) !== null && _a !== void 0 ? _a : (this.getToken(node, attribute_list_tokens_1.AttributeOptionShorthand) && "option")) !== null && _b !== void 0 ? _b : (this.getToken(node, attribute_list_tokens_1.AttributeRoleShorthand) && "role"); const values = this.childTokens(node) .filter((child) => child.tokenType === attribute_list_tokens_1.AttributeInlineText) .map((child) => child.image); return { type: "attributeNode", location: this.location(node), key, value: { location: this.location(node), type: "attributeValueNode", values, }, }; } [parse_to_cst_1.Rules.AttributeValue](node) { const valueTokens = [string_tokens_1.StringText, attribute_list_tokens_1.AttributeInlineText]; const values = this.childTokens(node) .filter((token) => valueTokens.includes(token.tokenType)) .map((token) => token.image); return { type: "attributeValueNode", location: this.location(node), values, }; } [parse_to_cst_1.Rules.Paragraph](node) { return { type: "paragraph", location: this.location(node), lines: this.childNodes(node).map((child) => this.TextRule(child)), isLiteral: !!this.getToken(node, shared_token_1.Space), }; } [parse_to_cst_1.Rules.Text](node) { const texts = this.childTokens(node) .filter((token) => token.tokenType === shared_token_1.InlineText) .map((token) => token.image); return { type: "text", location: this.location(node), text: texts, }; } getToken(node, token) { var _a; const result = (_a = node.children[token.name]) === null || _a === void 0 ? void 0 : _a[0]; if (result && "tokenType" in result) { return result; } return undefined; } getNode(node, rule) { var _a; const result = (_a = node.children[rule]) === null || _a === void 0 ? void 0 : _a[0]; if (result && "children" in result) { return result; } return undefined; } location(node) { if (!node.location) { throw new Error(`Missing location on CST node "${node.name}".`); } if (!node.location.endOffset || !node.location.startOffset) { return { start: -1, end: -1 }; } return { start: node.location.startOffset, end: node.location.endOffset }; } childNodes(node) { const childValues = Object.values(node.children); const children = childValues.flat(); return children .filter((child) => "location" in child) .sort((a, b) => this.getStart(a) - this.getStart(b)); } childTokens(node) { const childValues = Object.values(node.children); const children = childValues.flat(); return children .filter((child) => "image" in child) .sort((a, b) => this.getStart(a) - this.getStart(b)); } getStart(element) { if ("location" in element && element.location) { return element.location.startOffset; } if ("startOffset" in element) { return element.startOffset; } throw new Error("Couldn't resolve start of CSTElement."); } getEnd(element) { if ("location" in element && element.location && element.location.endOffset) { return element.location.endOffset; } if ("endOffset" in element) { return element.endOffset; } throw new Error("Couldn't resolve start of CSTElement."); } } const visitor = new CSTVisitor(); function cstToAst(cst) { return visitor.visit(cst); } exports.cstToAst = cstToAst;