MCPcopy Create free account
hub / github.com/KaTeX/KaTeX / parseAtom

Method parseAtom

src/Parser.ts:338–456  ·  view source on GitHub ↗

* Parses a group with optional super/subscripts.

(breakOnTokenText?: BreakToken)

Source from the content-addressed store, hash-verified

336 * Parses a group with optional super/subscripts.
337 */
338 parseAtom(breakOnTokenText?: BreakToken): AnyParseNode | null {
339 // The body of an atom is an implicit group, so that things like
340 // \left(x\right)^2 work correctly.
341 const base = this.parseGroup("atom", breakOnTokenText);
342
343 // Internal nodes (e.g. \relax) cannot support super/subscripts.
344 // Instead, we will pick up super/subscripts with blank base next round.
345 if (base?.type === "internal") {
346 return base;
347 }
348
349 // In text mode, we don't have superscripts or subscripts
350 if (this.mode === "text") {
351 return base;
352 }
353
354 let superscript: AnyParseNode | undefined;
355 let subscript: AnyParseNode | undefined;
356 while (true) {
357 // Guaranteed in math mode, so eat any spaces first.
358 this.consumeSpaces();
359
360 // Lex the first token
361 const lex = this.fetch();
362
363 if (lex.text === "\\limits" || lex.text === "\\nolimits") {
364 // We got a limit control
365 if (base && base.type === "op") {
366 base.limits = lex.text === "\\limits";
367 base.alwaysHandleSupSub = true;
368 } else if (base && base.type === "operatorname") {
369 if (base.alwaysHandleSupSub) {
370 base.limits = lex.text === "\\limits";
371 }
372 } else {
373 throw new ParseError(
374 "Limit controls must follow a math operator",
375 lex);
376 }
377 this.consume();
378 } else if (lex.text === "^") {
379 // We got a superscript start
380 if (superscript) {
381 throw new ParseError("Double superscript", lex);
382 }
383 superscript = this.handleSupSubscript("superscript");
384 } else if (lex.text === "_") {
385 // We got a subscript start
386 if (subscript) {
387 throw new ParseError("Double subscript", lex);
388 }
389 subscript = this.handleSupSubscript("subscript");
390 } else if (lex.text === "'") {
391 // We got a prime
392 if (superscript) {
393 throw new ParseError("Double superscript", lex);
394 }
395

Callers 1

parseExpressionMethod · 0.95

Calls 6

parseGroupMethod · 0.95
consumeSpacesMethod · 0.95
fetchMethod · 0.95
consumeMethod · 0.95
handleSupSubscriptMethod · 0.95
subparseMethod · 0.95

Tested by

no test coverage detected