compute: introduce template scanner
Created by: rvantonder
Stacked on https://github.com/sourcegraph/sourcegraph/pull/27883.
This introduces a small template scanner to recognize certain variables in an output template. For example, I'm going for a solution where you could express:
(\w+) -> $repo $1
and it's possible to substitute $repo
for a value of a repo for a match and $1
still substitutes for the capture group $1
.
This might look like a big PR, but I assure you it's just same old scanner code used from other places, but tuned to recognize $var
as variables.
What about Go's templating package?
Yes, I will actually piggyback on top of Go's templating package to "execute" (substitute) variables for values. But I don't want to borrow the Go templating syntax like {{...}}
, because:
(1) we already have a recognizable substitution syntax with $1
using regex. The Go template package allows to set custom Delims
but it wants a trailing delimiter, and that's not strictly necessary and breaks the consistency of $blah
syntax. Also you don't get to control the escaping mechanisms.
(2) I don't want this our v0 template language to import the huge syntax surface area of Go's templating language. (1) Because it adds a ton of complexity and things I don't want to think about. We grow our own language based on need. (2) Some of the Go templating execution for conditional statements, etc., could allow people to construct fairly arbitrary and potentially expensive operations, and it's just too much right now.