File size: 6,587 Bytes
6efa67a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | /** @typedef {import('./MacroCstWalker.js').MacroCall} MacroCall */
/** @typedef {import('./MacroRegistry.js').MacroDefinition} MacroDefinition */
/** @typedef {import('chevrotain').ILexingError} ILexingError */
/** @typedef {import('chevrotain').IRecognitionException} IRecognitionException */
/**
* @typedef {Object} MacroErrorContext
* @property {string} [macroName]
* @property {MacroCall} [call]
* @property {MacroDefinition} [def]
*/
/**
* Options for creating a macro runtime error.
*
* @typedef {MacroErrorContext & { message: string }} MacroRuntimeErrorOptions
*/
/**
* Options for logging macro warnings or errors.
*
* @typedef {MacroErrorContext & { message: string, error?: any }} MacroLogOptions
*/
/**
* Creates an error representing a runtime macro invocation problem (such as
* arity or type mismatches). These errors are intended to be caught by the
* MacroEngine, which will log them as runtime warnings and leave the macro
* raw in the evaluated text.
*
* @param {MacroRuntimeErrorOptions} options
* @returns {Error}
*/
export function createMacroRuntimeError({ message, call, def, macroName }) {
const inferredName = inferMacroName(call, def, macroName);
const error = new Error(message);
error.name = 'MacroRuntimeError';
// @ts-ignore - custom tagging for downstream classification
error.isMacroRuntimeError = true;
// @ts-ignore - helpful metadata for debugging
error.macroName = inferredName;
// @ts-ignore - best-effort location information
error.macroRange = call && call.range ? call.range : null;
// @ts-ignore - attach raw call/definition for convenience
if (call) error.macroCall = call;
// @ts-ignore
if (def) error.macroDefinition = def;
return error;
}
/**
* Logs a macro runtime warning with consistent, helpful context. These
* correspond to issues in how a macro was written in the text (e.g. invalid
* arguments), not bugs in macro definitions or the engine itself.
*
* @param {MacroLogOptions} options
*/
export function logMacroRuntimeWarning({ message, call, def, macroName, error }) {
const payload = buildMacroPayload({ call, def, macroName, error });
console.warn('[Macro] Warning:', message, payload);
}
/**
* Logs an internal macro error (definition or engine bug) with a consistent
* schema. These are surfaced as red errors in the console.
*
* @param {MacroLogOptions} options
*/
export function logMacroInternalError({ message, call, macroName, error }) {
const payload = buildMacroPayload({ call, def: undefined, macroName, error });
console.error('[Macro] Error:', message, payload);
}
/**
* Logs a warning during macro registration.
*
* @param {{ message: string, macroName?: string, error?: any }} options
*/
export function logMacroRegisterWarning({ message, macroName, error = undefined }) {
const payload = buildMacroPayload({ macroName, error });
console.warn('[Macro] Warning:', message, payload);
}
/**
* Logs an error during macro registration. Used when registration fails
* and the macro will not be available.
*
* @param {{ message: string, macroName?: string, error?: any }} options
*/
export function logMacroRegisterError({ message, macroName, error = undefined }) {
const payload = buildMacroPayload({ macroName, error });
console.error('[Macro] Registration Error:', message, payload);
}
/**
* Logs a macro error with a consistent schema.
*
* @param {{ message: string, error?: any }} options
*/
export function logMacroGeneralError({ message, error }) {
console.error('[Macro] Error:', message, error);
}
/**
* Logs lexer/parser syntax warnings for the macro engine with a compact,
* human-readable payload.
*
* @param {{ phase: 'lexing', input: string, errors: ILexingError[] }|{ phase: 'parsing', input: string, errors: IRecognitionException[] }} options
*/
export function logMacroSyntaxWarning({ phase, input, errors }) {
if (!errors || errors.length === 0) {
return;
}
/** @type {{ message: string, line: number|null, column: number|null, length: number|null }[]} */
const issues = errors.map((err) => {
const hasOwnLine = typeof err.line === 'number';
const hasOwnColumn = typeof err.column === 'number';
const token = /** @type {{ startLine?: number, startColumn?: number, startOffset?: number, endOffset?: number }|undefined} */ (err.token);
const line = hasOwnLine ? err.line : (token && typeof token.startLine === 'number' ? token.startLine : null);
const column = hasOwnColumn ? err.column : (token && typeof token.startColumn === 'number' ? token.startColumn : null);
/** @type {number|null} */
let length = null;
if (typeof err.length === 'number') {
length = err.length;
} else if (token && typeof token.startOffset === 'number' && typeof token.endOffset === 'number') {
length = token.endOffset - token.startOffset + 1;
}
return {
message: err.message,
line,
column,
length,
};
});
const label = phase === 'lexing' ? 'Lexing' : 'Parsing';
/** @type {Record<string, any>} */
const payload = {
phase,
count: issues.length,
issues,
input,
};
console.warn('[Macro] Warning:', `${label} errors detected`, payload);
}
/**
* Builds a structured payload for macro logging.
*
* @param {MacroErrorContext & { error?: any }} ctx
*/
function buildMacroPayload({ call, def, macroName, error }) {
const inferredName = inferMacroName(call, def, macroName);
/** @type {Record<string, any>} */
const payload = {
macroName: inferredName,
};
if (call && call.range) payload.range = call.range;
if (call && typeof call.rawInner === 'string') payload.raw = call.rawInner;
if (call) payload.call = call;
if (def) payload.def = def;
if (error) payload.error = error;
return payload;
}
/**
* Infers the most appropriate macro name from the available context.
*
* @param {MacroCall} [call]
* @param {MacroDefinition} [def]
* @param {string} [explicit]
* @returns {string}
*/
function inferMacroName(call, def, explicit) {
if (typeof explicit === 'string' && explicit.trim()) {
return explicit.trim();
}
if (call && typeof call.name === 'string' && call.name.trim()) {
return call.name.trim();
}
if (def && typeof def.name === 'string' && def.name.trim()) {
return def.name.trim();
}
return 'unknown';
}
|