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

Commit

Permalink
cue/ast: allow parentheses as labels
Browse files Browse the repository at this point in the history
changed:
- parser
- formatter
- compiler
- resolver

Change-Id: I911e22859ad696acbb37bc829154d0294761d5cf
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9323
Reviewed-by: Paul Jolly <[email protected]>
Reviewed-by: CUE cueckoo <[email protected]>
  • Loading branch information
mpvl committed Apr 8, 2021
1 parent 5c2b281 commit 460357b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ type ParenExpr struct {

comments
expr
label
}

// A SelectorExpr node represents an expression followed by a selector.
Expand Down
3 changes: 3 additions & 0 deletions cue/ast/astutil/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ func (s *scope) Before(n ast.Node) (w visitor) {
}

switch label := n.(type) {
case *ast.ParenExpr:
walk(s, label)

case *ast.Interpolation:
walk(s, label)

Expand Down
3 changes: 3 additions & 0 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ func (f *formatter) label(l ast.Label, optional bool) {
case *ast.ListLit:
f.expr(n)

case *ast.ParenExpr:
f.expr(n)

case *ast.Interpolation:
f.expr(n)

Expand Down
2 changes: 2 additions & 0 deletions cue/format/testdata/simplify.golden
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ quux: 5
// Issue #294
"\("x")": "x"

(x): "foo"

a :: {
foo: 2
...
Expand Down
2 changes: 2 additions & 0 deletions cue/format/testdata/simplify.input
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ a:
// Issue #294
"\("x")": "x"

(x): "foo"

a :: {
[string]: _
foo: 2
Expand Down
5 changes: 3 additions & 2 deletions cue/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@ func (p *parser) parseField() (decl ast.Decl) {
return a
}
switch tok {
case token.IDENT, token.LBRACK, token.STRING, token.INTERPOLATION,
case token.IDENT, token.LBRACK, token.LPAREN,
token.STRING, token.INTERPOLATION,
token.NULL, token.TRUE, token.FALSE,
token.FOR, token.IF, token.LET, token.IN:
return &ast.EmbedDecl{Expr: expr}
Expand Down Expand Up @@ -1011,7 +1012,7 @@ func (p *parser) parseLabel(rhs bool) (label ast.Label, expr ast.Expr, decl ast.
}
expr = ident

case token.IDENT, token.STRING, token.INTERPOLATION,
case token.IDENT, token.STRING, token.INTERPOLATION, token.LPAREN,
token.NULL, token.TRUE, token.FALSE, token.IN:
expr = p.parseExpr()

Expand Down
10 changes: 10 additions & 0 deletions cue/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ func TestParse(t *testing.T) {
a: { a: 1 }
}`,
"{[foo=_]: {a: int}, a: {a: 1}}",
}, {
"dynamic labels",
`{
(x): a: int
x: "foo"
a: {
(a.b)
}
}`,
`{(x): {a: int}, x: "foo", a: {(a.b)}}`,
}, {
"foo",
`[
Expand Down
31 changes: 31 additions & 0 deletions cue/testdata/eval/dynamic_field.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ c: X
withError: {
issue799: {key: int32} & {"\(1&2)": 123}
}

parenExprRefParent: {
a: (x): {}
x: "foo"
}
parenExprRefEqual: {
(x): {}
x: "foo"
}
-- out/eval --
Errors:
invalid interpolation: conflicting values 2 and 1:
Expand All @@ -34,6 +43,18 @@ Result:
key: (int){ &(>=-2147483648, <=2147483647, int) }
}
}
parenExprRefParent: (struct){
a: (struct){
foo: (struct){
}
}
x: (string){ "foo" }
}
parenExprRefEqual: (struct){
x: (string){ "foo" }
foo: (struct){
}
}
foo: (struct){
b: (struct){
c: (struct){
Expand Down Expand Up @@ -69,4 +90,14 @@ Result:
"\((1 & 2))": 123
})
}
parenExprRefParent: {
a: {
〈1;x〉: {}
}
x: "foo"
}
parenExprRefEqual: {
〈0;x〉: {}
x: "foo"
}
}
4 changes: 2 additions & 2 deletions internal/core/adt/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func labelFromValue(c *OpContext, src Expr, v Value) Feature {
}
switch v.Kind() {
case IntKind, NumKind:
x, _ := v.(*Num)
x, _ := Unwrap(v).(*Num)
if x == nil {
c.addErrf(IncompleteError, pos(v), msgGround, v, "int")
return InvalidLabel
Expand Down Expand Up @@ -213,7 +213,7 @@ func labelFromValue(c *OpContext, src Expr, v Value) Feature {
}

case StringKind:
x, _ := v.(*String)
x, _ := Unwrap(v).(*String)
if x == nil {
c.addErrf(IncompleteError, pos(v), msgGround, v, "string")
return InvalidLabel
Expand Down
12 changes: 11 additions & 1 deletion internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,17 @@ func (c *compiler) decl(d ast.Decl) adt.Decl {
Label: label,
}

case *ast.Interpolation: // *ast.ParenExpr,
case *ast.ParenExpr:
if x.Token == token.ISA {
c.errf(x, "definitions not supported for dynamic fields")
}
return &adt.DynamicField{
Src: x,
Key: c.expr(l),
Value: value,
}

case *ast.Interpolation:
if x.Token == token.ISA {
c.errf(x, "definitions not supported for interpolations")
}
Expand Down

0 comments on commit 460357b

Please sign in to comment.