Skip to content

Commit

Permalink
Implement "iequals" operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Jan 11, 2022
1 parent dcbdccd commit f72e569
Show file tree
Hide file tree
Showing 12 changed files with 1,069 additions and 1,014 deletions.
2 changes: 1 addition & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ func (o *Operation) AsProto() *pb.Expression {
OpLessThan, OpGreaterThan, OpLessOrEqual, OpGreaterOrEqual,
OpBitOr, OpBitAnd, OpBitXor, OpShiftLeft, OpShiftRight,
OpContains, OpIContains, OpStartsWith, OpIStartsWith,
OpEndsWith, OpIEndsWith, OpMatches:
OpEndsWith, OpIEndsWith, OpMatches, OpIEquals:
expr = terms[0]
for _, term := range terms[1:] {
expr = &pb.Expression{
Expand Down
2 changes: 2 additions & 0 deletions ast/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
OpIStartsWith OperatorType = "istartswith"
OpEndsWith OperatorType = "endswith"
OpIEndsWith OperatorType = "iendswith"
OpIEquals OperatorType = "iequals"
OpMatches OperatorType = "matches"
// Non public operation types. This are operations that exist in the
// protobuf, but are not translated into an Operation node in the AST.
Expand All @@ -54,6 +55,7 @@ var OpPrecedence = map[OperatorType]int{
OpIStartsWith: 4,
OpEndsWith: 4,
OpIEndsWith: 4,
OpIEquals: 4,
OpMatches: 4,
OpBitOr: 5,
OpBitXor: 6,
Expand Down
2 changes: 2 additions & 0 deletions ast/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var pbToAst = map[pb.BinaryExpression_Operator]OperatorType{
pb.BinaryExpression_ISTARTSWITH: OpIStartsWith,
pb.BinaryExpression_ENDSWITH: OpEndsWith,
pb.BinaryExpression_IENDSWITH: OpIEndsWith,
pb.BinaryExpression_IEQUALS: OpIEquals,
pb.BinaryExpression_MATCHES: OpMatches,
// Operations that exist in the protobuf but are not translated to an
// operation in the AST.
Expand Down Expand Up @@ -110,6 +111,7 @@ var astToPb = map[OperatorType]pb.BinaryExpression_Operator{
OpEndsWith: pb.BinaryExpression_ENDSWITH,
OpIEndsWith: pb.BinaryExpression_IENDSWITH,
OpMatches: pb.BinaryExpression_MATCHES,
OpIEquals: pb.BinaryExpression_IEQUALS,
}

// createOperationExpression creates an operation given an operator and a list of
Expand Down
6 changes: 6 additions & 0 deletions gyp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ rule foo {
condition:
"foobar" icontains "foo"
}
`,
`
rule foo {
condition:
"foobar" iequals "foo"
}
`,
`
rule foo {
Expand Down
10 changes: 9 additions & 1 deletion parser/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type stringModifiers struct {
%token _ISTARTSWITH_
%token _ENDSWITH_
%token _IENDSWITH_
%token _IEQUALS_
%token _IMPORT_
%token _TRUE_
%token _FALSE_
Expand All @@ -125,7 +126,7 @@ type stringModifiers struct {
%left '|'
%left '^'
%left '&'
%left _EQ_ _NEQ_
%left _EQ_ _NEQ_ _ICONTAINS_ _STARTSWITH_ _ENDSWITH_ _ISTARTSWITH_ _IENDSWITH_ _IEQUALS_ _MATCHES_
%left _LT_ _LE_ _GT_ _GE_
%left _SHIFT_LEFT_ _SHIFT_RIGHT_
%left '+' '-'
Expand Down Expand Up @@ -784,6 +785,13 @@ expression
Operands: []ast.Expression{$1, $3},
}
}
| primary_expression _IEQUALS_ primary_expression
{
$$ = &ast.Operation{
Operator: ast.OpIEquals,
Operands: []ast.Expression{$1, $3},
}
}
| _STRING_IDENTIFIER_
{
$$ = &ast.StringIdentifier{
Expand Down
780 changes: 395 additions & 385 deletions parser/lexer.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions parser/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ octdigit [0-7]
"istartswith" { return yy.Token(_ISTARTSWITH_); }
"endswith" { return yy.Token(_ENDSWITH_); }
"iendswith" { return yy.Token(_IENDSWITH_); }
"iequals" { return yy.Token(_IEQUALS_); }
"import" { return yy.Token(_IMPORT_); }
"include" { return yy.Token(_INCLUDE_); }

Expand Down
Loading

0 comments on commit f72e569

Please sign in to comment.