forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow tripple qouted strings as format strings for
[@]printf
This is a temporary hack for JuliaLang#2682, but the proper fix would be to fix the parser to not emit macroall to the AST for triple quoted strings. This commit also includes a ugly hack that will error when it is time to remove this fast fix. See: https://groups.google.com/forum/#!topic/julia-users/U1shoGwnCCQ
- Loading branch information
Showing
1 changed file
with
17 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -758,12 +758,28 @@ end | |
|
||
_is_str_expr(ex) = | ||
isa(ex,Expr) && ex.head==:macrocall && isa(ex.args[1],Symbol) && | ||
(ex.args[1] == :str || endswith(string(ex.args[1]),"_str")) | ||
(ex.args[1] == :str || endswith(string(ex.args[1]),"str")) | ||
|
||
#TODO: remove when triple quotes parses to a string and not a @mstr macrocall | ||
# and the following line fails with an exception | ||
:("""hi""").head | ||
function _convert_mstr_macro(ex) | ||
!isa(ex, Expr) && return ex | ||
if ex.head == :macrocall && | ||
ex.args[1] == symbol("@mstr") && | ||
length(ex.args) == 2 && | ||
isa(ex.args[2], String) | ||
return ex.args[2] | ||
end | ||
ex | ||
end | ||
|
||
macro printf(args...) | ||
if length(args) == 0 | ||
error("@printf: called with zero arguments") | ||
end | ||
#TODO: remove when @mstr macrocall gets removed | ||
args = map(_convert_mstr_macro, args) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ivarne
Author
Owner
|
||
if !isa(args[1],String) && !(length(args) > 1 && isa(args[2],String)) | ||
if _is_str_expr(args[1]) || length(args) > 1 && _is_str_expr(args[2]) | ||
error("format must be a plain static string (no interpolation or prefix)") | ||
|
Can't you just do:
args = map(macroexpand,args)
? That seems to work just fine in my tests. Plus, it wouldn't necessarily need to be removed after@mstr
is removed… since this is perhaps the most-used user-facing macro it might make sense for it to accept other macros (particularly string macros) as arguments.