Skip to content

Commit

Permalink
Tweak contactql grammar to enforce stricter limits on condition left-…
Browse files Browse the repository at this point in the history
…hand sides
  • Loading branch information
rowanseymour committed May 14, 2024
1 parent 83bc27a commit bc0ef23
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 189 deletions.
12 changes: 9 additions & 3 deletions antlr/ContactQL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ grammar ContactQL;

import LexUnicode;

// Lexer rules
fragment HAS: [Hh][Aa][Ss];
fragment IS: [Ii][Ss];

Expand All @@ -20,6 +21,8 @@ COMPARATOR: (
| HAS
| IS
);
STRING: '"' (~["] | '\\"')* '"';
NAME: (UnicodeLetter | UnicodeDigit | '_' | ':')+;
TEXT: (
UnicodeLetter
| UnicodeDigit
Expand All @@ -32,20 +35,23 @@ TEXT: (
| '@'
| ':'
)+;
STRING: '"' (~["] | '\\"')* '"';
WS: [ \t\n\r]+ -> skip; // ignore whitespace
ERROR: .;
// Parser rules
parse: expression EOF;
expression:
expression AND expression # combinationAnd
| expression expression # combinationImpicitAnd
| expression OR expression # combinationOr
| LPAREN expression RPAREN # expressionGrouping
| TEXT COMPARATOR literal # condition
| NAME COMPARATOR literal # condition
| literal # implicitCondition;
literal: TEXT # textLiteral | STRING # stringLiteral;
literal:
NAME # textLiteral
| TEXT # textLiteral
| STRING # stringLiteral;
6 changes: 4 additions & 2 deletions antlr/gen/contactql/ContactQL.interp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ null
null
null
null
null

token symbolic names:
null
Expand All @@ -17,8 +18,9 @@ RPAREN
AND
OR
COMPARATOR
TEXT
STRING
NAME
TEXT
WS
ERROR

Expand All @@ -29,4 +31,4 @@ literal


atn:
[4, 1, 9, 38, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 19, 8, 1, 1, 1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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 29, 8, 1, 10, 1, 12, 1, 32, 9, 1, 1, 2, 1, 2, 1, 2, 3, 2, 37, 8, 2, 1, 2, 0, 1, 2, 3, 0, 2, 4, 0, 0, 42, 0, 6, 1, 0, 0, 0, 2, 18, 1, 0, 0, 0, 4, 36, 1, 0, 0, 0, 6, 7, 3, 2, 1, 0, 7, 8, 5, 0, 0, 1, 8, 1, 1, 0, 0, 0, 9, 10, 6, 1, -1, 0, 10, 11, 5, 1, 0, 0, 11, 12, 3, 2, 1, 0, 12, 13, 5, 2, 0, 0, 13, 19, 1, 0, 0, 0, 14, 15, 5, 7, 0, 0, 15, 16, 5, 5, 0, 0, 16, 19, 3, 4, 2, 0, 17, 19, 3, 4, 2, 0, 18, 9, 1, 0, 0, 0, 18, 14, 1, 0, 0, 0, 18, 17, 1, 0, 0, 0, 19, 30, 1, 0, 0, 0, 20, 21, 10, 6, 0, 0, 21, 22, 5, 3, 0, 0, 22, 29, 3, 2, 1, 7, 23, 24, 10, 5, 0, 0, 24, 29, 3, 2, 1, 6, 25, 26, 10, 4, 0, 0, 26, 27, 5, 4, 0, 0, 27, 29, 3, 2, 1, 5, 28, 20, 1, 0, 0, 0, 28, 23, 1, 0, 0, 0, 28, 25, 1, 0, 0, 0, 29, 32, 1, 0, 0, 0, 30, 28, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 3, 1, 0, 0, 0, 32, 30, 1, 0, 0, 0, 33, 37, 5, 7, 0, 0, 34, 37, 5, 8, 0, 0, 35, 37, 5, 6, 0, 0, 36, 33, 1, 0, 0, 0, 36, 34, 1, 0, 0, 0, 36, 35, 1, 0, 0, 0, 37, 5, 1, 0, 0, 0, 4, 18, 28, 30, 36]
9 changes: 5 additions & 4 deletions antlr/gen/contactql/ContactQL.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ RPAREN=2
AND=3
OR=4
COMPARATOR=5
TEXT=6
STRING=7
WS=8
ERROR=9
STRING=6
NAME=7
TEXT=8
WS=9
ERROR=10
'('=1
')'=2
9 changes: 6 additions & 3 deletions antlr/gen/contactql/ContactQLLexer.interp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions antlr/gen/contactql/ContactQLLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ RPAREN=2
AND=3
OR=4
COMPARATOR=5
TEXT=6
STRING=7
WS=8
ERROR=9
STRING=6
NAME=7
TEXT=8
WS=9
ERROR=10
'('=1
')'=2
282 changes: 144 additions & 138 deletions antlr/gen/contactql/contactql_lexer.go

Large diffs are not rendered by default.

75 changes: 46 additions & 29 deletions antlr/gen/contactql/contactql_parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions contactql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,28 @@ func TestParsingErrors(t *testing.T) {
}{
{
query: `$`,
errMsg: "mismatched input '$' expecting {'(', TEXT, STRING}",
errMsg: "mismatched input '$' expecting {'(', STRING, NAME, TEXT}",
errCode: "unexpected_token",
errExtra: map[string]string{"token": "$"},
},
{
query: `name = `,
errMsg: "mismatched input '<EOF>' expecting {TEXT, STRING}",
errMsg: "mismatched input '<EOF>' expecting {STRING, NAME, TEXT}",
errCode: "unexpected_token",
errExtra: map[string]string{"token": "<EOF>"},
},
{
query: `name = "x`,
errMsg: "extraneous input '\"' expecting {TEXT, STRING}",
errMsg: "extraneous input '\"' expecting {STRING, NAME, TEXT}",
errCode: "",
errExtra: nil,
},
{
query: `nam/e = "x"`,
errMsg: "mismatched input '=' expecting <EOF>",
errCode: "unexpected_token",
errExtra: map[string]string{"token": "="},
},
{
query: `age = XZ`,
errMsg: "can't convert 'XZ' to a number",
Expand Down
6 changes: 3 additions & 3 deletions contactql/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func (v *visitor) VisitImplicitCondition(ctx *gen.ImplicitConditionContext) any
return NewCondition(AttributeName, PropertyTypeAttribute, operator, value)
}

// expression : TEXT COMPARATOR literal
// expression : NAME COMPARATOR literal
func (v *visitor) VisitCondition(ctx *gen.ConditionContext) any {
propKey := strings.ToLower(ctx.TEXT().GetText())
propKey := strings.ToLower(ctx.NAME().GetText())
operatorText := strings.ToLower(ctx.COMPARATOR().GetText())
value := v.Visit(ctx.Literal()).(string)

Expand Down Expand Up @@ -176,7 +176,7 @@ func (v *visitor) VisitExpressionGrouping(ctx *gen.ExpressionGroupingContext) an
return v.Visit(ctx.Expression())
}

// literal : TEXT
// literal : TEXT | NAME
func (v *visitor) VisitTextLiteral(ctx *gen.TextLiteralContext) any {
return ctx.GetText()
}
Expand Down

0 comments on commit bc0ef23

Please sign in to comment.