Fix libraryRegexp to allow attributes
Bug: 86590
Change-Id: I7623b363125384f1bb3f2bd1eb2e30a2c8b261f1
Reviewed-on: https://fuchsia-review.googlesource.com/c/fidlbolt/+/593801
Reviewed-by: Yifei Teng <yifeit@google.com>
diff --git a/backend/analyze.go b/backend/analyze.go
index fd2fb6b..eb130a2 100644
--- a/backend/analyze.go
+++ b/backend/analyze.go
@@ -392,25 +392,27 @@
return info
}
+// TODO(fxbug.dev/86590): Stop relying on regexes.
var (
// https://fuchsia.dev/fuchsia-src/development/languages/fidl/reference/language.md#identifiers
fidlIdentifierPattern = `[a-zA-Z](?:[a-zA-Z0-9_]*[a-zA-Z0-9])?`
- // Although "library" can be used anywhere (e.g. as a type name), this regex
- // is robust because the the library declaration must appear at the top of
- // the file (only comments and whitespace can precede it).
+ // Works most of the time, but breaks if there is an attribute with a string
+ // argument that contains ")".
libraryRegexp = regexp.MustCompile(`` +
- `^(?:\s*//[^\n]*\n)*\s*` +
+ `^(?:` +
+ /* space */ `\s` +
+ /* comment */ `|//[^\n]*\n|` +
+ /* attribute */ `|@\s*` + fidlIdentifierPattern + `\s*` +
+ /* arguments */ `(?:\([^)]*\))?` +
+ `)*` +
`library\s+(` +
fidlIdentifierPattern +
`(?:\.` + fidlIdentifierPattern + `)*` +
`)\s*;`)
- // Although "using" can be used anywhere (e.g. as a type name), this regex
- // is robust because it only matches imports of platform libraries (ones
- // that start with fidl, fuchsia, or test) and the special library zx. The
- // only problem is it can match commented imports, so we have to check for
- // this after matching.
+ // Works most of the time, but breaks if there is a string literal whose
+ // value looks like an import.
platformImportRegexp = regexp.MustCompile(`` +
`\busing\s+(` +
`zx|` +