Skip to content

Variables: Rework how expansion strings work

Description

Previously we repeatedly recursively used regular expressions to expand our variables. This change replaces that with an explicitly separate parse vs. expand phase, and caches the parsed variables since a significant proportion of variables are identical from element to element.

The following explanation of the expansion string data structure is copied from the code to help reviewers...

# Throughout this code you will see variables named things like `expstr`.
# These hold data structures called "expansion strings" and are the parsed
# form of the strings which are the input to this subsystem.  Strings
# such as "Hello %{name}, how are you?" are parsed into the form:
# (3, ["Hello ", "name", ", how are you?"])
# i.e. a tuple of an integer and a list, where the integer is the cached
# length of the list, and the list consists of one or more strings.
# Strings in even indices of the list (0, 2, 4, etc) are constants which
# are copied into the output of the expansion algorithm.  Strings in the
# odd indices (1, 3, 5, etc) are the names of further expansions to make.
# In the example above, first "Hello " is copied, then "name" is expanded
# and so must be another named expansion string passed in to the constructor
# of the Variables class, and whatever is yielded from the expansion of "name"
# is added to the concatenation for the result.  Finally ", how are you?" is
# copied in and the whole lot concatenated for return.

Edited by Daniel Silverstone

Merge request reports

Loading