Skip to content

Commit

Permalink
syncing to v1's internal ini package (aws#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz authored Oct 31, 2018
1 parent a7b51e7 commit 27ba331
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 26 deletions.
Binary file removed internal/ini/.DS_Store
Binary file not shown.
7 changes: 0 additions & 7 deletions internal/ini/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ini

import (
oldini "github.com/go-ini/ini"
"testing"
)

Expand All @@ -26,12 +25,6 @@ func BenchmarkINIParser(b *testing.B) {
}
}

func BenchmarkGoINIParser(b *testing.B) {
for i := 0; i < b.N; i++ {
oldini.Load([]byte(section))
}
}

func BenchmarkTokenize(b *testing.B) {
lexer := iniLexer{}
for i := 0; i < b.N; i++ {
Expand Down
4 changes: 0 additions & 4 deletions internal/ini/comment_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ func isComment(b []rune) bool {
return true
case '#':
return true
case '/':
if len(b) > 1 {
return b[1] == '/'
}
}

return false
Expand Down
3 changes: 1 addition & 2 deletions internal/ini/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
//
// SkipState will skip (NL WS)+
//
// comment -> # comment' | ; comment' | / comment_slash
// comment_slash -> / comment'
// comment -> # comment' | ; comment'
// comment' -> epsilon | value
package ini
9 changes: 7 additions & 2 deletions internal/ini/ini_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const (
// SkipTokenState will skip any token and push the previous
// state onto the stack.
SkipTokenState
// comment -> # comment' | ; comment' | / comment_slash
// comment_slash -> / comment'
// comment -> # comment' | ; comment'
// comment' -> MarkComplete | value
CommentState
// MarkComplete state will complete statements and move that
Expand Down Expand Up @@ -157,6 +156,12 @@ loop:

step := parseTable[k.Kind][tok.Type()]
if s.ShouldSkip(tok) {
// being in a skip state with no tokens will break out of
// the parse loop since there is nothing left to process.
if len(tokens) == 0 {
break loop
}

step = SkipTokenState
}

Expand Down
21 changes: 16 additions & 5 deletions internal/ini/ini_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func TestParser(t *testing.T) {
xID, _, _ := newLitToken([]rune("x = 1234"))
s3ID, _, _ := newLitToken([]rune("s3 = 1234"))
fooSlashes, _, _ := newLitToken([]rune("//foo"))

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

fooSlashesExpr := newExpression(fooSlashes)

xEQ1234 := newEqualExpr(newExpression(xID), equalOp)
xEQ1234.AppendChild(newExpression(numLit))
xEQColon1234 := newEqualExpr(newExpression(xID), equalColonOp)
Expand Down Expand Up @@ -101,24 +104,32 @@ func TestParser(t *testing.T) {
},
},
{
name: "// comment",
r: bytes.NewBuffer([]byte(`// foo`)),
name: "// not a comment",
r: bytes.NewBuffer([]byte(`//foo`)),
expectedStack: []AST{
newCommentStatement(newToken(TokenComment, []rune("// foo"), NoneType)),
fooSlashesExpr,
},
},
{
name: "multiple comments",
r: bytes.NewBuffer([]byte(`;foo
//bar
# baz
`)),
expectedStack: []AST{
newCommentStatement(newToken(TokenComment, []rune(";foo"), NoneType)),
newCommentStatement(newToken(TokenComment, []rune("//bar"), NoneType)),
newCommentStatement(newToken(TokenComment, []rune("# baz"), NoneType)),
},
},
{
name: "comment followed by skip state",
r: bytes.NewBuffer([]byte(`;foo
//foo
# baz
`)),
expectedStack: []AST{
newCommentStatement(newToken(TokenComment, []rune(";foo"), NoneType)),
},
},
{
name: "assignment",
r: bytes.NewBuffer([]byte(`x = 1234`)),
Expand Down
9 changes: 6 additions & 3 deletions internal/ini/skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package ini
// files. See example below
//
// [ foo ]
// nested = // this section will be skipped
// nested = ; this section will be skipped
// a=b
// c=d
// bar=baz // this will be included
// bar=baz ; this will be included
type skipper struct {
shouldSkip bool
TokenSet bool
Expand All @@ -22,7 +22,10 @@ func newSkipper() skipper {
}

func (s *skipper) ShouldSkip(tok Token) bool {
if s.shouldSkip && s.prevTok.Type() == TokenNL && tok.Type() != TokenWS {
if s.shouldSkip &&
s.prevTok.Type() == TokenNL &&
tok.Type() != TokenWS {

s.Continue()
return false
}
Expand Down
5 changes: 2 additions & 3 deletions internal/ini/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ func newExprStatement(ast AST) AST {
// CommentStatement represents a comment in the ini defintion.
//
// grammar:
// comment -> #comment' | ;comment' | /comment_slash
// comment_slash -> /comment'
// comment' -> value
// comment -> #comment' | ;comment'
// comment' -> epsilon | value
func newCommentStatement(tok Token) AST {
return newAST(ASTKindCommentStatement, newExpression(tok))
}
Expand Down
6 changes: 6 additions & 0 deletions internal/ini/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ type Section struct {
values values
}

// Has will return whether or not an entry exists in a given section
func (t Section) Has(k string) bool {
_, ok := t.values[k]
return ok
}

// ValueType will returned what type the union is set to. If
// k was not found, the NoneType will be returned.
func (t Section) ValueType(k string) (ValueType, bool) {
Expand Down

0 comments on commit 27ba331

Please sign in to comment.