1package gintersect 2 3import ( 4 "github.com/pkg/errors" 5) 6 7var ( 8 errBadImplementation = errors.New("this logical path is invalid") 9) 10 11// Match implements single-Token matching, ignoring flags. 12// Example: [a-d] and [b-e] match, while [a-z] and [0-9] do not. 13func Match(t1 Token, t2 Token) bool { 14 var temp Token 15 if t1.Type() > t2.Type() { 16 temp = t1 17 t1 = t2 18 t2 = temp 19 } 20 21 switch t1.Type() { 22 case TTCharacter: 23 ch := t1.(*character) 24 25 switch t2.Type() { 26 case TTCharacter: 27 return matchCharacters(ch, t2.(*character)) 28 case TTDot: 29 return matchCharacterDot(ch, t2.(*dot)) 30 case TTSet: 31 return matchCharacterSet(ch, t2.(*set)) 32 default: 33 panic(errBadImplementation) 34 } 35 36 case TTDot: 37 d := t1.(*dot) 38 39 switch t2.Type() { 40 case TTDot: 41 return matchDots(d, t2.(*dot)) 42 case TTSet: 43 return matchDotSet(d, t2.(*set)) 44 default: 45 panic(errBadImplementation) 46 } 47 48 case TTSet: 49 switch t2.Type() { 50 case TTSet: 51 return matchSets(t1.(*set), t2.(*set)) 52 default: 53 panic(errBadImplementation) 54 } 55 56 default: 57 panic(errBadImplementation) 58 59 } 60} 61 62func matchCharacters(a *character, b *character) bool { 63 return a.Rune() == b.Rune() 64} 65 66func matchCharacterDot(a *character, b *dot) bool { 67 return true 68} 69 70func matchCharacterSet(a *character, b *set) bool { 71 _, ok := b.Runes()[a.Rune()] 72 return ok 73} 74 75func matchDots(a *dot, b *dot) bool { 76 return true 77} 78 79func matchDotSet(a *dot, b *set) bool { 80 return true 81} 82 83func matchSets(a *set, b *set) bool { 84 for k, _ := range a.Runes() { 85 if _, ok := b.Runes()[k]; ok { 86 return true 87 } 88 } 89 90 return false 91} 92