Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/ini: allowing LHS of equal expression to contain spaces #2265

Merged
merged 1 commit into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions internal/ini/ini_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var parseTable = map[ASTKind]map[TokenType]int{
TokenOp: StatementPrimeState,
TokenLit: ValueState,
TokenSep: OpenScopeState,
TokenWS: SkipTokenState,
TokenWS: ValueState,
TokenNL: SkipState,
TokenComment: CommentState,
TokenNone: MarkCompleteState,
Expand Down Expand Up @@ -198,6 +198,7 @@ loop:
)
}

k = trimSpaces(k)
expr := newEqualExpr(k, tok)
stack.Push(expr)
case ValueState:
Expand All @@ -220,6 +221,9 @@ loop:
// assiging a value to some key
k.AppendChild(newExpression(tok))
stack.Push(newExprStatement(k))
case ASTKindExpr:
k.Root.raw = append(k.Root.raw, tok.Raw()...)
stack.Push(k)
case ASTKindExprStatement:
root := k.GetRoot()
children := root.GetChildren()
Expand Down Expand Up @@ -254,26 +258,7 @@ loop:
return nil, NewParseError("expected ']'")
}

// trim left hand side of spaces
for i := 0; i < len(k.Root.raw); i++ {
if !isWhitespace(k.Root.raw[i]) {
break
}

k.Root.raw = k.Root.raw[1:]
i--
}

// trim right hand side of spaces
for i := len(k.Root.raw) - 1; i > 0; i-- {
if !isWhitespace(k.Root.raw[i]) {
break
}

k.Root.raw = k.Root.raw[:len(k.Root.raw)-1]
i--
}

k = trimSpaces(k)
stack.Push(newCompletedSectionStatement(k))
case SectionState:
var stmt AST
Expand Down Expand Up @@ -335,3 +320,29 @@ loop:
// returns a sublist which exludes the start symbol
return stack.List(), nil
}

// trimSpaces will trim spaces on the left and right hand side of
// the literal.
func trimSpaces(k AST) AST {
// trim left hand side of spaces
for i := 0; i < len(k.Root.raw); i++ {
if !isWhitespace(k.Root.raw[i]) {
break
}

k.Root.raw = k.Root.raw[1:]
i--
}

// trim right hand side of spaces
for i := len(k.Root.raw) - 1; i > 0; i-- {
if !isWhitespace(k.Root.raw[i]) {
break
}

k.Root.raw = k.Root.raw[:len(k.Root.raw)-1]
i--
}

return k
}
2 changes: 2 additions & 0 deletions internal/ini/testdata/valid/space_lhs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ hyphen-profile-name ]
aws region = "foo-region"
5 changes: 5 additions & 0 deletions internal/ini/testdata/valid/space_lhs_expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hyphen-profile-name": {
"aws region": "foo-region"
}
}