Skip to content

Commit

Permalink
add triple-quote string literals, e.g., """abc"""
Browse files Browse the repository at this point in the history
  • Loading branch information
nolta committed Feb 15, 2013
1 parent c5bbe90 commit b8eae0e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1311,12 +1311,22 @@
(error "incomplete: invalid string syntax")
c))

(define (take-char p)
(begin (read-char p) p))

; reads a raw string literal with no processing.
; quote can be escaped with \, but the \ is left in place.
; returns ("str" . b), b is a boolean telling whether interpolation is used
(define (parse-string-literal s)
(let ((p (ts:port s)))
(if (eqv? (peek-char p) #\")
(if (eqv? (peek-char (take-char p)) #\")
(parse-string-literal-3 (take-char p))
(cons "" #f))
(parse-string-literal-1 p))))

(define (parse-string-literal-1 p)
(let ((b (open-output-string))
(p (ts:port s))
(interpolate #f))
(let loop ((c (read-char p)))
(if (eqv? c #\")
Expand All @@ -1332,6 +1342,32 @@
(loop (read-char p)))))
(cons (io.tostring! b) interpolate)))

(define (parse-string-literal-3 p)
(let ((b (open-output-string))
(interpolate #f))
(let loop ((c (read-char p)))
(if (eqv? c #\")
(let ((nextch (read-char p)))
(if (eqv? nextch #\")
(let ((nextch2 (read-char p)))
(if (eqv? nextch2 #\")
#t
(begin (write-char #\\ b) (write-char #\" b)
(write-char #\\ b) (write-char #\" b)
(loop nextch2))))
(begin (write-char #\\ b) (write-char #\" b)
(loop nextch))))
(begin (if (eqv? c #\\)
(let ((nextch (read-char p)))
(begin (write-char #\\ b)
(write-char (not-eof-3 nextch) b)))
(begin
(if (eqv? c #\$)
(set! interpolate #t))
(write-char (not-eof-3 c) b)))
(loop (read-char p)))))
(cons (io.tostring! b) interpolate)))

(define (not-eof-1 c)
(if (eof-object? c)
(error "incomplete: invalid character literal")
Expand Down
7 changes: 7 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,10 @@ str_a = [str...]

str = "s\u2200"
@test str[1:end] == str

# triple-quote delimited strings
@test """abc""" == "abc"
@test """ab"c""" == "ab\"c"
@test """ab""c""" == "ab\"\"c"
@test """ab"\"c""" == "ab\"\"c"
@test """abc\"""" == "abc\""

0 comments on commit b8eae0e

Please sign in to comment.