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

Consider newlines as spaces for space-sensitive parsing #20265

Merged
merged 10 commits into from
Jul 24, 2018
11 changes: 7 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@
(begin0 (ts:last-tok s)
(ts:set-tok! s #f))))

(define (space-before-next-token? s)
(or (skip-ws (ts:port s) #f) (eqv? #\newline (peek-char (ts:port s)))))

;; --- misc ---

; Log a syntax deprecation, attributing it to current-filename and the line
Expand Down Expand Up @@ -744,7 +747,7 @@
(take-token s)
(cond ((eq? t '~) ;; ~ is the only non-syntactic assignment-precedence operators
(if (and space-sensitive (ts:space? s)
(not (eqv? (peek-char (ts:port s)) #\ )))
(not (space-before-next-token? s)))
(begin (ts:put-back! s t (ts:space? s))
ex)
(list 'call t ex (parse-assignment s down))))
Expand Down Expand Up @@ -839,7 +842,7 @@
((and range-colon-enabled (eq? t ':))
(take-token s)
(if (and space-sensitive spc
(or (peek-token s) #t) (not (ts:space? s)))
(not (space-before-next-token? s)))
;; "a :b" in space sensitive mode
(begin (ts:put-back! s ': spc)
ex)
Expand Down Expand Up @@ -873,7 +876,7 @@
(let ((spc (ts:space? s)))
(take-token s)
(cond ((and space-sensitive spc (memq t unary-and-binary-ops)
(not (eqv? (peek-char (ts:port s)) #\ )))
(not (space-before-next-token? s)))
;; here we have "x -y"
(ts:put-back! s t spc)
(reverse! chain))
Expand All @@ -890,7 +893,7 @@
(let ((spc (ts:space? s)))
(take-token s)
(cond ((and space-sensitive spc (memq t unary-and-binary-ops)
(not (eqv? (peek-char (ts:port s)) #\ )))
(not (space-before-next-token? s)))
;; here we have "x -y"
(ts:put-back! s t spc)
ex)
Expand Down
26 changes: 26 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,32 @@ end
# error throwing branch from #10560
@test_throws ArgumentError Base.tryparse_internal(Bool, "foo", 1, 2, 10, true)

# issue #16594
@test Meta.parse("@x a + \nb") == Meta.parse("@x a +\nb")
@test [1 +
1] == [2]
@test [1 +1] == [1 1]

# issue #16594, note for the macro tests, order is important
# because the line number is included as part of the expression
# (i.e. both macros must start on the same line)
@test :(@test((1+1) == 2)) == :(@test 1 +
1 == 2)
@test :(@x 1 +1 -1) == :(@x(1, +1, -1))
@test :(@x 1 + 1 -1) == :(@x(1+1, -1))
@test :(@x 1 + 1 - 1) == :(@x(1 + 1 - 1))
@test :(@x(1 + 1 - 1)) == :(@x 1 +
1 -
1)
@test :(@x(1 + 1 + 1)) == :(@x 1 +
1 +
1)
@test :([x .+
y]) == :([x .+ y])

# line break in : expression disallowed
@test_throws Meta.ParseError Meta.parse("[1 :\n2] == [1:2]")

@test tryparse(Float64, "1.23") === 1.23
@test tryparse(Float32, "1.23") === 1.23f0
@test tryparse(Float16, "1.23") === Float16(1.23)
Expand Down