forked from alex-eg/sex
Every later stage's error says file:line, and the reader's said only what; an unclosed list, met at the end of the file, now names the line it opened on as well.
204 lines
9.2 KiB
Scheme
204 lines
9.2 KiB
Scheme
(import (chicken condition)
|
|
(chicken irregex)
|
|
(chicken port)
|
|
reader)
|
|
|
|
(define-syntax feature-test
|
|
(syntax-rules ()
|
|
((feature-test result features string)
|
|
(test result
|
|
(parameterize ((current-features 'features))
|
|
(with-input-from-string string
|
|
(lambda () (read-raw-forms 'stdin))))))))
|
|
|
|
(define (read-error-at string)
|
|
"The message of the error reading STRING raises, where included, or
|
|
#f if it reads."
|
|
(handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
|
(with-input-from-string string (lambda () (read-raw-forms 'stdin)))
|
|
#f))
|
|
|
|
(define (read-error string)
|
|
"The message of the error reading STRING raises, or #f if it reads."
|
|
(let ((message (read-error-at string)))
|
|
(and message (irregex-replace "^[^:]*:[0-9]+: " message ""))))
|
|
|
|
(define-syntax reader-test
|
|
(syntax-rules ()
|
|
((reader-test result string)
|
|
(test result
|
|
(with-input-from-string string
|
|
(lambda () (read-raw-forms 'stdin)))))))
|
|
|
|
(test-group "reader"
|
|
;; []-syntax. For array types and array access expressions
|
|
(reader-test '((¤ * char)) "[* char]")
|
|
(reader-test '((¤ * * char const 512)) "[* * char const 512]")
|
|
(reader-test '((¤)) "[]")
|
|
(reader-test '((¤ (¤))) "[[]]")
|
|
(reader-test '((¤ (¤ const char))) "[[const char]]")
|
|
|
|
;; leading `.' becomes the dot-access operator
|
|
(reader-test '((dot-access obj field)) "(. obj field)")
|
|
(reader-test '((dot-access obj method a b)) "(. obj method a b)")
|
|
(reader-test '((dot-access a b)) "(. a b)")
|
|
;; nested leading dot
|
|
(reader-test '((foo (dot-access a b))) "(foo (. a b))")
|
|
|
|
;; dotted pairs are preserved (only a *leading* dot is special)
|
|
(reader-test '((a . b)) "(a . b)")
|
|
(reader-test '((a b . c)) "(a b . c)")
|
|
(reader-test '((quote (a . b))) "'(a . b)")
|
|
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
|
|
(reader-test '((.field obj)) "(.field obj)")
|
|
|
|
;; a leading `-' on a name is negation, the way it is on a number:
|
|
;; no name is spelled with one, and -INFINITY otherwise mangled into
|
|
;; _INFINITY. The operators that start with one are left alone
|
|
(reader-test '((- INFINITY)) "-INFINITY")
|
|
(reader-test '((var best double (- INFINITY))) "(var best double -INFINITY)")
|
|
(reader-test '((- _x)) "-_x")
|
|
(reader-test '(-1 -1.5) "-1 -1.5")
|
|
(reader-test '(- -> ->> -- -=) "- -> ->> -- -=")
|
|
|
|
;; a number in another radix, spelled the way C does or the way
|
|
;; Scheme does; either is an ordinary integer from here on
|
|
(reader-test '(224 224 5 5 -16) "0xE0 0Xe0 0b101 0B101 -0x10")
|
|
(reader-test '(128 128 5 8 -16) "#x80 #X80 #b101 #o10 #x-10")
|
|
;; ...and only a number: a prefix with nothing after it is a name
|
|
(reader-test '(0x 0xg) "0x 0xg")
|
|
|
|
;; `_' between two digits groups them, the way C23's ' does, which in
|
|
;; Sex is quote. Anywhere else it is part of a name
|
|
(reader-test '(1000000 65535 165 1000.5 -1000 100000.0 32768)
|
|
"1_000_000 0xFF_FF 0b1010_0101 1_000.5 -1_000 1e0_5 #x80_00")
|
|
(reader-test '(_1 1_ 1__0 0x_FF 0xF_p) "_1 1_ 1__0 0x_FF 0xF_p")
|
|
|
|
;; Scheme reads numbers C has no literal for, and they reached C as
|
|
;; written: 1/2 is integer division there, and +inf.0 no number at all
|
|
(test "a ratio is not a C number"
|
|
"C has no literal for this number" (read-error "1/2"))
|
|
(test "nor a double out of range"
|
|
"C has no literal for this number" (read-error "1e400"))
|
|
(test "nor a float" "C has no literal for this number" (read-error "1e39f"))
|
|
(test "but a long double may be wider than a double"
|
|
#f (read-error "1e400L"))
|
|
(test "a hex float is held to the same ranges"
|
|
"C has no literal for this number" (read-error "0x1p1024"))
|
|
(test "a float's too" "C has no literal for this number" (read-error "0x1p128f"))
|
|
(test "and a long double's" #f (read-error "0x1p1024L"))
|
|
(test "nor an infinity" "C has no literal for this number" (read-error "-inf.0"))
|
|
(test "nor a NaN" "C has no literal for this number" (read-error "+nan.0"))
|
|
(test "nor a complex number" "C has no literal for this number" (read-error "1+2i"))
|
|
(test "an integer has 64 bits at most"
|
|
"too large for any C integer type" (read-error "18446744073709551616"))
|
|
(test "and a signed suffix leaves it 63"
|
|
"too large for its suffix" (read-error "9223372036854775808ll"))
|
|
(test "an unsigned one does not"
|
|
#f (read-error "18446744073709551615ull"))
|
|
;; a name that only begins like a number is still a name
|
|
(reader-test '(3dmath.h) "3dmath.h")
|
|
|
|
;; A string is text, and an escape is one of its characters, spelled
|
|
;; the way C spells it. An escape the reader did not know lost its
|
|
;; backslash, so "\x41" read as "x41" and "\012" as NUL and "12"
|
|
(reader-test '("A" "A" "A" "A") "\"\\x41\" \"\\101\" \"\\u0041\" \"\\U00000041\"")
|
|
(reader-test '("\n" "'?" "\x7f;") "\"\\012\" \"\\'\\?\" \"\\x7f\"")
|
|
(reader-test '("é" "😀") "\"\\u00e9\" \"\\U0001F600\"")
|
|
;; a backslash ending a line joins it to the next, as in C
|
|
(reader-test '("ab") "\"a\\\nb\"")
|
|
;; C reads \xff and \377 as a byte, which text has none of: what is
|
|
;; past ASCII is written as the character, or its \u
|
|
(test "a byte past ASCII is not text"
|
|
"a byte past ASCII is not text; write the character or \\u, or a [u8 N] array for bytes"
|
|
(read-error "\"\\xff\""))
|
|
(test "in octal neither" #t (string? (read-error "\"\\377\"")))
|
|
(test "nor is a run of hex digits, which \\x takes all of"
|
|
#t (string? (read-error "\"\\x41BC\"")))
|
|
(test "an escape C has not is an error" "unknown escape" (read-error "\"\\q\""))
|
|
(test "\\x needs a digit" "\\x needs a hex digit" (read-error "\"\\xg\""))
|
|
(test "\\u takes four"
|
|
"\\u takes four hex digits and \\U eight" (read-error "\"\\u41\""))
|
|
(test "a surrogate is no character"
|
|
"no character has this code point" (read-error "\"\\uD800\""))
|
|
|
|
;; A char is C's, one byte, and #\λ came out a multi-character
|
|
;; constant whose value is the compiler's to choose
|
|
(test "a char is one byte"
|
|
"a char is one byte, and this is not: write its code point, or a string"
|
|
(read-error "#\\λ"))
|
|
(test "Latin-1 is no exception" #t (string? (read-error "#\\é")))
|
|
(reader-test '(#\a #\~ #\space) "#\\a #\\~ #\\space")
|
|
|
|
;; A reader error said what and not where, which the error of every
|
|
;; later stage does
|
|
(test "a reader error says where"
|
|
"stdin:2: unknown escape"
|
|
(read-error-at "(a)\n\"\\q\""))
|
|
(test "an unclosed list, where it opened"
|
|
"stdin:3: Unexpected end of input inside a list opened on line 2"
|
|
(read-error-at "(a)\n(b\n (c)"))
|
|
|
|
;; `;' comments are preserved as (comment "...") forms
|
|
(reader-test '((comment " hi")) "; hi")
|
|
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
|
(reader-test '((foo (comment " c") bar)) "(foo ; c\n bar)")
|
|
;; a trailing top-level comment is its own form
|
|
(reader-test '((a b) (comment " t")) "(a b) ; t")
|
|
;; a `;' inside a string is not a comment
|
|
(reader-test '("a;b") "\"a;b\"")
|
|
|
|
;; #+ / #- feature expressions. What does not apply is read and
|
|
;; dropped, so it never reaches the compiler at all
|
|
(feature-test '((a)) (linux) "#+linux (a)")
|
|
(feature-test '() (macosx) "#+linux (a)")
|
|
(feature-test '() (linux) "#-linux (a)")
|
|
(feature-test '((a)) (macosx) "#-linux (a)")
|
|
;; the guarded datum can be anything, not only a list
|
|
(feature-test '(42) (x) "#+x 42")
|
|
(feature-test '("s") (x) "#+x \"s\"")
|
|
|
|
;; and / or / not
|
|
(feature-test '((a)) (unix linux) "#+(and unix linux) (a)")
|
|
(feature-test '() (unix) "#+(and unix linux) (a)")
|
|
(feature-test '((a)) (unix) "#+(or linux unix) (a)")
|
|
(feature-test '() (bsd) "#+(or linux unix) (a)")
|
|
(feature-test '((a)) (unix) "#+(and unix (not macosx)) (a)")
|
|
(feature-test '() (unix macosx) "#+(and unix (not macosx)) (a)")
|
|
;; (and) is true and (or) is false, as they are in CL
|
|
(feature-test '((a)) () "#+(and) (a)")
|
|
(feature-test '() () "#+(or) (a)")
|
|
|
|
;; a guard inside a form, including as the last element -- dropping
|
|
;; continues with the next token, so the closing paren still arrives
|
|
(feature-test '((f 1 3)) (a) "(f #+a 1 #-a 2 3)")
|
|
(feature-test '((f 2 3)) (b) "(f #+a 1 #-a 2 3)")
|
|
(feature-test '((f 1)) (a) "(f #+a 1 #+b 2)")
|
|
(feature-test '((f)) (b) "(f #+a 1)")
|
|
;; ...and as the last form in the file
|
|
(feature-test '((a)) (x) "(a) #+y (b)")
|
|
|
|
;; guards nest
|
|
(feature-test '((a)) (x y) "#+x #+y (a)")
|
|
(feature-test '((b)) (x) "#+x #+y (a) (b)")
|
|
;; ...and the inner one may leave nothing behind: the end of the
|
|
;; file, or the paren closing the list, is what the outer one then
|
|
;; produces, and neither is an error
|
|
(feature-test '() (x) "#+x #+y (a)")
|
|
(feature-test '((f)) (x) "(f #+x #+y 1)")
|
|
(feature-test '((f 2)) (x) "(f #+x #+y 1 2)")
|
|
(feature-test '((a)) (x) "(a) #+x #+y (b)")
|
|
|
|
;; a comment between a guard and the form it guards describes the
|
|
;; guard. Taking it for the guarded datum would leave the form itself
|
|
;; unconditional
|
|
(feature-test '((a)) (x) "#+x ;; why\n (a)")
|
|
(feature-test '() (y) "#+x ;; why\n (a)")
|
|
(feature-test '((b)) (y) "#+x ;; why\n (a) (b)")
|
|
;; a comment after the guarded form is an ordinary form, and stays
|
|
(feature-test '((comment "; tail")) (y) "#+x (a) ;; tail")
|
|
|
|
;; a feature the program was not given is simply absent
|
|
(feature-test '() () "#+anything (a)")
|
|
)
|