-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentPragma.go
49 lines (40 loc) · 1.17 KB
/
documentPragma.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
package atv
import (
"fmt"
"regexp"
"strings"
"github.com/alecthomas/participle/lexer"
)
// documentPragma represents a pragma in an ATV configuration document.
type documentPragma struct {
Pos lexer.Position
Name string
Value string
}
// Dupe returns a copy of the document node.
func (pragma *documentPragma) Dupe() *documentPragma {
if pragma == nil {
return nil
}
return &documentPragma{
Name: pragma.Name,
Value: pragma.Value,
}
}
var pragmaSplitterRegex = regexp.MustCompile(`#(\w+)(?:\s+(.*))?$`)
// Capture initializes the pragma object when the parser encounters a pragma token in an ATV document.
func (pragma *documentPragma) Capture(values []string) error {
match := pragmaSplitterRegex.FindStringSubmatch(values[0])
if len(match) == 0 {
return fmt.Errorf("Splitting pragma failed")
}
pragma.Name = match[1]
pragma.Value = match[2]
return nil
}
// WriteDocumentPart writes a part of the ATV document to the specified writer.
func (pragma *documentPragma) WriteDocumentPart(writer *strings.Builder, indent int) error {
line := fmt.Sprintf("%s#%s %s\n", spacer(indent), pragma.Name, pragma.Value)
_, err := writer.WriteString(line)
return err
}