@tsukiroku/tiny
Version:
Tiny interpreter
86 lines (85 loc) • 4.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printError = exports.error = exports.errorFormatter = exports.localization = void 0;
const colors_1 = __importDefault(require("colors"));
colors_1.default.enabled = true;
const errorsLocale = {
en: {
parseError: {
invalidIdentifier: 'Invalid identifier',
invalidNumber: 'Invalid number',
invalidString: 'Unterminated string: {0}',
unexpectedToken: "Expected next token to be '{0}', got '{1}' instead.",
unexpectedExpression: `Expected expression, got {0} instead.`,
invalidBodyBlock: `Invalid body block.`,
decoratorRequiresFunction: `Decorator requires a function.`,
voidRequiresExpression: `'void' requires an expression.`,
},
runtimeError: {
invalidArgument: "'{0}' expected {1} arguments but got {2}.",
invalidFunction: "'{0}' is not a function.",
identifierNotDefined_1: "'{0}' is not defined.",
identifierNotDefined_2: "Identifier '{0}' is not defined.",
typeMismatch_1: 'Type mismatch.',
typeMismatch_2: 'Type mismatch: [{0}], [{1}].',
indexOutOfRange: 'Index out of range.',
deleteRequiresIdentifier: "'delete' can only be used on identifiers.",
useRequiresString: "'use' can only be used on strings.",
},
builtinError: {
invalidArgument: 'Invalid argument.',
disableAllowEval: "'allowEval' is not allowed",
disableAllowJavaScript: "'allowJavaScript' is not allowed",
couldNotEval: '`Could not evalulate JavaScript: {0}',
},
},
ko: {
parseError: {
invalidIdentifier: '유효하지 않은 식별자입니다.',
invalidNumber: '유효하지 않은 숫자입니다.',
invalidString: '종료되지 않은 문자열입니다: {0}',
unexpectedToken: "'{0}' 을(를) 예상했지만, '{1}' 을(를) 받았습니다.",
unexpectedExpression: `식을 예상했지만, {0}을(를) 받았습니다.`,
invalidBodyBlock: `유효하지 않은 블록입니다.`,
decoratorRequiresFunction: `데코레이터는 함수를 요구합니다.`,
voidRequiresExpression: `'void'는 식을 요구합니다.`,
},
runtimeError: {
invalidArgument: "'{0}' 에는 {1} 개의 인수가 필요하지만, {2} 개의 인수를 받았습니다.",
invalidFunction: "'{0}' 은(는) 함수가 아닙니다.",
identifierNotDefined_1: "'{0}' 이(가) 정의되지 않았습니다.",
identifierNotDefined_2: "식별자 '{0}' 이(가) 정의되지 않았습니다.",
typeMismatch_1: '유형이 일치하지 않습니다.',
typeMismatch_2: '유형이 일치하지 않습니다: [{0}], [{1}].',
indexOutOfRange: '인덱스가 범위를 벗어났습니다.',
deleteRequiresIdentifier: "'delete' 는 식별자만 사용할 수 있습니다.",
useRequiresString: "'use' 는 문자열만 사용할 수 있습니다.",
},
builtinError: {
invalidArgument: '유효하지 않은 인수입니다.',
disableAllowEval: "'allowEval' 은(는) 허용되지 않습니다.",
disableAllowJavaScript: "'allowJavaScript' 은(는) 허용되지 않습니다.",
couldNotEval: '`자바스크립트를 실행하지 못했습니다: {0}',
},
},
};
const localization = (options) => options.locale ? errorsLocale[options.locale] ?? errorsLocale.en : errorsLocale.en;
exports.localization = localization;
const errorFormatter = (message, ...args) => args.reduce((message, curr, index) => message.replaceAll(`{${index}}`, curr), message);
exports.errorFormatter = errorFormatter;
const error = (message, line, column) => ({
kind: 308 /* Tiny.ObjectKind.ERROR */,
message,
line,
column,
});
exports.error = error;
const printError = (error, file, stderr, options) => {
const { line, column, message } = error;
stderr(`${options.stderrPrefix ? `${options.stderrColor ? `[Error]`.bgRed : `[Error]`} ` : ''}${options.stderrColor ? message.red : message} (${options.stderrColor ? `${file}:${`${line}:${column}`.yellow}` : `${file}:${line}:${column}`})`);
};
exports.printError = printError;
exports.default = exports.error;