<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/parse.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2022-08-27 18:11:10 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2022-08-27 18:11:10 +0100
commitcfbe645715114234510bda068d2f30ffbe208eae (patch)
treec8be19865a676c7bd53bd121c3f5a2be43206a7e /main/parse.go
parent094c9a8921fb5f54a34d8cdcb924b5dbacd336d8 (diff)
downloadstred-go-cfbe645715114234510bda068d2f30ffbe208eae.tar
Adds new filters
- Begin terminals - End terminals - All terminals - Negate - AND
Diffstat (limited to 'main/parse.go')
-rw-r--r--main/parse.go50
1 files changed, 34 insertions, 16 deletions
diff --git a/main/parse.go b/main/parse.go
index 492b58f..0767c0d 100644
--- a/main/parse.go
+++ b/main/parse.go
@@ -90,22 +90,36 @@ func (p *parser) parsePathPatternFilter(minPower int) PathFilterAST {
return lhs
}
-// TODO: should only return a single filter
-func (p *parser) parseFilter() []Filter {
- var filters []Filter
+func (p *parser) parseFilter(minPower int) Filter {
+ var lhs Filter
token := p.next()
switch token.typ {
case TokenHash, TokenAt, TokenDot:
p.rewind(token)
filterAst := p.parsePathPatternFilter(0)
- filters = append(filters, compilePathFilterAST(filterAst))
- token = p.next()
+ lhs = compilePathFilterAST(filterAst)
+ case TokenHat:
+ lhs = BeginTerminalFilter{}
+ case TokenDollar:
+ lhs = EndTerminalFilter{}
+ case TokenHatDollar:
+ lhs = TerminalFilter{}
+ case TokenTilde:
+ lhs = RootFilter{}
+ default:
+ panic("Expected filter")
}
- if len(filters) == 0 {
- panic("Missing filter")
+ loop: for {
+ token = p.next()
+ switch {
+ case token.typ == TokenAnd && 2 >= minPower:
+ lhs = AndFilter {lhs, p.parseFilter(3)}
+ default:
+ p.rewind(token)
+ break loop
+ }
}
- p.rewind(token)
- return filters
+ return lhs
}
func (p *parser) parseBasicCommand(commandChar rune) Command {
@@ -122,15 +136,19 @@ func (p *parser) parseBasicCommand(commandChar rune) Command {
func (p *parser) parseCommand() Command {
token := p.next()
switch token.typ {
- case TokenHash, TokenAt, TokenDot:
+ case TokenHash, TokenAt, TokenDot, TokenLParen, TokenHat, TokenDollar, TokenHatDollar, TokenTilde:
p.rewind(token)
- filters := p.parseFilter()
+ filter := p.parseFilter(0)
+ notToken := p.next()
+ if notToken.typ == TokenExclamation {
+ filter = NotFilter {filter}
+ } else {
+ p.rewind(notToken)
+ }
command := p.parseCommand()
- for _, filter := range filters {
- command = FilteredCommand {
- filter: filter,
- command: command,
- }
+ command = FilteredCommand {
+ filter: filter,
+ command: command,
}
return command
case TokenCommand: