mirror of
https://github.com/borbann-platform/end-of-semester-report.git
synced 2025-12-19 06:44:03 +01:00
125 lines
3.3 KiB
JavaScript
125 lines
3.3 KiB
JavaScript
import { match } from "@unified-latex/unified-latex-util-match";
|
|
import { printRaw } from "@unified-latex/unified-latex-util-print-raw";
|
|
import Trie from "trie-prefix-tree";
|
|
import { default as default2 } from "trie-prefix-tree";
|
|
function scan(nodes, token, options) {
|
|
const { startIndex, onlySkipWhitespaceAndComments, allowSubstringMatches } = options || {};
|
|
if (typeof token === "string") {
|
|
token = { type: "string", content: token };
|
|
}
|
|
for (let i = startIndex || 0; i < nodes.length; i++) {
|
|
const node = nodes[i];
|
|
if (node.type === token.type) {
|
|
switch (node.type) {
|
|
case "comment":
|
|
case "displaymath":
|
|
case "inlinemath":
|
|
case "root":
|
|
case "parbreak":
|
|
case "whitespace":
|
|
case "verb":
|
|
case "verbatim":
|
|
case "group":
|
|
return i;
|
|
case "macro":
|
|
if (node.content === token.content) {
|
|
return i;
|
|
}
|
|
break;
|
|
case "environment":
|
|
case "mathenv":
|
|
if (printRaw(node.env) === printRaw(token.env)) {
|
|
return i;
|
|
}
|
|
break;
|
|
case "string":
|
|
if (node.content === token.content) {
|
|
return i;
|
|
}
|
|
if (allowSubstringMatches && node.content.indexOf(token.content) >= 0) {
|
|
return i;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if (onlySkipWhitespaceAndComments && !match.whitespace(node) && !match.comment(node)) {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
function prefixMatch(nodes, prefixes, options) {
|
|
const {
|
|
startIndex = 0,
|
|
matchSubstrings = false,
|
|
assumeOneCharStrings = false
|
|
} = options || {};
|
|
if (typeof prefixes === "string") {
|
|
prefixes = [prefixes];
|
|
}
|
|
if (Array.isArray(prefixes)) {
|
|
prefixes = Trie(prefixes);
|
|
}
|
|
const prefixTree = prefixes;
|
|
const history = {
|
|
lastPrefix: "",
|
|
lastWord: "",
|
|
index: startIndex,
|
|
partialMatch: ""
|
|
};
|
|
function tryToMatchNextChar(char, index) {
|
|
let ret = false;
|
|
if (prefixTree.isPrefix(history.lastPrefix + char)) {
|
|
history.lastPrefix += char;
|
|
history.index = index;
|
|
ret = true;
|
|
}
|
|
if (prefixTree.hasWord(history.lastPrefix)) {
|
|
history.lastWord = history.lastPrefix;
|
|
}
|
|
return ret;
|
|
}
|
|
for (let i = 0; startIndex + i < nodes.length; i++) {
|
|
const node = nodes[startIndex + i];
|
|
if (!match.string(node)) {
|
|
break;
|
|
}
|
|
if (assumeOneCharStrings && node.content.length !== 1) {
|
|
break;
|
|
}
|
|
if (matchSubstrings) {
|
|
let fullMatch = true;
|
|
history.partialMatch = "";
|
|
for (let j = 0; j < node.content.length; j++) {
|
|
const char = node.content[j];
|
|
if (tryToMatchNextChar(char, startIndex + i)) {
|
|
history.partialMatch += char;
|
|
} else {
|
|
fullMatch = false;
|
|
break;
|
|
}
|
|
}
|
|
if (fullMatch) {
|
|
history.partialMatch = "";
|
|
} else {
|
|
break;
|
|
}
|
|
} else {
|
|
if (!tryToMatchNextChar(node.content, startIndex + i)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return history.lastWord ? {
|
|
match: history.lastWord,
|
|
endNodeIndex: history.index,
|
|
endNodePartialMatch: history.partialMatch ? history.partialMatch : null
|
|
} : null;
|
|
}
|
|
export {
|
|
default2 as Trie,
|
|
prefixMatch,
|
|
scan
|
|
};
|
|
//# sourceMappingURL=index.js.map
|