Skip to content

Commit

Permalink
Merge pull request #220 from julia-vscode/import-as
Browse files Browse the repository at this point in the history
handle import .. as .. syntax
  • Loading branch information
pfitzseb authored Jan 12, 2021
2 parents 386c1c1 + 2cfc193 commit ecb7efd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/components/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ end
"""
Helper function for parsing import/using statements.
"""
function parse_dot_mod(ps::ParseState, is_colon = false)
function parse_dot_mod(ps::ParseState, is_colon = false, allow_as = false)
ret = EXPR(EXPR(:OPERATOR, 0, 0, "."), EXPR[], EXPR[])

prevpos = position(ps)
Expand Down Expand Up @@ -387,6 +387,13 @@ function parse_dot_mod(ps::ParseState, is_colon = false)
pushtotrivia!(ret, EXPR(:DOT, 1, 1))
ps.nt = RawToken(kindof(ps.nt), ps.nt.startpos, ps.nt.endpos, ps.nt.startbyte + 1, ps.nt.endbyte, ps.nt.token_error, false, ps.nt.suffix)
else
@static if VERSION > v"1.6-"
if allow_as && !isnewlinews(ps.ws) && isidentifier(ps.nt) && val(ps.nt, ps) == "as"
as = EXPR(:AS, next(ps))
as_val = parse_importexport_item(ps, is_colon)
ret = EXPR(:as, EXPR[ret, as_val], EXPR[as])
end
end
break
end
prevpos = loop_check(ps, prevpos)
Expand Down
11 changes: 6 additions & 5 deletions src/components/keywords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,21 @@ end

function parse_imports(ps::ParseState)
kw = EXPR(ps)
kwt = is_import(kw) ? :import : :using
allow_as = is_import(kw)
kwt = allow_as ? :import : :using

arg = parse_dot_mod(ps)
arg = parse_dot_mod(ps, false, allow_as)
if !iscomma(ps.nt) && !iscolon(ps.nt)
ret = EXPR(kwt, EXPR[arg], EXPR[kw])
elseif iscolon(ps.nt)
ret = EXPR(kwt, EXPR[EXPR(EXPR(:OPERATOR, next(ps)), EXPR[arg])], EXPR[kw])

arg = parse_dot_mod(ps, true)
arg = parse_dot_mod(ps, true, allow_as)
push!(ret.args[1], arg)
prevpos = position(ps)
while iscomma(ps.nt)
pushtotrivia!(ret.args[1], accept_comma(ps))
arg = parse_dot_mod(ps, true)
arg = parse_dot_mod(ps, true, allow_as)
push!(ret.args[1], arg)
prevpos = loop_check(ps, prevpos)
end
Expand All @@ -184,7 +185,7 @@ function parse_imports(ps::ParseState)
prevpos = position(ps)
while iscomma(ps.nt)
pushtotrivia!(ret, accept_comma(ps))
arg = parse_dot_mod(ps)
arg = parse_dot_mod(ps, false, allow_as)
push!(ret, arg)
prevpos = loop_check(ps, prevpos)
end
Expand Down
2 changes: 2 additions & 0 deletions src/iterate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ end
function _getindex(x::EXPR, i)
if headof(x) === :abstract
_abstract(x, i)
elseif headof(x) === :as
odda_event(x, i)
elseif headof(x) === :block
_block(x, i)
elseif headof(x) === :braces
Expand Down
7 changes: 7 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -878,4 +878,11 @@ end""" |> test_expr
@test test_expr("a +₊ b *₊ c")
@test test_expr("a *₊ b +₊ c")
end
@static if VERSION > v"1.6-"
@testset "import .. as .. syntax" begin
@test test_expr("import a as b")
@test test_expr("import a as b, c")
@test CSTParser.parse("using a as b")[2].head === :errortoken
end
end
end

0 comments on commit ecb7efd

Please sign in to comment.