* This file contains the parser used to parse out a TeX expression from the * input. Since TeX isn't context-free, standard parsers don't work particularly * well. * * The strategy of this parser is as such: * * The main functions (the `.parse...` ones) take a position in the current * parse
| 14894 | * The functions return ParseNodes. |
| 14895 | */ |
| 14896 | class Parser { |
| 14897 | constructor(input, settings) { |
| 14898 | this.mode = void 0; |
| 14899 | this.gullet = void 0; |
| 14900 | this.settings = void 0; |
| 14901 | this.leftrightDepth = void 0; |
| 14902 | this.nextToken = void 0; |
| 14903 | // Start in math mode |
| 14904 | this.mode = "math"; // Create a new macro expander (gullet) and (indirectly via that) also a |
| 14905 | // new lexer (mouth) for this parser (stomach, in the language of TeX) |
| 14906 | |
| 14907 | this.gullet = new MacroExpander(input, settings, this.mode); // Store the settings for use in parsing |
| 14908 | |
| 14909 | this.settings = settings; // Count leftright depth (for \middle errors) |
| 14910 | |
| 14911 | this.leftrightDepth = 0; |
| 14912 | } |
| 14913 | /** |
| 14914 | * Checks a result to make sure it has the right type, and throws an |
| 14915 | * appropriate error otherwise. |
| 14916 | */ |
| 14917 | |
| 14918 | |
| 14919 | expect(text, consume) { |
| 14920 | if (consume === void 0) { |
| 14921 | consume = true; |
| 14922 | } |
| 14923 | |
| 14924 | if (this.nextToken.text !== text) { |
| 14925 | throw new ParseError("Expected '" + text + "', got '" + this.nextToken.text + "'", this.nextToken); |
| 14926 | } |
| 14927 | |
| 14928 | if (consume) { |
| 14929 | this.consume(); |
| 14930 | } |
| 14931 | } |
| 14932 | /** |
| 14933 | * Considers the current look ahead token as consumed, |
| 14934 | * and fetches the one after that as the new look ahead. |
| 14935 | */ |
| 14936 | |
| 14937 | |
| 14938 | consume() { |
| 14939 | this.nextToken = this.gullet.expandNextToken(); |
| 14940 | } |
| 14941 | /** |
| 14942 | * Switches between "text" and "math" modes. |
| 14943 | */ |
| 14944 | |
| 14945 | |
| 14946 | switchMode(newMode) { |
| 14947 | this.mode = newMode; |
| 14948 | this.gullet.switchMode(newMode); |
| 14949 | } |
| 14950 | /** |
| 14951 | * Main parsing function, which parses an entire input. |
| 14952 | */ |
| 14953 |
nothing calls this directly
no outgoing calls
no test coverage detected