Skip to content

Commit ad4b85d

Browse files
authored
allowing LHS of equal expr to contain spaces (#237)
1 parent da966e8 commit ad4b85d

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

internal/ini/ini_parser.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var parseTable = map[ASTKind]map[TokenType]int{
5757
TokenOp: StatementPrimeState,
5858
TokenLit: ValueState,
5959
TokenSep: OpenScopeState,
60-
TokenWS: SkipTokenState,
60+
TokenWS: ValueState,
6161
TokenNL: SkipState,
6262
TokenComment: CommentState,
6363
TokenNone: MarkCompleteState,
@@ -198,6 +198,7 @@ loop:
198198
)
199199
}
200200

201+
k = trimSpaces(k)
201202
expr := newEqualExpr(k, tok)
202203
stack.Push(expr)
203204
case ValueState:
@@ -220,6 +221,9 @@ loop:
220221
// assiging a value to some key
221222
k.AppendChild(newExpression(tok))
222223
stack.Push(newExprStatement(k))
224+
case ASTKindExpr:
225+
k.Root.raw = append(k.Root.raw, tok.Raw()...)
226+
stack.Push(k)
223227
case ASTKindExprStatement:
224228
root := k.GetRoot()
225229
children := root.GetChildren()
@@ -254,26 +258,7 @@ loop:
254258
return nil, NewParseError("expected ']'")
255259
}
256260

257-
// trim left hand side of spaces
258-
for i := 0; i < len(k.Root.raw); i++ {
259-
if !isWhitespace(k.Root.raw[i]) {
260-
break
261-
}
262-
263-
k.Root.raw = k.Root.raw[1:]
264-
i--
265-
}
266-
267-
// trim right hand side of spaces
268-
for i := len(k.Root.raw) - 1; i > 0; i-- {
269-
if !isWhitespace(k.Root.raw[i]) {
270-
break
271-
}
272-
273-
k.Root.raw = k.Root.raw[:len(k.Root.raw)-1]
274-
i--
275-
}
276-
261+
k = trimSpaces(k)
277262
stack.Push(newCompletedSectionStatement(k))
278263
case SectionState:
279264
var stmt AST
@@ -335,3 +320,29 @@ loop:
335320
// returns a sublist which exludes the start symbol
336321
return stack.List(), nil
337322
}
323+
324+
// trimSpaces will trim spaces on the left and right hand side of
325+
// the literal.
326+
func trimSpaces(k AST) AST {
327+
// trim left hand side of spaces
328+
for i := 0; i < len(k.Root.raw); i++ {
329+
if !isWhitespace(k.Root.raw[i]) {
330+
break
331+
}
332+
333+
k.Root.raw = k.Root.raw[1:]
334+
i--
335+
}
336+
337+
// trim right hand side of spaces
338+
for i := len(k.Root.raw) - 1; i > 0; i-- {
339+
if !isWhitespace(k.Root.raw[i]) {
340+
break
341+
}
342+
343+
k.Root.raw = k.Root.raw[:len(k.Root.raw)-1]
344+
i--
345+
}
346+
347+
return k
348+
}

internal/ini/testdata/valid/space_lhs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[ hyphen-profile-name ]
2+
aws region = "foo-region"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"hyphen-profile-name": {
3+
"aws region": "foo-region"
4+
}
5+
}

internal/ini/visitor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ func (t Section) ValueType(k string) (ValueType, bool) {
141141
return v.Type, ok
142142
}
143143

144+
// Bool returns a bool value at k
145+
func (t Section) Bool(k string) bool {
146+
return t.values[k].BoolValue()
147+
}
148+
144149
// Int returns an integer value at k
145150
func (t Section) Int(k string) int64 {
146151
return t.values[k].IntValue()

0 commit comments

Comments
 (0)