Skip to content

Commit

Permalink
feat: add lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambrt committed Aug 7, 2023
1 parent a4a2b03 commit 6f78ed7
Show file tree
Hide file tree
Showing 10 changed files with 788 additions and 0 deletions.
27 changes: 27 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) HashiCorp, Inc.

package mql

import (
"fmt"
"reflect"
)

// isNil reports if a is nil
func isNil(a any) bool {
if a == nil {
return true
}
switch reflect.TypeOf(a).Kind() {
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Slice, reflect.Func:
return reflect.ValueOf(a).IsNil()
}
return false
}

// panicIfNil will panic if a is nil
func panicIfNil(a any, caller, missing string) {
if isNil(a) {
panic(fmt.Sprintf("%s: missing %s", caller, missing))
}
}
63 changes: 63 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package mql

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_panicIfNil(t *testing.T) {

assert.Panics(t, func() {
panicIfNil(nil, "test", "missing bit")
})
}

func Test_isNil(t *testing.T) {
t.Parallel()

var testErrNilPtr *testError
var testMapNilPtr map[string]struct{}
var testArrayNilPtr *[1]string
var testChanNilPtr *chan string
var testSliceNilPtr *[]string
var testFuncNil func()

var testChanString chan string

tc := []struct {
i any
want bool
}{
{i: &testError{}, want: false},
{i: testError{}, want: false},
{i: &map[string]struct{}{}, want: false},
{i: map[string]struct{}{}, want: false},
{i: [1]string{}, want: false},
{i: &[1]string{}, want: false},
{i: &testChanString, want: false},
{i: "string", want: false},
{i: []string{}, want: false},
{i: func() {}, want: false},
{i: nil, want: true},
{i: testErrNilPtr, want: true},
{i: testMapNilPtr, want: true},
{i: testArrayNilPtr, want: true},
{i: testChanNilPtr, want: true},
{i: testChanString, want: true},
{i: testSliceNilPtr, want: true},
{i: testFuncNil, want: true},
}

for i, tc := range tc {
t.Run(fmt.Sprintf("test #%d", i+1), func(t *testing.T) {
assert := assert.New(t)
assert.Equal(tc.want, isNil(tc.i))
})
}
}

type testError struct{}

func (*testError) Error() string { return "error" }
7 changes: 7 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mql

import "errors"

var (
ErrInvalidNotEqual = errors.New(`invalid "!=" token`)
)
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module mql

go 1.20

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 6f78ed7

Please sign in to comment.