Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: allow spaces around attribute args
Browse files Browse the repository at this point in the history
This was already allowed in some cases. This makes that more consistent.

Change-Id: I6c12653c6fd3110bc0329fb20ae2ca0b523ec6f4
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9186
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Mar 29, 2021
1 parent 4ca1a5d commit ccca558
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions cue/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var testTokens = [...]elt{
{token.ATTRIBUTE, `@foo("",a="")`, special},
{token.ATTRIBUTE, `@foo(2,bytes,a.b=c)`, special},
{token.ATTRIBUTE, `@foo([{()}]())`, special},
{token.ATTRIBUTE, `@foo("{")`, special},

// Identifiers and basic type literals
{token.BOTTOM, "_|_", literal},
Expand Down
19 changes: 16 additions & 3 deletions internal/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"strconv"
"strings"
"unicode"

"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/literal"
Expand Down Expand Up @@ -111,6 +112,7 @@ func (a *Attr) Lookup(pos int, key string) (val string, found bool, err error) {
func ParseAttrBody(pos token.Pos, s string) (a Attr) {
i := 0
for {
i += skipSpace(s[i:])
// always scan at least one, possibly empty element.
n, err := scanAttributeElem(pos, s[i:], &a)
if err != nil {
Expand All @@ -119,6 +121,7 @@ func ParseAttrBody(pos token.Pos, s string) (a Attr) {
if i += n; i >= len(s) {
break
}
i += skipSpace(s[i:])
if s[i] != ',' {
return Attr{Err: errors.Newf(pos, "invalid attribute: expected comma")}
}
Expand All @@ -127,6 +130,15 @@ func ParseAttrBody(pos token.Pos, s string) (a Attr) {
return a
}

func skipSpace(s string) int {
for n, r := range s {
if !unicode.IsSpace(r) {
return n
}
}
return 0
}

func scanAttributeElem(pos token.Pos, s string, a *Attr) (n int, err errors.Error) {
// try CUE string
kv := keyValue{}
Expand All @@ -135,16 +147,17 @@ func scanAttributeElem(pos token.Pos, s string, a *Attr) (n int, err errors.Erro
p := strings.IndexAny(s, ",=") // ) is assumed to be stripped.
switch {
case p < 0:
kv.data = s
kv.data = strings.TrimSpace(s)
n = len(s)

default: // ','
n = p
kv.data = s[:n]
kv.data = strings.TrimSpace(s[:n])

case s[p] == '=':
kv.equal = p
offset := p + 1
offset += skipSpace(s[offset:])
var str string
if p, str, err = scanAttributeString(pos, s[offset:]); p > 0 {
n = offset + p
Expand All @@ -154,7 +167,7 @@ func scanAttributeElem(pos token.Pos, s string, a *Attr) (n int, err errors.Erro
if p = strings.IndexByte(s[offset:], ','); p >= 0 {
n = offset + p
}
kv.data = s[:n]
kv.data = strings.TrimSpace(s[:n])
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion internal/attrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func TestAttributeBody(t *testing.T) {
}, {
in: `foo,"bar",#"baz"#`,
out: "[{foo 0} {bar 0} {baz 0}]",
}, {
in: `foo,bar,baz`,
out: "[{foo 0} {bar 0} {baz 0}]",
}, {
in: `1,"map<int,string>"`,
out: "[{1 0} {map<int,string> 0}]",
}, {
in: `1, "map<int,string>"`,
out: "[{1 0} {map<int,string> 0}]",
}, {
in: `bar=str`,
out: "[{bar=str 3}]",
Expand All @@ -59,6 +68,12 @@ func TestAttributeBody(t *testing.T) {
}, {
in: `foo=1,bar="str",baz=free form`,
out: "[{foo=1 3} {bar=str 3} {baz=free form 3}]",
}, {
in: `foo=1,bar="str",baz=free form `,
out: "[{foo=1 3} {bar=str 3} {baz=free form 3}]",
}, {
in: `foo=1,bar="str" ,baz="free form "`,
out: "[{foo=1 3} {bar=str 3} {baz=free form 3}]",
}, {
in: `"""
"""`,
Expand All @@ -70,7 +85,7 @@ func TestAttributeBody(t *testing.T) {
out: "[{ 0}]",
}, {
in: "'' ,b",
err: "invalid attribute",
out: "[{ 0} {b 0}]",
}, {
in: "' ,b",
err: "not terminated",
Expand Down

0 comments on commit ccca558

Please sign in to comment.