| 1 |
;;; |
| 2 |
;;; convert the original SSAX.scm into Gauche's preferable format. |
| 3 |
;;; |
| 4 |
|
| 5 |
|
| 6 |
(use util.match) |
| 7 |
(use srfi.13) |
| 8 |
|
| 9 |
;; The conversion procedure: |
| 10 |
;; |
| 11 |
;; - Comments are deleted. |
| 12 |
;; - Run-test macro definition and uses are eliminated. |
| 13 |
;; - Some macro and function definitions are replaced for |
| 14 |
;; the ones that uses Gauche's native method for efficiency. |
| 15 |
|
| 16 |
(define *trans-table* |
| 17 |
'(;; remove run-test stuff |
| 18 |
((define-macro run-test ...)) |
| 19 |
((define-macro (run-test ...) ...)) |
| 20 |
((run-test ...)) |
| 21 |
((include ...)) |
| 22 |
((cerr ...)) |
| 23 |
((let (...) (run-test ...) ...)) |
| 24 |
((let* (...) (run-test ...) ...)) |
| 25 |
;; optimizing xml-token construction stuff |
| 26 |
((define (make-xml-token ...) ...) |
| 27 |
(define make-xml-token cons)) |
| 28 |
((define-macro xml-token-kind ...) |
| 29 |
(define xml-token-kind car)) |
| 30 |
((define-macro xml-token-head ...) |
| 31 |
(define xml-token-head cdr)) |
| 32 |
;; Gauche has those procedures natively. |
| 33 |
((define (cons* ...) ...)) |
| 34 |
((define (string-whitespace? ...) ...) |
| 35 |
(define (string-whitespace? str) (string-every #[\s] str))) |
| 36 |
((define (fold-right ...) ...)) |
| 37 |
((define (fold ...) ...)) |
| 38 |
;; We have constant definition instead. |
| 39 |
((define ssax:S-chars ...)) |
| 40 |
;; These forms are in sxml-tools. |
| 41 |
;; We have Gauche-specific versions for them |
| 42 |
((define-macro (sxml:find-name-separator ...) ...)) |
| 43 |
((define (sxml:error ...) ...)) |
| 44 |
)) |
| 45 |
|
| 46 |
(define (prelude file) |
| 47 |
(format #t ";;; Generated from ~s\n" (sys-basename file))) |
| 48 |
|
| 49 |
;; *very limited* pattern matcher |
| 50 |
(define (match? rule sexp) |
| 51 |
(cond |
| 52 |
[(null? rule) (null? sexp)] |
| 53 |
[(eq? rule '...) #t] |
| 54 |
[(not (pair? rule)) (eq? rule sexp)] |
| 55 |
[else (let loop ([eltr rule] [elts sexp]) |
| 56 |
(cond [(null? eltr) (null? elts)] |
| 57 |
[(eq? (car eltr) '...) #t] |
| 58 |
[(not (pair? elts)) #f] |
| 59 |
[(match? (car eltr) (car elts)) (loop (cdr eltr) (cdr elts))] |
| 60 |
[else #f]))])) |
| 61 |
|
| 62 |
(define (replace rules sexp) |
| 63 |
(let loop ([rules rules]) |
| 64 |
(cond [(null? rules) (list sexp)] |
| 65 |
[(match? (caar rules) sexp) (cdar rules)] |
| 66 |
[else (loop (cdr rules))]))) |
| 67 |
|
| 68 |
(define (include-translating file process) |
| 69 |
(with-input-from-file file |
| 70 |
(^[] |
| 71 |
(prelude file) |
| 72 |
(generator-for-each process read)))) |
| 73 |
|
| 74 |
(define (process-body file excludes) |
| 75 |
(include-translating file |
| 76 |
(^[sexp] |
| 77 |
(unless (skip? sexp excludes) |
| 78 |
(for-each write (replace *trans-table* sexp)) |
| 79 |
(newline))))) |
| 80 |
|
| 81 |
(define (process-test file) |
| 82 |
(include-translating file |
| 83 |
(^[sexp] |
| 84 |
(when (and (pair? sexp) (eq? (car sexp) 'run-test)) |
| 85 |
(unless (and (pair? (cadr sexp)) |
| 86 |
(eq? 'define (caadr sexp)) |
| 87 |
(pair? (cadadr sexp)) |
| 88 |
(memq (car (cadadr sexp)) |
| 89 |
'(ssax:warn parser-error))) |
| 90 |
(write `(%run-test ,@(cdr sexp))) |
| 91 |
(newline)))))) |
| 92 |
|
| 93 |
(define (adjpath templ file) #"~(sys-dirname templ)/~file") |
| 94 |
|
| 95 |
(define (skip? sexp excludes) |
| 96 |
(match sexp |
| 97 |
[('define (name . _) . _) (boolean (memq name excludes))] |
| 98 |
[_ #f])) |
| 99 |
|
| 100 |
;; entry point |
| 101 |
(define (main args) |
| 102 |
(unless (= (length args) 2) |
| 103 |
(error "Usage: gosh trans.scm <template-file>")) |
| 104 |
(let* ([templ (cadr args)] |
| 105 |
[dest (sys-basename (if (string-suffix? ".scm.in" templ) |
| 106 |
(string-drop-right templ 3) |
| 107 |
(string-append templ ".out.scm")))]) |
| 108 |
(with-input-from-file templ |
| 109 |
(^[] (with-output-to-file dest |
| 110 |
(^[] (generator-for-each |
| 111 |
(^[line] (rxmatch-case line |
| 112 |
[#/^;#include-body "(.*)"\s*(\(.*\))?/ |
| 113 |
(#f file excludes) |
| 114 |
(process-body (adjpath templ file) |
| 115 |
(if excludes |
| 116 |
(read-from-string excludes) |
| 117 |
'()))] |
| 118 |
[#/^;#include-test "(.*)"/ (#f file) |
| 119 |
(process-test (adjpath templ file))] |
| 120 |
[else (display line) (newline)])) |
| 121 |
read-line))))) |
| 122 |
0)) |