-
Notifications
You must be signed in to change notification settings - Fork 137
/
source.go
44 lines (35 loc) · 968 Bytes
/
source.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
package template
import (
"bytes"
"github.com/k14s/ytt/pkg/filepos"
)
type TemplateLine struct {
Instruction Instruction
SourceLine *SourceLine
}
type SourceLine struct {
Position *filepos.Position
Content string
}
func NewCodeFromBytes(bs []byte, instructions *InstructionSet) []TemplateLine {
return NewCodeFromBytesAtPosition(bs, filepos.NewPosition(1), instructions)
}
func NewCodeFromBytesAtPosition(bs []byte, pos *filepos.Position, instructions *InstructionSet) []TemplateLine {
var result []TemplateLine
for i, line := range bytes.Split(bs, []byte("\n")) {
result = append(result, TemplateLine{
Instruction: instructions.NewCode(string(line)),
SourceLine: &SourceLine{
Position: filepos.NewPosition(pos.Line() + i),
Content: string(line),
},
})
}
return result
}
func (l *TemplateLine) Position() *filepos.Position {
if l.SourceLine != nil {
return l.SourceLine.Position
}
return filepos.NewUnknownPosition()
}