1 | package gcss
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "fmt"
|
---|
5 | "io"
|
---|
6 | "strings"
|
---|
7 | )
|
---|
8 |
|
---|
9 | // mixinDeclaration represents a mixin declaration.
|
---|
10 | type mixinDeclaration struct {
|
---|
11 | elementBase
|
---|
12 | name string
|
---|
13 | paramNames []string
|
---|
14 | }
|
---|
15 |
|
---|
16 | // WriteTo writes the selector to the writer.
|
---|
17 | func (md *mixinDeclaration) WriteTo(w io.Writer) (int64, error) {
|
---|
18 | return 0, nil
|
---|
19 | }
|
---|
20 |
|
---|
21 | // mixinNP extracts a mixin name and parameters from the line.
|
---|
22 | func mixinNP(ln *line, isDeclaration bool) (string, []string, error) {
|
---|
23 | s := strings.TrimSpace(ln.s)
|
---|
24 |
|
---|
25 | if !strings.HasPrefix(s, dollarMark) {
|
---|
26 | return "", nil, fmt.Errorf("mixin must start with %q [line: %d]", dollarMark, ln.no)
|
---|
27 | }
|
---|
28 |
|
---|
29 | s = strings.TrimPrefix(s, dollarMark)
|
---|
30 |
|
---|
31 | np := strings.Split(s, openParenthesis)
|
---|
32 |
|
---|
33 | if len(np) != 2 {
|
---|
34 | return "", nil, fmt.Errorf("mixin's format is invalid [line: %d]", ln.no)
|
---|
35 | }
|
---|
36 |
|
---|
37 | paramsS := strings.TrimSpace(np[1])
|
---|
38 |
|
---|
39 | if !strings.HasSuffix(paramsS, closeParenthesis) {
|
---|
40 | return "", nil, fmt.Errorf("mixin must end with %q [line: %d]", closeParenthesis, ln.no)
|
---|
41 | }
|
---|
42 |
|
---|
43 | paramsS = strings.TrimSuffix(paramsS, closeParenthesis)
|
---|
44 |
|
---|
45 | if strings.Index(paramsS, closeParenthesis) != -1 {
|
---|
46 | return "", nil, fmt.Errorf("mixin's format is invalid [line: %d]", ln.no)
|
---|
47 | }
|
---|
48 |
|
---|
49 | var params []string
|
---|
50 |
|
---|
51 | if paramsS != "" {
|
---|
52 | params = strings.Split(paramsS, comma)
|
---|
53 | }
|
---|
54 |
|
---|
55 | for i, p := range params {
|
---|
56 | p = strings.TrimSpace(p)
|
---|
57 |
|
---|
58 | if isDeclaration {
|
---|
59 | if !strings.HasPrefix(p, dollarMark) {
|
---|
60 | return "", nil, fmt.Errorf("mixin's parameter must start with %q [line: %d]", dollarMark, ln.no)
|
---|
61 | }
|
---|
62 |
|
---|
63 | p = strings.TrimPrefix(p, dollarMark)
|
---|
64 | }
|
---|
65 |
|
---|
66 | params[i] = p
|
---|
67 | }
|
---|
68 |
|
---|
69 | return np[0], params, nil
|
---|
70 | }
|
---|
71 |
|
---|
72 | // newMixinDeclaration creates and returns a mixin declaration.
|
---|
73 | func newMixinDeclaration(ln *line, parent element) (*mixinDeclaration, error) {
|
---|
74 | name, paramNames, err := mixinNP(ln, true)
|
---|
75 |
|
---|
76 | if err != nil {
|
---|
77 | return nil, err
|
---|
78 | }
|
---|
79 |
|
---|
80 | return &mixinDeclaration{
|
---|
81 | elementBase: newElementBase(ln, parent),
|
---|
82 | name: name,
|
---|
83 | paramNames: paramNames,
|
---|
84 | }, nil
|
---|
85 | }
|
---|