Skip to content

Commit f7eb7b4

Browse files
committed
fixes to macro constructors, try blocks, is_func_call, _unescape_string
1 parent 2a475d5 commit f7eb7b4

File tree

7 files changed

+50
-22
lines changed

7 files changed

+50
-22
lines changed

src/components/internals.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ end
104104
105105
Parses a function call. Expects to start before the opening parentheses and is passed the expression declaring the function name, `ret`.
106106
"""
107-
function parse_call(ps::ParseState, ret)
107+
function parse_call(ps::ParseState, ret, ismacro = false)
108108
sb = ps.nt.startbyte - ret.fullspan
109109
if is_minus(ret) || is_not(ret)
110110
arg = @closer ps unary @closer ps inwhere @precedence ps 13 parse_expression(ps)
@@ -127,7 +127,7 @@ function parse_call(ps::ParseState, ret)
127127
arg = @precedence ps PowerOp parse_expression(ps)
128128
ret = EXPR{Call}(Any[ret; arg.args])
129129
else
130-
ismacro = ret isa EXPR{MacroName}
130+
!ismacro && ret isa EXPR{MacroName} && (ismacro = true)
131131
args = Any[ret, PUNCTUATION(next(ps))]
132132
@closeparen ps @default ps parse_comma_sep(ps, args, !ismacro)
133133
accept_rparen(ps, args)
@@ -222,7 +222,7 @@ function parse_macrocall(ps::ParseState)
222222
if ps.nt.kind == Tokens.COMMA
223223
return EXPR{MacroCall}(Any[mname], mname.fullspan, mname.span)
224224
elseif isemptyws(ps.ws) && ps.nt.kind == Tokens.LPAREN
225-
return parse_call(ps, mname)
225+
return parse_call(ps, mname, true)
226226
else
227227
args = Any[mname]
228228
insquare = ps.closer.insquare

src/components/keywords.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,22 @@ end
238238
sb1 = ps.nt.startbyte
239239
blockargs = parse_block(ps)
240240

241-
# return EXPR{Macro}(Any[kw, sig, EXPR{Block}(blockargs), accept_end(ps)])
242-
if isempty(blockargs)
243-
block = EXPR{Block}(blockargs, 0, 0)
241+
if sig isa IDENTIFIER
242+
ender = accept_end(ps)
243+
fullspan1 = ps.nt.startbyte - sb
244+
ret = EXPR{Macro}(Any[kw, sig, ender], fullspan1, fullspan1 - ender.fullspan + ender.span)
245+
elseif isempty(blockargs)
246+
ender = accept_end(ps)
247+
fullspan1 = ps.nt.startbyte - sb
248+
ret = EXPR{Macro}(Any[kw, sig, EXPR{Block}([]), ender], fullspan1, fullspan1 - ender.fullspan + ender.span)
244249
else
245250
fullspan = ps.nt.startbyte - sb1
246251
block = EXPR{Block}(blockargs, fullspan, fullspan - last(blockargs).fullspan + last(blockargs).span)
252+
ender = accept_end(ps)
253+
fullspan1 = ps.nt.startbyte - sb
254+
ret = EXPR{Macro}(Any[kw, sig, block, ender], fullspan1, fullspan1 - ender.fullspan + ender.span)
247255
end
248-
ender = accept_end(ps)
249-
fullspan1 = ps.nt.startbyte - sb
250-
return EXPR{Macro}(Any[kw, sig, block, ender], fullspan1, fullspan1 - ender.fullspan + ender.span)
256+
return ret
251257
end
252258

253259
# loops
@@ -379,7 +385,7 @@ end
379385
end
380386

381387
catchblockargs = parse_block(ps, Any[], (Tokens.END, Tokens.FINALLY))
382-
if !(caught isa IDENTIFIER || caught == FALSE)
388+
if !(caught isa IDENTIFIER || caught == FALSE || (caught isa UnarySyntaxOpCall && caught.arg1 isa OPERATOR && caught.arg1.kind == Tokens.EX_OR))
383389
pushfirst!(catchblockargs, caught)
384390
caught = FALSE
385391
end

src/components/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ function parse_operator(ps::ParseState, @nospecialize(ret), op)
319319
push!(ret, op)
320320
push!(ret, nextarg)
321321
ret = ret
322-
elseif ret isa BinaryOpCall && (is_star(op) || is_plus(op)) && op.kind == ret.op.kind && !ret.op.dot
322+
elseif ret isa BinaryOpCall && (is_star(op) || is_plus(op)) && op.kind == ret.op.kind && !ret.op.dot && ret.op.span > 0
323323
nextarg = @precedence ps P - LtoR(P) parse_expression(ps)
324324
ret = EXPR{ChainOpCall}(Any[ret.arg1, ret.op, ret.arg2, op, nextarg])
325325
elseif is_eq(op)

src/conversion.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ function Expr_int(x)
105105
is_hex && return sized_uint_literal(val, 4)
106106
is_oct && return sized_uint_oct_literal(val)
107107
is_bin && return sized_uint_literal(val, 1)
108-
sizeof(val) < sizeof(TYPEMAX_INT64_STR) && return Base.parse(Int64, val)
109-
val < TYPEMAX_INT64_STR && return Base.parse(Int64, val)
110-
sizeof(val) < sizeof(TYPEMAX_INT128_STR) && return Base.parse(Int128, val)
111-
val < TYPEMAX_INT128_STR && return Base.parse(Int128, val)
112-
Base.parse(BigInt, val)
108+
sizeof(val) <= sizeof(TYPEMAX_INT64_STR) && return Base.parse(Int64, val)
109+
return Meta.parse(val)
110+
# # val < TYPEMAX_INT64_STR && return Base.parse(Int64, val)
111+
# sizeof(val) <= sizeof(TYPEMAX_INTval < TYPEMAX_INT128_STR128_STR) && return Base.parse(Int128, val)
112+
# # val < TYPEMAX_INT128_STR && return Base.parse(Int128, val)
113+
# Base.parse(BigInt, val)
113114
end
114115

115116
function Expr_float(x)
@@ -203,7 +204,11 @@ end
203204

204205
function Expr(x::EXPR{MacroName})
205206
if x.args[2] isa IDENTIFIER
206-
return Symbol("@", x.args[2].val)
207+
if x.args[2].val == "."
208+
return Symbol("@", "__dot__")
209+
else
210+
return Symbol("@", x.args[2].val)
211+
end
207212
else
208213
return Symbol("@")
209214
end
@@ -333,7 +338,13 @@ function Expr(x::EXPR{FunctionDef})
333338
end
334339
ret
335340
end
336-
Expr(x::EXPR{Macro}) = Expr(:macro, Expr(x.args[2]), Expr(x.args[3]))
341+
function Expr(x::EXPR{Macro})
342+
if length(x.args) == 3
343+
Expr(:macro, Expr(x.args[2]))
344+
else
345+
Expr(:macro, Expr(x.args[2]), Expr(x.args[3]))
346+
end
347+
end
337348
Expr(x::EXPR{ModuleH}) = Expr(:module, true, Expr(x.args[2]), Expr(x.args[3]))
338349
Expr(x::EXPR{BareModule}) = Expr(:module, false, Expr(x.args[2]), Expr(x.args[3]))
339350

src/interface.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
is_func_call(x) = false
2-
is_func_call(x::EXPR) = false
32
is_func_call(x::EXPR{Call}) = true
3+
is_func_call(x::EXPR{InvisBrackets}) = is_func_call(x.args[2])
44
is_func_call(x::UnaryOpCall) = true
5+
is_func_call(x::BinaryOpCall) = true
56
function is_func_call(x::BinarySyntaxOpCall)
67
if is_decl(x.op)
78
return is_func_call(x.arg1)

src/utils.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,10 @@ function _unescape_string(io, s::AbstractString)
664664
popfirst!(a)
665665
end
666666
if k == 1
667-
throw(ArgumentError("invalid $(m == 2 ? "hex (\\x)" :
668-
"unicode (\\u)") escape sequence used in $(repr(s))"))
667+
# throw(ArgumentError("invalid $(m == 2 ? "hex (\\x)" :
668+
# "unicode (\\u)") escape sequence used in $(repr(s))"))
669+
# push error to ParseState?
670+
n = 0
669671
end
670672
if m == 2 # \x escape sequence
671673
write(io, UInt8(n))
@@ -681,7 +683,9 @@ function _unescape_string(io, s::AbstractString)
681683
popfirst!(a)
682684
end
683685
if n > 255
684-
throw(ArgumentError("octal escape sequence out of range"))
686+
# throw(ArgumentError("octal escape sequence out of range"))
687+
# push error to ParseState?
688+
n = 255
685689
end
686690
write(io, UInt8(n))
687691
else

test/parser.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ end
354354
end
355355

356356
@testset "Macros " begin
357+
@test "macro m end" |> test_expr
358+
@test "macro m() end" |> test_expr
359+
@test "macro m() a end" |> test_expr
357360
@test "@mac" |> test_expr
358361
@test "@mac a b c" |> test_expr
359362
@test "@mac f(5)" |> test_expr
@@ -688,6 +691,9 @@ end
688691
end""" |> test_expr
689692
@test "-1^a" |> test_expr_broken
690693
@test "function(f, args...; kw...) end" |> test_expr_broken
694+
@test "2a * b" |> test_expr
695+
@test "(g1090(x::T)::T) where {T} = x+1.0" |> test_expr
696+
@test "(:) = Colon()" |> test_expr
691697
end
692698

693699
@testset "Broken things" begin

0 commit comments

Comments
 (0)