Skip to content

Commit

Permalink
Tweaks for refactored julia internal parser interface
Browse files Browse the repository at this point in the history
The flisp parser is now available via Core.Compiler.fl_parse rather than
via a ccall to jl_parse_string. jl_parse_string will redirect to
Core._parse, which may be CSTParser if the Julia parser has been
overridden.
  • Loading branch information
c42f committed Jun 2, 2020
1 parent 819d6b4 commit 31b2921
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function sized_uint_oct_literal(s::AbstractString)
# (len < 45 || (len == 45 && s <= "0o3777777777777777777777777777777777777777777")) && return Base.parse(UInt128, s)
# return Base.parse(BigInt, s)
(len < 45 || (len == 45 && s <= "0o3777777777777777777777777777777777777777777")) && return Expr(:macrocall, GlobalRef(Core, Symbol("@uint128_str")), nothing, s)
return Meta.parse(s)
return flisp_parse(s)
end

function _literal_expr(x)
Expand Down Expand Up @@ -95,7 +95,7 @@ function Expr_int(x)
is_oct && return sized_uint_oct_literal(val)
is_bin && return sized_uint_literal(val, 1)
# sizeof(val) <= sizeof(TYPEMAX_INT64_STR) && return Base.parse(Int64, val)
return Meta.parse(val)
return flisp_parse(val)
# # val < TYPEMAX_INT64_STR && return Base.parse(Int64, val)
# sizeof(val) <= sizeof(TYPEMAX_INTval < TYPEMAX_INT128_STR128_STR) && return Base.parse(Int128, val)
# # val < TYPEMAX_INT128_STR && return Base.parse(Int128, val)
Expand Down
24 changes: 15 additions & 9 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,30 @@ function has_error(x::EXPR)
end
has_error(ps::ParseState) = ps.errored

# When using the FancyDiagnostics package, Base.parse, is the
# When using the FancyDiagnostics package, Meta.parse is the
# same as CSTParser.parse. Manually call the flisp parser here
# to make sure we test what we want, even when people load the
# FancyDiagnostics package.
function flisp_parse(str::AbstractString, pos::Int; greedy::Bool = true, raise::Bool = true)
# pos is one based byte offset.
# returns (expr, end_pos). expr is () in case of parse error.
bstr = String(str)
ex, pos = ccall(:jl_parse_string, Any,
(Ptr{UInt8}, Csize_t, Int32, Int32),
bstr, sizeof(bstr), pos - 1, greedy ? 1 : 0)
if VERSION < v"1.6-DEV"
bstr = String(str)
ex, pos = ccall(:jl_parse_string, Any,
(Ptr{UInt8}, Csize_t, Int32, Int32),
bstr, sizeof(bstr), pos - 1, greedy ? 1 : 0)
else
filename = "none"
rule = greedy ? :statement : :atom
ex, pos = Core.Compiler.fl_parse(str, filename, pos-1, rule)
end
if raise && isa(ex, Expr) && ex.head === :error
throw(Meta.ParseError(ex.args[1]))
end
if ex === ()
raise && throw(Meta.ParseError("end of input"))
ex = Expr(:error, "end of input")
end
return ex, pos + 1 # C is zero-based, Julia is 1-based
# pos is zero-based byte offset
return ex, pos+1
end

function flisp_parse(str::AbstractString; raise::Bool = true)
Expand All @@ -289,7 +294,8 @@ function flisp_parse(stream::IO; greedy::Bool = true, raise::Bool = true)
end

using Base.Meta
norm_ast(a::Any) = begin

function norm_ast(a::Any)
if isa(a, Expr)
for (i, arg) in enumerate(a.args)
a.args[i] = norm_ast(arg)
Expand Down

0 comments on commit 31b2921

Please sign in to comment.