From 3fe634ef7864b0e61658b2e6c936c2aa76299ef2 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Tue, 15 Jan 2019 10:08:21 +0100 Subject: [PATCH] Fix parser to support sequential calls to Parse*() functs --- crm/repository/ql/ast_parser.go | 2 +- crm/repository/ql/ast_parser_test.go | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crm/repository/ql/ast_parser.go b/crm/repository/ql/ast_parser.go index f1233f2b9..50310ae3b 100644 --- a/crm/repository/ql/ast_parser.go +++ b/crm/repository/ql/ast_parser.go @@ -25,7 +25,6 @@ type ( // NewParser returns a new instance of Parser. func NewParser() *Parser { p := &Parser{ - tokbuf: make([]Token, 3), OnIdent: func(ident Ident) (Ident, error) { return ident, nil }, OnFunction: func(ident Function) (Function, error) { return ident, nil }, } @@ -51,6 +50,7 @@ func (p *Parser) peekToken(s int) Token { func (p *Parser) initLexer(s string) { p.lexer = NewLexer(strings.NewReader(s)) + p.tokbuf = make([]Token, 3) for c := 1; c < cap(p.tokbuf); c++ { // Fill the buffer diff --git a/crm/repository/ql/ast_parser_test.go b/crm/repository/ql/ast_parser_test.go index 8f3d0eac7..2ec478598 100644 --- a/crm/repository/ql/ast_parser_test.go +++ b/crm/repository/ql/ast_parser_test.go @@ -242,10 +242,27 @@ func TestAstParser_ColumnParser(t *testing.T) { }, }, }, + { + in: `DATE_FORMAT(some_date, '%Y-%m-01')`, + cols: Columns{ + Column{ + Expr: ASTNodes{ + Function{ + Name: "DATE_FORMAT", + Arguments: ASTSet{ + Ident{Value: "some_date"}, + String{Value: "%Y-%m-01"}, + }, + }, + }, + }, + }, + }, } + p := NewParser() for i, test := range tests { - if cols, err := NewParser().ParseColumns(test.in); err != test.err { + if cols, err := p.ParseColumns(test.in); err != test.err { t.Fatalf("%d. %s: error mismatch:\n expected: %v\n got: %v\n\n", i, test.in, test.err, err) } else if test.err == nil && !reflect.DeepEqual(test.cols, cols) { t.Errorf("%d. %s\n\ncols does not match:\n\nexpected: %#v\n got: %#v\n\n", i, test.in, test.cols, cols) @@ -265,9 +282,8 @@ func TestAstParser_IdentModifier(t *testing.T) { }, } + p := NewParser() for i, test := range tests { - p := NewParser() - p.OnIdent = func(ident Ident) (Ident, error) { ident.Value = fmt.Sprintf("__wrap_%s_wrap__", ident.Value) return ident, nil