diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index a86290fd..649723a5 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -451,7 +451,9 @@ export class Pattern { * @noAutocomplete */ withHaps(func) { - return new Pattern((state) => func(this.query(state), state)); + const result = new Pattern((state) => func(this.query(state), state)); + result.weight = this.weight; + return result; } /** @@ -484,6 +486,7 @@ export class Pattern { const result = this.withHap((hap) => hap.setContext(func(hap.context))); if (this.__pure !== undefined) { result.__pure = this.__pure; + result.__pure_loc = this.__pure_loc; } return result; } @@ -510,10 +513,15 @@ export class Pattern { start, end, }; - return this.withContext((context) => { + const result = this.withContext((context) => { const locations = (context.locations || []).concat([location]); return { ...context, locations }; }); + if (this.__pure) { + result.__pure = this.__pure; + result.__pure_loc = location; + } + return result; } /** @@ -1608,7 +1616,12 @@ export function register(name, func, patternify = true, preserveWeight = false) if (firstArgs.every((arg) => arg.__pure != undefined)) { const pureArgs = firstArgs.map((arg) => arg.__pure); + const pureLocs = firstArgs.filter((arg) => arg.__pure_loc).map((arg) => arg.__pure_loc); result = func(...pureArgs, pat); + result = result.withContext((context) => { + const locations = (context.locations || []).concat(pureLocs); + return { ...context, locations }; + }); } else { const [left, ...right] = firstArgs;