cosmium/parsers/nosql/nosql.peg

242 lines
6.6 KiB
Plaintext
Raw Normal View History

{
package nosql
2024-02-11 21:14:30 +00:00
import "github.com/pikami/cosmium/parsers"
2024-02-14 19:03:49 +00:00
func makeSelectStmt(columns, table, whereClause interface{}, count interface{}) (parsers.SelectStmt, error) {
selectStmt := parsers.SelectStmt{
SelectItems: columns.([]parsers.SelectItem),
2024-02-14 19:03:49 +00:00
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
}
2024-02-14 19:03:49 +00:00
if n, ok := count.(int); ok {
selectStmt.Count = n
}
return selectStmt, nil
}
2024-02-13 20:42:18 +00:00
func makeSelectItem(name interface{}, path interface{}, selectItemType parsers.SelectItemType) (parsers.SelectItem, 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-13 20:42:18 +00:00
return parsers.SelectItem{Path: paths, Type: selectItemType}, nil
}
func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.SelectItem, error) {
collsAsArray := other_columns.([]interface{})
columnList := make([]parsers.SelectItem, len(collsAsArray) + 1)
columnList[0] = column.(parsers.SelectItem)
for i, v := range collsAsArray {
if col, ok := v.(parsers.SelectItem); ok {
columnList[i+1] = col
}
}
return columnList, nil
}
2024-02-13 20:42:18 +00:00
func makeSelectArray(columns interface{}) (parsers.SelectItem, error) {
return parsers.SelectItem{
2024-02-13 19:57:33 +00:00
SelectItems: columns.([]parsers.SelectItem),
Type: parsers.SelectItemTypeArray,
2024-02-13 20:42:18 +00:00
}, nil
}
2024-02-13 19:57:33 +00:00
2024-02-13 20:42:18 +00:00
func makeSelectObject(field interface{}, other_fields interface{}) (parsers.SelectItem, error) {
fieldsAsArray := other_fields.([]interface{})
fieldsList := make([]parsers.SelectItem, len(fieldsAsArray)+1)
fieldsList[0] = field.(parsers.SelectItem)
for i, v := range fieldsAsArray {
if col, ok := v.(parsers.SelectItem); ok {
fieldsList[i+1] = col
}
2024-02-13 19:57:33 +00:00
}
2024-02-13 20:42:18 +00:00
return parsers.SelectItem{
SelectItems: fieldsList,
Type: parsers.SelectItemTypeObject,
}, nil
2024-02-13 19:57:33 +00:00
}
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
}
2024-02-14 19:03:49 +00:00
SelectStmt <- Select ws topClause:TopClause? ws columns:Selection ws
From ws table:TableName ws
whereClause:(ws Where ws condition:Condition { return condition, nil })? {
2024-02-14 19:03:49 +00:00
return makeSelectStmt(columns, table, whereClause, topClause)
}
TopClause <- Top ws count:Integer {
return count, nil
}
Selection <- SelectValueSpec / ColumnList
ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return coll, nil })* {
return makeColumnList(column, other_columns)
}
SelectValueSpec <- "VALUE" ws column:SelectItem {
selectItem := column.(parsers.SelectItem)
selectItem.IsTopLevel = true
return makeColumnList(selectItem, make([]interface{}, 0))
}
TableName <- key:Identifier {
2024-02-11 21:14:30 +00:00
return parsers.Table{Value: key.(string)}, nil
}
2024-02-13 20:42:18 +00:00
SelectArray <- "[" ws columns:ColumnList ws "]" {
return makeSelectArray(columns)
2024-02-13 19:57:33 +00:00
}
2024-02-13 20:42:18 +00:00
SelectObject <- "{" ws field:SelectObjectField ws other_fields:(ws "," ws coll:SelectObjectField {return coll, nil })* ws "}" {
return makeSelectObject(field, other_fields)
}
SelectObjectField <- name:Identifier ws ":" ws selectItem:SelectItem {
item := selectItem.(parsers.SelectItem)
item.Alias = name.(string)
return item, nil
}
SelectProperty <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* {
return makeSelectItem(name, path, parsers.SelectItemTypeField)
}
SelectItem <- selectItem:(SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
item := selectItem.(parsers.SelectItem)
if aliasValue, ok := asClause.(string); ok {
item.Alias = aliasValue
}
return item, nil
}
2024-02-13 19:57:33 +00:00
AsClause <- ws As ws alias:Identifier { return alias, nil }
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 / SelectItem) ws op:ComparisonOperator ws right:(Literal / SelectItem) {
2024-02-11 21:14:30 +00:00
return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil
}
Select <- ("select" / "SELECT")
2024-02-14 19:03:49 +00:00
Top <- ("top" / "TOP")
2024-02-13 19:57:33 +00:00
As <- ("as" / "AS")
From <- ("from" / "FROM")
Where <- ("where" / "WHERE")
ComparisonOperator <- "=" / "!=" / "<" / "<=" / ">" / ">=" {
return string(c.text), nil
}
Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral
2024-02-14 19:03:49 +00:00
IntegerLiteral <- number:Integer {
return parsers.Constant{Type: parsers.ConstantTypeInteger, Value: number.(int)}, 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
}
2024-02-14 19:03:49 +00:00
Integer <- [0-9]+ {
return strconv.Atoi(string(c.text))
}
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 <- !.