<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/pathfilter.go
blob: 7b6c64fd7f73b2cb753a60508fa7309a3cc83e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main

type MapTerminalFilter struct {}
func (filter MapTerminalFilter) exec(state *ProgramState) bool {
	terminal, isTerminal := state.space.value.(TerminalValue)
	if !isTerminal {
		return false
	}
	return terminal == MapBegin || terminal == MapEnd
}

type NonTerminalFilter struct {}
func (filter NonTerminalFilter) exec(state *ProgramState) bool {
	_, isTerminal := state.space.value.(TerminalValue)
	return !isTerminal
}

type AnySegmentPathFilter struct {
	next PathFilterState
}
func (filter AnySegmentPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	res := make(map[PathFilterState]struct{})
	res[filter.next] = struct{}{}
	return res
}
func (filter AnySegmentPathFilter) accept() bool {
	return false
}

type GroupPathFilter struct {
	filters []PathFilterState
}
func (filter GroupPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	res := make(map[PathFilterState]struct{})
	for _, f := range filter.filters {
		for r := range f.eat(segment) {
			res[r] = struct{}{}
		}
	}
	return res
}
func (filter GroupPathFilter) accept() bool {
	for _, f := range filter.filters {
		if f.accept() {
			return true
		}
	}
	return false
}

type NonePathFilter struct {}
func (filter NonePathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	return make(map[PathFilterState]struct{})
}
func (filter NonePathFilter) accept() bool {
	return true
}

type StringSegmentPathFilter struct {
	index string
	next PathFilterState
}
func (filter StringSegmentPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	s, isString := segment.(string)
	res := make(map[PathFilterState]struct{})
	if isString && s == filter.index {
		res[filter.next] = struct{}{}
	}
	return res
}
func (filter StringSegmentPathFilter) accept() bool {
	return false
}

type PathFilterState interface {
	eat(PathSegment) map[PathFilterState]struct{}
	accept() bool
}