You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.8 KiB
JavaScript
100 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const pad = require("pad");
|
|
const chalk = Object.assign(require("chalk"), {enabled: true});
|
|
const defaultValue = require("default-value");
|
|
|
|
module.exports = function createBetterPegTracer(parsedString) {
|
|
let currentLevel = 0;
|
|
let lastWasFail = false
|
|
let parsedStringLines = parsedString.split("\n");
|
|
let lastSeenRule = [];
|
|
|
|
return {
|
|
trace: function trace(event) {
|
|
if (event.type === "rule.fail" || event.type === "rule.match") {
|
|
currentLevel -= 1;
|
|
}
|
|
|
|
let ruleIndent = pad("", currentLevel * 4);
|
|
let eventName = event.type.replace(/^rule\./, "");
|
|
let isMultiline = (parsedStringLines.length > 1);
|
|
|
|
let startLine = event.location.start.line;
|
|
let endLine = event.location.end.line;
|
|
let startColumn = event.location.start.column;
|
|
let endColumn = event.location.end.column;
|
|
|
|
let firstLine = parsedStringLines[startLine - 1];
|
|
let lastLine = parsedStringLines[endLine - 1];
|
|
|
|
let affectedString, color, startPosition, endPosition, position;
|
|
|
|
if (isMultiline) {
|
|
startPosition = `${pad(4, startLine)}:${pad(2, startColumn)}`;
|
|
|
|
if (startLine !== endLine || startColumn !== endColumn) {
|
|
endPosition = `${pad(4, endLine)}:${pad(2, endColumn)}`;
|
|
}
|
|
} else {
|
|
startPosition = startColumn;
|
|
|
|
if (startColumn !== endColumn) {
|
|
endPosition = endColumn;
|
|
}
|
|
}
|
|
|
|
position = `${pad(8, startPosition)} ${chalk.white("│")} ${pad(8, defaultValue(endPosition, ""))}`;
|
|
|
|
if (startLine === endLine) {
|
|
if (startColumn === endColumn) {
|
|
/* Not reading a range, but rather attempting to find a match from the current point. */
|
|
if (event.location.start.offset < parsedString.length) {
|
|
affectedString = chalk.gray(`${firstLine.slice(startColumn - 1)} ...`);
|
|
} else {
|
|
affectedString = "";
|
|
}
|
|
} else {
|
|
affectedString = firstLine.slice(startColumn - 1, endColumn - 1);
|
|
}
|
|
} else {
|
|
affectedString = `${firstLine.slice(startColumn - 1)} ... ${lastLine.slice(0, endColumn - 1)}`;
|
|
}
|
|
|
|
if (event.type === "rule.enter") {
|
|
lastSeenRule[currentLevel] = event.rule;
|
|
}
|
|
|
|
if (event.type === "rule.match") {
|
|
color = chalk.green;
|
|
} else if (event.type === "rule.fail") {
|
|
color = chalk.red;
|
|
} else {
|
|
color = chalk.white;
|
|
}
|
|
|
|
let crumbs = lastSeenRule.slice(0, currentLevel).concat([color(event.rule)]);
|
|
let limitedCrumbs;
|
|
|
|
if (crumbs.length > 6) {
|
|
limitedCrumbs = crumbs.slice(0, 2).concat([`... ${crumbs.length - 5} hidden ...`]).concat(crumbs.slice(-3));
|
|
} else {
|
|
limitedCrumbs = crumbs;
|
|
}
|
|
|
|
let line = [
|
|
position,
|
|
pad(eventName, 5),
|
|
chalk.gray(pad(limitedCrumbs.join(" -> "), "90")),
|
|
affectedString
|
|
].join(` ${chalk.white("│")} `);
|
|
|
|
process.stdout.write(color(line + "\n"));
|
|
|
|
if (event.type === "rule.enter") {
|
|
currentLevel += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|