Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/parser/parser.go
4096 views
1
// Package parser implements utilities for parsing River configuration files.
2
package parser
3
4
import (
5
"github.com/grafana/agent/pkg/river/ast"
6
"github.com/grafana/agent/pkg/river/token"
7
)
8
9
// ParseFile parses an entire River configuration file. The data parameter
10
// should hold the file contents to parse, while the filename parameter is used
11
// for reporting errors.
12
//
13
// If an error was encountered during parsing, the returned AST will be nil and
14
// err will be an diag.Diagnostics all the errors encountered during parsing.
15
func ParseFile(filename string, data []byte) (*ast.File, error) {
16
p := newParser(filename, data)
17
18
f := p.ParseFile()
19
if len(p.diags) > 0 {
20
return nil, p.diags
21
}
22
return f, nil
23
}
24
25
// ParseExpression parses a single River expression from expr.
26
//
27
// If an error was encountered during parsing, the returned expression will be
28
// nil and err will be an ErrorList with all the errors encountered during
29
// parsing.
30
func ParseExpression(expr string) (ast.Expr, error) {
31
p := newParser("", []byte(expr))
32
33
e := p.ParseExpression()
34
35
// If the current token is not a TERMINATOR then the parsing did not complete
36
// in full and there are still parts of the string left unparsed.
37
p.expect(token.TERMINATOR)
38
39
if len(p.diags) > 0 {
40
return nil, p.diags
41
}
42
return e, nil
43
}
44
45