MCPcopy Create free account
hub / github.com/TruthHun/BookStack / Lexer

Class Lexer

static/katex/katex.mjs:12828–12894  ·  view source on GitHub ↗

Main Lexer class

Source from the content-addressed store, hash-verified

12826/** Main Lexer class */
12827
12828class Lexer {
12829 // category codes, only supports comment characters (14) for now
12830 constructor(input, settings) {
12831 this.input = void 0;
12832 this.settings = void 0;
12833 this.tokenRegex = void 0;
12834 this.catcodes = void 0;
12835 // Separate accents from characters
12836 this.input = input;
12837 this.settings = settings;
12838 this.tokenRegex = new RegExp(tokenRegexString, 'g');
12839 this.catcodes = {
12840 "%": 14 // comment character
12841
12842 };
12843 }
12844
12845 setCatcode(char, code) {
12846 this.catcodes[char] = code;
12847 }
12848 /**
12849 * This function lexes a single token.
12850 */
12851
12852
12853 lex() {
12854 const input = this.input;
12855 const pos = this.tokenRegex.lastIndex;
12856
12857 if (pos === input.length) {
12858 return new Token("EOF", new SourceLocation(this, pos, pos));
12859 }
12860
12861 const match = this.tokenRegex.exec(input);
12862
12863 if (match === null || match.index !== pos) {
12864 throw new ParseError(`Unexpected character: '${input[pos]}'`, new Token(input[pos], new SourceLocation(this, pos, pos + 1)));
12865 }
12866
12867 let text = match[2] || " ";
12868
12869 if (this.catcodes[text] === 14) {
12870 // comment character
12871 const nlIndex = input.indexOf('\n', this.tokenRegex.lastIndex);
12872
12873 if (nlIndex === -1) {
12874 this.tokenRegex.lastIndex = input.length; // EOF
12875
12876 this.settings.reportNonstrict("commentAtEnd", "% comment has no terminating newline; LaTeX would " + "fail because of commenting the end of math mode (e.g. $)");
12877 } else {
12878 this.tokenRegex.lastIndex = nlIndex + 1;
12879 }
12880
12881 return this.lex();
12882 } // Trim any trailing whitespace from control word match
12883
12884
12885 const controlMatch = text.match(controlWordWhitespaceRegex);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected