-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package search | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/alecthomas/participle/v2" | ||
"github.com/alecthomas/participle/v2/lexer" | ||
) | ||
|
||
type Query struct { | ||
Terms []*Term `parser:"@@*" json:"terms"` | ||
} | ||
|
||
type Term struct { | ||
String *string `parser:"@String" json:"string,omitempty"` | ||
Qualifier *Qualifier `parser:"| @@" json:"qualifier,omitempty"` | ||
Word *string `parser:"| @Word" json:"word,omitempty"` | ||
Pos lexer.Position `parser:"" json:"start"` | ||
EndPos lexer.Position `parser:"" json:"end"` | ||
} | ||
|
||
type Qualifier struct { | ||
Key string `parser:"@Word ':'"` | ||
Value string `parser:"@Word (@':' @Word)*"` | ||
} | ||
|
||
var lex *lexer.StatefulDefinition | ||
var par *participle.Parser[Query] | ||
|
||
func init() { | ||
lex = lexer.MustSimple([]lexer.SimpleRule{ | ||
{Name: "Whitespace", Pattern: `[ \t]+`}, | ||
{Name: "Word", Pattern: `\w+`}, | ||
{Name: "String", Pattern: `"(\\"|[^"])*"`}, | ||
{Name: "Colon", Pattern: `:`}, | ||
}) | ||
|
||
par = participle.MustBuild[Query]( | ||
participle.Lexer(lex), | ||
participle.Elide("Whitespace"), | ||
participle.Unquote("String"), | ||
) | ||
} | ||
|
||
func Parse(str string) (*Query, error) { | ||
return par.ParseString("", str) | ||
} | ||
|
||
func (q *Query) QualifierInt(key string) (int, error) { | ||
if q == nil { | ||
return 0, fmt.Errorf("nil query") | ||
} | ||
|
||
if len(q.Terms) == 0 { | ||
return 0, fmt.Errorf("empty query") | ||
} | ||
|
||
if len(q.Terms) > 1 { | ||
return 0, fmt.Errorf("too many terms") | ||
} | ||
|
||
if q.Terms[0].Qualifier == nil { | ||
return 0, fmt.Errorf("no qualifier") | ||
} | ||
|
||
if q.Terms[0].Qualifier.Key != key { | ||
return 0, fmt.Errorf(`qualifier not %s`, key) | ||
} | ||
|
||
return strconv.Atoi(q.Terms[0].Qualifier.Value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters