Skip to content

Commit 76c4bf8

Browse files
committed
rename to to_codeobject
1 parent 39ffec8 commit 76c4bf8

File tree

7 files changed

+36
-29
lines changed

7 files changed

+36
-29
lines changed

src/conversion.jl

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,15 @@ end
127127

128128

129129
# Expressions
130-
to_expr(args...) = Expr(args...)
131-
function to_expr(x::EXPR)
130+
to_codeobject(args...) = Expr(args...)
131+
132+
"""
133+
to_codeobject(x::EXPR)
134+
135+
Convert an `EXPR` into the object that `Meta.parse` would have produced from
136+
the original string, which could e.g. be an `Expr`, `Symbol`, or literal.
137+
"""
138+
function to_codeobject(x::EXPR)
132139
if isidentifier(x)
133140
if headof(x) === :NONSTDIDENTIFIER
134141
if startswith(valof(x.args[1]), "@")
@@ -141,9 +148,9 @@ function to_expr(x::EXPR)
141148
end
142149
elseif iskeyword(x)
143150
if headof(x) === :BREAK
144-
return to_expr(:break)
151+
return to_codeobject(:break)
145152
elseif headof(x) === :CONTINUE
146-
return to_expr(:continue)
153+
return to_codeobject(:continue)
147154
else
148155
return Symbol(lowercase(string(headof(x))))
149156
end
@@ -154,7 +161,7 @@ function to_expr(x::EXPR)
154161
if x.args === nothing
155162
return :(.)
156163
elseif length(x.args) == 1 && isoperator(x.args[1])
157-
return Expr(:(.), to_expr(x.args[1]))
164+
return Expr(:(.), to_codeobject(x.args[1]))
158165
else
159166
Expr(:error)
160167
end
@@ -165,11 +172,11 @@ function to_expr(x::EXPR)
165172
elseif isliteral(x)
166173
return _literal_expr(x)
167174
elseif isbracketed(x)
168-
return to_expr(x.args[1])
175+
return to_codeobject(x.args[1])
169176
elseif x.head isa EXPR
170-
Expr(to_expr(x.head), to_expr.(x.args)...)
177+
Expr(to_codeobject(x.head), to_codeobject.(x.args)...)
171178
elseif x.head === :quotenode
172-
QuoteNode(to_expr(x.args[1]))
179+
QuoteNode(to_codeobject(x.args[1]))
173180
elseif x.head === :globalrefdoc
174181
GlobalRef(Core, Symbol("@doc"))
175182
elseif x.head === :globalrefcmd
@@ -183,20 +190,20 @@ function to_expr(x::EXPR)
183190
valofrhs = valof(x.args[1].args[2].args[1])
184191
valofrhs = valofrhs === nothing ? "" : valofrhs
185192
new_name = Expr(:., remove_at(x.args[1].args[1]), QuoteNode(Symbol("@", valofrhs)))
186-
Expr(:macrocall, new_name, to_expr.(x.args[2:end])...)
193+
Expr(:macrocall, new_name, to_codeobject.(x.args[2:end])...)
187194
elseif x.head === :macrocall && isidentifier(x.args[1]) && valof(x.args[1]) == "@."
188-
Expr(:macrocall, Symbol("@__dot__"), to_expr.(x.args[2:end])...)
195+
Expr(:macrocall, Symbol("@__dot__"), to_codeobject.(x.args[2:end])...)
189196
elseif x.head === :macrocall && length(x.args) == 3 && x.args[1].head === :globalrefcmd && x.args[3].head == :string
190-
Expr(:macrocall, to_expr(x.args[1]), to_expr(x.args[2]), x.args[3].meta)
197+
Expr(:macrocall, to_codeobject(x.args[1]), to_codeobject(x.args[2]), x.args[3].meta)
191198
elseif x.head === :string && length(x.args) > 0 && (x.args[1].head === :STRING || x.args[1].head === :TRIPLESTRING) && isempty(valof(x.args[1]))
192199
# Special conversion needed - the initial text section is treated as empty for the represented string following lowest-common-prefix adjustments, but exists in the source.
193-
Expr(:string, to_expr.(x.args[2:end])...)
200+
Expr(:string, to_codeobject.(x.args[2:end])...)
194201
elseif x.args === nothing
195202
Expr(Symbol(lowercase(String(x.head))))
196203
elseif x.head === :errortoken
197204
Expr(:error)
198205
else
199-
Expr(Symbol(lowercase(String(x.head))), to_expr.(x.args)...)
206+
Expr(Symbol(lowercase(String(x.head))), to_codeobject.(x.args)...)
200207
end
201208
end
202209

@@ -206,7 +213,7 @@ function remove_at(x)
206213
elseif is_getfield_w_quotenode(x)
207214
Expr(:., remove_at(x.args[1]), QuoteNode(remove_at(x.args[2].args[1])))
208215
else
209-
to_expr(x)
216+
to_codeobject(x)
210217
end
211218
end
212219

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ function str_value(x)
441441
elseif isidentifier(x)
442442
valof(x.args[2])
443443
elseif isoperator(x)
444-
return string(to_expr(x))
444+
return string(to_codeobject(x))
445445
else
446446
return ""
447447
end

test/check_base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function cst_parse_file(str)
7272
@error "CST spans inconsistent!"
7373
end
7474

