Skip to content

Instantly share code, notes, and snippets.

@anisbhsl
Created January 23, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anisbhsl/57bde268ca2874037adc6a47a693db19 to your computer and use it in GitHub Desktop.
Save anisbhsl/57bde268ca2874037adc6a47a693db19 to your computer and use it in GitHub Desktop.
{
package main
import (
"encoding/json"
"reflect"
)
type Query struct{
Collection string
Condition []string
}
func main(){
if len(os.Args)!=2{
log.Fatal("Usage sql 'EXPR'")
}
opts:=Memoize(true)
got,err:=ParseReader("",strings.NewReader(os.Args[1]),opts)
if err!=nil{
log.Fatal(err)
}
fmt.Println("parsed---> ",got)
}
func toIfaceSlice(v interface{}) []interface{} {
if v == nil {
return nil
}
return v.([]interface{})
}
func joinInterface(j []interface{})string{
m:=""
for _,v:=range j{
m+=fmt.Sprintf("%s",v)
}
return m
}
}
PARSE <- query:QUERY EOF{
return query,nil
}
QUERY <- "@"collectionName:GETCOLLECTION _ condition:GETCONDITION{
fmt.Println("condition",condition)
return Query{
Collection: collectionName.(string),
Condition: condition.([]string),
},nil
}
GETCOLLECTION <- [a-zA-Z0-9_\\-]+{
return string(c.text),nil
}
GETCONDITION <- cf:SelectCondition _ cx:SelectConditionRest*{
cond:=[]string{}
cond=append(cond,cf.(string))
for _,v:=range cx.([]interface{}){
cond=append(cond,v.(string))
}
return cond,nil
}
SelectCondition <- token:(LEFT_PARENTHESIS_TOKEN / AND_OPERATOR / OR_OPERATOR / NOT_OPERATOR / RIGHT_PARENTHESIS_TOKEN / PARSECONDITION){
//[a-z]+[" "]*(?:\\=|>|<|>\\=)[" "]*(?:\\"?.*\\"|[a-zA-Z0-9_]+) {
//[a-z]+(\s)*(?:=|>|<|>=|!=)(\s)*(?:"?[a-zA-Z0-9\s]+"|[a-zA-Z0-9_]+) &AND_OPERATOR{
//[a-z]+(\s)*(?:=|>|<|>=|!=|<=)(\s)*(?:"?[a-zA-Z0-9\s]+"|[a-zA-Z0-9_]+)
//[a-z]+[" "]*(?:\\=|>|<|>\\=)[" "]*(?:\\"?.*\\"|[a-zA-Z0-9_]+) {
//[a-z]+[" "\\=><\\!" "]+[\\"\\'A-Za-z0-9_]+){
conditionStr:=""
switch v:=token.(type){
case []uint8:
tmpStr:=""
for _,val:=range v{
tmpStr+=string(val)
}
conditionStr=tmpStr
case []interface{}:
tmpStr:=""
for _,val:=range v{
for _,val2:=range val.([]interface{}){
tmpStr+=fmt.Sprintf("%s",val2)
}
}
conditionStr=tmpStr
case string:
conditionStr=v
}
fmt.Println("[[selectcondition]]",conditionStr)
return conditionStr,nil
}
SelectConditionRest <- _ cx:SelectCondition{
return cx,nil
}
PARSECONDITION <- fname:SelectFieldName _ op:SelectOperator _ fvalue:SelectFieldValue{
// fmt.Println("[[CONDITION]]",reflect.TypeOf(fname),reflect.TypeOf(op),reflect.TypeOf(fvalue))
operatorStr:=""
switch v:=op.(type){
case []uint8:
tmpStr:=""
for _,val:=range v{
tmpStr+=string(val)
}
operatorStr=tmpStr
case string:
operatorStr=v
}
fmt.Println("condition is:",fname.(string)+operatorStr+fvalue.(string))
return fname.(string)+operatorStr+fvalue.(string),nil
}
SelectFieldName <- [a-z]+{
// fmt.Println("[[selectfieldname]]",reflect.TypeOf(string(c.text)))
return string(c.text),nil
}
SelectOperator <- ">=" / "<=" / "!=" / ">" / "<" / "="{
// fmt.Println("[[selectoeprat]]",reflect.TypeOf(string(c.text)))
return string(c.text),nil
}
SelectFieldValue <- [a-zA-Z0-9"]+ _ [a-zA-Z0-9"]* !(AND_OPERATOR/OR_OPERATOR/NOT_OPERATOR) {
// fmt.Println("[[selectfieldvalue]]",reflect.TypeOf(string(c.text)))
return string(c.text),nil
}
/* ---TOKENS HERE------- */
LEFT_PARENTHESIS_TOKEN <- "("
RIGHT_PARENTHESIS_TOKEN <- ")"
AND_OPERATOR <- "AND"
OR_OPERATOR <- "OR"
NOT_OPERATOR <- "NOT"
/*--------------------*/
_ <- ( WhiteSpace / NewLine )*
WhiteSpace "whitespace"
<- " "
/ "\t"
/ "\v"
/ "\f"
NewLine "newline" <- "\r\n"
/ "\r"
/ "\n"
/ "\u2028"
/ "\u2029"
EOF <- !.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment