-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse.go
152 lines (122 loc) · 3.29 KB
/
parse.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package tmpl
import (
"bytes"
"go/token"
"io"
"os"
"strings"
"text/scanner"
"text/template/parse"
)
type Template struct {
Filename string
Trees map[string]*parse.Tree
Inspector *Inspector
// GoFilePos holds the position from which .go file the template originates
GoFilePos token.Position
// OffsetLookup holds the first position of all line starts.
OffsetLookup []token.Position
// Comments holds the comments of each line number
Comments map[int][]string
}
func Parse(filepath string) (*Template, error) {
src, errF := os.ReadFile(filepath)
if errF != nil {
return nil, errF
}
return ParseBytes(filepath, src)
}
func ParseString(name, content string) (*Template, error) {
return ParseBytes(name, []byte(content))
}
func ParseBytes(name string, src []byte) (*Template, error) {
t := &Template{
Filename: name,
Trees: make(map[string]*parse.Tree),
Comments: make(map[int][]string),
Inspector: nil,
}
tree := &parse.Tree{
Name: name,
Mode: parse.ParseComments | parse.SkipFuncCheck,
}
_, err := tree.Parse(string(src), "{{", "}}", t.Trees, map[string]any{})
if err != nil {
return nil, err
}
roodNotes := make([]parse.Node, 0, len(t.Trees))
for _, tree := range t.Trees {
roodNotes = append(roodNotes, tree.Root)
}
t.OffsetLookup = extractLineInfos(name, bytes.NewReader(src))
t.Inspector = newInspector(roodNotes)
return t, nil
}
func extractLineInfos(filename string, src io.Reader) []token.Position {
infos := make([]token.Position, 0, 50)
infos = append(infos, token.Position{Filename: filename, Offset: 0, Line: 1, Column: 1})
var s scanner.Scanner
s.Filename = filename
s.Init(src)
s.Whitespace ^= 1<<'\t' | 1<<'\n'
s.Mode ^= scanner.SkipComments
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch tok {
case '\n':
p := s.Pos()
tokPos := token.Position{
Filename: p.Filename,
Offset: p.Offset,
Line: p.Line,
Column: p.Column,
}
infos = append(infos, tokPos)
}
}
return infos
}
func (t *Template) ExtractComments() {
t.Inspector.Nodes([]parse.Node{&parse.CommentNode{}}, func(rawNode parse.Node, push bool) (proceed bool) {
proceed = false
node := rawNode.(*parse.CommentNode)
comment := strings.TrimSpace(node.Text)
comment = strings.TrimPrefix(comment, "/*")
comment = strings.TrimSuffix(comment, "*/")
comment = strings.TrimSpace(comment)
pos := t.Position(node.Pos)
t.Comments[pos.Line] = append(t.Comments[pos.Line], comment)
return
})
}
func (t *Template) Position(offset parse.Pos) token.Position {
var pos token.Position
pos.Filename = t.Filename
for i, p := range t.OffsetLookup {
if p.Offset > int(offset) {
pos = t.OffsetLookup[i-1]
break
}
}
if pos.IsValid() {
pos.Column = int(offset) - pos.Offset
pos.Offset += int(offset)
}
if t.GoFilePos.IsValid() {
pos.Filename = t.GoFilePos.Filename
pos.Line += t.GoFilePos.Line - 1
pos.Column += t.GoFilePos.Column - 1
pos.Offset += t.GoFilePos.Offset
}
return pos
}
func (t *Template) GetComments(offset parse.Pos) []string {
pos := t.Position(offset)
var comments []string
if _, ok := t.Comments[pos.Line]; ok {
comments = append(comments, t.Comments[pos.Line]...)
}
if _, ok := t.Comments[pos.Line-1]; ok {
comments = append(comments, t.Comments[pos.Line-1]...)
}
return comments
}