2
;;; Copyright (c) 2010-2011 uim Project http://code.google.com/p/uim/
4
;;; All rights reserved.
6
;;; Redistribution and use in source and binary forms, with or without
7
;;; modification, are permitted provided that the following conditions
9
;;; 1. Redistributions of source code must retain the above copyright
10
;;; notice, this list of conditions and the following disclaimer.
11
;;; 2. Redistributions in binary form must reproduce the above copyright
12
;;; notice, this list of conditions and the following disclaimer in the
13
;;; documentation and/or other materials provided with the distribution.
14
;;; 3. Neither the name of authors nor the names of its contributors
15
;;; may be used to endorse or promote products derived from this software
16
;;; without specific prior written permission.
18
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19
;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
22
;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
;; ct.scm: provides rk-lib equivalent functions using composing table
33
;; following functions are used by rk.scm if table is used as a rule
35
;; ct-lib-expect-key-for-seq?
37
;; ct-lib-find-partial-seq
39
;; NB: composing table needs to be sorted
41
(require-dynlib "look")
43
(define ct-lib-find-seq
45
(let ((looked (look-lib-look
49
(string-append (sys-pkgdatadir) "/tables/" table)
50
(apply string-append seq))))
54
(= (string-contains (car looked) " " 0) 0))
55
(list (list seq) (read-from-string (car looked)))
58
;; return a rule of partial match
59
(define ct-lib-find-partial-seq
61
;; search 2 entries matching (including partial match) with look
62
(let ((looked (look-lib-look
66
(string-append (sys-pkgdatadir) "/tables/" table)
67
(apply string-append seq))))
71
(let ((first (car looked))
72
(second (if (null? (cdr looked))
76
;; second one is partial
79
(string=? (substring first 0 1) " "))
82
(string-to-list (car (string-split second " ")))))
84
(apply string-append (cdr (string-split second " ")))))
85
(list (list (append seq partial)) (read-from-string cands))))
86
;; first one is partial
87
((not (string=? (substring first 0 1) " "))
90
(string-to-list (car (string-split first " ")))))
92
(apply string-append (cdr (string-split first " ")))))
93
(list (list (append seq partial)) (read-from-string cands))))
98
(define ct-lib-expect-key-for-seq?
99
(lambda (seq table str)
100
(let* ((lst (ct-find-cands-incl-minimal-partial seq table))
102
(filter-map (lambda (x) (if (string=? (cdr x) "")
104
(substring (cdr x) 0 1))) lst)))
105
(if (member str residuals)
109
(define ct-lib-expect-seq
110
(lambda (seq table keystr)
111
(let* ((lst (ct-find-cands-incl-minimal-partial seq table))
113
(filter-map (lambda (x) (if (string=? (cdr x) "")
115
(substring (cdr x) 0 1))) lst)))
118
(define ct-find-cands-incl-minimal-partial
120
(let ((looked (look-lib-look
123
5000 ;; is it sufficient enough?
124
(string-append (sys-pkgdatadir) "/tables/" table)
125
(apply string-append seq))))
128
(not (null? looked)))
129
(let* ((min-partial-pos
131
(let ((maxlen (apply max (map string-length lst))))
139
(string=? (substring x n (+ n 1)) " "))
143
(pos (min-partial-pos looked))
147
(or (string=? (substring x pos (+ pos 1)) " ")
148
(string=? (substring x 0 1) " ")))
150
(str (map (lambda (x) (string-split x " ")) match))
151
(residual (map (lambda (x) (car x)) str))
155
(read-from-string (apply string-append (cdr x)))) str))
156
(lst (map (lambda (x y) (cons x y)) cands residual)))