Draft: Parameter parsing overhaul

This merge request modifies how parameters are parsed. The explanation is separated in 2 sections: expansion and trimming.

I tried to make it consistent with the rest of the code. Each commit does not correspond to a precise part, so I tried to explain which changes are related to each part, to understand what has been changed globally. I wrote on a Google spreadsheet all changes made to each specific function, as a lot of parts of the code have been moved and merged, and it is not really easy to understand what is happening seeing the github comparator.

I can do multiple merge requests for these changes if you prefer.

English is not my native language, so tell me if something is unclear.

Expansion

a. Named parameters

Description

The key and the value of named parameters are separated after expanding the preprocessor node, using explode('=', …, 2). It means that {{…|a{{=}}b}} is the same as {{…|a=b}} and not as {{…|1=a=b}}. This is inconsistent with the way templates work. However, it could also be an "additional functionality", as all list functions using named parameters do not use unnamed parameters. However, there is also #ueswitch: {{#ueswitch: a=a | a{{=}}a = match | default }} currently returns default instead of match.

Proposed changes

For each parameter, ParserPower::arrangeParams() and ParserPowerSimple::ueswitchRender() get the name and value nodes using PPNode::splitArg(), then expands and trims the name (and not the value, see the c. section).

b. Templates and parameters within parameters

Description

Some parameters (e.g. tokens) are expanded using the PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES flags, that prevent parameter and template parsing. It means that |token=a{{!}}b sets the token to a{{!}}b instead of a|b. These flags make the parameters they are used on work like Loops and Arrays pattern parameters. However, this extension provides escape sequences, allowing to delay template and parameter parsing. Without the flags, the same behavior can be obtained using |token=<esc>a{{!}}b</esc> or |token=a\{\{!\}\}b.

Proposed changes

These flags are removed. The parameters using them that are not unescaped are now unescaped after being expanded and trimmed.

c. Unused parameters

Description

The majority of list parser functions expand and trim all their parameters at the start: used and unused parameters. It takes extra parsing time, especially when a unused default parameter contains a lot of wikicode, or a slow template (e.g. with Cargo queries). Moreover, some of the unused expanded parameters can affect the frame, like setting a DPL variable (or from the Variables extension).

Proposed changes

Parameters are expanded when their value is used. They are however still expanded in the main function for consistency. Some functions have been partially moved to make it consistent and make it work properly.

d. Results

Description

The result of parser functions is always expanded, as […, 'noparse' => false] is used everywhere. This is unneeded in the majority of the cases, as a lot of values are already expanded. It is also used where an empty string is returned.

Proposed changes

The functions do not ask for the return value to be parsed (with the noparse field) where it is unneeded.

Trimming

a. Parameters

Description

The #ueif and #ueifeq parser functions do not trim their result parameters after expanding them. This is inconsistent with #if and #ifeq from ParserFunctions. If trailing spaces are needed, this extension provides escaping sequences: {{#ueif: 1 | true }} could be replaced with {{#ueif: 1 | \_true\_ }} if spacing matter.

Proposed changes

The #ueif and #ueifeq parser functions trim all their parameters.

b. List parameters

Description

The token parameters are separated using explode() and some of them are trimmed using array_map('trim'). It means that | token = @a, @b is parsed as [ '@a', ' @b' ] and not as [ '@a', '@b' ]. The trimming was partially fixed in a previous merge request, but I missed some parameters that have a different name (token1 and token2).

Proposed changes

All token parameters are set using ParserPowerLists::arrayTrimUnescape() after being exploded. This function trims and unescapes all elements, but it also removes the empty ones. However, it is possible that parts of the list element are not used in the pattern, in this case @first, , @third should be parsed as [ '@first', '', '@third ] and not as [ '@first', '@third ]. To make it work properly ParserPowerLists::arrayTrimUnescape() is renamed ParserPowerLists::arrayTrimFilterUnescape() and ParserPowerLists::arrayTrimUnescape() does the same thing, except it does not remove empty elements.

Other changes

  • A lot of if statements are replaced with guards for code flattening and lisibility.
  • Some double quotes and equality operators are replaced with single quotes and identity operators for consistency.
  • Some for loops have been replaced with foreach loops for performance and lisibility.
  • Some parameters are converted from int to string, trimmed, or unescaped two times, before calling the function and giving it the parameter, and inside the called function. The ones I found out are fixed (I may have missed some).
Edited by Adrien LESÉNÉCHAL

Merge request reports

Loading