Skip to content

Commit 0f8bfc4

Browse files
committed
Refactor CQL parser code
1 parent 934bdb0 commit 0f8bfc4

File tree

5 files changed

+767
-174
lines changed

5 files changed

+767
-174
lines changed

internal/cql/CQLParser.g4

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ predicate : comparisonPredicate
4646
# an operator to test if a scalar expression is NULL or not.
4747
#============================================================================*/
4848

49-
comparisonPredicate : binaryComparisonPredicate
50-
| isLikePredicate
51-
| isBetweenPredicate
52-
| isInListPredicate
53-
| isNullPredicate;
49+
comparisonPredicate : binaryComparisonPredicate # PredicateBinaryComp
50+
| isLikePredicate # PredicateLike
51+
| isBetweenPredicate # PredicateBetween
52+
| isInListPredicate # PredicateIn
53+
| isNullPredicate # PredicateIsNull
54+
;
5455

55-
binaryComparisonPredicate : scalarExpression ComparisonOperator scalarExpression;
56+
binaryComparisonPredicate : left=scalarExpression op=ComparisonOperator right=scalarExpression;
5657

5758
isLikePredicate : propertyName (NOT)? ( LIKE | ILIKE ) characterLiteral;
5859

@@ -78,16 +79,16 @@ isNullPredicate : propertyName IS (NOT)? NULL;
7879
# The Postgres parser will provide a final check on the correctness.
7980
#============================================================================*/
8081

81-
scalarExpression : scalarValue
82-
| LEFTPAREN scalarExpression RIGHTPAREN
83-
| scalarExpression ArithmeticOperator scalarExpression
84-
;
82+
scalarExpression : val=scalarValue # ScalarVal
83+
| LEFTPAREN expr=scalarExpression RIGHTPAREN # ScalarParen
84+
| left=scalarExpression op=ArithmeticOperator right=scalarExpression # ScalarExpr
85+
;
8586

86-
scalarValue : propertyName
87-
| characterLiteral
88-
| numericLiteral
89-
| booleanLiteral
90-
| temporalLiteral
87+
scalarValue : propertyName # LiteralName
88+
| characterLiteral # LiteralString
89+
| numericLiteral # LiteralNumeric
90+
| booleanLiteral # LiteralBoolean
91+
| temporalLiteral # LiteralTemporal
9192
// | function
9293
;
9394

internal/cql/cql.go

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,6 @@ func (l *cqlListener) ExitCqlFilter(ctx *CqlFilterContext) {
211211
l.sql = sqlFor(ctx.BooleanExpression())
212212
}
213213

