Skip to content

Commit

Permalink
Merge pull request #11338 from yuyichao/export-interp
Browse files Browse the repository at this point in the history
Allow interpolation in using and export statement
  • Loading branch information
JeffBezanson committed May 26, 2015
2 parents 97072c8 + 1415d5f commit e650220
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
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)))

0 comments on commit e650220

Please sign in to comment.