aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/query/lexer.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-06-02 13:37:43 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-06-09 14:59:26 -0400
commitd6cb444af696ea23d23b1252f2aa51235aff2d63 (patch)
treea33828ff9193b7d7d30158e6ce2a7f95d25f993a /pkg/query/lexer.go
parentbff7aeafc3a61f933ab200afa789b74beab03e91 (diff)
Fix lookback error in lexer
Diffstat (limited to 'pkg/query/lexer.go')
-rw-r--r--pkg/query/lexer.go27
1 files changed, 12 insertions, 15 deletions
diff --git a/pkg/query/lexer.go b/pkg/query/lexer.go
index db7ea28..a53f0b4 100644
--- a/pkg/query/lexer.go
+++ b/pkg/query/lexer.go
@@ -9,7 +9,8 @@ import (
type queryTokenType int
-var lexRegex, oldLexRegex *regexp.Regexp
+var LexRegex *regexp.Regexp
+var LexRegexPattern string
const (
TOK_UNKNOWN queryTokenType = iota
@@ -148,20 +149,20 @@ func Lex(query string) []Token {
CLAUSE_END
)
- matches := lexRegex.FindAllStringSubmatch(query, -1)
+ matches := LexRegex.FindAllStringSubmatch(query, -1)
tokens := make([]Token, 0, 4*len(matches))
tokens = append(tokens, Token{Type: TOK_CLAUSE_START})
tokens = append(tokens, Token{TOK_CLAUSE_AND, "and"}) // default to and'ing all args
clauseLevel := 1
- for i, match := range matches {
+ for _, match := range matches {
if match[CLAUSE_START] != "" {
tokens = append(tokens, Token{Type: TOK_CLAUSE_START})
// TODO: set maximum nest level
clauseLevel += 1
}
if match[CLAUSE_OPERATOR] != "" {
- if i == 0 || tokens[i-1].Type != TOK_CLAUSE_START {
+ if len(tokens) == 0 || tokens[len(tokens)-1].Type != TOK_CLAUSE_START {
tokens = append(tokens, Token{Type: TOK_CLAUSE_START})
clauseLevel += 1
}
@@ -283,15 +284,10 @@ func tokenizeValue(s string, catType queryTokenType) Token {
return t
}
-func treeStringify(tokens []Token) string {
+func TokensStringify(tokens []Token) string {
b := strings.Builder{}
indentLvl := 0
- writeIndent := func(level int) {
- for range level {
- b.WriteString("\t")
- }
- }
writeToken := func(t Token) {
b.WriteByte('`')
b.WriteString(t.String())
@@ -301,11 +297,11 @@ func treeStringify(tokens []Token) string {
for i, token := range tokens {
switch token.Type {
case TOK_CLAUSE_START:
- writeIndent(indentLvl)
+ writeIndent(&b, indentLvl)
b.WriteByte('(')
case TOK_CLAUSE_END:
indentLvl -= 1
- writeIndent(indentLvl)
+ writeIndent(&b, indentLvl)
b.WriteString(")\n")
case TOK_CLAUSE_OR:
b.WriteString("or\n")
@@ -315,7 +311,7 @@ func treeStringify(tokens []Token) string {
indentLvl += 1
case TOK_CAT_TITLE, TOK_CAT_AUTHOR, TOK_CAT_DATE, TOK_CAT_FILETIME, TOK_CAT_TAGS, TOK_CAT_LINKS, TOK_CAT_META, TOK_OP_NEG:
if i == 0 || tokens[i-1].Type != TOK_OP_NEG {
- writeIndent(indentLvl)
+ writeIndent(&b, indentLvl)
}
writeToken(token)
case TOK_VAL_STR, TOK_VAL_DATETIME, TOK_UNKNOWN:
@@ -340,7 +336,8 @@ func init() {
clauseOpPattern := `(?<clause_operator>(?i)and|or)?`
clauseStart := `(?<clause_start>\()?`
clauseEnd := `(?<clause_end>\))?`
- clausePattern := clauseStart + `\s*` + clauseOpPattern + `\s*(?:` + statementPattern + `|` + unknownPattern + `)\s*` + clauseEnd
+ clausePattern := clauseStart + `\s*` + clauseOpPattern + `\s*(?:` + statementPattern + `|` + unknownPattern + `)\s*` + clauseEnd + `\s*`
+ LexRegexPattern = clausePattern
- lexRegex = regexp.MustCompile(clausePattern)
+ LexRegex = regexp.MustCompile(LexRegexPattern)
}