Skip to content

Commit

Permalink
Small AST cleanups (#393)
Browse files Browse the repository at this point in the history
Make Literal into a full fledged ast Node (which means that in json it
gains an Op property).  Also convert the node formerly known as
BooleanLiteral to MatchAll to better reflect its meaning and avoid
confusion with actual Literal nodes.
  • Loading branch information
aswan authored Mar 6, 2020
1 parent 41e5c88 commit 6757dbe
Show file tree
Hide file tree
Showing 11 changed files with 1,132 additions and 1,132 deletions.
18 changes: 9 additions & 9 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type FieldExpr interface {
// the native Go types) and value is a string representation of that value that
// must conform to the provided type.
type Literal struct {
Node
Type string `json:"type"`
Value string `json:"value"`
}
Expand Down Expand Up @@ -70,10 +71,9 @@ type (
Node
Expr BooleanExpr `json:"expr"`
}
// A BooleanLiteral node represents the constant true of false.
BooleanLiteral struct {
// A MatchAll node represents a filter that matches all records.
MatchAll struct {
Node
Value bool `json:"value"`
}
// A CompareAny node represents a comparison operator with all of
// the fields in a record.
Expand All @@ -96,12 +96,12 @@ type (
// booleanEpxrNode() ensures that only boolean expression nodes can be
// assigned to a BooleanExpr.
//
func (*LogicalAnd) booleanExprNode() {}
func (*LogicalOr) booleanExprNode() {}
func (*LogicalNot) booleanExprNode() {}
func (*BooleanLiteral) booleanExprNode() {}
func (*CompareAny) booleanExprNode() {}
func (*CompareField) booleanExprNode() {}
func (*LogicalAnd) booleanExprNode() {}
func (*LogicalOr) booleanExprNode() {}
func (*LogicalNot) booleanExprNode() {}
func (*MatchAll) booleanExprNode() {}
func (*CompareAny) booleanExprNode() {}
func (*CompareField) booleanExprNode() {}

// A FieldExpr is any expression that refers to a field.
type (
Expand Down
2 changes: 1 addition & 1 deletion ast/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (b *LogicalNot) Copy() BooleanExpr {
}
}

func (b *BooleanLiteral) Copy() BooleanExpr {
func (b *MatchAll) Copy() BooleanExpr {
copy := *b
return &copy
}
Expand Down
4 changes: 2 additions & 2 deletions ast/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func unpackBooleanExpr(node joe.JSON) (BooleanExpr, error) {
return nil, err
}
return &LogicalNot{Expr: child}, nil
case "BooleanLiteral":
return &BooleanLiteral{}, nil
case "MatchAll":
return &MatchAll{}, nil
case "CompareAny":
return &CompareAny{}, nil
case "CompareField":
Expand Down
4 changes: 2 additions & 2 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func Compile(node ast.BooleanExpr) (Filter, error) {
}
return LogicalOr(left, right), nil

case *ast.BooleanLiteral:
return func(*zng.Record) bool { return v.Value }, nil
case *ast.MatchAll:
return func(*zng.Record) bool { return true }, nil

case *ast.CompareField:
if v.Comparator == "in" {
Expand Down
2 changes: 1 addition & 1 deletion zng/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func ParseLiteral(literal ast.Literal) (interface{}, error) {
// specifically arrays of bytes that do not correspond to
// UTF-8 encoded strings).
if literal.Type == "string" {
literal = ast.Literal{"bstring", literal.Value}
literal = ast.Literal{ast.Node{"Literal"}, "bstring", literal.Value}
}
v, err := Parse(literal)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions zng/resolver/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func val(t, v string) ast.Literal {
return ast.Literal{t, v}
return ast.Literal{ast.Node{"Literal"}, t, v}
}

func runArray(f zx.Predicate, vals []ast.Literal, expected []bool) error {
Expand Down Expand Up @@ -46,15 +46,15 @@ func TestZeek(t *testing.T) {
t.Parallel()

vals := []ast.Literal{
{"int32", "100"},
{"int32", "101"},
{"float64", "100"},
{"float64", "100.0"},
{"float64", "100.5"},
{"ip", "128.32.1.1"},
{"string", "hello"},
{"port", "80"},
{"port", "8080"},
val("int32", "100"),
val("int32", "101"),
val("float64", "100"),
val("float64", "100.0"),
val("float64", "100.5"),
val("ip", "128.32.1.1"),
val("string", "hello"),
val("port", "80"),
val("port", "8080"),
}
err := run(vals, "lt", val("int32", "101"), []bool{true, false, true, true, true, false, false, true, false})
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions zql/parser-support.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func makeParallelProc(procsIn interface{}) ast.Proc {
}

func makeLiteral(typ string, val interface{}) *ast.Literal {
return &ast.Literal{typ, val.(string)}
return &ast.Literal{ast.Node{"Literal"}, typ, val.(string)}
}

func getValueType(val interface{}) string {
Expand Down Expand Up @@ -84,8 +84,8 @@ func chainFieldCalls(base, derefs interface{}) ast.FieldExpr {
return ret
}

func makeBooleanLiteral(val bool) *ast.BooleanLiteral {
return &ast.BooleanLiteral{ast.Node{"BooleanLiteral"}, val}
func makeMatchAll() *ast.MatchAll {
return &ast.MatchAll{ast.Node{"MatchAll"}}
}

func makeCompareField(comparatorIn, fieldIn, valueIn interface{}) *ast.CompareField {
Expand Down
6 changes: 3 additions & 3 deletions zql/parser-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function makeParallelProc(procs) {
return { op: "ParallelProc", procs };
}

function makeLiteral(type, value) { return { type, value }; }
function makeLiteral(type, value) { return { op: "Literal", type, value }; }
function getValueType(v) { return v.type; }

function makeFieldCall(fn, field, param) {
Expand All @@ -23,8 +23,8 @@ function chainFieldCalls(base, derefs) {
return ret
}

function makeBooleanLiteral(value) {
return { op: "BooleanLiteral", value };
function makeMatchAll() {
return { op: "MatchAll", value };
}

function makeCompareField(comparator, field, value) {
Expand Down
Loading

0 comments on commit 6757dbe

Please sign in to comment.