parser.scm 20.8 KB
Newer Older
1 2 3
(define-module (logic guile-log parser)
  #:use-module (logic guile-log parsing scanner)
  #:use-module (logic guile-log)
4
  #:use-module (logic guile-log fstream)
5
  #:use-module (logic guile-log umatch)
6
  #:use-module ((ice-9 match) #:renamer (symbol-prefix-proc 'ice:))
7
  #:use-module (ice-9 rdelim)
8
  #:use-module (ice-9 pretty-print)
9
  #:export (pmatch plambda X XL N M <p-define> <p-lambda> .. xx <p-cc>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
10 11 12
             s-scan-item
             f-test f-test!  f-char f-read
             f-char!
13
             clear-tokens mk-token tok-ws* tok-ws* Ds
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
14
             s* s+ fn f< f> f-pk f-do-nl
15 16
             mk-simple-token p-freeze
             make-file-reader file-next-line file-skip
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
17
             pr-char pr-test pr-reg
18 19 20
             s-tag s-tag! pr-tag s-clear-body pr-not
             s-rpl f-rpl s-tr f-tr
             f-and f-and! f-and!! 
21
             f-or  f-or! f-not* f-not f-not!
22
             f-seq f-seq! f-seq!!
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
23
             f* f+ f? f-tag f-tag! f-tag-pr	   
24
             f-reg f-reg! f-reg-pr
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
25
             f-false f-true f-id
26
             f-eof f-nl f-nl! f-nl-pr
27
	     f-line f-cons f-list f-append f-cons* ff? ff* ff+ f-out
28 29
             parse parse-no-clear
             *current-file-parsing*
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
30
             *translator* *whitespace*
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
31
	     setup-parser
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
32 33
	     f-clear-body
	     f-ichar))
