Repository navigation
Description
The generated source from RegexGenerator today heavily uses goto statements which in many cases (like backtracking for example) is very useful but in practice the statement is rarely used due to its tendency to make things harder to read/follow due to its code jumping nature. One of the purposes of the source generator is to make code readable and to look as close as possible to code that any average developer would write so that it is also easier to debug, and so we should probably consider avoid it in a few places if we can.
For example, consider a very simple pattern a{1,2} which will try to match an a one or two times greedily. The generated Go method currently looks something like:
protected override void Go()
{
// .. some variable initialization
int iteration = 0;
while (iteration < 2 && (uint)iteration < (uint)slice.Length && slice[iteration] == 'a')
{
iteration++;
}
if (iteration == 0)
{
goto NoMatch;
}
slice = slice.Slice(iteration);
pos += iteration;
// The input matched.
base.runtextpos = pos;
base.Capture(0, original_pos, pos);
return;
// The input didn't match.
NoMatch:;
}We could instead produce code that would feel more natural for a C# developer like a function call instead, so that it looks more like:
protected override void Go()
{
// .. some variable initialization
int iteration = 0;
while (iteration < 2 && (uint)iteration < (uint)slice.Length && slice[iteration] == 'a')
{
iteration++;
}
if (iteration == 0)
{
return NoMatch();
}
slice = slice.Slice(iteration);
pos += iteration;
// The input matched.
base.runtextpos = pos;
base.Capture(0, original_pos, pos);
return;
void NoMatch()
{
//The input didn't match.
}
}We should probably consider this not only for NoMatch case, but instead also in our other backtracking options, but if we do decide to remove some uses of goto it could very well be done in stages.
cc: @stephentoub
Activity
stephentoub commentedon Dec 10, 2021
For the top-level "goto NoMatch", yeah, we can just change that to "return;", if there's nothing to uncapture (no goto or local function call needed). If there is something to uncapture, we could do what you suggest and put the uncapturing inside of a local NoMatch function, though I'm not sure how much that one goto removal would actually help with readability... you need to visually jump to NoMatch to see what it's doing, at which point it's not clear to me putting it into a function actually helps understandability (the uncapturing is one function call itself).
Beyond the single top-level NoMatch label, I expect most of the other goto usage will be really hard to remove, but please do share suggestions.
stephentoub commentedon Dec 11, 2021
Thinking about your suggestion more, for the top-level NoMatch, it probably makes sense to just emit "return;" or "UncaptureUntil(0); return;" (if there are captures) rather than "goto NoMatch;" as we do today.
joperezr commentedon Dec 11, 2021
Sounds good I'll take care of that one, and think if any other make sense too.
stephentoub commentedon Dec 13, 2021
You might want to hold off on this one. If we end up implementing Scan by combining the code from FindFirstChar / Go into a single method, essentially FFC's loop wrapped around the contents of Go, we'll likely have an easier time doing so when that body jumps to the end of the FFC loop body, as would happen with the goto code today.