Skip to content

Commit

Permalink
Only allow $ as prefix operator in export and import and do not look …
Browse files Browse the repository at this point in the history
…across newlines. Fix up JuliaLang#11338
  • Loading branch information
yuyichao committed May 30, 2015
1 parent 6b4c275 commit fe1edeb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,14 @@
(else (list op (parse-unary-prefix s)))))
(parse-atom s))))

(define (parse-atom-or-interpolate s)
(if (eq? (peek-token s) '$)
(begin (take-token s)
(if (let ((op (peek-token s)))
(or (newline? op) (closing-token? op)))
'$ (list '$ (parse-atom s))))
(parse-atom s)))

;; parse function call, indexing, dot, and transpose expressions
;; also handles looking for syntactic reserved words
(define (parse-call s)
Expand Down Expand Up @@ -1250,7 +1258,7 @@
body))))
((export)
(let ((es (map macrocall-to-atsym
(parse-comma-separated s parse-unary-prefix))))
(parse-comma-separated s parse-atom-or-interpolate))))
(if (not (every symbol-or-interpolate? es))
(error "invalid \"export\" statement"))
`(export ,@es)))
Expand Down Expand Up @@ -1332,7 +1340,7 @@
(begin (take-token s)
(loop (list* '|.| '|.| '|.| '|.| l) (peek-token s))))
(else
(cons (macrocall-to-atsym (parse-unary-prefix s)) l)))))
(cons (macrocall-to-atsym (parse-atom-or-interpolate s)) l)))))

(define (parse-import s word)
(let loop ((path (parse-import-dots s)))
Expand All @@ -1342,7 +1350,7 @@
(cond
((eq? nxt '|.|)
(take-token s)
(loop (cons (macrocall-to-atsym (parse-unary-prefix s)) path)))
(loop (cons (macrocall-to-atsym (parse-atom-or-interpolate s)) path)))
((or (memv nxt '(#\newline #\; #\, :))
(eof-object? nxt))
`(,word ,@(reverse path)))
Expand Down
22 changes: 22 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

function parseall(str)
pos = start(str)
exs = []
while !done(str, pos)
ex, pos = parse(str, pos)
push!(exs, ex)
end
if length(exs) == 0
throw(ParseError("end of input"))
elseif length(exs) == 1
return exs[1]
else
return Expr(:block, exs...)
end
end

# issue #9684
let
for (ex1, ex2) in [("5.≠x", "5.!=x"),
Expand Down Expand Up @@ -120,3 +136,9 @@ macro test999_str(args...); args; end
@test parse("using \$a: \$b, \$c.\$d") ==
Expr(:toplevel, Expr(:using, Expr(:$, :a), Expr(:$, :b)),
Expr(:using, Expr(:$, :a), Expr(:$, :c), Expr(:$, :d)))

# fix pr #11338
@test parseall("using \$\na") == Expr(:block, Expr(:using, :$), :a)
@test parseall("using \$,\na") == Expr(:toplevel, Expr(:using, :$),
Expr(:using, :a))
@test parseall("using &\na") == Expr(:block, Expr(:using, :&), :a)

0 comments on commit fe1edeb

Please sign in to comment.