-
Notifications
You must be signed in to change notification settings - Fork 39
/
variable.go
62 lines (48 loc) · 1.37 KB
/
variable.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
package gcss
import (
"fmt"
"io"
"strings"
)
// variable represents a GCSS variable.
type variable struct {
elementBase
name string
value string
}
// WriteTo writes the variable to the writer.
func (v *variable) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write([]byte(v.value))
return int64(n), err
}
// variableNV extracts a variable name and value
// from the line.
func variableNV(ln *line) (string, string, error) {
s := strings.TrimSpace(ln.s)
if !strings.HasPrefix(s, dollarMark) {
return "", "", fmt.Errorf("variable must start with %q [line: %d]", dollarMark, ln.no)
}
nv := strings.SplitN(s, space, 2)
if len(nv) < 2 {
return "", "", fmt.Errorf("variable's name and value should be divided by a space [line: %d]", ln.no)
}
if !strings.HasSuffix(nv[0], colon) {
return "", "", fmt.Errorf("variable's name should end with a colon [line: %d]", ln.no)
}
return strings.TrimSuffix(strings.TrimPrefix(nv[0], dollarMark), colon), nv[1], nil
}
// newVariable creates and returns a variable.
func newVariable(ln *line, parent element) (*variable, error) {
name, value, err := variableNV(ln)
if err != nil {
return nil, err
}
if strings.HasSuffix(value, semicolon) {
return nil, fmt.Errorf("variable must not end with %q", semicolon)
}
return &variable{
elementBase: newElementBase(ln, parent),
name: name,
value: value,
}, nil
}