Motivation
It is useful to be able to pass simple parameters, such as string and number literals, into the info resolution path. For example, {number add(1)} comes handy in many cases. This also allows to implement new info types like {stringList join(", ")}.
Goals
- Provide a way such that infos can take in extra, simple parameters
- Perform minimal modification on the syntax to support parsing parameters, named or unnamed
Non-goals
- This issue does not propose dynamic infos. That is, we do not intend to allow arbitrary info names. Info paths should still be statically analyzable so that
Graph::pathFind continues to work. This also means return type of an info is fixed, invariant of the input parameters.
- This issue does not intend to implement complex ASTs that could confuse readers. There are no plans to add recursive infos yet.
- While this proposal allows lists to be implemented, there are no plans to implement generic infos.
Design details
Parsing
Currently, the template syntax inside {} is defined as
template := "{" path ("|" path)* "}"
path := qualified_name (" " qualified_name)*
qualified_name := (TOKEN ".")* TOKEN
It is proposed that qualified_name be modified to
qualified_name := (TOKEN ".")* TOKEN parameter_list?
parameter_list := "(" (named_params | unnamed_params) ")"
named_params := TOKEN ":" param_value ("," TOKEN ":" param_value)*
unnamed_params := param_value ("," param_value)*
param_value := JSON_STRING_LITERAL | JSON_NUMBER_LITERAL
In particular, the parser must properly support ) and | inside string literals.
API
Add a new method provideParameterizedInfo, which takes a closure Closure(Info, Arguments): Info. Arguments is one of NamedArguments providing get(string $key) or UnnamedArguments providing get(int $index).
Possible enhancements
Use parameterized infos to implement the following new infos:
NumberInfo add(number) -> NumberInfo
NumberInfo sub(number) -> NumberInfo
NumberInfo mul(number) -> NumberInfo
NumberInfo div(number) -> NumberInfo
NumberInfo reciprocal -> NumberInfo (such that {a reciprocal mul(b)} gives b / a)
NumberInfo mod(number) -> NumberInfo
NumberInfo imod(number) -> NumberInfo (mod with arguments flipped)
StringInfo wrapIfNonEmpty(prefix?: string, suffix?: string) -> StringInfo
Motivation
It is useful to be able to pass simple parameters, such as string and number literals, into the info resolution path. For example,
{number add(1)}comes handy in many cases. This also allows to implement new info types like{stringList join(", ")}.Goals
Non-goals
Graph::pathFindcontinues to work. This also means return type of an info is fixed, invariant of the input parameters.Design details
Parsing
Currently, the template syntax inside
{}is defined asIt is proposed that
qualified_namebe modified toIn particular, the parser must properly support
)and|inside string literals.API
Add a new method
provideParameterizedInfo, which takes a closureClosure(Info, Arguments): Info.Argumentsis one ofNamedArgumentsprovidingget(string $key)orUnnamedArgumentsprovidingget(int $index).Possible enhancements
Use parameterized infos to implement the following new infos:
NumberInfo add(number) -> NumberInfoNumberInfo sub(number) -> NumberInfoNumberInfo mul(number) -> NumberInfoNumberInfo div(number) -> NumberInfoNumberInfo reciprocal -> NumberInfo(such that{a reciprocal mul(b)}givesb / a)NumberInfo mod(number) -> NumberInfoNumberInfo imod(number) -> NumberInfo(modwith arguments flipped)StringInfo wrapIfNonEmpty(prefix?: string, suffix?: string) -> StringInfo