@@ -490,48 +490,28 @@ function show_type_name(io::IO, tn::Core.TypeName)
490
490
end
491
491
end
492
492
sym = (globfunc ? globname : tn. name):: Symbol
493
- if get (io, :compact , false )
494
- if globfunc
495
- return print (io, " typeof(" , sym, " )" )
496
- else
497
- return print (io, sym)
498
- end
499
- end
500
- sym_str = string (sym)
501
- hidden = ! globfunc && ' #' ∈ sym_str
493
+ globfunc && print (io, " typeof(" )
502
494
quo = false
503
- if hidden
504
- print (io, " getfield(" )
505
- elseif globfunc
506
- print (io, " typeof(" )
507
- end
508
- # Print module prefix unless type is visible from module passed to IOContext
509
- # If :module is not set, default to Main. nothing can be used to force printing prefix
510
- from = get (io, :module , Main)
511
- if isdefined (tn, :module ) && (hidden || from === nothing || ! isvisible (sym, tn. module, from))
512
- show (io, tn. module)
513
- if ! hidden
495
+ if ! get (io, :compact , false )
496
+ # Print module prefix unless type is visible from module passed to
497
+ # IOContext If :module is not set, default to Main. nothing can be used
498
+ # to force printing prefix
499
+ from = get (io, :module , Main)
500
+ if isdefined (tn, :module ) && (from === nothing || ! isvisible (sym, tn. module, from))
501
+ show (io, tn. module)
514
502
print (io, " ." )
515
- if globfunc && ! is_id_start_char (first (sym_str ))
516
- print (io, " : " )
517
- if sym == :( == )
518
- print (io, " ( " )
503
+ if globfunc && ! is_id_start_char (first (string (sym) ))
504
+ print (io, ' : ' )
505
+ if sym in quoted_syms
506
+ print (io, ' ( ' )
519
507
quo = true
520
508
end
521
509
end
522
510
end
523
511
end
524
- if hidden
525
- print (io, " , Symbol(\" " , sym_str, " \" ))" )
526
- else
527
- print (io, sym_str)
528
- if globfunc
529
- print (io, " )" )
530
- if quo
531
- print (io, " )" )
532
- end
533
- end
534
- end
512
+ show_sym (io, sym)
513
+ quo && print (io, " )" )
514
+ globfunc && print (io, " )" )
535
515
end
536
516
537
517
function show_datatype (io:: IO , x:: DataType )
@@ -814,7 +794,7 @@ julia> Base.isoperator(:+), Base.isoperator(:f)
814
794
(true, false)
815
795
```
816
796
"""
817
- isoperator (s:: Symbol ) = ccall (:jl_is_operator , Cint, (Cstring,), s) != 0
797
+ isoperator (s:: Union{ Symbol,AbstractString} ) = ccall (:jl_is_operator , Cint, (Cstring,), s) != 0
818
798
819
799
"""
820
800
isunaryoperator(s::Symbol)
981
961
function show_call (io:: IO , head, func, func_args, indent)
982
962
op, cl = expr_calls[head]
983
963
if (isa (func, Symbol) && func != = :(:) && ! (head === :. && isoperator (func))) ||
984
- (isa (func, Expr) && (func. head == :. || func. head == :curly )) ||
964
+ (isa (func, Expr) && (func. head == :. || func. head == :curly || func . head == :macroname )) ||
985
965
isa (func, GlobalRef)
986
966
show_unquoted (io, func, indent)
987
967
else
@@ -1003,10 +983,24 @@ function show_call(io::IO, head, func, func_args, indent)
1003
983
end
1004
984
end
1005
985
986
+ # Print `sym` as it would appear as an identifier name in code
987
+ # * Print valid identifiers & operators literally; also macros names if allow_macroname=true
988
+ # * Escape invalid identifiers with var"" syntax
989
+ function show_sym (io:: IO , sym; allow_macroname= false )
990
+ if isidentifier (sym) || isoperator (sym)
991
+ print (io, sym)
992
+ elseif allow_macroname && (sym_str = string (sym); startswith (sym_str, ' @' ))
993
+ print (io, ' @' )
994
+ show_sym (io, sym_str[2 : end ])
995
+ else
996
+ print (io, " var" , repr (string (sym)))
997
+ end
998
+ end
999
+
1006
1000
# # AST printing ##
1007
1001
1008
1002
show_unquoted (io:: IO , val:: SSAValue , :: Int , :: Int ) = print (io, " %" , val. id)
1009
- show_unquoted (io:: IO , sym:: Symbol , :: Int , :: Int ) = print (io, sym)
1003
+ show_unquoted (io:: IO , sym:: Symbol , :: Int , :: Int ) = show_sym (io, sym)
1010
1004
show_unquoted (io:: IO , ex:: LineNumberNode , :: Int , :: Int ) = show_linenumber (io, ex. line, ex. file)
1011
1005
show_unquoted (io:: IO , ex:: GotoNode , :: Int , :: Int ) = print (io, " goto %" , ex. label)
1012
1006
function show_unquoted (io:: IO , ex:: GlobalRef , :: Int , :: Int )
@@ -1016,7 +1010,7 @@ function show_unquoted(io::IO, ex::GlobalRef, ::Int, ::Int)
1016
1010
parens = quoted && (! isoperator (ex. name) || (ex. name in quoted_syms))
1017
1011
quoted && print (io, ' :' )
1018
1012
parens && print (io, ' (' )
1019
- print (io, ex. name)
1013
+ show_sym (io, ex. name, allow_macroname = true )
1020
1014
parens && print (io, ' )' )
1021
1015
nothing
1022
1016
end
@@ -1094,7 +1088,7 @@ end
1094
1088
1095
1089
function show_import_path (io:: IO , ex)
1096
1090
if ! isa (ex, Expr)
1097
- print (io, ex)
1091
+ show_unquoted (io, ex)
1098
1092
elseif ex. head === :(:)
1099
1093
show_import_path (io, ex. args[1 ])
1100
1094
print (io, " : " )
@@ -1105,18 +1099,21 @@ function show_import_path(io::IO, ex)
1105
1099
show_import_path (io, ex. args[i])
1106
1100
end
1107
1101
elseif ex. head === :(.)
1108
- print (io, ex. args[1 ])
1109
- for i = 2 : length (ex. args)
1110
- if ex. args[i- 1 ] != :(.)
1102
+ for i = 1 : length (ex. args)
1103
+ if i > 1 && ex. args[i- 1 ] != :(.)
1111
1104
print (io, ' .' )
1112
1105
end
1113
- print (io, ex. args[i])
1106
+ show_sym (io, ex. args[i], allow_macroname = (i == length (ex . args)) )
1114
1107
end
1115
1108
else
1116
1109
show_unquoted (io, ex)
1117
1110
end
1118
1111
end
1119
1112
1113
+ # Wrap symbols for macro names to allow them to be printed literally
1114
+ allow_macroname (ex) = ex isa Symbol && first (string (ex)) == ' @' ?
1115
+ Expr (:macroname , ex) : ex
1116
+
1120
1117
# TODO : implement interpolated strings
1121
1118
function show_unquoted (io:: IO , ex:: Expr , indent:: Int , prec:: Int )
1122
1119
head, args, nargs = ex. head, ex. args, length (ex. args)
@@ -1279,7 +1276,9 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
1279
1276
print (io, " end" )
1280
1277
1281
1278
elseif (head === :function || head === :macro ) && nargs == 1
1282
- print (io, head, ' ' , args[1 ], " end" )
1279
+ print (io, head, ' ' )
1280
+ show_unquoted (io, args[1 ])
1281
+ print (io, " end" )
1283
1282
1284
1283
elseif head === :do && nargs == 2
1285
1284
show_unquoted (io, args[1 ], indent, - 1 )
@@ -1344,26 +1343,39 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
1344
1343
print (io, head)
1345
1344
1346
1345
elseif (nargs == 1 && head in (:return , :const )) ||
1347
- head in (:local , :global , :export )
1346
+ head in (:local , :global )
1348
1347
print (io, head, ' ' )
1349
1348
show_list (io, args, " , " , indent)
1350
1349
1350
+ elseif head === :export
1351
+ print (io, head, ' ' )
1352
+ show_list (io, allow_macroname .(args), " , " , indent)
1353
+
1351
1354
elseif head === :macrocall && nargs >= 2
1352
1355
# first show the line number argument as a comment
1353
1356
if isa (args[2 ], LineNumberNode) || is_expr (args[2 ], :line )
1354
1357
print (io, args[2 ], ' ' )
1355
1358
end
1356
1359
# Use the functional syntax unless specifically designated with prec=-1
1357
1360
# and hide the line number argument from the argument list
1361
+ mname = allow_macroname (args[1 ])
1358
1362
if prec >= 0
1359
- show_call (io, :call , args[ 1 ] , args[3 : end ], indent)
1363
+ show_call (io, :call , mname , args[3 : end ], indent)
1360
1364
else
1361
1365
show_args = Vector {Any} (undef, nargs - 1 )
1362
- show_args[1 ] = args[ 1 ]
1366
+ show_args[1 ] = mname
1363
1367
show_args[2 : end ] = args[3 : end ]
1364
1368
show_list (io, show_args, ' ' , indent)
1365
1369
end
1366
1370
1371
+ elseif head === :macroname && nargs == 1
1372
+ arg1 = args[1 ]
1373
+ if arg1 isa Symbol
1374
+ show_sym (io, arg1, allow_macroname= true )
1375
+ else
1376
+ show_unquoted (io, arg1)
1377
+ end
1378
+
1367
1379
elseif head === :line && 1 <= nargs <= 2
1368
1380
show_linenumber (io, args... )
1369
1381
@@ -1406,11 +1418,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
1406
1418
for x in args
1407
1419
if ! isa (x,AbstractString)
1408
1420
print (io, " \$ (" )
1409
- if isa (x,Symbol) && ! (x in quoted_syms)
1410
- print (io, x)
1411
- else
1412
- show_unquoted (io, x)
1413
- end
1421
+ show_unquoted (io, x)
1414
1422
print (io, " )" )
1415
1423
else
1416
1424
escape_string (io, x, " \"\$ " )
0 commit comments