made 23238 stable w/ shared file prefix; Closes PR 12093 again

This commit is contained in:
Matthias Felleisen 2011-08-14 10:31:23 -04:00
parent 00d3f72f21
commit 6716b9b96a
4 changed files with 19 additions and 21 deletions

View File

@ -3,7 +3,7 @@
;; ---------------------------------------------------------------------------------------------------
;; functions that demonstrate how one and the same function can be used in three different contexts
(require mzlib/etc lang/prim htdp/error)
(require mzlib/etc lang/prim lang/htdp-langs-save-file-prefix htdp/error )
(provide-higher-order-primitive convert-gui (f2c))
(provide-higher-order-primitive convert-repl (f2c))
@ -243,7 +243,7 @@
"first" in)
(check-proc 'convert-file f 1 "convert-file" "one argument")
(check-arg 'convert-file (string? out) "string" "third" out)
;; [IPort -> Void] -> Void
;; [ -> Void] -> Void
;; perform the actual conversion on the file after optionally reading a prelude
(define (convert-file prefix)
(with-output-to-file out #:exists 'replace
@ -254,20 +254,16 @@
(define reader-exception? (regexp-match "#reader" message))
(cond
[reader-exception?
(with-handlers ((exn:fail:read?
(lambda (x)
(error 'convert-file CONVERT-FILE-MESSAGE))))
(with-handlers ((exn:fail:read? (lambda (y) (raise x))))
(convert-file
(lambda ()
;; this assumes that DrRacket adds three lines of header
;; material to files saved in a Menu-selected language
(read-line)
(read-line)
(read-line))))]
(unless (htdp-file-prefix? (current-input-port)) (raise x)))))]
[else (raise x)]))))
(convert-file void)))
;; make-reader-for-f : [Number -> Number] [IPort -> Void] -> [ -> void]
(define *debug (current-output-port))
;; make-reader-for-f : [Number -> Number] [ -> Void] -> [ -> void]
;; make-reader-for-f creates a function that reads numbers from a file
;; converts them according to f, and prints the results
;; effect: if any of the S-expressions in the file aren't numbers or
@ -285,4 +281,4 @@
(if (number? out) (printf "~s\n" out) (error 'convert OUT-ERROR out))
(read-until-eof)]
[else (error 'convert IN-ERROR in)])))
read-until-eof)
read-until-eof)

View File

@ -6,8 +6,7 @@
(convert-file "convert-drracket-non-error.txt" Fahrenheit->Celsius "out.dat")
(with-handlers ((exn? (lambda (x)
(unless (cons? (regexp-match "DrRacket" (exn-message x)))
(error 'test "something went wrong with the test (1)")))))
(with-handlers ((exn:fail:read? void))
"The input file contains a bad header. The next line should raise an exn."
(convert-file "convert-drracket-error.txt" Fahrenheit->Celsius "out.dat")
(raise `(test "something went wrong with the test (2)")))
(raise `(test "this test should have failed but didn't")))

View File

@ -1,5 +1,4 @@
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname convert-drracket-error) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
#reader(lib "htdp-beginner-reader.ss" "lang")((modname convert-drracket-non-error) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
214

View File

@ -1,11 +1,16 @@
#lang racket/base
(require racket/contract
;; ---------------------------------------------------------------------------------------------------
;; exports the header for a file saved from a drracket buffer in a menu-selected teaching language
(require racket/contract
racket/port)
(provide/contract
[htdp-save-file-prefix (listof string?)]
[htdp-file-prefix? (-> input-port? boolean?)])
(define htdp-save-file-prefix
(list ";; The first three lines of this file were inserted by DrRacket. They record metadata"
";; about the language level of this file in a form that our tools can easily process."))
@ -27,8 +32,7 @@
(cond
[(null? prefix)
(define l (read-line port 'any))
(and (string? l)
(regexp-match #rx"^#reader" l))]
(and (string? l) (pair? (regexp-match #rx"^#reader" l)))]
[else
(define l (read-line port 'any))
(and (string? l)