75-
x0 = norm_ast(to_expr(x))
75+
x0 = norm_ast(to_codeobject(x))
7676
x0, CSTParser.has_error(ps) || !isempty(sp)
7777
end
7878

test/errparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
rethrow(e)
4747
end
4848
try
49-
to_expr(x)
49+
to_codeobject(x)
5050
catch e
5151
@info "Couldn't convert:"
5252
@info s

test/iterate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function test_iter_spans(x)
55
for i = 1:length(x)
66
a = x[i]
77
if !(a isa EXPR)
8-
@info i, headof(x), to_expr(x)
8+
@info i, headof(x), to_codeobject(x)
99
end
1010
@test a isa EXPR
1111
test_iter_spans(a)

test/parser.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ end
3131
function test_expr(str, show_data=true)
3232
x, ps = CSTParser.parse(ParseState(str))
3333

34-
x0 = to_expr(x)
34+
x0 = to_codeobject(x)
3535
x1 = remlineinfo!(Meta.parse(str))
3636

3737
@test x.args === nothing || all(x === parentof(a) for a in x.args)
@@ -624,10 +624,10 @@ end
624624
@test valof(CSTParser.parse("\"\"\"a\"\"\"")) == "a"
625625
@test valof(CSTParser.parse("\"\"\"\"\"\"")) == ""
626626
@test valof(CSTParser.parse("\"\"\"\n\t \ta\n\n\t \tb\"\"\"")) == "a\n\nb"
627-
@test to_expr(CSTParser.parse("\"\"\"\ta\n\tb \$c\n\td\n\"\"\"")) == Expr(:string, "\ta\n\tb ", :c, "\n\td\n")
628-
@test to_expr(CSTParser.parse("\"\"\"\n\ta\n\tb \$c\n\td\n\"\"\"")) == Expr(:string, "\ta\n\tb ", :c, "\n\td\n")
629-
@test to_expr(CSTParser.parse("\"\"\"\n\ta\n\tb \$c\n\td\n\t\"\"\"")) == Expr(:string, "a\nb ", :c, "\nd\n")
630-
@test to_expr(CSTParser.parse("\"\"\"\n\t \ta\$(1+\n1)\n\t \tb\"\"\"")) == Expr(:string, "a", :(1 + 1), "\nb")
627+
@test to_codeobject(CSTParser.parse("\"\"\"\ta\n\tb \$c\n\td\n\"\"\"")) == Expr(:string, "\ta\n\tb ", :c, "\n\td\n")
628+
@test to_codeobject(CSTParser.parse("\"\"\"\n\ta\n\tb \$c\n\td\n\"\"\"")) == Expr(:string, "\ta\n\tb ", :c, "\n\td\n")
629+
@test to_codeobject(CSTParser.parse("\"\"\"\n\ta\n\tb \$c\n\td\n\t\"\"\"")) == Expr(:string, "a\nb ", :c, "\nd\n")
630+
@test to_codeobject(CSTParser.parse("\"\"\"\n\t \ta\$(1+\n1)\n\t \tb\"\"\"")) == Expr(:string, "a", :(1 + 1), "\nb")
631631
ws = " "
632632
"\"\"\"\n$ws%rv = atomicrmw \$rmw \$lt* %0, \$lt %1 acq_rel\n$(ws)ret \$lt %rv\n$ws\"\"\"" |> test_expr
633633
ws1 = " "
@@ -1061,7 +1061,7 @@ end
10611061
end
10621062
end
10631063
@testset "bad uint" begin
1064-
@test to_expr(CSTParser.parse("0x.")) == Expr(:error)
1064+
@test to_codeobject(CSTParser.parse("0x.")) == Expr(:error)
10651065
end
10661066

10671067
@testset "endswithtrivia" begin

test/spec.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CSTParser: @cst_str, headof, parentof, check_span, EXPR, to_expr
1+
using CSTParser: @cst_str, headof, parentof, check_span, EXPR, to_codeobject
22
jl_parse(s) = CSTParser.remlineinfo!(Meta.parse(s))
33

44
function check_parents(x::EXPR)
@@ -33,7 +33,7 @@ function test_expr(s, head, n, endswithtrivia = false)
3333
@test length(x) === n
3434
@test x.args === nothing || all(x === parentof(a) for a in x.args)
3535
@test x.trivia === nothing || all(x === parentof(a) for a in x.trivia)
36-
@test to_expr(x) == jl_parse(s)
36+
@test to_codeobject(x) == jl_parse(s)
3737
@test isempty(check_span(x))
3838
check_parents(x)
3939
test_iter(x)
@@ -266,17 +266,17 @@ end
266266
let s = "``"
267267
x = CSTParser.parse(s)
268268
x1 = jl_parse(s)
269-
@test x1 == to_expr(x)
269+
@test x1 == to_codeobject(x)
270270
end
271271
let s = "`a`"
272272
x = CSTParser.parse(s)
273273
x1 = jl_parse(s)
274-
@test x1 == to_expr(x)
274+
@test x1 == to_codeobject(x)
275275
end
276276
let s = "`a \$a`"
277277
x = CSTParser.parse(s)
278278
x1 = jl_parse(s)
279-
@test x1 == to_expr(x)
279+
@test x1 == to_codeobject(x)
280280
end
281281
test_expr("a``", nothing, 3, false)
282282
test_expr("a`a`", nothing, 3, false)

0 commit comments

Comments
 (0)