Involved Source Files
Package constraint implements parsing and evaluation of build constraint lines.
See https://golang.org/cmd/go/#hdr-Build_constraints for documentation about build constraints themselves.
This package parses both the original “// +build” syntax and the “//go:build” syntax that will be added in Go 1.17.
The parser is being included in Go 1.16 to allow tools that need to process Go 1.17 source code
to still be built against the Go 1.16 release.
See https://golang.org/design/draft-gobuild for details about the “//go:build” syntax.
Package-Level Type Names (total 7, in which 6 are exported)
An Expr is a build tag constraint expression.
The underlying concrete type is *AndExpr, *OrExpr, *NotExpr, or *TagExpr.
Eval reports whether the expression evaluates to true.
It calls ok(tag) as needed to find out whether a given build tag
is satisfied by the current build configuration.
String returns the string form of the expression,
using the boolean syntax used in //go:build lines.
The presence of an isExpr method explicitly marks the type as an Expr.
Only implementations in this package should be used as Exprs.
*AndExpr
*NotExpr
*OrExpr
*TagExpr
Expr : expvar.Var
Expr : fmt.Stringer
Expr : context.stringer
Expr : github.com/aws/smithy-go/middleware.stringer
Expr : runtime.stringer
func Parse(line string) (Expr, error)
func and(x, y Expr) Expr
func appendSplitAnd(list []Expr, x Expr) []Expr
func appendSplitOr(list []Expr, x Expr) []Expr
func not(x Expr) Expr
func or(x, y Expr) Expr
func parseExpr(text string) (x Expr, err error)
func parsePlusBuildExpr(text string) Expr
func pushNot(x Expr, not bool) Expr
func tag(tag string) Expr
func PlusBuildLines(x Expr) ([]string, error)
func and(x, y Expr) Expr
func andArg(x Expr) string
func appendSplitAnd(list []Expr, x Expr) []Expr
func appendSplitAnd(list []Expr, x Expr) []Expr
func appendSplitOr(list []Expr, x Expr) []Expr
func appendSplitOr(list []Expr, x Expr) []Expr
func not(x Expr) Expr
func or(x, y Expr) Expr
func orArg(x Expr) string
func pushNot(x Expr, not bool) Expr
func go/build.(*Context).eval(x Expr, allTags map[string]bool) bool
A NotExpr represents the expression !X (the negation of X).
XExpr(*NotExpr) Eval(ok func(tag string) bool) bool(*NotExpr) String() string(*NotExpr) isExpr()
*NotExpr : Expr
*NotExpr : expvar.Var
*NotExpr : fmt.Stringer
*NotExpr : context.stringer
*NotExpr : github.com/aws/smithy-go/middleware.stringer
*NotExpr : runtime.stringer
A SyntaxError reports a syntax error in a parsed build expression.
// description of error
// byte offset in input where error was detected
(*SyntaxError) Error() string
*SyntaxError : error
A TagExpr is an Expr for the single tag Tag.
// for example, “linux” or “cgo”
(*TagExpr) Eval(ok func(tag string) bool) bool(*TagExpr) String() string(*TagExpr) isExpr()
*TagExpr : Expr
*TagExpr : expvar.Var
*TagExpr : fmt.Stringer
*TagExpr : context.stringer
*TagExpr : github.com/aws/smithy-go/middleware.stringer
*TagExpr : runtime.stringer
An exprParser holds state for parsing a build expression.
// next read location in s
isTagbool
// position (start) of last token
// input string
// last token read
and parses a sequence of && expressions.
On entry, the next input token has not yet been lexed.
On exit, the next input token has been lexed and is in p.tok.
atom parses a tag or a parenthesized expression.
On entry, the next input token HAS been lexed.
On exit, the next input token has been lexed and is in p.tok.
lex finds and consumes the next token in the input stream.
On return, p.tok is set to the token text,
p.isTag reports whether the token was a tag,
and p.pos records the byte offset of the start of the token in the input stream.
If lex reaches the end of the input, p.tok is set to the empty string.
For any other syntax error, lex panics with a SyntaxError.
not parses a ! expression.
On entry, the next input token has not yet been lexed.
On exit, the next input token has been lexed and is in p.tok.
or parses a sequence of || expressions.
On entry, the next input token has not yet been lexed.
On exit, the next input token has been lexed and is in p.tok.
Package-Level Functions (total 18, in which 4 are exported)
IsGoBuild reports whether the line of text is a “//go:build” constraint.
It only checks the prefix of the text, not that the expression itself parses.
IsPlusBuild reports whether the line of text is a “// +build” constraint.
It only checks the prefix of the text, not that the expression itself parses.
Parse parses a single build constraint line of the form “//go:build ...” or “// +build ...”
and returns the corresponding boolean expression.
PlusBuildLines returns a sequence of “// +build” lines that evaluate to the build expression x.
If the expression is too complex to convert directly to “// +build” lines, PlusBuildLines returns an error.
appendSplitAnd appends x to list while splitting apart any top-level && expressions.
For example, appendSplitAnd({W}, X && Y && Z) = {W, X, Y, Z}.
appendSplitOr appends x to list while splitting apart any top-level || expressions.
For example, appendSplitOr({W}, X || Y || Z) = {W, X, Y, Z}.
isValidTag reports whether the word is a valid build tag.
Tags must be letters, digits, underscores or dots.
Unlike in Go identifiers, all digits are fine (e.g., "386").
parsePlusBuildExpr parses a legacy build tag expression (as used with “// +build”).
pushNot applies DeMorgan's law to push negations down the expression,
so that only tags are negated in the result.
(It applies the rewrites !(X && Y) => (!X || !Y) and !(X || Y) => (!X && !Y).)
splitGoBuild splits apart the leading //go:build prefix in line from the build expression itself.
It returns "", false if the input is not a //go:build line or if the input contains multiple lines.
splitPlusBuild splits apart the leading // +build prefix in line from the build expression itself.
It returns "", false if the input is not a // +build line or if the input contains multiple lines.