-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
service.go
63 lines (50 loc) · 1.85 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Package language exposes the flux parser as an interface.
package fluxlang
import (
"context"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/ast/astutil"
"github.com/influxdata/flux/complete"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/parser"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
// SourceQuery is a query for a source.
type SourceQuery struct {
Query string `json:"query"`
Type string `json:"type"`
}
// FluxLanguageService is a service for interacting with flux code.
type FluxLanguageService interface {
// Parse will take flux source code and produce a package.
// If there are errors when parsing, the first error is returned.
// An ast.Package may be returned when a parsing error occurs,
// but it may be null if parsing didn't even occur.
Parse(source string) (*ast.Package, error)
// Format will produce a string for the given *ast.File.
Format(f *ast.File) (string, error)
// EvalAST will evaluate and run an AST.
EvalAST(ctx context.Context, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error)
// Completer will return a flux completer.
Completer() complete.Completer
}
// DefaultService is the default language service.
var DefaultService FluxLanguageService = defaultService{}
type defaultService struct{}
func (d defaultService) Parse(source string) (pkg *ast.Package, err error) {
pkg = parser.ParseSource(source)
if ast.Check(pkg) > 0 {
err = ast.GetError(pkg)
}
return pkg, err
}
func (d defaultService) Format(f *ast.File) (string, error) {
return astutil.Format(f)
}
func (d defaultService) EvalAST(ctx context.Context, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error) {
return runtime.EvalAST(ctx, astPkg)
}
func (d defaultService) Completer() complete.Completer {
return complete.NewCompleter(runtime.Prelude())
}