Compare commits

..

No commits in common. "1190f32ab41148fa2396f524c5c67fd4d1ff50f3" and "4edcffdc3a0d8eacf67d90d7200b1abd1183afc9" have entirely different histories.

9 changed files with 27 additions and 142 deletions

View File

@ -1,61 +0,0 @@
(import (scheme base) (scheme char) (scheme file)
(scheme read) (scheme write))
(define (read-declarations-from-file filename)
(let ((bytes (let ((bytes (call-with-port
(open-binary-input-file filename)
(lambda (port) (read-bytevector 1000 port)))))
(if (eof-object? bytes) (make-bytevector 0) bytes)))
(i 0))
(define (peek-next-ascii-byte)
(if (not (< i (bytevector-length bytes)))
(eof-object)
(let ((next-byte (bytevector-u8-ref bytes i)))
(if (<= 1 next-byte 126)
next-byte
(begin (set! i (+ i 1))
(peek-next-ascii-byte))))))
(define (read-char? k)
(let* ((next-byte (peek-next-ascii-byte))
(next-char (if (eof-object? next-byte)
next-byte (integer->char next-byte)))
(consume? (cond ((procedure? k) (k next-char))
((char? k) (eqv? k next-char))
(else #f))))
(cond (consume? (set! i (+ i 1)) next-char)
(else #f))))
(define (read-char* k)
(let loop ((chars '()))
(let ((c (read-char? k)))
(if c (loop (append chars (list c)))
(if (null? chars) #f (list->string chars))))))
(define (symbol-char? c)
(not (or (eof-object? c) (char-whitespace? c)
(eqv? c #\") (eqv? c #\() (eqv? c #\)))))
(define (read->eol)
(read-char* (lambda (c) (not (eqv? c #\newline)))))
(define (skip-white-comm)
(cond ((read-char? #\;) (read->eol) (skip-white-comm))
((read-char? char-whitespace?) (skip-white-comm))
(else #f)))
(define (read-lexeme)
(skip-white-comm)
(cond ((read-char? #\()) ((read-char? #\))) ((read-char* symbol-char?))
(else #f)))
(let* ((shebang (and (read-char? #\#) (read-char? #\!) (read->eol))))
(cons shebang
(and (eqv? (read-lexeme) #\() (equal? (read-lexeme) "declare-file")
(eqv? (read-lexeme) #\() (equal? (read-lexeme) "coding")
(let ((coding (read-lexeme)))
(and (string? coding) (eqv? (read-lexeme) #\))
(string-downcase coding))))))))
(define (writeln x)
(write x)
(newline))
(writeln (read-declarations-from-file "test-big5.scm"))
(writeln (read-declarations-from-file "test-euc-jp.scm"))
(writeln (read-declarations-from-file "test-shift-jis.scm"))
(writeln (read-declarations-from-file "test-utf16-be.scm"))
(writeln (read-declarations-from-file "test-utf16-le.scm"))

View File

@ -10,19 +10,17 @@
(lambda (port) (read-bytevector 1000 port))))) (lambda (port) (read-bytevector 1000 port)))))
(if (eof-object? bytes) (make-bytevector 0) bytes))) (if (eof-object? bytes) (make-bytevector 0) bytes)))
(i 0)) (i 0))
(define (peek-next-ascii-byte)
(if (not (< i (bytevector-length bytes)))
(eof-object)
(let ((next-byte (bytevector-u8-ref bytes i)))
(if (<= 1 next-byte 126)
next-byte
(begin (set! i (+ i 1))
(peek-next-ascii-byte))))))
(define (read-char? k) (define (read-char? k)
(let* ((next-byte (peek-next-ascii-byte)) (let* ((remain? (< i (bytevector-length bytes)))
(next-char (if (eof-object? next-byte) (next-byte (if remain?
next-byte (bytevector-u8-ref bytes i)
(integer->char next-byte))) (eof-object)))
(next-char (cond ((eof-object? next-byte)
next-byte)
((<= 1 next-byte 126)
(integer->char next-byte))
(else
next-byte)))
(consume? (cond ((procedure? k) (k next-char)) (consume? (cond ((procedure? k) (k next-char))
((char? k) (eqv? k next-char)) ((char? k) (eqv? k next-char))
(else #f)))) (else #f))))
@ -42,13 +40,6 @@
(eqv? c #\") (eqv? c #\")
(eqv? c #\() (eqv? c #\()
(eqv? c #\))))) (eqv? c #\)))))
(define (string-char? c)
(not (or (eof-object? c)
(eqv? c #\tab)
(eqv? c #\newline)
(eqv? c #\return)
(eqv? c #\")
(eqv? c #\\))))
(define (skip-char* k) (define (skip-char* k)
(when (read-char? k) (skip-char* k))) (when (read-char? k) (skip-char* k)))
(define (read-rest-of-line) (define (read-rest-of-line)
@ -69,14 +60,6 @@
#f #f
(list->string chars)) (list->string chars))
(loop (append chars (list c))))))) (loop (append chars (list c)))))))
(define (read-string)
(let loop ((chars '()))
(if (read-char? #\")
(list->string chars)
(let ((c (read-char? string-char?)))
(if (not c)
(eof-object)
(loop (append chars (list c))))))))
(define (read-list) (define (read-list)
(let loop ((xs '())) (let loop ((xs '()))
(skip-whitespace-and-comments) (skip-whitespace-and-comments)
@ -88,23 +71,19 @@
(loop (append xs (list x)))))))) (loop (append xs (list x))))))))
(define (read-form) (define (read-form)
(skip-whitespace-and-comments) (skip-whitespace-and-comments)
(cond ((read-char? #\() (if (read-char? #\()
(read-list)) (read-list)
((read-char? #\")
(read-string))
(else
(let ((symbol-name (read-char* not-special-char?))) (let ((symbol-name (read-char* not-special-char?)))
(if symbol-name (if symbol-name
(string->symbol symbol-name) (string->symbol symbol-name)
(eof-object)))))) (eof-object)))))
(let* ((shebang (if (and (read-char? #\#) (read-char? #\!)) (let* ((shebang (if (and (read-char? #\#) (read-char? #\!))
(read-rest-of-line) (read-rest-of-line)
#f)) #f))
(first-form (read-form)) (first-form (read-form))
(declarations (or (and (list? first-form) (declarations (and (list? first-form)
(eqv? 'declare-file (car first-form)) (eqv? 'declare-file (car first-form))
(cdr first-form)) (cdr first-form))))
'())))
(if shebang (if shebang
(cons (list 'shebang shebang) declarations) (cons (list 'shebang shebang) declarations)
declarations)))) declarations))))
@ -113,12 +92,4 @@
(write x) (write x)
(newline)) (newline))
(define (try-file filename) (for-each writeln (read-declarations-from-file "test.scm"))
(for-each writeln (read-declarations-from-file filename))
(newline))
(try-file "test-big5.scm")
(try-file "test-euc-jp.scm")
(try-file "test-shift-jis.scm")
(try-file "test-utf16-be.scm")
(try-file "test-utf16-le.scm")

View File

@ -1,10 +0,0 @@
#! /usr/bin/env gosh
(declare-file
(coding Big5)
(language scheme r7rs)
(copyright 2019 "Lassi Kortela")
(spdx-license-identifier "ISC"))
(display "你好世界")
(newline)

View File

@ -1,10 +0,0 @@
#! /usr/bin/env gosh
(declare-file
(coding EUC-JP)
(language scheme r7rs)
(copyright 2019 "Lassi Kortela")
(spdx-license-identifier "ISC"))
(display "こんにちは世界")
(newline)

View File

@ -1,2 +0,0 @@
#!/bin/sh
kawa encoding-reader.scm

View File

@ -1,10 +0,0 @@
#! /usr/bin/env gosh
(declare-file
(coding shift_jis)
(language scheme r7rs)
(copyright 2019 "Lassi Kortela")
(spdx-license-identifier "ISC"))
(display "こんにちは世界")
(newline)

Binary file not shown.

Binary file not shown.

7
test.scm Normal file
View File

@ -0,0 +1,7 @@
#! /usr/bin/env gosh
(declare-file
(coding shift_jis))
(display "こんにちは世界")
(newline)