UNPKG

@creditkarma/thrift-parser

Version:

A parser for Thrift written in TypeScript

82 lines 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const os = require("os"); function noopReporter(err) { throw new Error(`${err.type}: Line: ${err.loc.start.line}: ${err.message}`); } exports.noopReporter = noopReporter; function padLeft(num, str) { while (str.length < num) { str = ' ' + str; } return str; } function indicatorForLocaction(loc) { const indicator = padLeft(loc.start.column, '^'); return indicator; } function padStart(length, str) { let paddedStr = str; while (length--) { paddedStr = ' ' + paddedStr; } return paddedStr; } function errorType(type) { switch (type) { case "ParseError" /* ParseError */: return 'Parse Error:'; case "ScanError" /* ScanError */: return 'Scan Error:'; } } function createDebugger(source) { const sourceLines = source.split(os.EOL); const formattedErrors = []; const rawErrors = []; function getSourceLine(lineNumber) { return sourceLines[(lineNumber - 1)]; } function formatError(err) { return { sourceLine: getSourceLine(err.loc.start.line), locIndicator: indicatorForLocaction(err.loc), line: err.loc.start.line, column: err.loc.start.column, message: err.message, type: err.type, }; } return { hasError() { return formattedErrors.length > 0; }, getErrors() { return rawErrors; }, getFormattedErrors() { return formattedErrors; }, report(err) { const formattedError = formatError(err); formattedErrors.push(formattedError); rawErrors.push(err); }, print() { console.log(`Parse Failure: ${formattedErrors.length} errors found:`); console.log(); formattedErrors.forEach((err) => { const prefix = `${err.line} | `; console.log(); console.log(`${errorType(err.type)}\n`); console.log(`Message: ${err.message}`); console.log(); console.log(`${prefix}${err.sourceLine}`); console.log(padStart(prefix.length, err.locIndicator)); console.log(); }); }, }; } exports.createDebugger = createDebugger; //# sourceMappingURL=debugger.js.map