Compare commits

...

10 Commits

Author SHA1 Message Date
Lassi Kortela 1190f32ab4 Simplify 2019-05-13 00:43:00 +03:00
Lassi Kortela 7f70dd8906 Add simple version
Does not "parse" any more structure than needed to get the coding C
from a (declare-file (coding C) ...) form. The coding must be the
first declaration inside the declare-file. This version does not check
that the rest of the declare-file form is valid.
2019-05-13 00:36:37 +03:00
Lassi Kortela d5ea8c21ec Add Big5 test file 2019-05-12 17:25:55 +03:00
Lassi Kortela eeeea2c93e Add EUC-JP test file 2019-05-12 17:24:27 +03:00
Lassi Kortela 0f7db91954 Add Kawa test script 2019-05-12 17:22:52 +03:00
Lassi Kortela 298a4337a8 Add UTF-16 tests 2019-05-12 17:22:44 +03:00
Lassi Kortela aac4a4bdec Skip non-ASCII bytes altogether
This permits UTF-16, with or without byte order mark
2019-05-12 17:21:48 +03:00
Lassi Kortela 0a9c678e64 Change author metadata to match REUSE initiative 2019-05-12 16:40:59 +03:00
Lassi Kortela 2ab02b063f Parse strings and illustrate some more metadata 2019-05-12 16:38:03 +03:00
Lassi Kortela 000a898778 Copy with empty declarations again 2019-05-12 16:28:58 +03:00
9 changed files with 142 additions and 27 deletions

View File

@ -0,0 +1,61 @@
(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,17 +10,19 @@
(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* ((remain? (< i (bytevector-length bytes)))
(next-byte (if remain?
(bytevector-u8-ref bytes i)
(eof-object)))
(next-char (cond ((eof-object? next-byte)
next-byte)
((<= 1 next-byte 126)
(integer->char next-byte))
(else
next-byte)))
(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))))
@ -40,6 +42,13 @@
(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)
(when (read-char? k) (skip-char* k)))
(define (read-rest-of-line)
@ -60,6 +69,14 @@
#f
(list->string chars))
(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)
(let loop ((xs '()))
(skip-whitespace-and-comments)
@ -71,19 +88,23 @@
(loop (append xs (list x))))))))
(define (read-form)
(skip-whitespace-and-comments)
(if (read-char? #\()
(read-list)
(let ((symbol-name (read-char* not-special-char?)))
(if symbol-name
(string->symbol symbol-name)
(eof-object)))))
(cond ((read-char? #\()
(read-list))
((read-char? #\")
(read-string))
(else
(let ((symbol-name (read-char* not-special-char?)))
(if symbol-name
(string->symbol symbol-name)
(eof-object))))))
(let* ((shebang (if (and (read-char? #\#) (read-char? #\!))
(read-rest-of-line)
#f))
(first-form (read-form))
(declarations (and (list? first-form)
(eqv? 'declare-file (car first-form))
(cdr first-form))))
(declarations (or (and (list? first-form)
(eqv? 'declare-file (car first-form))
(cdr first-form))
'())))
(if shebang
(cons (list 'shebang shebang) declarations)
declarations))))
@ -92,4 +113,12 @@
(write x)
(newline))
(for-each writeln (read-declarations-from-file "test.scm"))
(define (try-file filename)
(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")

10
test-big5.scm Normal file
View File

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

10
test-euc-jp.scm Normal file
View File

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

2
test-kawa.sh Executable file
View File

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

10
test-shift-jis.scm Normal file
View File

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

BIN
test-utf16-be.scm Normal file

Binary file not shown.

BIN
test-utf16-le.scm Normal file

Binary file not shown.

View File

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