@making-sense/antlr-editor
Version:
ANTLR Typescript editor
231 lines • 7.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyntaxEntry = exports.SyntaxLevel = exports.SyntaxCollection = void 0;
const monaco_editor_1 = require("monaco-editor");
class SyntaxCollection {
constructor() {
this.entries = [];
this._levels = [];
this.terminated = false;
this.isEmpty = () => this.entries.length === 0;
this.notEmpty = () => this.entries.length !== 0;
this.size = () => this.entries.length;
}
addAll(entries) {
this.entries.push(...entries);
}
createLevel(optional) {
const level = new SyntaxLevel(optional);
this._levels.unshift(level);
return level;
}
terminateLevel() {
const level = this._levels.shift();
if (level) {
level.terminate();
this.entries.push(...level.entries);
}
}
addKeyword(keyword, syntax, snippet, optional) {
const lastLevel = this._levels[0];
if (!lastLevel)
return;
for (const level of this._levels) {
if (optional) {
level.append("", syntax, "");
}
else {
level.append(keyword, syntax, snippet);
}
if (level.optional)
break;
}
const entry = new SyntaxEntry(keyword);
if (optional)
entry.terminate();
lastLevel.add(entry);
}
append(syntax, snippet) {
for (const level of this._levels) {
level.append("", syntax, snippet);
if (level.optional)
break;
}
}
distinct() {
const distinct = new Map();
const entries = [];
this.entries.forEach(entry => {
let subentries = distinct.get(entry.keyword);
if (subentries) {
if (!subentries.some(subentry => subentry.equals(entry))) {
subentries.push(entry);
entries.push(entry);
}
}
else {
subentries = [];
subentries.push(entry);
distinct.set(entry.keyword, subentries);
entries.push(entry);
}
});
this.entries = entries;
}
}
exports.SyntaxCollection = SyntaxCollection;
class SyntaxLevel {
constructor(optional) {
this.entries = [];
this.optional = false;
this.optional = optional;
}
add(entry) {
this.entries.push(entry);
}
append(keyword, syntax, snippet) {
this.entries.forEach(entry => {
entry.append(keyword, syntax, snippet);
});
}
terminate() {
this.entries.forEach(entry => entry.terminate());
}
}
exports.SyntaxLevel = SyntaxLevel;
class SyntaxEntry {
constructor(keyword) {
this._markdown = { value: "" };
this._terminated = false;
this._kind = monaco_editor_1.languages.CompletionItemKind.Keyword;
this.round = false;
this.openRound = 0;
this.square = false;
this.openSquare = 0;
this.curly = false;
this.openCurly = 0;
this.angle = false;
this.openAngle = 0;
this._keyword = keyword;
this._syntax = keyword;
this._snippet = keyword;
}
terminate() {
this._terminated = true;
}
get keyword() {
return this._keyword;
}
get syntax() {
return this._syntax;
}
get markdown() {
return this._markdown;
}
get snippet() {
return this._snippet;
}
get terminated() {
return this._terminated;
}
append(keyword, syntax, snippet) {
if (this._terminated)
return;
if (this._keyword === this._syntax && keyword !== "") {
this._keyword = this._keyword + " " + keyword;
}
switch (syntax) {
case "(": {
this.round = true;
this.openRound++;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
break;
}
case ")": {
if (this.round && this.openRound > 0) {
this.openRound--;
this.round = this.openRound > 0;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
}
else {
this.terminate();
}
break;
}
case "[": {
this.square = true;
this.openSquare++;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
break;
}
case "]": {
if (this.square && this.openSquare > 0) {
this.openSquare--;
this.square = this.openSquare > 0;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
}
else {
this.terminate();
}
break;
}
case "{": {
this.curly = true;
this.openCurly++;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
break;
}
case "}": {
if (this.curly && this.openCurly > 0) {
this.openCurly--;
this.curly = this.openCurly > 0;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
}
else {
this.terminate();
}
break;
}
case "<": {
this.angle = true;
this.openAngle++;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
break;
}
case ">": {
if (this.angle && this.openAngle > 0) {
this.openAngle--;
this.angle = this.openAngle > 0;
this._syntax = this._syntax + " " + syntax;
this._snippet = this._snippet + " " + snippet;
}
else {
this.terminate();
}
break;
}
default: {
this._syntax = this._syntax + (syntax !== "" ? " " + syntax : "");
this._snippet = this._snippet + (snippet !== "" ? " " + snippet : "");
}
}
}
equals(entry) {
return (this._keyword === entry._keyword &&
this._syntax === entry._syntax &&
this._snippet === entry._snippet &&
this._terminated === entry._terminated);
}
completionKind() {
return this._kind;
}
}
exports.SyntaxEntry = SyntaxEntry;
//# sourceMappingURL=syntaxCollection.js.map