Skip to content

Commit 29ca98a

Browse files
committed
fix: allow multiple calls to subst with a regexp
This change disables the ``checkRegex`` helper when a regexp is used by the builtin ``subst`` as there will be no symbols created on the symbol table, and the compiler already compiles and appends the regexp to the regexp table for use by the VM. fixes: #693, #810
1 parent f29bb2b commit 29ca98a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

internal/runtime/compiler/checker/checker.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type checker struct {
3535
tooDeep bool
3636
maxRecursionDepth int
3737
maxRegexLength int
38+
skipRegexCheck bool
3839
}
3940

4041
// Check performs a semantic check of the astNode, and returns a potentially
@@ -83,6 +84,12 @@ func (c *checker) VisitBefore(node ast.Node) (ast.Visitor, ast.Node) {
8384
glog.V(2).Infof("Created new scope %v in condstmt", n.Scope)
8485
return c, n
8586

87+
case *ast.BuiltinExpr:
88+
if n.Name == "subst" {
89+
c.skipRegexCheck = true
90+
}
91+
return c, n
92+
8693
case *ast.CaprefTerm:
8794
if n.Symbol == nil {
8895
sym := c.scope.Lookup(n.Name, symbol.CaprefSymbol)
@@ -821,6 +828,10 @@ func (c *checker) VisitAfter(node ast.Node) ast.Node {
821828
return n
822829
}
823830

831+
case "subst":
832+
c.skipRegexCheck = false
833+
return n
834+
824835
case "tolower":
825836
if !types.Equals(gotType.Args[0], types.String) {
826837
c.errors.Add(n.Args.(*ast.ExprList).Children[0].Pos(), fmt.Sprintf("Expecting a String for argument 1 of tolower(), not %v.", gotType.Args[0]))
@@ -838,7 +849,9 @@ func (c *checker) VisitAfter(node ast.Node) ast.Node {
838849
return n
839850
}
840851
n.Pattern = pe.pattern.String()
841-
c.checkRegex(n.Pattern, n)
852+
if !c.skipRegexCheck {
853+
c.checkRegex(n.Pattern, n)
854+
}
842855
return n
843856

844857
case *ast.PatternFragment:

internal/runtime/compiler/checker/checker_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ foo = subst(",", "", $1)
576576
}`},
577577
{"regexp subst", `
578578
subst(/\d+/, "d", "1234")
579+
`},
580+
{"regexp subst twice", `
581+
text value
582+
value = subst(/[a-zA-Z]+/, "a", "1234abcd")
583+
value = subst(/\d+/, "d", value)
579584
`},
580585
}
581586

0 commit comments

Comments
 (0)