34
 
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
35 36 37
(define *translator*           (make-fluid (lambda (x) x)))
(define *current-file-parsing* (make-fluid 'repl))
(define *whitespace* (make-fluid (lambda (s p cc . x) (apply cc s p x))))
38

39
(define do-print #f)
40 41 42 43 44 45 46 47 48 49 50
(define pp
  (case-lambda
   ((s x)
    (when do-print
      (pretty-print `(,s ,(syntax->datum x))))
    x)
   ((x)
    (when do-print
      (pretty-print (syntax->datum x)))
    x)))

51 52 53 54 55 56 57 58 59 60 61
(define ppp
  (case-lambda
   ((s x)
    (when #t
      (pretty-print `(,s ,(syntax->datum x))))
    x)
   ((x)
    (when #t
      (pretty-print (syntax->datum x)))
    x)))

62 63
(define-syntax-rule (Ds f) (lambda x (apply f x)))

64 65 66 67 68 69 70 71 72 73
(define *freeze-map*   #f)

(define head #f)
(define clear-tokens
  (let ((fl (make-fluid #f)))
    (set! *freeze-map* fl)
    (lambda ()
      (let ((ht (make-hash-table)))
	(set! head ht)
	(fluid-set! fl ht)))))
74 75
(clear-tokens)

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
(define make-file-reader 
  (case-lambda
   ((stream)
    (if (port? stream)
	(rw-fstream-from-port stream)
	(let ((s (fluid-ref stream)))
	  (cond
	   ((fstream-rw? s)
	    s)
	   ((fstream-r? s)
	    (r->rw-fstream s))))))
   (()
    (make-file-reader (current-input-port)))))

(define (print-last-line n xl)
  (if (and do-print (not (feof? xl)))
      (if (and (fluid? xl) (fstream-rw? (fluid-ref xl)))
	  (format #t "endline (~a) : ~a~%" n
		  (list->string (reverse (freadline-l xl)))))))

(define file-next-line  
97
  (case-lambda 
98 99 100 101
   ((data i)
    (error "not supported file-next-line verison"))
   ((data)   
    (freadline data))))
102

103
(define (file-skip n data) ((caddr data) (+ n (car data))))
104

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
(define-guile-log-parser-tool (<p-lambda> (X XL N M)) <p-define> .. xx <p-cc>)

(make-guile-log-scanner-tools <p-lambda> <fail> <p-cc> <succeds> .. 
			      (X XL N M)
			      (c) (d)
			      s-false s-true s-mk-seq s-mk-and s-mk-or)


(define wrap-n
  (lambda (f)
    (lambda (s p cc x xl n m . u)
      (let ((c  (cdr (reverse u)))
	    (cc (lambda (s p x xl n m c) (cc s p x xl n m 
					      (reverse 
					       (cons c 
						     (cdr (reverse u))))))))
      (f s p cc x xl n m c)))))

(define f-nl
  (lambda (s p cc x xl n m . u)
    (ice:match x
      ('()
127 128
       (call-with-values (lambda () (freadline xl))
	 (lambda (xl x)
129
	   (if (not (and (null? x) (feof? xl)))
130
	       (apply cc s p x xl 0 (+ m 1) u)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
131 132 133 134 135 136 137 138 139
	       (p)))))
      (_ (p)))))


(define f-nl!
  (wrap-n
  (lambda (s p cc x xl n m c)
    (ice:match x
      ('()
140 141
       (call-with-values (lambda () (freadline xl))
	 (lambda (xl x)
142
	   (if (not (and (null? x) (feof? xl)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
143 144 145
	       (let* ((v (gp-var! s))
		      (s (gp-unify! c (cons #\newline v) s)))
		 (if s
146
		     (cc s p x xl 0 (+ m 1) v)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
147 148 149 150 151 152 153 154 155
		     (p)))
	       (p)))))
      (_ (p))))))


(define f-nl-pr
  (lambda (s p cc x xl n m . u)
    (ice:match x
      ('()
156
       (call-with-values (lambda () (freadline xl))
157
	 (lambda (xl x)
158
	   (if (not (and (null? x) (feof? xl)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
159 160
	       (begin
		 (format #t "~%")
161
		 (apply cc s p x xl 0 (+ m 1) u))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
162 163 164 165 166 167
	       (p)))))
      (_ (p)))))


(define f-do-nl
  (lambda (s p cc x xl n m . u)
168 169 170 171
    (call-with-values (lambda () (freadline xl))
      (lambda (xl x)
	(if (not (feof? xl))
	    (apply cc s p x xl 0 (+ m 1) u)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	    (p))))))

(define (s-scan-item nn k)
  (let ((i k))
    (lambda (f)
       (lambda (s p cc x xl n m . u)
	 (if (= i 0)
	     (begin
	       (set! i k)
	       (let ((xl-save xl))
		 (apply f s p (lambda (s p x xl n m . u)
			  (file-skip nn xl-save)
			  (apply cc s p x xl n m u))
		    x xl n m u)))
	     (begin
	       (set! i (- i 1))
	       (apply f s p cc x xl n m u)))))))


191
(define-syntax-rule (setup-parser-0
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
192
		     <p-define> <p-lambda> <fail> <p-cc> <succeds> .. xx
193
		     X XL ((N NI) (M MI) (XX Init) ...)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
194
		     s-false s-true s-mk-seq s-mk-and s-mk-or
195
                     f-read f-test f-test! pr-test f-tag f-tag! pr-tag
196
                     chtr f-id f-pk s-tag s-tag! spr-tag
197 198
		     s-seq s-and s-and! s-and!! s-and-i s-or s-or-i
                     f-or f-or! f-and f-and! f-seq f? f+ f* ff? ff+ ff*
199 200
                     f-line f-cons f-list f-cons* f-append mk-token p-freeze
		     parse 
201
                     f-out
202 203 204 205 206
                     f-true f-false f-nl ss f< f> fn f-eof
                     <s-match> <s-lambda> mk-simple-token f-clear-body
                     f-char f-char! pr-char f-reg f-reg! pr-reg
                     f-ws f-ws* pr-ws+ tok-ws* tok-ws+ parse-no-clear
                     s-rpl f-rpl s-tr f-tr f-1-char f-1-char! pr-1-char
207
                     f-not f-not! f-not* pr-not f-seq! f-seq!! f-deb
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
208
		     pp do-print f-wrap f-tag-pr f-ichar)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
209 210 211 212 213 214 215 216 217 218 219 220 221
(begin
(define f-read
 (<p-lambda> (c)
   (when (pair? X)
     (<let> ((a (chtr (car X)))
	     (l (cdr X))
	     (n (+ N 1)))
	(<syntax-parameterize> ((X (lambda x #'l))
				(N (lambda x #'n)))
	  (<p-cc> a))))))


			  
222
(define (f-test! f)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
223 224 225 226 227 228 229 230 231 232 233 234 235
 (<p-lambda> (c)
   (when (pair? X)
     (<let> ((a (chtr (car X)))
	     (l (cdr X))
	     (n (+ N 1)))
	(<match> (#:mode + #:name f-test!) (X c)    
	   (((= chtr (? f ch)) . l) (ch . cc)
	    (<syntax-parameterize> ((X (lambda x #'l))
				    (N (lambda x #'n)))
			 
	      (<p-cc> cc)))
	   (_  _ (<cut> <fail>)))))))

236
(define (f-test f)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
237 238 239 240 241 242 243 244 245 246 247 248 249
 (<p-lambda> (c)
   (when (pair? X)
     (<let> ((a (chtr (car X)))
	     (l (cdr X))
	     (n (+ N 1)))
	(<match> (#:mode + #:name f-test) (X)    
	   (((= chtr (? f ch)) . l)
	    (<syntax-parameterize> ((X (lambda x #'l))
				    (N (lambda x #'n)))
			 
	      (<p-cc> c)))
	   (_ (<cut> <fail>)))))))

250
(define (pr-test f)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
251 252 253 254 255 256 257 258 259 260 261 262 263
 (<p-lambda> (c)
   (when (pair? X)
     (<let> ((a (chtr (car X)))
	     (l (cdr X))
	     (n (+ N 1)))
	(<match> (#:mode + #:name pr-test!) (X)    
	   (((= chtr (? f ch)) . l)
	    (<syntax-parameterize> ((X (lambda x #'l))
				    (N (lambda x #'n)))
	      (<format> #t "~a" ch)
	      (<p-cc> c)))
	   (_ (<cut> <fail>)))))))

264

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
265
(define (chtr x) ((fluid-ref *translator*) x))
266

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
267
(<p-define> (f-id c) (<p-cc> c))
268 269 270 271 272 273 274 275 276 277 278

(s-mk-seq s-seq   <and>)

(s-mk-and s-and   <and>)
(s-mk-and s-and!  <and!>)
(s-mk-and s-and!! <and!!>)
(s-mk-and s-and-i <and-i>)

(s-mk-or  s-or    <or>)
(s-mk-or  s-or-i  <or-i>)

279 280 281 282 283 284 285
(define (head-n n x)
  (let lp ((i n) (x x) (r '()))
    (if (= i 0)
        (reverse r)
        (if (pair? x)
            (lp (- i 1) (cdr x) (cons (car x) r))
            (reverse r)))))
286

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
287
(define (f-pk nm)
288
  (<p-lambda> (c)
289 290 291 292 293 294 295 296
    (<pp-dyn> `(,nm ,(head-n 10 X) ,c) `(leaving ,nm))
    (<p-cc> c)))

(define (f-deb nm)
  (<p-lambda> (c)
    (if do-print
        (<pp-dyn> `(,nm ,X ,c) `(leaving ,nm))
        <cc>)
297 298
    (<p-cc> c)))

299 300 301 302 303 304 305 306 307
(define (f-wrap s f)
  (case-lambda
    ((x . l)
     (if (symbol? x)
         (s-seq (f-deb `(,x ,s)) (apply f l) (f-deb `(success ,x ,s)))
         (apply f x l)))
    (()
     (f))))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
308 309 310 311
(define (ss x)
  (cond
   ((string? x)
    (let ((ws (fluid-ref *whitespace*)))
312
      (f-seq ws (f-tag x) ws)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
313 314 315 316 317 318
   ((procedure?  x)
    x)
   (else
    (f-out x))))

(define f-and
319 320 321 322 323 324 325 326 327 328 329
  (f-wrap 'f-and
    (case-lambda 
      ((f)    (ss f))
      ((f g)  (s-and (ss f) (ss g)))
      ((f g . l) (s-and (ss f) (ss g) (apply f-and l)))
      (()     s-true))))

(define f-and!
  (f-wrap 'f-and!
    (lambda l
      (s-and! (apply f-and l)))))
330

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
331
(define f-or
332 333 334 335 336 337 338 339 340 341 342
  (f-wrap 'f-or
   (case-lambda 
     ((f)       (ss f))
     ((f g)     (s-or (ss f) (ss g)))
     ((f g . l) (s-or (ss f) (ss g) (apply f-or l)))
     (()         s-false))))

(define f-or!
  (f-wrap 'f-or!
   (lambda x
     (f-and! (apply f-or  x)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
343 344 345

(define (f-out tag)
  (lambda (s p cc . x)
346
    (apply cc s p (reverse (cons tag (cdr (reverse x)))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
347
    
348
(define (f> f n)    (letrec ((ret (lambda (n)
349 350 351 352
				    (if (= n 0)
					s-true
					(s-seq f (ret (- n 1)))))))
		      (ret n)))
353
(define (f< f n)    (letrec ((ret (lambda (n)
354 355
				    (if (= n 0)
					s-true
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
356
					(f-or! (s-seq f (ret (- n 1)))
357
					       s-true)))))
358
		      (ret n)))
359
(define (fn f n m) (s-and! (s-seq (f> f n) (f< f (- m n)))))
360 361 362

(define-guile-log  <s-match>
  (syntax-rules ()
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
363
    ((<s-match>  w (x xl n m c) (p1 p2 code (... ...)) (... ...))
364
     (<match> w (#:mode + #:name '<s-match>) (x c)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
365 366
       ((* * p1) p2 (<cut> (<and> code (... ...))))
       (... ...)
367 368 369 370 371 372
       (_ _   (<cut> <fail>))))))

(define-syntax-rule (<s-lambda> (x xl n m c) . l)
  (<lambda> (x xl n m c)
    (<s-match> (x xl n m c) . l)))

373 374 375 376 377 378 379 380
(define (mk-simple-token nm f)
 (<p-lambda> (x)
   (<var> (ret cin)
     (.. (cout) (f cin))
     (<=> cout '())
     (<=> ret (nm ,(list->string (<scm> cin))))
     (<p-cc> ret))))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
381
(<p-define> (f-eof cin)
382
   (if (and (null? X)
383
	    (call-with-values (lambda () (freadline XL))
384
	      (lambda (xl x) (feof? xl))))
385 386 387
       (<p-cc> cin)
       <fail>))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
388
(define (f-clear-body f)
389
  (<p-lambda> (c)
390
    (<let> ((op P)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
391
	    (os S)
392
	    (p  (<newframe>)))
393 394
      (<with-s> p
        (.. (c) (f c))
395
	(<code> (<unwind-tail> p))
396
	(<with-fail> op
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
397 398
	(<with-s>    os
	  (<p-cc> c)))))))
399

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
400 401 402
(define (f-char!  ch) (f-test! (lambda (x) (eq? x ch))))   
(define (f-char   ch) (f-test  (lambda (x) (eq? x ch))))   
(define (pr-char  ch) (pr-test (lambda (x) (eq? x ch))))   
403 404


405
(define (char->string x) (list->string (list x)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
406
(define (f-reg pat-str)
407
  (let ((reg (make-regexp pat-str)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
408
    (f-test (lambda (x) 
409 410 411
	      (let ((x (char->string x)))
		(regexp-exec reg x))))))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
412
(define (f-reg! pat-str)
413
  (let ((reg (make-regexp pat-str)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
414
    (f-test! (lambda (x) 
415 416 417
	       (let ((x (char->string x)))
		 (regexp-exec reg x))))))

418
(define (pr-reg pat-str)
419 420 421 422 423
  (let ((reg (make-regexp pat-str)))
    (pr-test (lambda (x) 
	       (let ((x (char->string x)))
		 (regexp-exec reg x))))))

424 425 426 427 428 429
;; The idea of this function is to perform a tokenizing activity
;; utilizing this means that we loose the ability to redo and undo
;; inside the scanner part.
;; This is not ass effective as a regexp tokenizer but should be a
;; much faster then doing a full parser of everything.

430 431 432 433 434
(define (mk-token f)
 (<p-lambda> (c)
    (<var> (cnew)
       (.. (cc) (f cnew))	       
       (<=> cc '())
435 436 437
       (<let*> ((x   (<scm> cnew))
		(ret (if (gp-var? x S) "" (list->string x))))
	 (<p-cc> ret)))))
438

439 440

(define (p-freeze tok f mk)
441 442
  (<p-lambda> (c)
     (<and!>
443 444
      (<let> ((val (hash-ref (fluid-ref *freeze-map*)
                             (cons* N M tok) #f))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
445 446 447
	      (op  P)
	      (os  S)
	      (fr  (<newframe>)))
448
	 (if (not val)
449
	     (<let> ((n N) (m M))
450 451 452
	       (<or>
                (<and>
                 (.. (cc) (f c))
453
                 (<let> ((val2 (mk S c cc)))		   
454
                   (<code>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
455 456 457 458 459 460 461
                    (<unwind-tail> fr))
		   (<with-fail> op
		   (<with-s>    os
		    (<code>
		     (hash-set! (fluid-ref *freeze-map*)
				(cons* n m tok) (list X XL N M XX ... val2)))
		    (<p-cc> val2)))))
462 463
                (<let> ((val2 'fail))
                  (<code>
464 465
		   (hash-set! (fluid-ref *freeze-map*)
			      (cons* n m tok) val2))
466
                  <fail>)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
467 468 469 470 471 472 473
	     (if (pair? val)
		 (<and>
		  (<code> (gp-unwind-tail fr))
		  (<with-fail> op
		  (<with-s>    os
		   (<apply> f-true val))))
		 <fail>))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
474 475

(define (f* f) (letrec ((ret (f-or (s-seq f ret) s-true))) (s-and! ret)))
476
(define (f+ f) (s-seq f (f* f)))
477
(define f-ws  (f-or! (f-char #\space) f-nl (f-char #\tab)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
478 479
(define f-ws* (f* f-ws))
(define f-ws+ (f+ f-ws))
480

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
481 482
(define tok-ws* (f-or!
		 (p-freeze 'ws* (mk-token (s-seq f-ws f-ws f-ws*))
483
			   (lambda (s cin cout) cin))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
484
		 f-ws*))
485

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
486 487
(define tok-ws+ (f-or!
		 (p-freeze 'ws+ (mk-token (s-seq f-ws f-ws f-ws*))
488
			   (lambda (s cin cout) cin))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
489
		 f-ws+))
490

491
(define parse*
492 493
  (case-lambda
   ((str matcher)
494
    (define f
495
      (lambda (stream)
496 497
        (with-fluids ((*freeze-map* (fluid-ref *freeze-map*)))
          (clear-tokens)
498
          ((<lambda> ()
499 500
	    (<values> (x xl n . l)
			(matcher '() (make-file-reader stream) 
501
				 NI MI Init ... <_>))
502
	      (<code> (print-last-line n (if xl xl '())))
503 504 505 506
	      (<cc> (car (reverse l))))
	   (fluid-ref *current-stack*)
	   (lambda () #f)
	   (lambda (s p cc) cc)))))
507
    (if (string? str)
508
	(f (make-fluid (rw-fstream-from-string str)))
509
	(f str)))
510

511
   ((matcher)
512 513
    (with-fluids ((*freeze-map* (fluid-ref *freeze-map*)))
      (clear-tokens)
514 515
      ((<lambda> ()
	(<and>
516
	 (<values> (x xl n . l)
517
		   (matcher '() (make-file-reader) NI MI Init ... <_>))
518
	 (<code> (print-last-line n (if xl xl '())))
519 520 521 522 523
	 (<cc> (car (reverse l)))))
       (fluid-ref *current-stack*)
       (lambda () #f)
       (lambda (s p cc) cc))))))

524 525 526 527 528 529
(define parse
  (case-lambda
    ((str matcher)
     (parse* str (f-seq f-nl matcher)))
    ((matcher)
     (parse* matcher))))
530

531 532 533 534 535
(define (equalize stream x xl nstart)
  (define n (- (rw-fstream-n xl) (length x)))
  (if (port? stream)
      (seek stream (+ nstart n) SEEK_SET)
      (let ((s (fluid-ref stream)))
536
	(fluid-set! stream (fmove s n)))))
537 538 539 540 541 542 543 544 545 546 547 548 549

(define (get-pos stream)
  (cond
   ((port? stream)
    (file-position stream))
   ((fluid? stream)
    (let ((s (fluid-ref stream)))
      (if (fstream? s)
	  (fposition s)
	  0)))
   (else
    0)))

550 551
(define parse-no-clear
  (case-lambda
552 553
   ((stream str matcher)
    (define N (get-pos stream))
554 555
    (define f
      (lambda ()
556 557 558 559
	(with-fluids ((*freeze-map* (fluid-ref *freeze-map*)))
	  (clear-tokens)
	  (<run> 1 (cout) 
		 (<values> (x xl n m out)
560
			   (matcher '() (make-file-reader stream) 0 0 <_>))
561 562 563 564
		 (<code> 
		  (equalize stream x xl N)
		  (print-last-line n xl))
		 (<=> cout out)))))
565 566 567 568 569

    (if (string? str)
	(with-input-from-string str f)
	(with-input-from-port   str f)))

570 571
   ((stream matcher)
    (define N (get-pos stream))
572 573 574
    (with-fluids ((*freeze-map* (fluid-ref *freeze-map*)))
      (clear-tokens)
      (<run> 1 (cout) 
575
	   (<values> (x xl n m out)
576
		     (matcher '() (make-file-reader stream) 0 0 <_>))
577 578 579
           (<code>
	    (equalize stream x xl N) 
	    (print-last-line n xl))
580
	   (<=> cout out))))))
581

582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
(define (s-rpl m y)
  (<p-lambda> (c)
    (.. (c) (m c))
    (<format> #t "~a" y)
    (<p-cc> c)))

(define (f-rpl m f)
  (<p-lambda> (c1)
    (.. (c2) (m c1))
    (<code> (f c2))
    (<p-cc> c2)))


(define-syntax-rule (s-tr x y) (s-rpl (s-tag x) y))
(define-syntax-rule (f-tr x y) (f-rpl (mk-token (s-tag! x)) y))
598
	    
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
599 600
(define f-1-char  (f-reg  "."))
(define f-1-char! (f-reg! "."))
601 602
(define pr-1-char (pr-reg "."))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
603
(define (f-not f)
604 605
  (<p-lambda> (c)
    (<not> (<and> (.. (q) (f c))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
606
    (.. (c) (f-1-char c))
607 608
    (<p-cc> c)))

609 610 611 612 613
(define (f-not* f)
  (<p-lambda> (c)
    (<not> (<and> (.. (q) (f c))))
    (<p-cc> c)))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
614
(define (f-not! f)
615 616
  (<p-lambda> (c)
    (<not> (<and> (.. (q) (f c))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
617
    (.. (c) (f-1-char! c))
618 619
    (<p-cc> c)))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
620 621 622 623 624 625 626 627
(define (f-ichar ch)
  (let ((ch (if (string? ch) 
		(car (string->list ch))
		ch)))
    (<p-lambda> (c)
      (<var> (v)
	(<=> (ch . v) c)
	(<p-cc> v)))))
628

629 630 631 632 633
(define (pr-not f)
  (<p-lambda> (c)
    (<not> (<and> (.. (q) (f c))))
    (.. (c) (pr-1-char c))
    (<p-cc> c)))
634

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
635
(define f-seq
636 637 638 639 640 641 642
  (f-wrap 'f-seq
    (case-lambda 
      ((f)       (ss f))
      ((f g)     (s-seq (ss f) (ss g)))
      ((f g . l) (s-seq (ss f) (ss g) (apply f-seq l)))
      (()         s-true))))

643 644 645 646
(define f-line
  (<p-lambda> (c)
    (<p-cc> (cons M N))))

647 648 649 650 651 652
(define f-cons
  (f-wrap 'f-cons
    (lambda (f g)
    (<p-lambda> (c)
      (.. (c1) ((ss f) c))
      (.. (c2) ((ss g) c1))
653 654 655
      (if (eq? c1 c)
	  (<p-cc> c2)
	  (<p-cc> (cons c1 c2)))))))
656

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
657
(define f-cons*
658 659 660 661 662 663
  (f-wrap 'f-cons*
    (case-lambda
      ((x)    (ss x))
      ((x y)  (f-cons x y))
      ((x y . l)
       (f-cons x (apply f-cons* y l))))))
664

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
665
(define f-list
666 667 668 669 670 671 672
  (f-wrap 'f-list
    (case-lambda
      ((x)    (f-cons x (f-out '())))
      ((x y)  (f-cons x (f-cons y (f-out '()))))
      ((x y . l)
       (f-cons x (apply f-list y l))))))

673 674 675 676 677 678 679 680 681 682 683 684 685
(define f-append
  (f-wrap 'f-list
    (case-lambda
      ((x)    x)
      ((f g)  
       (<p-lambda> (c)
         (.. (c1) ((ss f) c))
         (.. (c2) ((ss g) c1))
         (<p-cc> (append c1 c2))))
       
      ((x y . l)
       (f-append x (apply f-append y l))))))

686

687 688 689 690
(define f*
  (f-wrap 'f*
    (lambda (x)
      (f-or! (s-seq (s-and! (ss x)) (Ds (f* x))) s-true))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
691 692

(define ff*
693 694 695 696 697 698 699 700 701 702 703
  (f-wrap 'ff*
   (case-lambda 
     ((x)
      (f-or! (f-cons (f-and! (ss x)) (Ds (ff* x))) (f-out '())))
     ((x d)
      (f-or! (f-cons (f-and! (ss x)) (Ds (ff* x d))) (f-out d))))))

(define f?
  (f-wrap 'f?
    (lambda (x)
      (f-or! (ss x) s-true))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
704 705

(define ff? 
706 707 708 709 710 711
  (f-wrap 'ff?
    (case-lambda
      ((x)
       (f-or! (ss x) (f-out #f)))
      ((x default)
       (f-or! (ss x) (f-out default))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
712 713
   

714 715 716 717
(define f+ 
  (f-wrap 'f+
    (lambda (x)
      (s-seq (ss x) (f* x)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
718

719 720 721 722
(define ff+
  (f-wrap 'ff+
    (lambda (x . l)
      (f-cons (ss x) (apply ff* x l)))))
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
(define-syntax s-tag
  (lambda (x)
    (syntax-case x ()
      ((_ x)
       (let* ((w (syntax->datum #'x))
	      (w (cond
		  ((symbol? w) (string->list (symbol->string w)))
		  ((string? w) (string->list w))
		  (else #f))))
	 (if w		   		 
	     (with-syntax (((ch (... ...)) w))
               #'(f-seq (f-char ch) (... ...)))
	     (error "argument to s-tag is either string or symbol")))))))

(define-syntax s-tag!
  (lambda (x)
    (syntax-case x ()
      ((_ x)
       (let* ((w (syntax->datum #'x))
	      (w (cond
		  ((symbol? w) (string->list (symbol->string w)))
		  ((string? w) (string->list w))
		  (else #f))))
	 (if w		   		 
	     (with-syntax (((ch (... ...)) w))
		#'(f-seq (f-char! ch) (... ...)))
	     (error "argument to s-tag is either string or symbol")))))))

(define-syntax spr-tag
  (lambda (x)
    (syntax-case x ()
      ((_ x)
       (let* ((w (syntax->datum #'x))
	      (w (cond
		  ((symbol? w) (string->list (symbol->string w)))
		  ((string? w) (string->list w))
		  (else #f))))
	 (if w		   		 
	     (with-syntax (((ch (... ...)) w))
		#'(f-seq (pr-char ch) (... ...)))
	     (error "argument to s-tag is either string or symbol")))))))

766
(define (f-tag x)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
767
  (let ((l ((@ (guile) map) (lambda (x) (f-char x))
768 769
	    (string->list (format #f "~a" x)))))
    (apply f-seq l)))
770
(define (f-tag! x)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
771
  (let ((l ((@ (guile) map) (lambda (x) (f-char! x))
772 773 774 775 776 777 778
	    (string->list (format #f "~a" x)))))
    (apply f-seq l)))
(define (f-tag-pr x)
  (let ((l ((@ (guile) map) (lambda (x) (pr-char x))
	    (string->list (format #f "~a" x)))))
    (apply f-seq l)))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
779 780 781
(define (f-seq!  . x) (f-and! (apply f-seq x)))
(define (f-seq!! . x) (apply f-seq (map (lambda (x) (f-and! x)) x)))
(define (f-and!! . x) (apply f-and (map (lambda (x) (f-and! x)) x)))
782

783 784
(define f-true s-true)
(define f-false s-false)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
785 786
))

787 788
(eval-when (compile load eval)
  (define names '(f-read f-test f-test! pr-test f-tag f-tag! pr-tag
789
                         chtr f-id f-pk s-tag s-tag! spr-tag
790 791
                         s-seq s-and s-and! s-and!! s-and-i s-or s-or-i
                         f-or f-or! f-and f-and! f-seq f? f+ f* ff? ff+ ff*
792
                         f-line f-cons f-list f-cons* f-append
793
                         mk-token p-freeze parse f-out
794 795 796 797 798
                         f-true f-false f-nl ss f< f> fn f-eof
                         <s-match> <s-lambda> mk-simple-token f-clear-body
                         f-char f-char! pr-char f-reg f-reg! pr-reg
                         f-ws f-ws* pr-ws+ tok-ws* tok-ws+ parse-no-clear
                         s-rpl f-rpl s-tr f-tr f-1-char f-1-char! pr-1-char
799
                         f-not f-not! f-not*  pr-not f-seq! f-seq!! f-deb
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
800 801
                         pp do-print f-wrap f-tag-pr
			 f-ichar)))
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

(define-syntax setup-parser
  (lambda (x)
    (syntax-case x ()
      ((w <p-define> <p-lambda> <fail> <p-cc> <succeds> .. xx 
          X XL ((N NI) ...)
          s-false s-true s-mk-seq s-mk-and s-mk-or)
       
       (with-syntax (((nm ...) (map (lambda (x) 
                                      (datum->syntax #'w x))
                                    names)))
         #'(setup-parser-0
             <p-define> <p-lambda> <fail> <p-cc> <succeds> .. xx 
             X XL ((N NI) ...)
             s-false s-true s-mk-seq s-mk-and s-mk-or
             nm ...))))))

;; Creating the standard parser
(setup-parser  <p-define> <p-lambda> <fail> <p-cc> <succeds> .. xx 
               X XL ((N 0) (M 0))
822
               s-false s-true s-mk-seq s-mk-and s-mk-or)