UNPKG

prettier-plugin-asciidoc

Version:

Format AsciiDoc files with prettier 📖

125 lines (124 loc) • 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseToCST = exports.Rules = void 0; const chevrotain_1 = require("chevrotain"); const tokenize_1 = require("./lexer/tokenize"); const attribute_list_tokens_1 = require("./lexer/attribute-list-tokens"); const shared_token_1 = require("./lexer/shared-token"); const string_tokens_1 = require("./lexer/string-tokens"); exports.Rules = { Text: "TextRule", Paragraph: "ParagraphRule", Headline: "HeadlineRule", EmptyLine: "EmptyLineRule", AttributeList: "AttributeList", AttributeBlock: "AttributeBlock", AttributeEntry: "AttributeEntry", AttributeEntryShorthand: "AttributeEntryShorthand", AttributeValue: "AttributeValue", Adoc: "AdocRule", }; class AdocParser extends chevrotain_1.CstParser { constructor() { super(tokenize_1.AllTokens, { nodeLocationTracking: "full" }); this.text = this.RULE(exports.Rules.Text, () => this.AT_LEAST_ONE(() => { this.CONSUME(shared_token_1.InlineText); this.OPTION(() => this.CONSUME1(shared_token_1.Space)); })); this.headline = this.RULE(exports.Rules.Headline, () => { this.CONSUME(shared_token_1.Newline); this.CONSUME(shared_token_1.Headline); this.MANY(() => this.CONSUME(shared_token_1.Space)); this.SUBRULE(this.text); }); this.emptyLine = this.RULE(exports.Rules.EmptyLine, () => { this.CONSUME(shared_token_1.EmptyLine); }); this.paragraph = this.RULE(exports.Rules.Paragraph, () => { this.AT_LEAST_ONE(() => { this.CONSUME(shared_token_1.Newline); this.MANY(() => this.CONSUME1(shared_token_1.Space)); this.SUBRULE(this.text); }); }); this.attributeValue = this.RULE(exports.Rules.AttributeValue, () => { this.OR([ { ALT: () => { this.CONSUME1(string_tokens_1.StringStart); this.MANY_SEP({ SEP: shared_token_1.Space, DEF: () => this.CONSUME2(string_tokens_1.StringText), }); this.CONSUME3(string_tokens_1.StringEnd); }, }, { ALT: () => this.CONSUME4(attribute_list_tokens_1.AttributeInlineText) }, ]); }); this.attributeEntry = this.RULE(exports.Rules.AttributeEntry, () => { this.OPTION(() => { this.CONSUME(attribute_list_tokens_1.AttributeInlineText); this.CONSUME(shared_token_1.Assignment); }); this.SUBRULE1(this.attributeValue); }); this.shorthandAttributeEntry = this.RULE(exports.Rules.AttributeEntryShorthand, () => { this.OR([ { ALT: () => this.AT_LEAST_ONE(() => { this.CONSUME5(attribute_list_tokens_1.AttributeIdShorthand); this.CONSUME6(attribute_list_tokens_1.AttributeInlineText); }), }, { ALT: () => this.AT_LEAST_ONE1(() => { this.CONSUME7(attribute_list_tokens_1.AttributeRoleShorthand); this.CONSUME8(attribute_list_tokens_1.AttributeInlineText); }), }, { ALT: () => this.AT_LEAST_ONE2(() => { this.CONSUME9(attribute_list_tokens_1.AttributeOptionShorthand); this.consume(10, attribute_list_tokens_1.AttributeInlineText); }), }, ]); }); this.attributeBlock = this.RULE(exports.Rules.AttributeBlock, () => { this.CONSUME(shared_token_1.Newline); this.SUBRULE(this.attributeList); }); this.attributeList = this.RULE(exports.Rules.AttributeList, () => { this.CONSUME1(attribute_list_tokens_1.AttributeListStart); this.MANY(() => this.OR([ { ALT: () => this.SUBRULE(this.shorthandAttributeEntry) }, { ALT: () => this.MANY_SEP({ DEF: () => this.SUBRULE(this.attributeEntry), SEP: shared_token_1.Comma, }), }, ])); this.CONSUME2(attribute_list_tokens_1.AttributeListEnd); }); this.adoc = this.RULE(exports.Rules.Adoc, () => this.MANY(() => this.OR([ { ALT: () => this.SUBRULE1(this.headline) }, { ALT: () => this.SUBRULE4(this.attributeBlock) }, { ALT: () => this.SUBRULE2(this.paragraph) }, { ALT: () => this.SUBRULE3(this.emptyLine) }, ]))); this.performSelfAnalysis(); } } const parser = new AdocParser(); function parseToCST(tokens) { parser.input = tokens; const result = parser.adoc(); const error = parser.errors[0]; if (error) { throw error; } return result; } exports.parseToCST = parseToCST;