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

Allow interpolation in using and export statement #11338

Merged
merged 2 commits into from
May 26, 2015
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
15 changes: 10 additions & 5 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
(define syntactic-op? (Set syntactic-operators))
(define syntactic-unary-op? (Set syntactic-unary-operators))

(define (symbol-or-interpolate? ex)
(or (symbol? ex)
(and (pair? ex)
(eq? '$ (car ex)))))

(define trans-op (string->symbol ".'"))
(define ctrans-op (string->symbol "'"))
(define vararg-op (string->symbol "..."))
Expand Down Expand Up @@ -1245,8 +1250,8 @@
body))))
((export)
(let ((es (map macrocall-to-atsym
(parse-comma-separated s parse-atom))))
(if (not (every symbol? es))
(parse-comma-separated s parse-unary-prefix))))
(if (not (every symbol-or-interpolate? es))
(error "invalid \"export\" statement"))
`(export ,@es)))
((import using importall)
Expand Down Expand Up @@ -1327,17 +1332,17 @@
(begin (take-token s)
(loop (list* '|.| '|.| '|.| '|.| l) (peek-token s))))
(else
(cons (macrocall-to-atsym (parse-atom s)) l)))))
(cons (macrocall-to-atsym (parse-unary-prefix s)) l)))))

(define (parse-import s word)
(let loop ((path (parse-import-dots s)))
(if (not (symbol? (car path)))
(if (not (symbol-or-interpolate? (car path)))
(error (string "invalid \"" word "\" statement: expected identifier")))
(let ((nxt (peek-token s)))
(cond
((eq? nxt '|.|)
(take-token s)
(loop (cons (macrocall-to-atsym (parse-atom s)) path)))
(loop (cons (macrocall-to-atsym (parse-unary-prefix s)) path)))
((or (memv nxt '(#\newline #\; #\, :))
(eof-object? nxt))
`(,word ,@(reverse path)))
Expand Down
11 changes: 11 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,14 @@ macro test999_str(args...); args; end
Expr(:toplevel,
Expr(:import, :A, :b),
Expr(:import, :A, :c, :d)))

# issue #11332
@test parse("export \$(symbol(\"A\"))") == :(export $(Expr(:$, :(symbol("A")))))
@test parse("export \$A") == :(export $(Expr(:$, :A)))
@test parse("using \$a.\$b") == Expr(:using, Expr(:$, :a), Expr(:$, :b))
@test parse("using \$a.\$b, \$c") == Expr(:toplevel, Expr(:using, Expr(:$, :a),
Expr(:$, :b)),
Expr(:using, Expr(:$, :c)))
@test parse("using \$a: \$b, \$c.\$d") ==
Expr(:toplevel, Expr(:using, Expr(:$, :a), Expr(:$, :b)),
Expr(:using, Expr(:$, :a), Expr(:$, :c), Expr(:$, :d)))