home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / chrsyn.scm < prev    next >
Text File  |  2000-11-20  |  5KB  |  144 lines

  1. ;;; -*-Scheme-*-
  2. ;;;
  3. ;;; $Id: chrsyn.scm,v 1.3 2000/11/20 13:25:41 cph Exp $
  4. ;;;
  5. ;;; Copyright (c) 1986, 1989-2000 Massachusetts Institute of Technology
  6. ;;;
  7. ;;; This program is free software; you can redistribute it and/or
  8. ;;; modify it under the terms of the GNU General Public License as
  9. ;;; published by the Free Software Foundation; either version 2 of the
  10. ;;; License, or (at your option) any later version.
  11. ;;;
  12. ;;; This program is distributed in the hope that it will be useful,
  13. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. ;;; General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with this program; if not, write to the Free Software
  19. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;;; Character-Syntax Tables
  22. ;;; package: (runtime char-syntax)
  23.  
  24. (declare (usual-integrations))
  25.  
  26. (define-structure (char-syntax-table (constructor %make-char-syntax-table)
  27.                      (conc-name char-syntax-table/))
  28.   (entries #f read-only #t))
  29.  
  30. (define (guarantee-char-syntax-table table procedure)
  31.   (if (not (char-syntax-table? table))
  32.       (error:wrong-type-argument table "char-syntax table" procedure))
  33.   (char-syntax-table/entries table))
  34.  
  35. (define (make-char-syntax-table #!optional table)
  36.   (%make-char-syntax-table
  37.    (vector-copy
  38.     (if (or (default-object? table) (not table))
  39.     (char-syntax-table/entries standard-char-syntax-table)
  40.     (guarantee-char-syntax-table table 'MAKE-CHAR-SYNTAX-TABLE)))))
  41.  
  42. (define (get-char-syntax table char)
  43.   (if (not (char? char))
  44.       (error:wrong-type-argument char "character" 'GET-CHAR-SYNTAX))
  45.   (vector-ref (guarantee-char-syntax-table table 'GET-CHAR-SYNTAX)
  46.           (char->ascii char)))
  47.  
  48. (define (set-char-syntax! table char string)
  49.   (let ((entries (guarantee-char-syntax-table table 'SET-CHAR-SYNTAX!))
  50.     (entry (string->char-syntax string)))
  51.     (cond ((char? char)
  52.        (vector-set! entries (char->ascii char) entry))
  53.       ((char-set? char)
  54.        (for-each (lambda (char)
  55.                (vector-set! entries (char->ascii char) entry))
  56.              (char-set-members char)))
  57.       (else
  58.        (error:wrong-type-argument char "character" 'SET-CHAR-SYNTAX!)))))
  59.  
  60. (define standard-char-syntax-table)
  61.  
  62. (define (initialize-package!)
  63.   (let ((table
  64.      (%make-char-syntax-table
  65.       (make-vector 256 (string->char-syntax "")))))
  66.     (set-char-syntax! table char-set:alphanumeric "w")
  67.     (set-char-syntax! table #\$ "w")
  68.     (set-char-syntax! table #\% "w")
  69.     (set-char-syntax! table #\( "()")
  70.     (set-char-syntax! table #\) ")(")
  71.     (set-char-syntax! table #\[ "(]")
  72.     (set-char-syntax! table #\] ")[")
  73.     (set-char-syntax! table #\{ "(}")
  74.     (set-char-syntax! table #\} "){")
  75.     (set-char-syntax! table #\" "\"")
  76.     (set-char-syntax! table #\\ "\\")
  77.     (set-char-syntax! table (string->char-set "_-+*/&|<>=") "_")
  78.     (set-char-syntax! table (string->char-set ".,;:?!#@~^'`") ".")
  79.     (set! standard-char-syntax-table table)
  80.     unspecific))
  81.  
  82. (define-primitives
  83.   (string->char-syntax string->syntax-entry))
  84.  
  85. (define (char-syntax->string entry)
  86.   (guarantee-char-syntax entry 'CHAR-SYNTAX->STRING)
  87.   (let ((code (fix:and #xf entry)))
  88.     (string-append
  89.      (vector-ref char-syntax-codes code)
  90.      (let ((match (fix:and #xff (fix:lsh entry -4))))
  91.        (if (zero? match)
  92.        " "
  93.        (string (ascii->char match))))
  94.      (let ((cbits (fix:and #xFF (fix:lsh entry -12))))
  95.        (string-append
  96.     (if (fix:= 0 (fix:and #x40 cbits)) "" "1")
  97.     (if (fix:= 0 (fix:and #x10 cbits)) "" "2")
  98.     (if (fix:= 0 (fix:and #x04 cbits)) "" "3")
  99.     (if (fix:= 0 (fix:and #x01 cbits)) "" "4")
  100.     (if (or (fix:= 0 (fix:and #x80 cbits))
  101.         (and (fix:= code 11)
  102.              (fix:= #x80 (fix:and #xC0 cbits))))
  103.         ""
  104.         "5")
  105.     (if (fix:= 0 (fix:and #x20 cbits)) "" "6")
  106.     (if (or (fix:= 0 (fix:and #x08 cbits))
  107.         (and (fix:= code 12)
  108.              (fix:= #x08 (fix:and #x0C cbits))))
  109.         ""
  110.         "7")
  111.     (if (fix:= 0 (fix:and #x02 cbits)) "" "8")))
  112.      (if (fix:= 0 (fix:and #x100000 entry)) "" "p"))))
  113.  
  114. (define (guarantee-char-syntax object procedure)
  115.   (if (not (index-fixnum? object))
  116.       (error:wrong-type-argument object "non-negative fixnum" procedure))
  117.   (if (not (and (fix:< object #x200000)
  118.         (fix:<= (fix:and #xf object) 12)))
  119.       (error:bad-range-argument object procedure)))
  120.  
  121. (define char-syntax-codes
  122.   '#(" " "." "w" "_" "(" ")" "'" "\"" "$" "\\" "/" "<" ">"))
  123.  
  124. (define (substring-find-next-char-of-syntax string start end table code)
  125.   (guarantee-substring string start end 'SUBSTRING-FIND-NEXT-CHAR-OF-SYNTAX)
  126.   (let loop ((index start))
  127.     (and (fix:< index end)
  128.      (if (char=? code (char->syntax-code table (string-ref string index)))
  129.          index
  130.          (loop (fix:+ index 1))))))
  131.  
  132. (define (substring-find-next-char-not-of-syntax string start end table code)
  133.   (guarantee-substring string start end
  134.                'SUBSTRING-FIND-NEXT-CHAR-NOT-OF-SYNTAX)
  135.   (let loop ((index start))
  136.     (and (fix:< index end)
  137.      (if (char=? code (char->syntax-code table (string-ref string index)))
  138.          (loop (fix:+ index 1))
  139.          index))))
  140.  
  141. (define (char->syntax-code table char)
  142.   (string-ref (vector-ref char-syntax-codes
  143.               (fix:and #xf (get-char-syntax table char)))
  144.           0))