UNPKG

@making-sense/antlr-editor

Version:
71 lines 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GrammarGraph = void 0; const grammarStatement_1 = require("./grammarStatement"); const ruleTokenizer_1 = require("./ruleTokenizer"); const statementType_1 = require("./statementType"); const syntaxCollection_1 = require("./syntaxCollection"); class GrammarGraph { constructor(vocabulary, grammar) { this.rules = new Map(); this.operators = new Map(); this.keywords = new syntaxCollection_1.SyntaxCollection(); this.vocabulary = vocabulary; this.tokenizer = new ruleTokenizer_1.RuleTokenizer(vocabulary); if (grammar && grammar.length > 0) this.processRaw(grammar); } processRaw(grammar) { // Strip import wrapping, comments, newlines and tabulators let processed = grammar.replace(/^export default "|\/\*.*?\*\/|(\\[rnt])+|";$/g, " "); // Replace literal tokens with names from vocabulary this.vocabulary.getLiteralNames().forEach((literal, index) => { const symbolic = this.vocabulary.symbolicName(index); if (!!literal && processed.includes(literal)) { // Escape all RegExp special characters first processed = processed.replace(new RegExp(literal.replace(ruleTokenizer_1.rgxEscape, ruleTokenizer_1.rgxReplace), "g"), " " + symbolic + " "); } }); // Split grammar into rules processed .split(";") .filter(value => value.includes(":")) .forEach((value, index) => { let colon = value.indexOf(":"); const name = this.tokenizer.ruleName(value.substr(0, colon).trim(), index); const tokens = this.tokenizer.tokenize(value.substr(++colon).trim().replace(/ +/g, " ")); this.addRule(name, tokens); }); // Resolve remaining statements and collect operators this.rules.forEach(rule => rule.resolveStatements(this.rules, this.operators, new Map())); // Resolve syntax of functions and operators this.rules.forEach(rule => rule.resolveSyntax(this.keywords)); this.keywords.distinct(); // Find root const rule0 = this.vocabulary.ruleName(0); if (rule0) this.root = this.rules.get(rule0); } addRule(name, tokens) { const rule = new grammarStatement_1.GrammarStatement(statementType_1.StatementType.Rule, tokens, name); this.rules.set(name, rule); rule.processRule(); return rule; } rootName() { return !!this.root ? this.root.name : undefined; } suggestions() { return this.keywords.entries.map(entry => { return { label: entry.keyword, kind: entry.completionKind(), insertText: entry.snippet, detail: entry.syntax, documentation: "" }; }); } } exports.GrammarGraph = GrammarGraph; //# sourceMappingURL=grammarGraph.js.map