/
node.go
84 lines (67 loc) · 1.87 KB
/
node.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Package ast contains functions and types for parsing a Booklit document into
// a Node.
package ast
import "strings"
// Node is a Booklit syntax tree.
type Node interface {
Visit(Visitor) error
}
// Visitor is implemented in order to traverse Node.
type Visitor interface {
VisitString(String) error
VisitInvoke(Invoke) error
VisitSequence(Sequence) error
VisitParagraph(Paragraph) error
VisitPreformatted(Preformatted) error
}
// String is literal text, not including linebreaks.
type String string
// Visit calls VisitString.
func (node String) Visit(visitor Visitor) error {
return visitor.VisitString(node)
}
// Location represents the location of a syntax node within the
// document.
type Location struct {
Line int
Col int
Offset int
}
// Invoke is a function call, e.g. \foo{bar}.
type Invoke struct {
Function string
Arguments []Node
Location Location
}
// Visit calls VisitInvoke.
func (node Invoke) Visit(visitor Visitor) error {
return visitor.VisitInvoke(node)
}
// Method returns a method name by splitting Function on '-' and title-casing
// each word.
func (node Invoke) Method() string {
camel := ""
for _, word := range strings.Split(node.Function, "-") {
camel += strings.Title(word)
}
return camel
}
// Sequence represents adjacent nodes typically within a single
// line.
type Sequence []Node
// Visit calls VisitSequence.
func (node Sequence) Visit(visitor Visitor) error {
return visitor.VisitSequence(node)
}
// Paragraph is a grouping of lines separated by two linebreaks.
type Paragraph []Sequence
// Visit calls VisitParagraph.
func (node Paragraph) Visit(visitor Visitor) error {
return visitor.VisitParagraph(node)
}
// Preformatted is a grouping of lines, typically parsed
type Preformatted []Sequence
// Visit calls VisitPreformatted.
func (node Preformatted) Visit(visitor Visitor) error {
return visitor.VisitPreformatted(node)
}