Skip to content

Commit 27ba331

Browse files
authored
syncing to v1's internal ini package (#234)
1 parent a7b51e7 commit 27ba331

File tree

9 files changed

+38
-26
lines changed

9 files changed

+38
-26
lines changed

internal/ini/.DS_Store

-10 KB
Binary file not shown.

internal/ini/bench_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ini
22

33
import (
4-
oldini "github.com/go-ini/ini"
54
"testing"
65
)
76

@@ -26,12 +25,6 @@ func BenchmarkINIParser(b *testing.B) {
2625
}
2726
}
2827

29-
func BenchmarkGoINIParser(b *testing.B) {
30-
for i := 0; i < b.N; i++ {
31-
oldini.Load([]byte(section))
32-
}
33-
}
34-
3528
func BenchmarkTokenize(b *testing.B) {
3629
lexer := iniLexer{}
3730
for i := 0; i < b.N; i++ {

internal/ini/comment_token.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ func isComment(b []rune) bool {
1212
return true
1313
case '#':
1414
return true
15-
case '/':
16-
if len(b) > 1 {
17-
return b[1] == '/'
18-
}
1915
}
2016

2117
return false

internal/ini/doc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
//
2525
// SkipState will skip (NL WS)+
2626
//
27-
// comment -> # comment' | ; comment' | / comment_slash
28-
// comment_slash -> / comment'
27+
// comment -> # comment' | ; comment'
2928
// comment' -> epsilon | value
3029
package ini

internal/ini/ini_parser.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ const (
2525
// SkipTokenState will skip any token and push the previous
2626
// state onto the stack.
2727
SkipTokenState
28-
// comment -> # comment' | ; comment' | / comment_slash
29-
// comment_slash -> / comment'
28+
// comment -> # comment' | ; comment'
3029
// comment' -> MarkComplete | value
3130
CommentState
3231
// MarkComplete state will complete statements and move that
@@ -157,6 +156,12 @@ loop:
157156

158157
step := parseTable[k.Kind][tok.Type()]
159158
if s.ShouldSkip(tok) {
159+
// being in a skip state with no tokens will break out of
160+
// the parse loop since there is nothing left to process.
161+
if len(tokens) == 0 {
162+
break loop
163+
}
164+
160165
step = SkipTokenState
161166
}
162167

internal/ini/ini_parser_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func TestParser(t *testing.T) {
1414
xID, _, _ := newLitToken([]rune("x = 1234"))
1515
s3ID, _, _ := newLitToken([]rune("s3 = 1234"))
16+
fooSlashes, _, _ := newLitToken([]rune("//foo"))
1617

1718
regionID, _, _ := newLitToken([]rune("region"))
1819
regionLit, _, _ := newLitToken([]rune(`"us-west-2"`))
@@ -33,6 +34,8 @@ func TestParser(t *testing.T) {
3334
defaultProfileStmt := newSectionStatement(defaultID)
3435
assumeProfileStmt := newSectionStatement(assumeID)
3536

37+
fooSlashesExpr := newExpression(fooSlashes)
38+
3639
xEQ1234 := newEqualExpr(newExpression(xID), equalOp)
3740
xEQ1234.AppendChild(newExpression(numLit))
3841
xEQColon1234 := newEqualExpr(newExpression(xID), equalColonOp)
@@ -101,24 +104,32 @@ func TestParser(t *testing.T) {
101104
},
102105
},
103106
{
104-
name: "// comment",
105-
r: bytes.NewBuffer([]byte(`// foo`)),
107+
name: "// not a comment",
108+
r: bytes.NewBuffer([]byte(`//foo`)),
106109
expectedStack: []AST{
107-
newCommentStatement(newToken(TokenComment, []rune("// foo"), NoneType)),
110+
fooSlashesExpr,
108111
},
109112
},
110113
{
111114
name: "multiple comments",
112115
r: bytes.NewBuffer([]byte(`;foo
113-
//bar
114116
# baz
115117
`)),
116118
expectedStack: []AST{
117119
newCommentStatement(newToken(TokenComment, []rune(";foo"), NoneType)),
118-
newCommentStatement(newToken(TokenComment, []rune("//bar"), NoneType)),
119120
newCommentStatement(newToken(TokenComment, []rune("# baz"), NoneType)),
120121
},
121122
},
123+
{
124+
name: "comment followed by skip state",
125+
r: bytes.NewBuffer([]byte(`;foo
126+
//foo
127+
# baz
128+
`)),
129+
expectedStack: []AST{
130+
newCommentStatement(newToken(TokenComment, []rune(";foo"), NoneType)),
131+
},
132+
},
122133
{
123134
name: "assignment",
124135
r: bytes.NewBuffer([]byte(`x = 1234`)),

internal/ini/skipper.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package ini
55
// files. See example below
66
//
77
// [ foo ]
8-
// nested = // this section will be skipped
8+
// nested = ; this section will be skipped
99
// a=b
1010
// c=d
11-
// bar=baz // this will be included
11+
// bar=baz ; this will be included
1212
type skipper struct {
1313
shouldSkip bool
1414
TokenSet bool
@@ -22,7 +22,10 @@ func newSkipper() skipper {
2222
}
2323

2424
func (s *skipper) ShouldSkip(tok Token) bool {
25-
if s.shouldSkip && s.prevTok.Type() == TokenNL && tok.Type() != TokenWS {
25+
if s.shouldSkip &&
26+
s.prevTok.Type() == TokenNL &&
27+
tok.Type() != TokenWS {
28+
2629
s.Continue()
2730
return false
2831
}

internal/ini/statement.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ func newExprStatement(ast AST) AST {
1818
// CommentStatement represents a comment in the ini defintion.
1919
//
2020
// grammar:
21-
// comment -> #comment' | ;comment' | /comment_slash
22-
// comment_slash -> /comment'
23-
// comment' -> value
21+
// comment -> #comment' | ;comment'
22+
// comment' -> epsilon | value
2423
func newCommentStatement(tok Token) AST {
2524
return newAST(ASTKindCommentStatement, newExpression(tok))
2625
}

internal/ini/visitor.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ type Section struct {
128128
values values
129129
}
130130

131+
// Has will return whether or not an entry exists in a given section
132+
func (t Section) Has(k string) bool {
133+
_, ok := t.values[k]
134+
return ok
135+
}
136+
131137
// ValueType will returned what type the union is set to. If
132138
// k was not found, the NoneType will be returned.
133139
func (t Section) ValueType(k string) (ValueType, bool) {

0 commit comments

Comments
 (0)