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

Function parse

src/compute-engine/latex-syntax/parse.ts:3231–3275  ·  view source on GitHub ↗
(
  latex: string,
  dictionary: IndexedLatexDictionary,
  options: Readonly<ParseLatexOptions>
)

Source from the content-addressed store, hash-verified

3229
3230 const start = this.index;
3231 // Skip visual space (e.g. `\ `, `\,`) before peeking postfix triggers, so
3232 // a `\{…\}` When-restriction can attach even when separated from its base
3233 // by space. Restricted to brace triggers: for other postfix operators —
3234 // notably `\left[…\right]` indexing — a preceding space means implicit
3235 // multiplication by a list literal (Desmos semantics), not a postfix.
3236 // The `this.index = start` no-match restore rolls back the skipped space.
3237 this.skipVisualSpace();
3238 if (this.index !== start) {
3239 const tok = this._tokens[this.index];
3240 const isBraceTrigger =
3241 tok === '\\{' ||
3242 (tok === '\\left' && this._tokens[this.index + 1] === '\\{');
3243 if (!isBraceTrigger) this.index = start;
3244 }
3245 const afterSpace = this.index;
3246 for (const [def, n] of this.peekDefinitions('postfix')) {
3247 this.index = afterSpace + n;
3248 const result = def.parse(this, lhs, until);
3249 if (result !== null) return result;
3250 }
3251 this.index = start;
3252 return null;
3253 }
3254
3255 /**
3256 * This method can be invoked when we know we're in an error situation,
3257 * for example when there are tokens remaining after we've finished parsing.
3258 *
3259 * In general, if a context does not apply, we return `null` to give
3260 * the chance to some other option to be considered. However, in some cases
3261 * we know we've exhausted all possibilities, and in this case this method
3262 * will return an error expression as informative as possible.
3263 *
3264 * We've encountered a LaTeX command or symbol but were not able to match it
3265 * to any entry in the LaTeX dictionary, or ran into it in an unexpected
3266 * context (postfix operator lacking an argument, for example)
3267 */
3268 parseSyntaxError(): MathJsonExpression {
3269 const start = this.index;
3270
3271 //
3272 // Is this an unexpected operator?
3273 // (this is an error handling code path)
3274 //
3275 // '^' is a special infix operator, with a custom parser
3276 if (this.peek === '^') {
3277 this.index += 1;
3278 return [

Callers

nothing calls this directly

Calls 8

matchesStringFunction · 0.90
stringValueFunction · 0.90
matchesSymbolFunction · 0.90
matchesNumberFunction · 0.90
parseCoreFunction · 0.85
containsErrorFunction · 0.70
sliceMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected