Skip to content

Commit

Permalink
Syntax experiment: String format syntax eg "$(x, digits=2)"
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Jun 9, 2023
1 parent 8a47045 commit 50336c9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/kinds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ const _kind_names =
"inert" # QuoteNode; not quasiquote
"juxtapose" # Numeric juxtaposition like 2x
"string" # A string interior node (possibly containing interpolations)
"format" # String formatting specification
"cmdstring" # A cmd string node (containing delimiters plus string)
"char" # A char string node (containing delims + char data)
"macrocall"
Expand Down
8 changes: 5 additions & 3 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3182,14 +3182,16 @@ function parse_string(ps::ParseState, raw::Bool)
m = position(ps)
bump(ps, TRIVIA_FLAG)
opts = parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
return (needs_parameters=false,
return (needs_parameters=true,
simple_interp=!had_commas && num_semis == 0 && num_subexprs == 1)
end
if !opts.simple_interp || peek_behind(ps, skip_parens=false).kind == K"generator"
# "$(x,y)" ==> (string (parens (error x y)))
emit(ps, m, K"error", error="invalid interpolation syntax")
emit(ps, m, K"format")
min_supported_version(v"1.10", ps, m, "string formatting syntax")
else
emit(ps, m, K"parens")
end
emit(ps, m, K"parens")
elseif k == K"var"
# var identifiers disabled in strings
# "$var" ==> (string var)
Expand Down

0 comments on commit 50336c9

Please sign in to comment.