-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.go
42 lines (38 loc) · 1.43 KB
/
expression.go
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
package stringutils
// Expression represents tokens to be matched in a list of tokens
// they might have a specific word or not, in case the word is empty, any token of a given Type will be matched
type Expression struct {
Type string // verb, keyword, separator, etc
Text string // words to be matched with a token (empty for any)
}
// ContainsExpressionsInTokens checks if a given Expression is present in a list of tokens
func ContainsExpressionsInTokens(expressions []Expression, tokens []Token) bool {
if len(expressions) > len(tokens) {
return false
}
if len(expressions) > 1 {
return ContainsExpressionsInTokens(expressions[1:], tokens[1:])
} else if len(expressions) == 1 {
if len(tokens) > 1 {
return ContainsExpressionInTokens(expressions[0], tokens[1:])
}
return ContainsExpressionInTokens(expressions[0], tokens)
}
return false
}
// ContainsExpressionInTokens checks if a given Expression is present in a list of tokens
func ContainsExpressionInTokens(expression Expression, tokens []Token) bool {
for _, token := range tokens {
if MatchToken(token, expression) {
return true
}
}
return false
}
// MatchToken checks if a given Expression matches a given Token
func MatchToken(token Token, expression Expression) bool {
if len(expression.Text) == 0 {
return EqualsIgnoreCase(token.Type, expression.Type)
}
return EqualsIgnoreCase(token.Type, expression.Type) && EqualsIgnoreCase(token.Text, expression.Text)
}