Skip to content

Commit

Permalink
Add line numbers in parser errors for disallowed space (JuliaLang#33855)
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f authored Nov 15, 2019
1 parent 1b03919 commit 38be538
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
28 changes: 16 additions & 12 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,11 @@
((eqv? next #\( )
(take-token s)
(let* ((opspc (ts:space? s))
(lno (input-port-line (ts:port s)))
(parens (parse-paren- s #t)))
(if (cdr parens) ;; found an argument list
(if opspc
(disallowed-space op #\( )
(disallowed-space-error lno op #\( )
(parse-factor-with-initial-ex
s
(fix-syntactic-unary (cons op (tuple-to-arglist (car parens))))))
Expand Down Expand Up @@ -1135,9 +1136,14 @@
(parse-where-chain s decl-sig)
decl-sig)))

(define (disallowed-space ex t)
(define (disallowed-space-error lno ex t)
(error (string "space before \"" t "\" not allowed in \""
(deparse ex) " " t "\"")))
(deparse ex) " " t "\" at "
current-filename ":" lno)))

(define (disallow-space s ex t)
(if (ts:space? s)
(disallowed-space-error (input-port-line (ts:port s)) ex t)))

;; string macro suffix for given delimiter t
(define (macsuffix t)
Expand All @@ -1156,7 +1162,7 @@
ex
(case t
((#\( )
(if (ts:space? s) (disallowed-space ex t))
(disallow-space s ex t)
(take-token s)
(let ((c (let ((al (parse-call-arglist s #\) )))
(receive
Expand All @@ -1177,7 +1183,7 @@
c)
(loop c))))
((#\[ )
(if (ts:space? s) (disallowed-space ex t))
(disallow-space s ex t)
(take-token s)
;; ref is syntax, so we can distinguish
;; a[i] = x from
Expand All @@ -1197,14 +1203,13 @@
(loop (list* 'typed_comprehension ex (cdr al))))
(else (error "unknown parse-cat result (internal error)")))))))
((|.|)
(if (ts:space? s) (disallowed-space ex t))
(disallow-space s ex t)
(take-token s)
(loop
(cond ((eqv? (peek-token s) #\()
(begin
(if (ts:space? s)
(error (string "space before \"(\" not allowed in \""
(deparse ex) ". (\"")))
(disallow-space s `(|.| ,ex (quote ||)) #\())
(take-token s)
`(|.| ,ex (tuple ,@(parse-call-arglist s #\) )))))
((eqv? (peek-token s) ':)
Expand All @@ -1231,7 +1236,7 @@
(loop (list t ex)))
((|.'|) (error "the \".'\" operator is discontinued"))
((#\{ )
(if (ts:space? s) (disallowed-space ex t))
(disallow-space s ex t)
(take-token s)
(loop (list* 'curly ex (parse-call-arglist s #\} ))))
((#\" #\`)
Expand Down Expand Up @@ -1600,7 +1605,7 @@
(let ((nxt (peek-token s)))
(cond
((eq? nxt '|.|)
(if (ts:space? s) (disallowed-space word nxt))
(disallow-space s (car path) nxt)
(take-token s)
(loop (cons (unquote (macrocall-to-atsym (parse-unary-prefix s))) path)))
((or (memv nxt '(#\newline #\; #\, :))
Expand Down Expand Up @@ -2342,8 +2347,7 @@
((eqv? t #\@)
(take-token s)
(let ((nxt (peek-token s)))
(if (ts:space? s)
(disallowed-space '@ nxt)))
(disallow-space s '@ nxt))
(with-space-sensitive
(let ((startloc (line-number-node s))
(head (if (eq? (peek-token s) '|.|)
Expand Down
13 changes: 11 additions & 2 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1327,9 +1327,18 @@ end
@test Meta.parse("-(x;;;)^2") == Expr(:call, :-, Expr(:call, :^, Expr(:block, :x), 2))
@test Meta.parse("+((1,2))") == Expr(:call, :+, Expr(:tuple, 1, 2))

@test_throws ParseError("space before \"(\" not allowed in \"+ (\"") Meta.parse("1 -+ (a=1, b=2)")
@test_throws ParseError("space before \"(\" not allowed in \"+ (\" at none:1") Meta.parse("1 -+ (a=1, b=2)")
# issue #29781
@test_throws ParseError("space before \"(\" not allowed in \"sin. (\"") Meta.parse("sin. (1)")
@test_throws ParseError("space before \"(\" not allowed in \"sin. (\" at none:1") Meta.parse("sin. (1)")
# Parser errors for disallowed space contain line numbers
@test_throws ParseError("space before \"[\" not allowed in \"f() [\" at none:2") Meta.parse("\nf() [i]")
@test_throws ParseError("space before \"(\" not allowed in \"f() (\" at none:2") Meta.parse("\nf() (i)")
@test_throws ParseError("space before \".\" not allowed in \"f() .\" at none:2") Meta.parse("\nf() .i")
@test_throws ParseError("space before \"{\" not allowed in \"f() {\" at none:2") Meta.parse("\nf() {i}")
@test_throws ParseError("space before \"m\" not allowed in \"@ m\" at none:2") Meta.parse("\n@ m")
@test_throws ParseError("space before \".\" not allowed in \"a .\" at none:2") Meta.parse("\nusing a .b")
@test_throws ParseError("space before \".\" not allowed in \"a .\" at none:2") Meta.parse("\nusing a .b")
@test_throws ParseError("space before \"(\" not allowed in \"+ (\" at none:2") Meta.parse("\n+ (x, y)")

@test Meta.parse("1 -+(a=1, b=2)") == Expr(:call, :-, 1,
Expr(:call, :+, Expr(:kw, :a, 1), Expr(:kw, :b, 2)))
Expand Down

0 comments on commit 38be538

Please sign in to comment.