Skip to content

This site describes solve-engine as it is on main: 2.43.0, which npm does not have yet. npm installs 2.40.0, so a page may show an answer that version does not give yet.

definePhrasePattern

function definePhrasePattern(opts): PrefixParselet;

Defined in: packages/engine/src/parser/PhrasePattern.ts:102

Build a PrefixParselet from a declarative list of phrase alternatives, instead of hand-writing parser.consume()/ parser.parseExpression() call sequences.

Covers the “keyword slot keyword slot keyword” shape shared by most of this codebase’s phrase-grammar parselets (roll between X and Y, clamp X between Y and Z, average of X, Y, Z’s single-item case, etc.) with one well-tested consumption/error-handling core, rather than each parselet re-deriving its own, the class of bug this is meant to prevent (a hand-rolled parselet mishandling an alternative-phrasing or precedence edge case) is exactly what produced real, shipped bugs this session (Dice’s bare-hyphen range, Vector’s tuple-vs-group ambiguity).

ParameterType
opts{ alternatives: PhraseAlternative[]; category: string; }
opts.alternativesPhraseAlternative[]
opts.categorystring

PrefixParselet

// "roll between X and Y" / "roll from X to Y"
definePhrasePattern({
category: "Dice",
alternatives: [
{
slots: [
{ kind: "keyword", tokenTypes: ["BETWEEN"] },
{ kind: "expr" },
{ kind: "keyword", tokenTypes: ["AND"] },
{ kind: "expr" },
],
emit: (builder) => {
builder.emitOpcode(OpCode.CALL_BUILTIN);
builder.emitIndex(DICE_ROLL_BUILTIN);
builder.emitIndex(2);
},
},
],
});