Fixing PR11649
This commit is contained in:
parent
d675c7b12a
commit
87865cb1bd
38
collects/tests/xml/srcloc.rkt
Normal file
38
collects/tests/xml/srcloc.rkt
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
#lang racket
|
||||||
|
(require xml
|
||||||
|
tests/eli-tester)
|
||||||
|
|
||||||
|
(define (ppos p)
|
||||||
|
(define-values (line col pos) (port-next-location p))
|
||||||
|
pos)
|
||||||
|
|
||||||
|
; open-input-string is broken
|
||||||
|
(define (the-test first? second?)
|
||||||
|
(define is (open-input-string "abcdef"))
|
||||||
|
|
||||||
|
(test #:failure-prefix (format "~a ~a" first? second?)
|
||||||
|
(test
|
||||||
|
(when first? (port-count-lines! is))
|
||||||
|
(ppos is) => 1 (read-char is)
|
||||||
|
(ppos is) => 2 (read-char is)
|
||||||
|
(ppos is) => 3 (read-char is)
|
||||||
|
(when second? (port-count-lines! is))
|
||||||
|
(ppos is) => 4 (read-char is)
|
||||||
|
(ppos is) => 5 (read-char is)
|
||||||
|
(ppos is) => 6 (read-char is))))
|
||||||
|
(test (the-test #f #f)
|
||||||
|
(the-test #t #f)
|
||||||
|
(the-test #f #t)
|
||||||
|
(the-test #t #t))
|
||||||
|
|
||||||
|
(define p (open-input-string "abcdef"))
|
||||||
|
(for ([x (in-range 0 6)]) (read-char p))
|
||||||
|
(define pos (ppos p))
|
||||||
|
|
||||||
|
(define exn
|
||||||
|
(with-handlers ((exn:fail? values))
|
||||||
|
(read-xml/element p)))
|
||||||
|
|
||||||
|
(test
|
||||||
|
pos => 7
|
||||||
|
(srcloc-position (first ((exn:srclocs-accessor exn) exn))) => 7)
|
|
@ -26,21 +26,19 @@
|
||||||
[(misc0 start) (read-misc in pos)])
|
[(misc0 start) (read-misc in pos)])
|
||||||
(make-document (make-prolog misc0 #f empty)
|
(make-document (make-prolog misc0 #f empty)
|
||||||
(read-xml-element-helper pos in start)
|
(read-xml-element-helper pos in start)
|
||||||
(let ([loc-before (pos)])
|
(let-values ([(misc1 end-of-file) (read-misc in pos)])
|
||||||
(let-values ([(misc1 end-of-file) (read-misc in pos)])
|
(unless (EOF? end-of-file)
|
||||||
(unless (eof-object? end-of-file)
|
(parse-error (list
|
||||||
(let ([loc-after (pos)])
|
(make-srcloc
|
||||||
(parse-error (list
|
(object-name in)
|
||||||
(make-srcloc
|
#f
|
||||||
(object-name in)
|
#f
|
||||||
#f
|
(location-offset (source-start end-of-file))
|
||||||
#f
|
(- (location-offset (source-stop end-of-file))
|
||||||
(location-offset loc-before)
|
(location-offset (source-start end-of-file)))))
|
||||||
(- (location-offset loc-after)
|
"extra stuff at end of document ~e"
|
||||||
(location-offset loc-before))))
|
end-of-file))
|
||||||
"extra stuff at end of document ~e"
|
misc1)))))
|
||||||
end-of-file)))
|
|
||||||
misc1))))))
|
|
||||||
|
|
||||||
;; read-xml/element : [Input-port] -> Element
|
;; read-xml/element : [Input-port] -> Element
|
||||||
(define read-xml/element
|
(define read-xml/element
|
||||||
|
@ -54,15 +52,26 @@
|
||||||
(cond
|
(cond
|
||||||
[(start-tag? start) (read-element start in pos)]
|
[(start-tag? start) (read-element start in pos)]
|
||||||
[(element? start) start]
|
[(element? start) start]
|
||||||
[else (parse-error (list
|
[else
|
||||||
(make-srcloc
|
(parse-error
|
||||||
(object-name in)
|
(list
|
||||||
#f
|
(make-srcloc
|
||||||
#f
|
(object-name in)
|
||||||
1
|
#f
|
||||||
(- (location-offset (pos)) 1)))
|
#f
|
||||||
"expected root element - received ~e"
|
; XXX Some data structures should really be changed to be sources
|
||||||
(if (pcdata? start) (pcdata-string start) start))]))
|
(if (source? start)
|
||||||
|
(location-offset (source-start start))
|
||||||
|
#f)
|
||||||
|
(if (source? start)
|
||||||
|
(- (location-offset (source-stop start))
|
||||||
|
(location-offset (source-start start)))
|
||||||
|
#f)))
|
||||||
|
"expected root element - received ~e"
|
||||||
|
(cond
|
||||||
|
[(pcdata? start) (pcdata-string start)]
|
||||||
|
[(EOF? start) eof]
|
||||||
|
[else start]))]))
|
||||||
|
|
||||||
;; read-misc : Input-port (-> Location) -> (listof Misc) Token
|
;; read-misc : Input-port (-> Location) -> (listof Misc) Token
|
||||||
(define (read-misc in pos)
|
(define (read-misc in pos)
|
||||||
|
@ -92,7 +101,7 @@
|
||||||
body))])
|
body))])
|
||||||
(let ([x (lex in pos)])
|
(let ([x (lex in pos)])
|
||||||
(cond
|
(cond
|
||||||
[(eof-object? x)
|
[(EOF? x)
|
||||||
(parse-error (list
|
(parse-error (list
|
||||||
(make-srcloc
|
(make-srcloc
|
||||||
(object-name in)
|
(object-name in)
|
||||||
|
@ -159,11 +168,13 @@
|
||||||
[(apos) "'"]
|
[(apos) "'"]
|
||||||
[else #f]))
|
[else #f]))
|
||||||
|
|
||||||
|
(define-struct (EOF source) ())
|
||||||
|
|
||||||
;; lex : Input-port (-> Location) -> (U Token special)
|
;; lex : Input-port (-> Location) -> (U Token special)
|
||||||
(define (lex in pos)
|
(define (lex in pos)
|
||||||
(let ([c (peek-char-or-special in)])
|
(let ([c (peek-char-or-special in)])
|
||||||
(cond
|
(cond
|
||||||
[(eof-object? c) c]
|
[(eof-object? c) (EOF (pos) (pos))]
|
||||||
[(eq? c #\&) (lex-entity in pos)]
|
[(eq? c #\&) (lex-entity in pos)]
|
||||||
[(eq? c #\<) (lex-tag-cdata-pi-comment in pos)]
|
[(eq? c #\<) (lex-tag-cdata-pi-comment in pos)]
|
||||||
[(not (char? c)) (read-char-or-special in)]
|
[(not (char? c)) (read-char-or-special in)]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user