Skip to content

permit tuples, generators, and named tuples within interpolation syntax #38734

@clarkevans

Description

@clarkevans

As I'm developing HypertextLiteral.jl to enable a "Julian" style construction of HTML, I've noticed that tuples are not interpolated within string literals. In our string literal syntax, we've added this feature.

 julia> htl"<div $(data_value=42, dataStyle=:green)/>"
 <div data-value=42 data-style=green/>

Unfortunately, when I use the @htl macro equivalent, which is based upon string interpolation syntax, doesn't like tuples.

julia> @htl("<div $(data_value=42, dataStyle=:green)/>")
ERROR: syntax: invalid interpolation syntax

The closest I can get to usability with @htl macro is...

julia> @htl("<div $(:data_value=>42) $("data-style"=>:green)/>")
<div data-value=42 data-style=green/>

Which is significantly less readable. Overall, I prefer the @htl() form, since it nests arbitrarily, over the string literal htl"" form. However, this (arbitrary?) limitation of the interpolation syntax is a tad limiting. In a similar way, generator syntax is also useful.

julia> a = "A"; b = "B";
julia> htl"$(x for x in (a,b))"
AB
julia> htl"$(a,b)"
AB

The closest I can come with the @htl macro (using string interpolation parsing) is...

julia> a = "A"; b = "B";
julia> @htl("$([x for x in (a,b)]...)")
AB
julia> @htl("$(tuple(a,b))")   # or @htl("$a$b")
HTL("AB")

Not that these are horrible, but, when mixed with already complicated HTML constructs, they are details that interfere with the readability of the expressions. While we can support this nicer syntax with string macro syntax, string macros don't nest like regular macros do. Also, these patterns may be more generally useful for regular string interpolation.

This could be quite advantageous if it were permitted. So far I have two forms, a string literal (@htl_str) and a macro (@htl). I've yet to figure out how to define the former in terms of the latter, so I've implemented a make-shift custom parser where I basically split the incoming string by $ and then use Meta.parse immediately after the $ blindly as described in the documentation. This results in some beautiful, compact syntax for constructing HTML.

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserLanguage parsing and surface syntax

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions