* Expand the next token only once if possible. * * If the token is expanded, the resulting tokens will be pushed onto * the stack in reverse order and will be returned as an array, * also in reverse order. * * If not, the next token will be returned without removing it * from th
()
| 13985 | |
| 13986 | |
| 13987 | expandOnce() { |
| 13988 | const topToken = this.popToken(); |
| 13989 | const name = topToken.text; |
| 13990 | |
| 13991 | const expansion = this._getExpansion(name); |
| 13992 | |
| 13993 | if (expansion == null) { |
| 13994 | // mainly checking for undefined here |
| 13995 | // Fully expanded |
| 13996 | this.pushToken(topToken); |
| 13997 | return topToken; |
| 13998 | } |
| 13999 | |
| 14000 | this.expansionCount++; |
| 14001 | |
| 14002 | if (this.expansionCount > this.settings.maxExpand) { |
| 14003 | throw new ParseError("Too many expansions: infinite loop or " + "need to increase maxExpand setting"); |
| 14004 | } |
| 14005 | |
| 14006 | let tokens = expansion.tokens; |
| 14007 | |
| 14008 | if (expansion.numArgs) { |
| 14009 | const args = this.consumeArgs(expansion.numArgs); // paste arguments in place of the placeholders |
| 14010 | |
| 14011 | tokens = tokens.slice(); // make a shallow copy |
| 14012 | |
| 14013 | for (let i = tokens.length - 1; i >= 0; --i) { |
| 14014 | let tok = tokens[i]; |
| 14015 | |
| 14016 | if (tok.text === "#") { |
| 14017 | if (i === 0) { |
| 14018 | throw new ParseError("Incomplete placeholder at end of macro body", tok); |
| 14019 | } |
| 14020 | |
| 14021 | tok = tokens[--i]; // next token on stack |
| 14022 | |
| 14023 | if (tok.text === "#") { |
| 14024 | // ## → # |
| 14025 | tokens.splice(i + 1, 1); // drop first # |
| 14026 | } else if (/^[1-9]$/.test(tok.text)) { |
| 14027 | // replace the placeholder with the indicated argument |
| 14028 | tokens.splice(i, 2, ...args[+tok.text - 1]); |
| 14029 | } else { |
| 14030 | throw new ParseError("Not a valid argument number", tok); |
| 14031 | } |
| 14032 | } |
| 14033 | } |
| 14034 | } // Concatenate expansion onto top of stack. |
| 14035 | |
| 14036 | |
| 14037 | this.pushTokens(tokens); |
| 14038 | return tokens; |
| 14039 | } |
| 14040 | /** |
| 14041 | * Expand the next token only once (if possible), and return the resulting |
| 14042 | * top token on the stack (without removing anything from the stack). |
no test coverage detected