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
21 changes: 21 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ end
# error throwing branch from #10560
@test_throws ArgumentError Base.tryparse_internal(Bool, "foo", 1, 2, 10, true)

# issue #16594
@test length(:(@test 1 +
1 == 2).args) == 3
@test [1 +
1] == [2]
@test [1 +1] == [1 1]
@test length(:(@x 1 +1 -1).args) == 5
@test length(:(@x 1 + 1 -1).args) == 4
@test length(:(@x 1 + 1 - 1).args) == 3
@test length(:(@x 1 +
1 -
1).args) == 3
@test length(:(@x 1 +
1 +
1).args) == 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer these tests to check more than just the number of arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just check Expr equality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I have improved the tests using @stevengj's suggestion.

@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