@@ -11,7 +11,8 @@ function is_stringchunk(node)
11
11
return k == K " String" || k == K " CmdString"
12
12
end
13
13
14
- function _to_expr (node:: SyntaxNode , iteration_spec= false , need_linenodes= true )
14
+ function _to_expr (node:: SyntaxNode ; iteration_spec= false , need_linenodes= true ,
15
+ eq_to_kw= false , inside_dot_expr= false , inside_vect_or_braces= false )
15
16
if ! haschildren (node)
16
17
val = node. val
17
18
if val isa Union{Int128,UInt128,BigInt}
@@ -20,7 +21,6 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
20
21
# representation of these.
21
22
str = replace (sourcetext (node), ' _' => " " )
22
23
headsym = :macrocall
23
- k = kind (node)
24
24
macname = val isa Int128 ? Symbol (" @int128_str" ) :
25
25
val isa UInt128 ? Symbol (" @uint128_str" ) :
26
26
Symbol (" @big_str" )
@@ -29,12 +29,15 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
29
29
return val
30
30
end
31
31
end
32
- if kind (node) == K " ?"
32
+ nodekind = kind (node)
33
+ if nodekind == K " ?"
33
34
headsym = :if
35
+ elseif nodekind == K " =" && ! is_decorated (node) && eq_to_kw
36
+ headsym = :kw
34
37
else
35
38
headstr = untokenize (head (node), include_flag_suff= false )
36
39
headsym = ! isnothing (headstr) ? Symbol (headstr) :
37
- error (" Can't untokenize head of kind $(kind (node) ) " )
40
+ error (" Can't untokenize head of kind $(nodekind ) " )
38
41
end
39
42
node_args = children (node)
40
43
if headsym == :string || headsym == :cmdstring
@@ -89,26 +92,38 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
89
92
args = Vector {Any} (undef, length (node_args)* (insert_linenums ? 2 : 1 ))
90
93
if headsym == :for && length (node_args) == 2
91
94
# No line numbers in for loop iteration spec
92
- args[1 ] = _to_expr (node_args[1 ], true , false )
95
+ args[1 ] = _to_expr (node_args[1 ], iteration_spec = true , need_linenodes = false )
93
96
args[2 ] = _to_expr (node_args[2 ])
94
97
elseif headsym == :let && length (node_args) == 2
95
98
# No line numbers in let statement binding list
96
- args[1 ] = _to_expr (node_args[1 ], false , false )
99
+ args[1 ] = _to_expr (node_args[1 ], need_linenodes = false )
97
100
args[2 ] = _to_expr (node_args[2 ])
98
101
else
102
+ eq_to_kw = headsym == :call && ! has_flags (node, INFIX_FLAG) ||
103
+ headsym == :ref ||
104
+ (headsym == :parameters && ! inside_vect_or_braces) ||
105
+ (headsym == :tuple && inside_dot_expr)
106
+ in_dot = headsym == :.
107
+ in_vb = headsym == :vect || headsym == :braces
99
108
if insert_linenums
100
109
if isempty (node_args)
101
110
push! (args, source_location (LineNumberNode, node. source, node. position))
102
111
else
103
112
for i in 1 : length (node_args)
104
113
n = node_args[i]
105
114
args[2 * i- 1 ] = source_location (LineNumberNode, n. source, n. position)
106
- args[2 * i] = _to_expr (n)
115
+ args[2 * i] = _to_expr (n,
116
+ eq_to_kw= eq_to_kw,
117
+ inside_dot_expr= in_dot,
118
+ inside_vect_or_braces= in_vb)
107
119
end
108
120
end
109
121
else
110
122
for i in 1 : length (node_args)
111
- args[i] = _to_expr (node_args[i])
123
+ args[i] = _to_expr (node_args[i],
124
+ eq_to_kw= eq_to_kw,
125
+ inside_dot_expr= in_dot,
126
+ inside_vect_or_braces= in_vb)
112
127
end
113
128
end
114
129
end
@@ -118,8 +133,9 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
118
133
if is_infix (node. raw)
119
134
args[2 ], args[1 ] = args[1 ], args[2 ]
120
135
end
136
+
137
+ # Special cases for various expression heads
121
138
loc = source_location (LineNumberNode, node. source, node. position)
122
- # Convert elements
123
139
if headsym == :macrocall
124
140
insert! (args, 2 , loc)
125
141
elseif headsym in (:call , :ref )
@@ -128,7 +144,7 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
128
144
insert! (args, 2 , args[end ])
129
145
pop! (args)
130
146
end
131
- elseif headsym in (:tuple , :parameters , :vect )
147
+ elseif headsym in (:tuple , :parameters , :vect , :braces )
132
148
# Move parameters blocks to args[1]
133
149
if length (args) > 1 && Meta. isexpr (args[end ], :parameters )
134
150
pushfirst! (args, args[end ])
@@ -181,7 +197,7 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
181
197
# elseif headsym == :string && length(args) == 1 && version <= (1,5)
182
198
# Strip string from interpolations in 1.5 and lower to preserve
183
199
# "hi$("ho")" ==> (string "hi" "ho")
184
- elseif headsym == :(= )
200
+ elseif headsym == :(= ) && ! is_decorated (node)
185
201
if is_eventually_call (args[1 ]) && ! iteration_spec && ! Meta. isexpr (args[2 ], :block )
186
202
# Add block for short form function locations
187
203
args[2 ] = Expr (:block , loc, args[2 ])
@@ -191,10 +207,7 @@ function _to_expr(node::SyntaxNode, iteration_spec=false, need_linenodes=true)
191
207
args[1 ] = Expr (:block , loc, args[1 ])
192
208
elseif headsym == :(-> )
193
209
if Meta. isexpr (args[2 ], :block )
194
- parent = node. parent
195
- if parent isa SyntaxNode && kind (parent) != K " do"
196
- pushfirst! (args[2 ]. args, loc)
197
- end
210
+ pushfirst! (args[2 ]. args, loc)
198
211
else
199
212
# Add block for source locations
200
213
args[2 ] = Expr (:block , loc, args[2 ])
0 commit comments