cosmium/parsers/nosql/nosql.peg

177 lines
4.8 KiB
Plaintext
Raw Normal View History

{
package nosql
2024-02-11 21:14:30 +00:00
import "github.com/pikami/cosmium/parsers"
2024-02-11 21:14:30 +00:00
func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt, error) {
selectStmt := parsers.SelectStmt{
Columns: columns.([]parsers.FieldPath),
Table: table.(parsers.Table),
}
2024-02-11 21:14:30 +00:00
if filters, ok := whereClause.(parsers.ComparisonExpression); ok {
selectStmt.Filters = filters
2024-02-11 21:14:30 +00:00
} else if filters, ok := whereClause.(parsers.LogicalExpression); ok {
selectStmt.Filters = filters
}
return selectStmt, nil
}
2024-02-11 21:14:30 +00:00
func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parsers.FieldPath, error) {
ps := path.([]interface{})
paths := make([]string, 1)
paths[0] = name.(string)
for _, p := range ps {
2024-02-11 22:55:07 +00:00
paths = append(paths, p.(string))
}
2024-02-11 21:14:30 +00:00
fieldPath := parsers.FieldPath{Path: paths}
if aliasValue, ok := alias.(string); ok {
fieldPath.Alias = aliasValue
}
return fieldPath, nil
}
2024-02-11 21:14:30 +00:00
func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.FieldPath, error) {
collsAsArray := other_columns.([]interface{})
2024-02-11 21:14:30 +00:00
columnList := make([]parsers.FieldPath, len(collsAsArray) + 1)
columnList[0] = column.(parsers.FieldPath)
for i, v := range collsAsArray {
2024-02-11 21:14:30 +00:00
if col, ok := v.(parsers.FieldPath); ok {
columnList[i+1] = col
}
}
return columnList, nil
}
func joinStrings(array []interface{}) string {
var stringsArray []string
for _, elem := range array {
str, ok := elem.(string)
if !ok {
continue
}
stringsArray = append(stringsArray, str)
}
return strings.Join(stringsArray, "")
}
2024-02-11 21:14:30 +00:00
func combineExpressions(ex1 interface{}, exs interface{}, operation parsers.LogicalExpressionType) (interface{}, error) {
if exs == nil || len(exs.([]interface{})) < 1 {
return ex1, nil
}
2024-02-11 21:14:30 +00:00
return parsers.LogicalExpression{
Expressions: append([]interface{}{ex1}, exs.([]interface{})...),
Operation: operation,
}, nil
}
}
Input <- selectStmt:SelectStmt {
return selectStmt, nil
}
SelectStmt <- Select ws columns:ColumnList ws
From ws table:TableName ws
whereClause:(ws Where ws condition:Condition { return condition, nil })? {
return makeSelectStmt(columns, table, whereClause)
}
ColumnList <- column:FieldPath other_columns:(ws "," ws coll:FieldPath {return coll, nil })* {
return makeColumnList(column, other_columns)
}
TableName <- key:Identifier {
2024-02-11 21:14:30 +00:00
return parsers.Table{Value: key.(string)}, nil
}
2024-02-11 22:55:07 +00:00
FieldPath <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)*
asClause:(ws "AS" ws alias:Identifier { return alias, nil })? {
return makeFieldPath(name, path, asClause)
}
2024-02-11 22:55:07 +00:00
DotFieldAccess <- "." id:Identifier {
return id, nil
}
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
return id, nil
}
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
return string(c.text), nil
}
Condition <- expression:OrExpression {
return expression, nil
}
OrExpression <- ex1:AndExpression ex2:(ws "OR" ws ex:AndExpression { return ex, nil })* {
2024-02-11 21:14:30 +00:00
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeOr)
}
AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpression { return ex, nil })* {
2024-02-11 21:14:30 +00:00
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd)
}
ComparisonExpression <- left:(Literal / FieldPath) ws op:ComparisonOperator ws right:(Literal / FieldPath) {
2024-02-11 21:14:30 +00:00
return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil
}
Select <- ("select" / "SELECT")
From <- ("from" / "FROM")
Where <- ("where" / "WHERE")
ComparisonOperator <- "=" / "!=" / "<" / "<=" / ">" / ">=" {
return string(c.text), nil
}
Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral
IntegerLiteral <- [0-9]+ {
intValue, _ := strconv.Atoi(string(c.text))
2024-02-11 21:14:30 +00:00
return parsers.Constant{Type: parsers.ConstantTypeInteger, Value: intValue}, nil
}
StringLiteral <- "\"" chars:StringCharacter* "\"" {
2024-02-11 21:14:30 +00:00
return parsers.Constant{Type: parsers.ConstantTypeString,Value: joinStrings(chars.([]interface{}))}, nil
}
FloatLiteral <- [0-9]+"."[0-9]+ {
floatValue, _ := strconv.ParseFloat(string(c.text), 64)
2024-02-11 21:14:30 +00:00
return parsers.Constant{Type: parsers.ConstantTypeFloat, Value: floatValue}, nil
}
BooleanLiteral <- ("true" / "false") {
boolValue, _ := strconv.ParseBool(string(c.text))
2024-02-11 21:14:30 +00:00
return parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: boolValue}, nil
}
StringCharacter <- !('"' / "\\") . { return string(c.text), nil }
/ "\\" seq:EscapeSequenceCharacter { return seq, nil }
EscapeSequenceCharacter <- char:EscapeCharacter
EscapeCharacter <- "'"
/ '"'
/ "\\"
/ "b" { return "\b", nil }
/ "f" { return "\f", nil }
/ "n" { return "\n", nil }
/ "r" { return "\r", nil }
/ "t" { return "\t", nil }
non_escape_character <- !(escape_character) char:.
{ return string(c.text), nil }
ws <- [ \t\n\r]*
EOF <- !.