(context, global)
| 13175 | // Also the \gdef and \global\def equivalents |
| 13176 | |
| 13177 | const def = (context, global) => { |
| 13178 | let arg = context.consumeArgs(1)[0]; |
| 13179 | |
| 13180 | if (arg.length !== 1) { |
| 13181 | throw new ParseError("\\gdef's first argument must be a macro name"); |
| 13182 | } |
| 13183 | |
| 13184 | const name = arg[0].text; // Count argument specifiers, and check they are in the order #1 #2 ... |
| 13185 | |
| 13186 | let numArgs = 0; |
| 13187 | arg = context.consumeArgs(1)[0]; |
| 13188 | |
| 13189 | while (arg.length === 1 && arg[0].text === "#") { |
| 13190 | arg = context.consumeArgs(1)[0]; |
| 13191 | |
| 13192 | if (arg.length !== 1) { |
| 13193 | throw new ParseError(`Invalid argument number length "${arg.length}"`); |
| 13194 | } |
| 13195 | |
| 13196 | if (!/^[1-9]$/.test(arg[0].text)) { |
| 13197 | throw new ParseError(`Invalid argument number "${arg[0].text}"`); |
| 13198 | } |
| 13199 | |
| 13200 | numArgs++; |
| 13201 | |
| 13202 | if (parseInt(arg[0].text) !== numArgs) { |
| 13203 | throw new ParseError(`Argument number "${arg[0].text}" out of order`); |
| 13204 | } |
| 13205 | |
| 13206 | arg = context.consumeArgs(1)[0]; |
| 13207 | } // Final arg is the expansion of the macro |
| 13208 | |
| 13209 | |
| 13210 | context.macros.set(name, { |
| 13211 | tokens: arg, |
| 13212 | numArgs |
| 13213 | }, global); |
| 13214 | return ''; |
| 13215 | }; |
| 13216 | |
| 13217 | defineMacro("\\gdef", context => def(context, true)); |
| 13218 | defineMacro("\\def", context => def(context, false)); |
no test coverage detected