wu-sheng commented on code in PR #821:
URL: 
https://github.com/apache/skywalking-banyandb/pull/821#discussion_r2454063722


##########
pkg/bydbql/parser.go:
##########
@@ -0,0 +1,254 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package bydbql
+
+import (
+       "fmt"
+       "regexp"
+       "strings"
+       "time"
+
+       "github.com/alecthomas/participle/v2"
+       "github.com/alecthomas/participle/v2/lexer"
+)
+
+// Keywords list - single source of truth for all BydbQL keywords.
+var bydbqlKeywords = []string{
+       "SELECT", "SHOW", "TOP", "FROM", "STREAM", "MEASURE", "TRACE", 
"PROPERTY",
+       "IN", "ON", "STAGES", "TIME", "BETWEEN", "AND", "OR", "WHERE", "GROUP", 
"BY", "ORDER",
+       "ASC", "DESC", "LIMIT", "OFFSET", "WITH", "QUERY_TRACE", "SUM", "MEAN",
+       "AVG", "COUNT", "MAX", "MIN", "TAG", "FIELD", "NOT", "HAVING", "MATCH",
+       "AGGREGATE", "NULL",
+}
+
+// Function keywords that can be directly followed by '('.
+var functionKeywords = map[string]bool{
+       "SUM": true, "MEAN": true, "AVG": true, "COUNT": true, "MAX": true, 
"MIN": true,
+       "MATCH": true, // MATCH can also be followed by '('
+}
+
+// Lexer and parser are initialized in init().
+var (
+       bydbqlLexer     lexer.Definition
+       particpleParser *participle.Parser[Grammar]
+       keywordPattern  *regexp.Regexp
+)
+
+func init() {
+       // Build lexer dynamically from keyword list to avoid duplication
+       keywordStr := strings.Join(bydbqlKeywords, "|")
+       bydbqlLexer = lexer.MustSimple([]lexer.SimpleRule{
+               {
+                       Name:    "Keyword",
+                       Pattern: fmt.Sprintf(`(?i)(%s)\b`, keywordStr),
+               },
+               {Name: "Ident", Pattern: `[a-zA-Z_][a-zA-Z0-9_-]*`},
+               {Name: "Int", Pattern: `[-+]?\d+`},
+               {Name: "String", Pattern: 
`'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"`},
+               {Name: "QuotedIdent", Pattern: 
`"[a-zA-Z_][a-zA-Z0-9_.]*"|'[a-zA-Z_][a-zA-Z0-9_.]*'`},
+               {Name: "Operators", Pattern: `!=|>=|<=|::|[=><,.()*]`},
+               {Name: "whitespace", Pattern: `\s+`},
+       })
+
+       // Build parser with the dynamically created lexer
+       var err error
+       particpleParser, err = participle.Build[Grammar](
+               participle.Lexer(bydbqlLexer),
+               participle.Unquote("String"),
+               participle.Unquote("QuotedIdent"),
+               participle.CaseInsensitive("Keyword"),
+               participle.UseLookahead(2),
+       )
+       if err != nil {
+               panic(fmt.Sprintf("failed to build BydbQL parser: %v", err))
+       }
+
+       // Build keyword pattern for validation (reuse keywordStr to avoid 
duplication)
+       keywordPattern = regexp.MustCompile(fmt.Sprintf(`(?i)\b(%s)\b`, 
keywordStr))
+}
+
+// validateKeywordSpacing checks that all keywords in the query are properly 
separated by whitespace.
+// Returns an error if any keyword is not preceded or followed by valid 
separators.
+func validateKeywordSpacing(query string) error {

Review Comment:
   Yes. Regular is dangerous. We could use context-aware(typically in quotation 
marks or not) flow to separate the words and strings. Then determine they are 
keywords or text as official lexal analysis. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to