Skip to content

Commit

Permalink
cmd/watchflakes/internal/script: lex !~ as a single token
Browse files Browse the repository at this point in the history
Even though both ~ and !~ operators are documented as supported at
https://go.dev/wiki/Watchflakes, it seems only the former was ever
supported in practice. As it was uncovered when I tried the latter
in https://go.dev/issue/66337#issuecomment-2569872000, it fails to
parse with an "unexpected !" error.

The problem is that lex doesn't find the !~ token, it reports that
sequence as separate tokens ! and ~. Fix its logic.

Fixes golang/go#71119.

Change-Id: I08dd845a59e976a5eb2687924dce972680c90077
Reviewed-on: https://go-review.googlesource.com/c/build/+/640275
Auto-Submit: Dmitri Shuralyov <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
dmitshur authored and gopherbot committed Jan 16, 2025
1 parent 05d007b commit 65faf70
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
12 changes: 10 additions & 2 deletions cmd/watchflakes/internal/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,23 @@ Top:
p.i++
p.tok = p.s[p.pos:p.i]
return
case '<': // <-, <=
case '<': // < <- <=
p.pos = p.i
p.i++
if p.i < len(p.s) && (p.s[p.i] == '-' || p.s[p.i] == '=') {
p.i++
}
p.tok = p.s[p.pos:p.i]
return
case '!', '>': // ! != > >=
case '!': // ! !~ !=
p.pos = p.i
p.i++
if p.i < len(p.s) && (p.s[p.i] == '~' || p.s[p.i] == '=') {
p.i++
}
p.tok = p.s[p.pos:p.i]
return
case '>': // > >=
p.pos = p.i
p.i++
if p.i < len(p.s) && p.s[p.i] == '=' {
Expand Down
2 changes: 1 addition & 1 deletion cmd/watchflakes/internal/script/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var lexTests = [...]struct {
{"x ~", "a ~"},
{"x &", "a err: :1.3: invalid syntax at &"},
{"x &y", "a err: :1.3: invalid syntax at &"},
{"output !~ `content`", "a ! ~ `"},
{"output !~ `content`", "a !~ `"},
}

func TestLex(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions cmd/watchflakes/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,17 @@ var scriptTests = [...]struct {
{
`default <- pkg == "cmd/go" && test == "TestScript" &&
output !~ ` + "`" + `The process cannot access the file because it is being used by another process.` + "`" + ` # tracked in go.dev/issue/71112`,
nil,
"script:2.22: unexpected !",
[]*script.Rule{{
Action: "default",
Pattern: &script.AndExpr{
X: &script.AndExpr{
X: &script.CmpExpr{Field: "pkg", Op: "==", Literal: "cmd/go"},
Y: &script.CmpExpr{Field: "test", Op: "==", Literal: "TestScript"},
},
Y: &script.RegExpr{Field: "output", Not: true, Regexp: regexp.MustCompile(`(?m)The process cannot access the file because it is being used by another process.`)},
},
}},
"",
},
{
`post <- pkg ~ "^cmd/go"`,
Expand Down

0 comments on commit 65faf70

Please sign in to comment.