214-
/*
215-
func (l *cqlListener) ExitBooleanExpression(ctx *BooleanExpressionContext) {
216-
sql := sqlFor(ctx.BooleanFactor())
217-
//sql := sqlFor(ctx.BooleanExpression(0))
218-
if ctx.OR(0) != nil {
219-
term := sqlFor(ctx.BooleanExpression(1))
220-
sql = sql + " OR " + term
221-
} else if ctx.LEFTPAREN() != nil {
222-
sql = "(" + sqlFor(ctx.BooleanExpression(0)) + ")"
223-
}
224-
ctx.SetSql(sql)
225-
}
226-
*/
227-
228214
func (l *cqlListener) ExitBoolExprTerm(ctx *BoolExprTermContext) {
229215
sql := sqlFor(ctx.BooleanTerm())
230216
ctx.SetSql(sql)
@@ -256,15 +242,6 @@ func (l *cqlListener) ExitBoolExprNot(ctx *BoolExprNotContext) {
256242
ctx.SetSql(sql)
257243
}
258244

259-
/*
260-
func (l *cqlListener) ExitBooleanTerm(ctx *BooleanTermContext) {
261-
sql := sqlFor(ctx.BooleanPrimary())
262-
if ctx.NOT() != nil {
263-
sql = " NOT " + sql
264-
}
265-
ctx.SetSql(sql)
266-
}
267-
*/
268245
func (l *cqlListener) ExitBooleanTerm(ctx *BooleanTermContext) {
269246
var sql string
270247
if ctx.BooleanLiteral() != nil {
@@ -289,58 +266,79 @@ func (l *cqlListener) ExitPredicate(ctx *PredicateContext) {
289266
ctx.SetSql(sql)
290267
}
291268

292-
func (l *cqlListener) ExitComparisonPredicate(ctx *ComparisonPredicateContext) {
293-
var sql string
294-
if ctx.BinaryComparisonPredicate() != nil {
295-
sql = sqlFor(ctx.BinaryComparisonPredicate())
296-
} else if ctx.IsLikePredicate() != nil {
297-
sql = sqlFor(ctx.IsLikePredicate())
298-
} else if ctx.IsBetweenPredicate() != nil {
299-
sql = sqlFor(ctx.IsBetweenPredicate())
300-
} else if ctx.IsNullPredicate() != nil {
301-
sql = sqlFor(ctx.IsNullPredicate())
302-
} else if ctx.IsInListPredicate() != nil {
303-
sql = sqlFor(ctx.IsInListPredicate())
304-
}
269+
func (l *cqlListener) ExitPredicateBinaryComp(ctx *PredicateBinaryCompContext) {
270+
sql := sqlFor(ctx.BinaryComparisonPredicate())
271+
ctx.SetSql(sql)
272+
}
273+
274+
func (l *cqlListener) ExitPredicateBetween(ctx *PredicateBetweenContext) {
275+
sql := sqlFor(ctx.IsBetweenPredicate())
276+
ctx.SetSql(sql)
277+
}
278+
279+
func (l *cqlListener) ExitPredicateLike(ctx *PredicateLikeContext) {
280+
sql := sqlFor(ctx.IsLikePredicate())
281+
ctx.SetSql(sql)
282+
}
283+
284+
func (l *cqlListener) ExitPredicateIn(ctx *PredicateInContext) {
285+
sql := sqlFor(ctx.IsInListPredicate())
286+
ctx.SetSql(sql)
287+
}
288+
289+
func (l *cqlListener) ExitPredicateIsNull(ctx *PredicateIsNullContext) {
290+
sql := sqlFor(ctx.IsNullPredicate())
305291
ctx.SetSql(sql)
306292
}
307293

308294
func (l *cqlListener) ExitBinaryComparisonPredicate(ctx *BinaryComparisonPredicateContext) {
309-
expr1 := sqlFor(ctx.ScalarExpression(0))
310-
expr2 := sqlFor(ctx.ScalarExpression(1))
311-
op := getNodeText(ctx.ComparisonOperator())
295+
expr1 := sqlFor(ctx.left)
296+
expr2 := sqlFor(ctx.right)
297+
op := ctx.op.GetText()
312298
sql := expr1 + " " + op + " " + expr2
313299
ctx.SetSql(sql)
314300
}
315301

316-
func (l *cqlListener) ExitScalarValue(ctx *ScalarValueContext) {
317-
var sql string
318-
if ctx.PropertyName() != nil {
319-
sql = quotedName(getText(ctx.PropertyName()))
320-
} else if ctx.CharacterLiteral() != nil {
321-
sql = quotedText(getText(ctx.CharacterLiteral()))
322-
} else if ctx.NumericLiteral() != nil {
323-
sql = getText(ctx.NumericLiteral())
324-
} else if ctx.BooleanLiteral() != nil {
325-
sql = getText(ctx.BooleanLiteral())
326-
} else if ctx.TemporalLiteral() != nil {
327-
sql = sqlFor(ctx.TemporalLiteral())
328-
}
302+
func (l *cqlListener) ExitLiteralName(ctx *LiteralNameContext) {
303+
sql := quotedName(getText(ctx.PropertyName()))
329304
ctx.SetSql(sql)
330305
}
331306

332-
func (l *cqlListener) ExitScalarExpression(ctx *ScalarExpressionContext) {
333-
var sql string
334-
if ctx.LEFTPAREN() != nil {
335-
sql = "(" + sqlFor(ctx.ScalarExpression(0)) + ")"
336-
} else if ctx.ArithmeticOperator() != nil {
337-
expr1 := sqlFor(ctx.ScalarExpression(0))
338-
expr2 := sqlFor(ctx.ScalarExpression(1))
339-
op := getNodeText(ctx.ArithmeticOperator())
340-
sql = expr1 + " " + op + " " + expr2
341-
} else {
342-
sql = sqlFor(ctx.ScalarValue())
343-
}
307+
func (l *cqlListener) ExitLiteralString(ctx *LiteralStringContext) {
308+
sql := quotedText(getText(ctx.CharacterLiteral()))
309+
ctx.SetSql(sql)
310+
}
311+
312+
func (l *cqlListener) ExitLiteralNumeric(ctx *LiteralNumericContext) {
313+
sql := getText(ctx.NumericLiteral())
314+
ctx.SetSql(sql)
315+
}
316+
317+
func (l *cqlListener) ExitLiteralBoolean(ctx *LiteralBooleanContext) {
318+
sql := getText(ctx.BooleanLiteral())
319+
ctx.SetSql(sql)
320+
}
321+
322+
func (l *cqlListener) ExitLiteralTemporal(ctx *LiteralTemporalContext) {
323+
sql := sqlFor(ctx.TemporalLiteral())
324+
ctx.SetSql(sql)
325+
}
326+
327+
func (l *cqlListener) ExitScalarVal(ctx *ScalarValContext) {
328+
sql := sqlFor(ctx.val)
329+
ctx.SetSql(sql)
330+
}
331+
332+
func (l *cqlListener) ExitScalarParen(ctx *ScalarParenContext) {
333+
sql := "(" + sqlFor(ctx.expr) + ")"
334+
ctx.SetSql(sql)
335+
}
336+
337+
func (l *cqlListener) ExitScalarExpr(ctx *ScalarExprContext) {
338+
expr1 := sqlFor(ctx.left)
339+
expr2 := sqlFor(ctx.right)
340+
op := ctx.op.GetText()
341+
sql := expr1 + " " + op + " " + expr2
344342
ctx.SetSql(sql)
345343
}
346344

0 commit comments

Comments
 (0)