Skip to content

Instantly share code, notes, and snippets.

@vlsi
Created April 17, 2021 13:17
Show Gist options
  • Save vlsi/352d375325b0e60f6a25ae0e807695d4 to your computer and use it in GitHub Desktop.
Save vlsi/352d375325b0e60f6a25ae0e807695d4 to your computer and use it in GitHub Desktop.
SpringDataПостроительПарсер
// See https://pegjs.org/online
// Sample expressions:
// FindBy${Name}
// FindFirstBy${Name}
// FindBy${Name}And${OperationType}OrderBy${Name}And${OperationType}DescCount
Query "query"
= s:Start t:Transform* f:Finalizer?
{ return {start: s, transforms: t, finalizer: f} }
Start "start"
= FindBy
Transform "transform"
= OrderBy
Finalizer "finalizer"
= 'Count' { return {op: 'COUNT'} }
FindBy "findBy"
= 'Find' finder:('First' / 'All')? 'By' e: Expression
{ return {op: 'FIND', finder: finder, expression: e} }
Expression "expr"
= head:SimpleExpr tail:('And' e: SimpleExpr { return e })*
{ return {type: 'AND', args: [head, ...tail]} }
SimpleExpr "simpleExpr"
= field:FieldName operation:('Contains' / 'StartsWith' / 'Between')?
{ return {type: 'field', fieldName: field, operation: operation} }
FieldName "fieldName"
= '${' name:([^}]+ { return text() }) '}' { return name }
OrderBy "orderBy"
= 'OrderBy' head:FieldCollation
tail:('And' field: FieldCollation { return field })* {
return {op: 'orderBy', columns: [head, ...tail]}
}
FieldCollation "fieldCollation"
= field:FieldName dir:SortDir? { return {field: field, dir: dir} }
SortDir "sortDir"
= 'Asc' / 'Desc'
// FindBy${Name}And${OperationType}OrderBy${Name}And${OperationType}DescCount
{
"start": {
"op": "FIND",
"finder": null,
"expression": {
"type": "AND",
"args": [
{
"type": "field",
"fieldName": "Name",
"operation": null
},
{
"type": "field",
"fieldName": "OperationType",
"operation": null
}
]
}
},
"transforms": [
{
"op": "orderBy",
"columns": [
{
"field": "Name",
"dir": null
},
{
"field": "OperationType",
"dir": "Desc"
}
]
}
],
"finalizer": {
"op": "COUNT"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment