Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions query/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,43 @@ func (o *orOperator) String() string {
return "orOp"
}

// caseScopeQ is a parse-time wrapper used to prevent case directives from an
// outer expression list from overriding an explicitly scoped inner `case:`.
type caseScopeQ struct {
Child Q
}

func (c *caseScopeQ) String() string {
return c.Child.String()
}

func stripCaseScopesList(qs []Q) []Q {
stripped := make([]Q, len(qs))
for i, q := range qs {
stripped[i] = stripCaseScopes(q)
}
return stripped
}

func stripCaseScopes(q Q) Q {
switch s := q.(type) {
case *And:
return &And{Children: stripCaseScopesList(s.Children)}
case *Or:
return &Or{Children: stripCaseScopesList(s.Children)}
case *Not:
return &Not{Child: stripCaseScopes(s.Child)}
case *Type:
return &Type{Type: s.Type, Child: stripCaseScopes(s.Child)}
case *Boost:
return &Boost{Boost: s.Boost, Child: stripCaseScopes(s.Child)}
case *caseScopeQ:
return stripCaseScopes(s.Child)
default:
return q
}
}

func isSpace(c byte) bool {
return c == ' ' || c == '\t'
}
Expand All @@ -90,6 +127,8 @@ func Parse(qStr string) (Q, error) {
return nil, err
}

q = stripCaseScopes(q)

return Simplify(q), nil
}

Expand Down Expand Up @@ -354,12 +393,14 @@ func parseExprList(in []byte) ([]Q, int, error) {
}

setCase := "auto"
hasCaseScope := false
newQS := qs[:0]
typeT := uint8(100)
for _, q := range qs {
switch s := q.(type) {
case *caseQ:
setCase = s.Flavor
hasCaseScope = true
case *Type:
if s.Type < typeT {
typeT = s.Type
Expand All @@ -377,6 +418,19 @@ func parseExprList(in []byte) ([]Q, int, error) {
if typeT != 100 {
qs = []Q{&Type{Type: typeT, Child: NewAnd(qs...)}}
}

if hasCaseScope {
scoped := make([]Q, 0, len(qs))
for _, q := range qs {
if _, isOrOperator := q.(*orOperator); isOrOperator {
scoped = append(scoped, q)
continue
}
scoped = append(scoped, &caseScopeQ{Child: q})
}
qs = scoped
}

return qs, len(in) - len(b), nil
}

Expand Down
20 changes: 20 additions & 0 deletions query/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ func TestParseQuery(t *testing.T) {
&Substring{Pattern: "abc", CaseSensitive: true},
&Not{Child: &Substring{Pattern: "def", FileName: true, CaseSensitive: true}},
)},
{"(foo case:yes) bar", NewAnd(
&Substring{Pattern: "foo", CaseSensitive: true},
&Substring{Pattern: "bar"},
)},
{"(case:yes foo) bar", NewAnd(
&Substring{Pattern: "foo", CaseSensitive: true},
&Substring{Pattern: "bar"},
)},
{"(case:yes foo (bar))", NewAnd(
&Substring{Pattern: "foo", CaseSensitive: true},
&Substring{Pattern: "bar", CaseSensitive: true},
)},
{"case:auto (foo case:yes) bar", NewAnd(
&Substring{Pattern: "foo", CaseSensitive: true},
&Substring{Pattern: "bar"},
)},
{"case:yes (foo case:no) bar", NewAnd(
&Substring{Pattern: "foo"},
&Substring{Pattern: "bar", CaseSensitive: true},
)},

// type
{"type:repo abc", &Type{Type: TypeRepo, Child: &Substring{Pattern: "abc"}}},
Expand Down
Loading