-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.go
63 lines (52 loc) · 1.82 KB
/
file.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 parser
import (
"fmt"
"github.com/twtiger/gosecco/tree"
)
// ParseError represents error parsing a policy file. It will report the filename and the line number as well as the actual error.
type ParseError struct {
originalError error
file string
line int
}
func (e *ParseError) Error() string {
return fmt.Sprintf("%s:%d: %s", e.file, e.line, e.originalError)
}
func parseLines(path string, lines []string) (tree.RawPolicy, error) {
result := []interface{}{}
for ix, l := range lines {
switch lineType(l) {
case commentLine: //ignore
case emptyLine: //ignore
case ruleLine:
parsedRule, err := parseRule(l)
if err != nil {
return tree.RawPolicy{}, &ParseError{err, path, ix}
}
result = append(result, parsedRule)
case assignmentLine, defaultAssignmentLine:
parsedBinding, err := parseBinding(l)
if err != nil {
return tree.RawPolicy{}, &ParseError{err, path, ix}
}
result = append(result, parsedBinding)
case unknownLine:
return tree.RawPolicy{}, &ParseError{fmt.Errorf("Couldn't parse line: '%s' - it doesn't match any kind of valid syntax", l), path, ix}
}
}
return tree.RawPolicy{RuleOrMacros: result}, nil
}
// ParseFile will parse the given file and return a raw parse tree or the error generated
// This function is deprecated and shouldn't be used in new code
func ParseFile(path string) (tree.RawPolicy, error) {
return Parse(&FileSource{path})
}
// ParseString will parse the given string and return a raw parse tree or the error generated
// This function is deprecated and shouldn't be used in new code
func ParseString(str string) (tree.RawPolicy, error) {
return Parse(&StringSource{"<string>", str})
}
// Parse will parse the given Source and return a raw parse tree or the error generated
func Parse(s Source) (tree.RawPolicy, error) {
return s.Parse()
}