diff --git a/src/components/internals.jl b/src/components/internals.jl index e2a56b76..0f371691 100644 --- a/src/components/internals.jl +++ b/src/components/internals.jl @@ -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) @@ -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) diff --git a/src/components/keywords.jl b/src/components/keywords.jl index 3926cd6a..7380d0eb 100644 --- a/src/components/keywords.jl +++ b/src/components/keywords.jl @@ -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 @@ -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 diff --git a/src/iterate.jl b/src/iterate.jl index 7733067d..2647d095 100644 --- a/src/iterate.jl +++ b/src/iterate.jl @@ -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 diff --git a/test/parser.jl b/test/parser.jl index 45aa44af..bbad5f63 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -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