Skip to content

Commit

Permalink
Updating based on feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz committed Aug 8, 2018
1 parent 6f4b117 commit 4364bde
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 42 deletions.
8 changes: 8 additions & 0 deletions internal/ini/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (k ASTKind) String() string {
return "expr"
case ASTKindStatement:
return "stmt"
case ASTKindSectionStatement:
return "section_stmt"
case ASTKindExprStatement:
return "expr_stmt"
case ASTKindCommentStatement:
Expand Down Expand Up @@ -73,10 +75,13 @@ func newASTWithRootToken(kind ASTKind, root Token, children ...AST) AST {
}
}

// AppendChild will append to the list of children an AST has.
func (a *AST) AppendChild(child AST) {
a.Children = append(a.Children, child)
}

// GetRoot will return the root AST which can be the first entry
// in the children list or a token.
func (a *AST) GetRoot() AST {
if a.RootToken {
return *a
Expand All @@ -89,6 +94,7 @@ func (a *AST) GetRoot() AST {
return a.Children[0]
}

// GetChildren will return the current AST's list of children
func (a *AST) GetChildren() []AST {
if len(a.Children) == 0 {
return []AST{}
Expand All @@ -101,6 +107,7 @@ func (a *AST) GetChildren() []AST {
return a.Children[1:]
}

// SetChildren will set and override all children of the AST.
func (a *AST) SetChildren(children []AST) {
if a.RootToken {
a.Children = children
Expand All @@ -109,4 +116,5 @@ func (a *AST) SetChildren(children []AST) {
}
}

// Start is used to indicate the starting state of the parse table.
var Start = newAST(ASTKindStart, AST{})
4 changes: 2 additions & 2 deletions internal/ini/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func BenchmarkGoINIParser(b *testing.B) {
}
}

/*func BenchmarkTokenize(b *testing.B) {
func BenchmarkTokenize(b *testing.B) {
lexer := iniLexer{}
for i := 0; i < b.N; i++ {
lexer.tokenize([]byte(section))
}
}*/
}
3 changes: 0 additions & 3 deletions internal/ini/expression.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package ini

// TODO: Since we added a new state 'ASTKindEqualExpr' we have to
// update the parser to reflect that change

// newExpression will return an expression AST.
// Expr represents an expression
//
Expand Down
1 change: 1 addition & 0 deletions internal/ini/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/internal/ini"
)

// TestFuzz is used to test for crashes and not validity of the input
func TestFuzz(t *testing.T) {
paths, err := filepath.Glob("testdata/fuzz/*")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/ini/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Parse(f io.Reader) (Sections, error) {
return v.Sections, nil
}

// ParseBytes will parse the given bytes and return the parsed sections.
func ParseBytes(b []byte) (Sections, error) {
tree, err := ParseASTBytes(b)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/ini/ini_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type iniLexer struct{}

// Tokenize will return a list of tokens during lexical analysis of the
// io.Reader.
// TODO: Change to use runes instead of bytes
func (l *iniLexer) Tokenize(r io.Reader) ([]Token, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
Expand Down Expand Up @@ -137,6 +136,7 @@ func countTokens(runes []rune) int {
return count + 1
}

// Token indicates a metadata about a given value.
type Token struct {
t TokenType
ValueType ValueType
Expand All @@ -154,10 +154,12 @@ func newToken(t TokenType, raw []rune, v ValueType) Token {
}
}

// Raw return the raw runes that were consumed
func (tok Token) Raw() []rune {
return tok.raw
}

// Type returns the token type
func (tok Token) Type() TokenType {
return tok.t
}
2 changes: 2 additions & 0 deletions internal/ini/ini_lexer_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
8 changes: 7 additions & 1 deletion internal/ini/ini_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func ParseAST(r io.Reader) ([]AST, error) {
return parse(tokens)
}

// ParseASTBytes will parse input from a byte slice using
// an LL(1) parser.
func ParseASTBytes(b []byte) ([]AST, error) {
lexer := iniLexer{}
tokens, err := lexer.tokenize(b)
Expand Down Expand Up @@ -260,9 +262,13 @@ loop:
// k will represent a SectionStatement with the children representing
// the label of the section
stmt = newSectionStatement(tok)
case ASTKindSectionStatement:
k.Root.raw = append(k.Root.raw, ' ')
k.Root.raw = append(k.Root.raw, tok.Raw()...)
stmt = k
default:
return nil, NewParseError(
fmt.Sprintf("invalid statement: expected statement: %T", k.Kind),
fmt.Sprintf("invalid statement: expected statement: %v", k.Kind),
)
}

Expand Down
2 changes: 2 additions & 0 deletions internal/ini/ini_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
4 changes: 4 additions & 0 deletions internal/ini/literal_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,17 @@ func newLitToken(b []rune) (Token, int, error) {
return token, n, err
}

// IntValue returns an integer value
func (v Value) IntValue() int64 {
return v.integer
}

// FloatValue returns a float value
func (v Value) FloatValue() float64 {
return v.decimal
}

// BoolValue returns a bool value
func (v Value) BoolValue() bool {
return v.boolean
}
Expand All @@ -276,6 +279,7 @@ func isTrimmable(r rune) bool {
return false
}

// StringValue returns the string value
func (v Value) StringValue() string {
switch v.Type {
case StringType:
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/literal_tokens_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
11 changes: 6 additions & 5 deletions internal/ini/number_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,20 @@ func (b numberHelper) String() string {
buf := bytes.Buffer{}
i := 0

if b.numberFormat == binary {
switch b.numberFormat {
case binary:
i++
buf.WriteString(strconv.Itoa(i) + ": binary format\n")
} else if b.numberFormat == octal {
case octal:
i++
buf.WriteString(strconv.Itoa(i) + ": octal format\n")
} else if b.numberFormat == hex {
case hex:
i++
buf.WriteString(strconv.Itoa(i) + ": hex format\n")
} else if b.numberFormat == exponent {
case exponent:
i++
buf.WriteString(strconv.Itoa(i) + ": exponent format\n")
} else {
default:
i++
buf.WriteString(strconv.Itoa(i) + ": integer format\n")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/number_helper_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/op_tokens_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
4 changes: 4 additions & 0 deletions internal/ini/parse_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ func NewParseError(message string) *ParseError {
}
}

// Code will return the ErrCodeParseError
func (err *ParseError) Code() string {
return ErrCodeParseError
}

// Message returns the error's message
func (err *ParseError) Message() string {
return err.msg
}

// OrigError return nothing since there will never be any
// original error.
func (err *ParseError) OrigError() error {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/ini/parse_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s *ParseStack) MarkComplete(ast AST) {
s.index++
}

// List will return the completed statements
func (s ParseStack) List() []AST {
return s.list[:s.index]
}
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/parse_stack_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/sep_tokens_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/skipper_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/testdata/valid/profile_name
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ profile foo ]
bar = baz
5 changes: 5 additions & 0 deletions internal/ini/testdata/valid/profile_name_expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"profile foo": {
"bar": "baz"
}
}
2 changes: 2 additions & 0 deletions internal/ini/value_util_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand Down
2 changes: 2 additions & 0 deletions internal/ini/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func (v *DefaultVisitor) VisitExpr(expr AST) error {
}

t.values[key] = v
default:
return NewParseError(fmt.Sprintf("unsupported expression %v", expr))
}
default:
return NewParseError(fmt.Sprintf("unsupported expression %v", expr))
Expand Down
49 changes: 19 additions & 30 deletions internal/ini/walker_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package ini

import (
Expand All @@ -12,52 +14,42 @@ func TestDataFiles(t *testing.T) {
path string
expectedParseError bool
expectedWalkError bool

expectedPath string
}{
{
path: "./testdata/valid/empty_profile",
expectedPath: "./testdata/valid/empty_profile_expected",
path: "./testdata/valid/empty_profile",
},
{
path: "./testdata/valid/array_profile",
},
{
path: "./testdata/valid/array_profile",
expectedPath: "./testdata/valid/array_profile_expected",
path: "./testdata/valid/simple_profile",
},
{
path: "./testdata/valid/simple_profile",
expectedPath: "./testdata/valid/simple_profile_expected",
path: "./testdata/valid/commented_profile",
},
{
path: "./testdata/valid/commented_profile",
expectedPath: "./testdata/valid/commented_profile_expected",
path: "./testdata/valid/sections_profile",
},
{
path: "./testdata/valid/sections_profile",
expectedPath: "./testdata/valid/sections_profile_expected",
path: "./testdata/valid/number_lhs_expr",
},
{
path: "./testdata/valid/number_lhs_expr",
expectedPath: "./testdata/valid/number_lhs_expr_expected",
path: "./testdata/valid/base_numbers_profile",
},
{
path: "./testdata/valid/base_numbers_profile",
expectedPath: "./testdata/valid/base_numbers_profile_expected",
path: "./testdata/valid/exponent_profile",
},
{
path: "./testdata/valid/exponent_profile",
expectedPath: "./testdata/valid/exponent_profile_expected",
path: "./testdata/valid/escaped_profile",
},
{
path: "./testdata/valid/escaped_profile",
expectedPath: "./testdata/valid/escaped_profile_expected",
path: "./testdata/valid/global_values_profile",
},
{
path: "./testdata/valid/global_values_profile",
expectedPath: "./testdata/valid/global_values_profile_expected",
path: "./testdata/valid/utf_8_profile",
},
{
path: "./testdata/valid/utf_8_profile",
expectedPath: "./testdata/valid/utf_8_profile_expected",
path: "./testdata/valid/profile_name",
},
{
path: "./testdata/invalid/bad_syntax_1",
Expand Down Expand Up @@ -99,15 +91,12 @@ func TestDataFiles(t *testing.T) {
t.Errorf("%d: expected error, but received none", i+1)
}

if len(c.expectedPath) == 0 {
return
}

expectedPath := c.path + "_expected"
e := map[string]interface{}{}

b, err := ioutil.ReadFile(c.expectedPath)
b, err := ioutil.ReadFile(expectedPath)
if err != nil {
t.Errorf("unexpected error opening expected file, %v", err)
return
}

err = json.Unmarshal(b, &e)
Expand Down

0 comments on commit 4364bde

Please sign in to comment.