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

Only allow $ as prefix operator in export and import and do not look across newlines #11495

Closed
wants to merge 1 commit into from
Closed
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
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)