-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
159 lines (132 loc) · 4.19 KB
/
parser.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
153
154
155
156
157
158
159
/* The MIT License (MIT)
Portions of this code Copyright (c) 2013-2015 Errplane Inc.
Copyright (c) 2015 Jesse Nelson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package metadata
import (
"fmt"
"io"
"github.com/hashicorp/go-version"
)
// Metadata represents a coookbook metadata file
type Metadata struct {
Depends []Dependency
Name string
Version version.Version
}
type Dependency struct {
Name string
Constraint version.Constraints // ... maybe look at hashi version.Constraint
}
// Parser represents a parser.
type Parser struct {
s *bufScanner
}
// NewParser returns a new instance of Parser.
func NewParser(r io.Reader) *Parser {
return &Parser{s: newBufScanner(r)}
}
// Parse parses a metadata file
func (p *Parser) Parse() (md *Metadata, err error) {
// return decl inits to nil, we need this to be something we can use
md = &Metadata{}
for {
tok, _, _ := p.scanIgnoreWhitespace()
switch tok {
case EOF:
return md, nil
case NAME:
md.Name, err = p.parseName()
if err != nil {
return md, err
}
p.unscan()
case DEPENDS:
d, err := p.parseDepends()
if err != nil {
return md, err
}
md.Depends = append(md.Depends, d)
p.unscan()
case VERSION:
v, err := p.parseVersion()
if err != nil {
return nil, err
}
md.Version = v
p.unscan()
}
}
}
func (p *Parser) parseVersion() (version.Version, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != STRING {
return version.Version{}, fmt.Errorf("found %s expected STRING at %d, %s", tok, pos, lit)
}
v, err := version.NewVersion(lit)
if err != nil {
return *v, fmt.Errorf("error parsing version at %d, %s : %s", pos, lit, err)
}
if len(v.Segments()) < 3 {
return *v, fmt.Errorf("error parsing version,not enough segments at %d, %s : %s", pos, lit, err)
}
return *v, nil
}
func (p *Parser) parseName() (string, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != STRING {
return "", fmt.Errorf("found %s expected STRING at %d, %s", tok, pos, lit)
}
return lit, nil
}
func (p *Parser) parseDepends() (Dependency, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != STRING {
return Dependency{}, fmt.Errorf("found %s expected STRING at %d, %s", tok, pos, lit)
}
d := Dependency{}
d.Name = lit
// scan forward for the expected comma if there is a dep, if no comma no dep!
tok, pos, lit = p.scanIgnoreWhitespace()
if tok != COMMA {
return d, nil
}
// scan forward for the dep
tok, pos, lit = p.scanIgnoreWhitespace()
if tok != STRING {
return d, fmt.Errorf("found %s expected constraint STRING at %d, %s", tok, pos, lit)
}
// we have a dependency here
c, err := version.NewConstraint(lit)
if err != nil {
return d, fmt.Errorf("error parsing constriant at %d, %s : %s", pos, lit, err)
}
d.Constraint = c
return d, nil
}
// scan returns the next token from the underlying scanner.
func (p *Parser) scan() (tok Token, pos Pos, lit string) { return p.s.Scan() }
// scanIgnoreWhitespace scans the next non-whitespace token.
func (p *Parser) scanIgnoreWhitespace() (tok Token, pos Pos, lit string) {
tok, pos, lit = p.scan()
if tok == WS {
tok, pos, lit = p.scan()
}
return
}
// unscan pushes the previously read token back onto the buffer.
func (p *Parser) unscan() { p.s.Unscan() }