aboutsummaryrefslogtreecommitdiff
path: root/legacy/bigloo/source.scm
blob: babadff3b1ee64f476e5aa72bcc4ebcd9369c260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/source.scm                */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Fri Aug 29 07:27:25 2003                          */
;*    Last change :  Tue Nov  2 14:25:50 2004 (serrano)                */
;*    Copyright   :  2003-04 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    The Bigloo handling of Skribe programs.                          */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_source
    
   (include "new.sch")
   
   (import  skribe_types
	    skribe_lib
	    skribe_resolve
	    skribe_eval
	    skribe_api
	    skribe_param)

   (export  (source-read-chars::bstring ::bstring ::int ::int ::obj)
	    (source-read-lines::bstring ::bstring ::obj ::obj ::obj)
	    (source-read-definition::bstring ::bstring ::obj ::obj ::obj)
	    (source-fontify ::obj ::obj)
	    (split-string-newline::pair-nil ::bstring)))

;*---------------------------------------------------------------------*/
;*    source-read-lines ...                                            */
;*---------------------------------------------------------------------*/
(define (source-read-chars file start stop tab)
   (define (readl p)
      (read/rp (regular-grammar ()
		  ((: (* (out #\Newline)) (? #\Newline))
		   (the-string))
		  (else
		   (the-failure)))
	       p))
   (let ((p (find-file/path file (skribe-source-path))))
      (if (or (not (string? p)) (not (file-exists? p)))
	  (skribe-error 'source
			(format "Can't find `~a' source file in path" file)
			(skribe-source-path))
	  (with-input-from-file p
	     (lambda ()
		(if (>fx *skribe-verbose* 0)
		    (fprint (current-error-port) "  [source file: " p "]"))
		(let loop ((c -1)
			   (s (readl (current-input-port)))
			   (r '()))
		   (let ((p (input-port-position (current-input-port))))
		      (cond
			 ((eof-object? s)
			  (apply string-append (reverse! r)))
			 ((>=fx p stop)
			  (let* ((len (-fx (-fx stop start) c))
				 (line (untabify (substring s 0 len) tab)))
			     (apply string-append
				    (reverse! (cons line r)))))
			 ((>=fx c 0)
			  (loop (+fx (string-length s) c)
				(readl (current-input-port))
				(cons (untabify s tab) r)))
			 ((>=fx p start)
			  (let* ((len (string-length s))
				 (nc (-fx p start)))
			     (if (>fx p stop)
				 (untabify
				  (substring s
					     (-fx len (-fx p start))
					     (-fx (-fx p stop) 1))
				  tab)
				 (loop nc
				       (readl (current-input-port))
				       (list 
					(untabify
					 (substring s
						    (-fx len (-fx p start))
						    len)
					 tab))))))
			 (else
			  (loop c (readl (current-input-port)) r))))))))))

;*---------------------------------------------------------------------*/
;*    source-read-lines ...                                            */
;*---------------------------------------------------------------------*/
(define (source-read-lines file start stop tab)
   (let ((p (find-file/path file (skribe-source-path))))
      (if (or (not (string? p)) (not (file-exists? p)))
	  (skribe-error 'source
			(format "Can't find `~a' source file in path" file)
			(skribe-source-path))
	  (with-input-from-file p
	     (lambda ()
		(if (>fx *skribe-verbose* 0)
		    (fprint (current-error-port) "  [source file: " p "]"))
		(let ((startl (if (string? start) (string-length start) -1))
		      (stopl (if (string? stop) (string-length stop) -1)))
		   (let loop ((l 1)
			      (armedp (not (or (integer? start)
					       (string? start))))
			      (s (read-line))
			      (r '()))
		      (cond
			 ((or (eof-object? s)
			      (and (integer? stop) (> l stop))
			      (and (string? stop) (substring=? stop s stopl)))
			  (apply string-append (reverse! r)))
			 (armedp
			  (loop (+fx l 1)
				#t
				(read-line)
				(cons* "\n" (untabify s tab) r)))
			 ((and (integer? start) (>= l start))
			  (loop (+fx l 1)
				#t
				(read-line)
				(cons* "\n" (untabify s tab) r)))
			 ((and (string? start) (substring=? start s startl))
			  (loop (+fx l 1) #t (read-line) r))
			 (else
			  (loop (+fx l 1) #f (read-line) r))))))))))

;*---------------------------------------------------------------------*/
;*    untabify ...                                                     */
;*---------------------------------------------------------------------*/
(define (untabify obj tab)
   (if (not tab)
       obj
       (let ((len (string-length obj))
	     (tabl tab))
	  (let loop ((i 0)
		     (col 1))
	     (cond
		((=fx i len)
		 (let ((nlen (-fx col 1)))
		    (if (=fx len nlen)
			obj
			(let ((new (make-string col #\space)))
			   (let liip ((i 0)
				      (j 0)
				      (col 1))
			      (cond
				 ((=fx i len)
				  new)
				 ((char=? (string-ref obj i) #\tab)
				  (let ((next-tab (*fx (/fx (+fx col tabl)
							    tabl)
						       tabl)))
				     (liip (+fx i 1)
					   next-tab
					   next-tab)))
				 (else
				  (string-set! new j (string-ref obj i))
				  (liip (+fx i 1) (+fx j 1) (+fx col 1)))))))))
		((char=? (string-ref obj i) #\tab)
		 (loop (+fx i 1)
		       (*fx (/fx (+fx col tabl) tabl) tabl)))
		(else
		 (loop (+fx i 1) (+fx col 1))))))))

;*---------------------------------------------------------------------*/
;*    source-read-definition ...                                       */
;*---------------------------------------------------------------------*/
(define (source-read-definition file definition tab lang)
   (let ((p (find-file/path file (skribe-source-path))))
      (cond
	 ((not (%language-extractor lang))
	  (skribe-error 'source
			"The specified language has not defined extractor"
			lang))
	 ((or (not p) (not (file-exists? p)))
	  (skribe-error 'source
			(format "Can't find `~a' program file in path" file)
			(skribe-source-path)))
	 (else
	  (let ((ip (open-input-file p)))
	     (if (>fx *skribe-verbose* 0)
		 (fprint (current-error-port) "  [source file: " p "]"))
	     (if (not (input-port? ip))
		 (skribe-error 'source "Can't open file for input" p)
		 (unwind-protect
		    (let ((s ((%language-extractor lang) ip definition tab)))
		       (if (not (string? s))
			   (skribe-error 'source
					 "Can't find definition"
					 definition)
			   s))
		    (close-input-port ip))))))))

;*---------------------------------------------------------------------*/
;*    source-fontify ...                                               */
;*---------------------------------------------------------------------*/
(define (source-fontify o language)
   (define (fontify f o)
      (cond
	 ((string? o) (f o))
	 ((pair? o) (map (lambda (s) (if (string? s) (f s) (fontify f s))) o))
	 (else o)))
   (let ((f (%language-fontifier language)))
      (if (procedure? f)
	  (fontify f o)
	  o)))

;*---------------------------------------------------------------------*/
;*    split-string-newline ...                                         */
;*---------------------------------------------------------------------*/
(define (split-string-newline str)
   (let ((l (string-length str)))
      (let loop ((i 0)
		 (j 0)
		 (r '()))
	 (cond
	    ((=fx i l)
	     (if (=fx i j)
		 (reverse! r)
		 (reverse! (cons (substring str j i) r))))
	    ((char=? (string-ref str i) #\Newline)
	     (loop (+fx i 1)
		   (+fx i 1)
		   (if (=fx i j)
		       (cons 'eol r)
		       (cons* 'eol (substring str j i) r))))
	    ((and (char=? (string-ref str i) #a013)
		  (<fx (+fx i 1) l)
		  (char=? (string-ref str (+fx i 1)) #\Newline))
	     (loop (+fx i 2)
		   (+fx i 2)
		   (if (=fx i j)
		       (cons 'eol r)
		       (cons* 'eol (substring str j i) r))))
	    (else
	     (loop (+fx i 1) j r))))))