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

Support expressions in string template #3662

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
49 changes: 49 additions & 0 deletions interpreter/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12509,4 +12509,53 @@ func TestInterpretStringTemplates(t *testing.T) {
inter.Globals.Get("x").GetValue(inter),
)
})

t.Run("func", func(t *testing.T) {
t.Parallel()

inter := parseCheckAndInterpret(t, `
let add = fun(): Int {
return 2+2
}
let x: String = "\(add())"
`)

AssertValuesEqual(
t,
inter,
interpreter.NewUnmeteredStringValue("4"),
inter.Globals.Get("x").GetValue(inter),
)
})

t.Run("ternary", func(t *testing.T) {
t.Parallel()

inter := parseCheckAndInterpret(t, `
let z = false
let x: String = "\(z ? "foo" : "bar" )"
`)

AssertValuesEqual(
t,
inter,
interpreter.NewUnmeteredStringValue("bar"),
inter.Globals.Get("x").GetValue(inter),
)
})

t.Run("nested", func(t *testing.T) {
t.Parallel()

inter := parseCheckAndInterpret(t, `
let x: String = "\(2*(4-2) + 1 == 5)"
`)

AssertValuesEqual(
t,
inter,
interpreter.NewUnmeteredStringValue("true"),
inter.Globals.Get("x").GetValue(inter),
)
})
}
4 changes: 0 additions & 4 deletions parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,10 +1191,6 @@ func defineStringExpression() {
if err != nil {
return nil, err
}
// limit string templates to identifiers only
if _, ok := value.(*ast.IdentifierExpression); !ok {
return nil, p.syntaxError("expected identifier got: %s", value.String())
}
_, err = p.mustOne(lexer.TokenParenClose)
if err != nil {
return nil, err
Expand Down
82 changes: 65 additions & 17 deletions parser/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6185,7 +6185,7 @@ func TestParseStringTemplate(t *testing.T) {
)
})

t.Run("invalid, num", func(t *testing.T) {
t.Run("valid, num", func(t *testing.T) {

t.Parallel()

Expand All @@ -6200,16 +6200,7 @@ func TestParseStringTemplate(t *testing.T) {
}
}

require.Error(t, err)
AssertEqualWithDiff(t,
[]error{
&SyntaxError{
Message: "expected identifier got: 2 + 2",
Pos: ast.Position{Offset: 13, Line: 2, Column: 12},
},
},
errs,
)
require.NoError(t, err)
})

t.Run("valid, nested identifier", func(t *testing.T) {
Expand Down Expand Up @@ -6278,7 +6269,7 @@ func TestParseStringTemplate(t *testing.T) {
)
})

t.Run("invalid, function identifier", func(t *testing.T) {
t.Run("valid, function identifier", func(t *testing.T) {

t.Parallel()

Expand All @@ -6293,24 +6284,42 @@ func TestParseStringTemplate(t *testing.T) {
}
}

require.NoError(t, err)
})

t.Run("invalid, missing paren", func(t *testing.T) {

t.Parallel()

_, errs := testParseExpression(`
"\(add"
`)

var err error
if len(errs) > 0 {
err = Error{
Errors: errs,
}
}

require.Error(t, err)
AssertEqualWithDiff(t,
[]error{
&SyntaxError{
Message: "expected identifier got: add()",
Pos: ast.Position{Offset: 12, Line: 2, Column: 11},
Message: "expected token ')'",
Pos: ast.Position{Offset: 10, Line: 2, Column: 9},
},
},
errs,
)
})

t.Run("invalid, unbalanced paren", func(t *testing.T) {
t.Run("invalid, nested expression paren", func(t *testing.T) {

t.Parallel()

_, errs := testParseExpression(`
"\(add"
"\((2+2)/2()"
`)

var err error
Expand All @@ -6325,7 +6334,7 @@ func TestParseStringTemplate(t *testing.T) {
[]error{
&SyntaxError{
Message: "expected token ')'",
Pos: ast.Position{Offset: 10, Line: 2, Column: 9},
Pos: ast.Position{Offset: 16, Line: 2, Column: 15},
},
},
errs,
Expand Down Expand Up @@ -6496,6 +6505,45 @@ func TestParseStringTemplate(t *testing.T) {

AssertEqualWithDiff(t, expected, actual)
})

t.Run("valid, extra closing paren", func(t *testing.T) {

t.Parallel()

actual, errs := testParseExpression(`
"\(a))"
`)

var err error
if len(errs) > 0 {
err = Error{
Errors: errs,
}
}

require.NoError(t, err)

expected := &ast.StringTemplateExpression{
Values: []string{
"",
")",
},
Expressions: []ast.Expression{
&ast.IdentifierExpression{
Identifier: ast.Identifier{
Identifier: "a",
Pos: ast.Position{Offset: 7, Line: 2, Column: 6},
},
},
},
Range: ast.Range{
StartPos: ast.Position{Offset: 4, Line: 2, Column: 3},
EndPos: ast.Position{Offset: 10, Line: 2, Column: 9},
},
}

AssertEqualWithDiff(t, expected, actual)
})
}

func TestParseNilCoalescing(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions sema/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,27 @@ func TestCheckStringTemplate(t *testing.T) {
assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})

t.Run("invalid, struct with tostring", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
access(all)
struct SomeStruct {
access(all)
view fun toString(): String {
return "SomeStruct"
}
}
let a = SomeStruct()
let x: String = "\(a)"
`)

errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})

t.Run("invalid, array", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -788,4 +809,18 @@ func TestCheckStringTemplate(t *testing.T) {

assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})

t.Run("invalid, expression type", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
let y: Int = 0
let x: String = "\(y > 0 ? "String" : true)"
`)

errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})
}
Loading