MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / parseModifier

Function parseModifier

src/compute-engine/boxed-expression/rules.ts:300–363  ·  view source on GitHub ↗
(parser: Parser)

Source from the content-addressed store, hash-verified

298 * Evaluate a rule-condition predicate and report whether it discharges.
299 *
300 * Rule guards must be *fail-closed*: an unprovable predicate (e.g. `w ≠ 0` for
301 * an unconstrained `w`) must NOT satisfy the guard. Plain `.evaluate()` applies
302 * the pragmatic top-level collapse where `Equal`/`NotEqual` of an undecided
303 * comparison return `False`/`True`, which would fire guards vacuously. Setting
304 * `_isVerifying` for the duration of the evaluation keeps those operators
305 * three-valued (unknown stays unknown), so the guard only discharges when the
306 * predicate provably evaluates to `True`. Assumption- and bounds-derived facts
307 * are still consulted (they read the fact index directly, independent of the
308 * pragmatic collapse), so `; z ≠ 0` still fires under `assume(z > 0)`.
309 */
310function conditionHolds(ce: ComputeEngine, condition: Expression): boolean {
311 const savedVerifying = ce._isVerifying;
312 ce._isVerifying = true;
313 try {
314 return isSymbol(condition.evaluate(), 'True');
315 } finally {
316 ce._isVerifying = savedVerifying;
317 }
318}
319
320function tokenizeLaTeX(input: string): string[] {
321 // Regular expression to match LaTeX tokens
322 const regex = /\\[a-zA-Z]+|[{}]|[\d]+|[+\-*/^_=()><,.;]|[a-zA-Z]/g;
323
324 const tokens = input.match(regex);
325
326 if (!tokens) return [];
327
328 // Filter blank spaces
329 return tokens.filter((x) => !/^[ \f\n\r\t\v\xA0\u2028\u2029]+$/.test(x));
330}
331
332function parseModifier(parser: Parser): string | null {
333 // Is it a tagged modifier of the form `\mathrm{modifier}`?
334 const next = parser.peek;
335 let modifier: string | null = null;
336 if (next === '\\mathrm') {
337 parser.nextToken();
338 modifier = parser.parseStringGroup();
339 } else if (/^[a-z]$/.test(next)) {
340 // We also accept unwrapped modifiers, i.e. `:modifier`
341 modifier = parser.nextToken();
342 while (/^[a-z]$/.test(parser.peek)) modifier += parser.nextToken();
343 } else {
344 // We accept some shortcuts for common conditions
345 const shortcuts = {
346 '>0': 'positive',
347 '\\gt0': 'positive',
348 '<0': 'negative',
349 '\\lt0': 'negative',
350 '>=0': 'nonnegative',
351 '\\geq0': 'nonnegative',
352 '<=0': 'nonpositive',
353 '\\leq0': 'nonpositive',
354 '!=0': 'notzero',
355 '\\neq0': 'notzero',
356 '\\neq1': 'notone',
357 '!=1': 'notone',

Callers 1

parserModifiersFunction · 0.85

Calls 5

tokenizeLaTeXFunction · 0.85
nextTokenMethod · 0.65
parseStringGroupMethod · 0.65
matchAllMethod · 0.65
keysMethod · 0.65

Tested by

no test coverage detected