v-regexp
Version:
JavaScript Regular Expression Parser and Visualizer.
176 lines • 6.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.highlight = void 0;
var tslib_1 = require("tslib");
var Kit_1 = tslib_1.__importDefault(require("../Kit"));
var common_1 = require("./common");
var constants_1 = require("../constants");
function highlight(tree, theme) {
var texts = [];
tree.forEach(function (node) {
if (node.sub) {
texts.push(common_1.getHighlightText({
str: '(',
theme: theme,
}));
if (node.type === constants_1.ASSERT_NODE) {
if (node.assertionType === constants_1.AssertLookahead) {
texts.push(common_1.getHighlightText({
str: '?=',
theme: theme,
}));
}
else {
texts.push(common_1.getHighlightText({
str: '?!',
theme: theme,
}));
}
}
else if (node.nonCapture) {
texts.push(common_1.getHighlightText({
str: '?:',
theme: theme,
}));
}
texts = texts.concat(highlight(node.sub, theme));
texts.push(common_1.getHighlightText({
str: ')',
theme: theme,
}));
}
else if (node.branches) {
node.branches
.map(function (node) { return highlight(node, theme); })
.forEach(function (ts) {
texts = texts.concat(ts);
texts.push(common_1.getHighlightText({
str: '|',
theme: theme,
}));
});
texts.pop();
}
else {
var color = theme.highlightColor[node.type] || theme.highlightColor.defaults;
var simple = common_1.onlyCharClass(node);
var raw = node.raw || '';
switch (node.type) {
case constants_1.CHARSET_NODE:
if (!simple || node.exclude) {
texts.push(common_1.getHighlightText({
str: '[',
theme: theme,
}));
}
if (node.exclude) {
texts.push(common_1.getHighlightText({
str: '^',
theme: theme,
color: theme.highlightColor.charsetExclude,
}));
}
node.ranges.forEach(function (rg) {
texts.push(common_1.getHighlightText({
str: common_1.charsetEscape(rg[0] + "-" + rg[1]),
theme: theme,
color: theme.highlightColor.charsetRange,
}));
});
node.classes.forEach(function (cls) {
texts.push(common_1.getHighlightText({
str: "\\" + cls,
theme: theme,
color: theme.highlightColor.charsetClass,
}));
});
texts.push(common_1.getHighlightText({
str: common_1.charsetEscape(node.chars),
theme: theme,
color: theme.highlightColor.charsetChars,
}));
if (!simple || node.exclude) {
texts.push(common_1.getHighlightText({
str: ']',
theme: theme,
}));
}
break;
default:
if (node.repeat) {
raw = raw.slice(0, node.repeat.beginIndex);
}
raw = Kit_1.default.toPrint(raw, true);
texts.push(common_1.getHighlightText({
str: raw,
theme: theme,
color: color,
}));
}
}
if (node.repeat) {
var min = node.repeat.min;
var max = node.repeat.max;
if (min === 0 && max === Infinity) {
texts.push(common_1.getHighlightText({
str: '*',
theme: theme,
}));
}
else if (min === 1 && max === Infinity) {
texts.push(common_1.getHighlightText({
str: '+',
theme: theme,
}));
}
else if (min === 0 && max === 1) {
texts.push(common_1.getHighlightText({
str: '?',
theme: theme,
}));
}
else {
texts.push(common_1.getHighlightText({
str: '{',
theme: theme,
}));
texts.push(common_1.getHighlightText({
str: min,
theme: theme,
}));
if (min === max) {
texts.push(common_1.getHighlightText({
str: '}',
theme: theme,
}));
}
else {
texts.push(common_1.getHighlightText({
str: ',',
theme: theme,
}));
if (Number.isFinite(max)) {
texts.push(common_1.getHighlightText({
str: max,
theme: theme,
}));
}
texts.push(common_1.getHighlightText({
str: '}',
theme: theme,
}));
}
}
if (node.repeat.nonGreedy) {
texts.push(common_1.getHighlightText({
str: '?',
theme: theme,
color: theme.highlightColor.repeatNonGreedy,
}));
}
}
});
return texts;
}
exports.highlight = highlight;
//# sourceMappingURL=highlight.js.map