* Parses the arguments of a function or environment
(func, // Should look like "\name" or "\begin{name}".
funcData)
| 15306 | |
| 15307 | |
| 15308 | parseArguments(func, // Should look like "\name" or "\begin{name}". |
| 15309 | funcData) { |
| 15310 | const totalArgs = funcData.numArgs + funcData.numOptionalArgs; |
| 15311 | |
| 15312 | if (totalArgs === 0) { |
| 15313 | return { |
| 15314 | args: [], |
| 15315 | optArgs: [] |
| 15316 | }; |
| 15317 | } |
| 15318 | |
| 15319 | const baseGreediness = funcData.greediness; |
| 15320 | const args = []; |
| 15321 | const optArgs = []; |
| 15322 | |
| 15323 | for (let i = 0; i < totalArgs; i++) { |
| 15324 | const argType = funcData.argTypes && funcData.argTypes[i]; |
| 15325 | const isOptional = i < funcData.numOptionalArgs; // Ignore spaces between arguments. As the TeXbook says: |
| 15326 | // "After you have said ‘\def\row#1#2{...}’, you are allowed to |
| 15327 | // put spaces between the arguments (e.g., ‘\row x n’), because |
| 15328 | // TeX doesn’t use single spaces as undelimited arguments." |
| 15329 | |
| 15330 | if (i > 0 && !isOptional) { |
| 15331 | this.consumeSpaces(); |
| 15332 | } // Also consume leading spaces in math mode, as parseSymbol |
| 15333 | // won't know what to do with them. This can only happen with |
| 15334 | // macros, e.g. \frac\foo\foo where \foo expands to a space symbol. |
| 15335 | // In LaTeX, the \foo's get treated as (blank) arguments). |
| 15336 | // In KaTeX, for now, both spaces will get consumed. |
| 15337 | // TODO(edemaine) |
| 15338 | |
| 15339 | |
| 15340 | if (i === 0 && !isOptional && this.mode === "math") { |
| 15341 | this.consumeSpaces(); |
| 15342 | } |
| 15343 | |
| 15344 | const nextToken = this.nextToken; |
| 15345 | const arg = this.parseGroupOfType("argument to '" + func + "'", argType, isOptional, baseGreediness); |
| 15346 | |
| 15347 | if (!arg) { |
| 15348 | if (isOptional) { |
| 15349 | optArgs.push(null); |
| 15350 | continue; |
| 15351 | } |
| 15352 | |
| 15353 | throw new ParseError("Expected group after '" + func + "'", nextToken); |
| 15354 | } |
| 15355 | |
| 15356 | (isOptional ? optArgs : args).push(arg); |
| 15357 | } |
| 15358 | |
| 15359 | return { |
| 15360 | args, |
| 15361 | optArgs |
| 15362 | }; |
| 15363 | } |
| 15364 | /** |
| 15365 | * Parses a group when the mode is changing. |
no test coverage detected