Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks for refactored julia internal parser interface #178

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -241,25 +241,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 @@ -282,7 +287,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