diff --git a/query/compile.go b/query/compile.go index d04cd7c5bc..4056bb71d5 100644 --- a/query/compile.go +++ b/query/compile.go @@ -42,29 +42,17 @@ func Compile(ctx context.Context, q string, now time.Time, opts ...Option) (*Spe for _, opt := range opts { opt(o) } + s, _ := opentracing.StartSpanFromContext(ctx, "parse") - astProg, err := parser.NewAST(q) - if err != nil { + itrp := NewInterpreter() + itrp.SetOption(nowOption, nowFunc(now)) + if err := Eval(itrp, q); err != nil { return nil, err } s.Finish() s, _ = opentracing.StartSpanFromContext(ctx, "compile") defer s.Finish() - itrp := NewInterpreter() - itrp.SetOption(nowOption, nowFunc(now)) - - _, decls := builtIns(itrp) - - // Convert AST program to a semantic program - semProg, err := semantic.New(astProg, decls) - if err != nil { - return nil, err - } - - if err := itrp.Eval(semProg); err != nil { - return nil, err - } spec := toSpec(itrp) if o.verbose { @@ -73,6 +61,27 @@ func Compile(ctx context.Context, q string, now time.Time, opts ...Option) (*Spe return spec, nil } +func Eval(itrp *interpreter.Interpreter, q string) error { + + astProg, err := parser.NewAST(q) + if err != nil { + return err + } + + _, decls := builtIns(itrp) + + // Convert AST program to a semantic program + semProg, err := semantic.New(astProg, decls) + if err != nil { + return err + } + + if err := itrp.Eval(semProg); err != nil { + return err + } + return nil +} + // NewInterpreter returns an interpreter instance with // pre-constructed options and global scopes. func NewInterpreter() *interpreter.Interpreter {