summaryrefslogtreecommitdiff
path: root/module/ice-9/peg.scm
blob: 261a53e424ac85fa1ee97870dd7eec0e45e6abaa (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
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
766
767
768
769
770
771
772
773
774
775
776
777
778
;;;; peg.scm --- Parsing Expression Grammar (PEG) parser generator
;;;;
;;;; 	Copyright (C) 2010, 2011 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;;;

(define-module (ice-9 peg)
  :export (peg-sexp-compile peg-string-compile context-flatten peg-parse define-nonterm define-nonterm-f peg-match get-code define-grammar define-grammar-f peg:start peg:end peg:string peg:tree peg:substring peg-record? keyword-flatten)
  :autoload (ice-9 pretty-print) (peg-sexp-compile peg-string-compile context-flatten peg-parse define-nonterm define-nonterm-f peg-match get-code define-grammar define-grammar-f keyword-flatten)
  :use-module (ice-9 pretty-print))

(use-modules (ice-9 pretty-print))

(eval-when (compile load eval)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; CONVENIENCE MACROS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (eeval exp)
  (eval exp (interaction-environment)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; MACRO BUILDERS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Safe-bind helps to bind macros safely.
;; e.g.:
;; (safe-bind
;;  (a b)
;;  `(,a ,b))
;; gives:
;; (#<uninterned-symbol a cc608d0> #<uninterned-symbol b cc608a0>)
(define-syntax safe-bind
  (lambda (x)
    (syntax-case x ()
      ((_ vals . actions)
       (datum->syntax x (apply safe-bind-f
                               (cons
                                (syntax->datum #'vals)
                                (syntax->datum #'actions))))))))
;; (define-macro (safe-bind vals . actions)
;;   (apply safe-bind-f (cons vals actions)))
(define (safe-bind-f vals . actions)
  `(let ,(map (lambda (val) `(,val (make-symbol ,(symbol->string val)))) vals)
     ,@actions))

;; Unsafe-bind is like safe-bind but uses symbols that are easier to read while
;; debugging rather than safe ones.  Currently unused.
;; (define-macro (unsafe-bind vals . actions)
;;   (apply unsafe-bind-f (cons vals actions)))
;; (define (unsafe-bind-f vals . actions)
;;   `(let ,(map (lambda (val) `(,val ',val)) vals)
;;      ,@actions))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; LOOPING CONSTRUCTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Perform ACTION. If it succeeded, return its return value.  If it failed, run
;; IF_FAILS and try again
(define-syntax until-works
  (lambda (x)
    (syntax-case x ()
      ((_ action if-fails)
       #'(let ((retval action))
           (while (not retval)
                  if-fails
                  (set! retval action))
           retval)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; GENERIC LIST-PROCESSING MACROS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Return #t if the list has only one element (calling length all the time on
;; potentially long lists was really slow).
(define-syntax single?
  (lambda (x)
    (syntax-case x ()
      ((_ lst)
       #'(and (list? lst) (not (null? lst)) (null? (cdr lst)))))))

;; Push an object onto a list.
(define-syntax push!
  (lambda (x)
    (syntax-case x ()
      ((_ lst obj)
       #'(set! lst (cons obj lst))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; CODE GENERATORS
;; These functions generate scheme code for parsing PEGs.
;; Conventions:
;;   accum: (all name body none)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Code we generate will be defined in a function, and always has to test
;; whether it's beyond the bounds of the string before it executes.
(define (cg-generic-lambda for-syntax str strlen at code)
  `(lambda (,str ,strlen ,at)
     (if (>= ,at ,strlen)
         #f
         ,code)))
;; The short name makes the formatting below much easier to read.
(define cggl cg-generic-lambda)

;; Optimizations for CG-GENERIC-RET below...
(define *op-known-single-body* '(cg-string cg-peg-any cg-range))
;; ...done with optimizations (could use more of these).

;; Code we generate will have a certain return structure depending on how we're
;; accumulating (the ACCUM variable).
(define (cg-generic-ret for-syntax accum name body-uneval at)
  (safe-bind
   (body)
   `(let ((,body ,body-uneval))
      ,(cond
        ((and (eq? accum 'all) name body)
         `(list ,at
                (cond
                 ((not (list? ,body)) (list ',name ,body))
                 ((null? ,body) ',name)
                 ((symbol? (car ,body)) (list ',name ,body))
                 (#t (cons ',name ,body)))))
        ((and (eq? accum 'name) name)
         `(list ,at ',name))
        ((and (eq? accum 'body) body)
         (cond
          ((member name *op-known-single-body*)
           `(list ,at ,body))
          (#t `(list ,at
                     (cond
                      (((@@ (ice-9 peg) single?) ,body) (car ,body))
                      (#t ,body))))))
        ((eq? accum 'none)
         `(list ,at '()))
        (#t
         (begin
           (pretty-print `(cg-generic-ret-error ,accum ,name ,body-uneval ,at))
           (pretty-print "Defaulting to accum of none.\n")
           `(list ,at '())))))))
;; The short name makes the formatting below much easier to read.
(define cggr cg-generic-ret)

;; Generates code that matches a particular string.
;; E.g.: (cg-string syntax "abc" 'body)
(define (cg-string for-syntax match accum)
  (let ((str (syntax str))
        (strlen (syntax strlen))
        (at (syntax at))
        (len (string-length match)))
    (datum->syntax for-syntax
     (cggl for-syntax str strlen at
           `(if (string=? (substring ,str ,at (min (+ ,at ,len) ,strlen))
                          ,match)
                ,(cggr for-syntax accum 'cg-string match `(+ ,at ,len))
                #f)))))

;; Generates code for matching any character.
;; E.g.: (cg-peg-any syntax 'body)
(define (cg-peg-any for-syntax accum)
  (safe-bind
   (str strlen at)
   (cggl for-syntax str strlen at
         (cggr for-syntax accum
               'cg-peg-any `(substring ,str ,at (+ ,at 1)) `(+ ,at 1)))))

;; Generates code for matching a range of characters between start and end.
;; E.g.: (cg-range syntax #\a #\z 'body)
(define (cg-range for-syntax start end accum)
  (safe-bind
   (str strlen at c)
   (cggl for-syntax str strlen at
         `(let ((,c (string-ref ,str ,at)))
            (if (and
                 (char>=? ,c ,start)
                 (char<=? ,c ,end))
                ,(cggr for-syntax accum 'cg-range `(string ,c) `(+ ,at 1))
                #f)))))

;; Filters the accum argument to peg-sexp-compile for buildings like string
;; literals (since we don't want to tag them with their name if we're doing an
;; "all" accum).
(define (builtin-accum-filter accum)
  (cond
   ((eq? accum 'all) 'body)
   ((eq? accum 'name) 'name)
   ((eq? accum 'body) 'body)
   ((eq? accum 'none) 'none)))
(define baf builtin-accum-filter)

;; Takes a value, prints some debug output, and returns it.
(define (error-val val)
  (begin
    (pretty-print val)
    (pretty-print "Inserting into code for debugging.\n")
    val))

;; Takes an arbitrary expressions and accumulation variable, then parses it.
;; E.g.: (peg-sexp-compile syntax '(and "abc" (or "-" (range #\a #\z))) 'all)
(define (peg-sexp-compile for-syntax match accum)
  (cond
   ((string? match) (cg-string for-syntax match (baf accum)))
   ((symbol? match) ;; either peg-any or a nonterminal
    (cond
     ((eq? match 'peg-any) (datum->syntax for-syntax
                                          (cg-peg-any for-syntax (baf accum))))
     ;; if match is any other symbol it's a nonterminal, so just return it
     (#t (datum->syntax for-syntax match))))
   ((or (not (list? match)) (null? match))
    ;; anything besides a string, symbol, or list is an error
    (datum->syntax for-syntax
                   (error-val `(peg-sexp-compile-error-1 ,match ,accum))))
   
   ((eq? (car match) 'range) ;; range of characters (e.g. [a-z])
    (datum->syntax for-syntax
                   (cg-range for-syntax (cadr match) (caddr match) (baf accum))))
   ((eq? (car match) 'ignore) ;; match but don't parse
    (peg-sexp-compile for-syntax (cadr match) 'none))
   ((eq? (car match) 'capture) ;; parse
    (peg-sexp-compile for-syntax (cadr match) 'body))
   ((eq? (car match) 'peg) ;; embedded PEG string
    (peg-string-compile for-syntax (cadr match) (baf accum)))
   ((eq? (car match) 'and)
    (datum->syntax for-syntax
                   (cg-and for-syntax (cdr match) (baf accum))))
   ((eq? (car match) 'or)
    (datum->syntax for-syntax
                   (cg-or for-syntax (cdr match) (baf accum))))
   ((eq? (car match) 'body)
    (if (not (= (length match) 4))
        (datum->syntax for-syntax
                       (error-val `(peg-sexp-compile-error-2 ,match ,accum)))
        (datum->syntax for-syntax
                       (apply cg-body for-syntax (cons (baf accum) (cdr match))))))
   (#t (datum->syntax for-syntax
                      (error-val `(peg-sexp-compile-error-3 ,match ,accum))))))

;;;;; Convenience macros for making sure things come out in a readable form.
;; If SYM is a list of one element, return (car SYM), else return SYM.
(define-syntax single-filter
  (lambda (x)
    (syntax-case x ()
      ((_ sym)
       #'(if (single? sym) (car sym) sym)))))
;; If OBJ is non-null, push it onto LST, otherwise do nothing.
(define-syntax push-not-null!
  (lambda (x)
    (syntax-case x ()
      ((_ lst obj)
       #'(if (not (null? obj)) (push! lst obj))))))

;; Top-level function builder for AND.  Reduces to a call to CG-AND-INT.
(define (cg-and for-syntax arglst accum)
  (safe-bind
   (str strlen at body)
   `(lambda (,str ,strlen ,at)
      (let ((,body '()))
        ,(cg-and-int for-syntax arglst accum str strlen at body)))))

;; Internal function builder for AND (calls itself).
(define (cg-and-int for-syntax arglst accum str strlen at body)
  (safe-bind
   (res newat newbody)
   (if (null? arglst)
       (cggr for-syntax accum 'cg-and `(reverse ,body) at) ;; base case
       (let ((mf (syntax->datum
                  (peg-sexp-compile for-syntax (car arglst) accum)))) ;; match function
         `(let ((,res (,mf ,str ,strlen ,at)))
            (if (not ,res) 
                #f ;; if the match failed, the and failed
                ;; otherwise update AT and BODY then recurse
                (let ((,newat (car ,res))
                      (,newbody (cadr ,res)))
                  (set! ,at ,newat)
                  ((@@ (ice-9 peg) push-not-null!) ,body ((@@ (ice-9 peg) single-filter) ,newbody))
                  ,(cg-and-int for-syntax (cdr arglst) accum str strlen at body))))))))

;; Top-level function builder for OR.  Reduces to a call to CG-OR-INT.
(define (cg-or for-syntax arglst accum)
  (safe-bind
   (str strlen at body)
   `(lambda (,str ,strlen ,at)
      ,(cg-or-int for-syntax arglst accum str strlen at body))))

;; Internal function builder for OR (calls itself).
(define (cg-or-int for-syntax arglst accum str strlen at body)
  (safe-bind
   (res)
   (if (null? arglst)
       #f ;; base case
       (let ((mf (syntax->datum
                  (peg-sexp-compile for-syntax (car arglst) accum))))
         `(let ((,res (,mf ,str ,strlen ,at)))
            (if ,res ;; if the match succeeds, we're done
                ,(cggr for-syntax accum 'cg-or `(cadr ,res) `(car ,res))
                ,(cg-or-int for-syntax (cdr arglst) accum str strlen at body)))))))

;; Returns a block of code that tries to match MATCH, and on success updates AT
;; and BODY, return #f on failure and #t on success.
(define (cg-body-test for-syntax match accum str strlen at body)
  (safe-bind
   (at2-body2 at2 body2)
   (let ((mf (syntax->datum
              (peg-sexp-compile for-syntax match accum))))
     `(let ((,at2-body2 (,mf ,str ,strlen ,at)))
        (if (or (not ,at2-body2) (= ,at (car ,at2-body2)))
            #f
            (let ((,at2 (car ,at2-body2))
                  (,body2 (cadr ,at2-body2)))
              (set! ,at ,at2)
              ((@@ (ice-9 peg) push-not-null!)
               ,body
               ((@@ (ice-9 peg) single-filter) ,body2))
              #t))))))

;; Returns a block of code that sees whether NUM wants us to try and match more
;; given that we've already matched COUNT.
(define (cg-body-more for-syntax num count)
  (cond ((number? num) `(< ,count ,num))
        ((eq? num '+) #t)
        ((eq? num '*) #t)
        ((eq? num '?) `(< ,count 1))
        (#t (error-val `(cg-body-more-error ,num ,count)))))

;; Returns a function that takes a paramter indicating whether or not the match
;; was succesful and returns what the body expression should return.
(define (cg-body-ret for-syntax accum type name body at at2)
  (safe-bind
   (success)
   `(lambda (,success)
      ,(cond ((eq? type '!)
              `(if ,success #f ,(cggr for-syntax accum name ''() at)))
             ((eq? type '&)
              `(if ,success ,(cggr for-syntax accum name ''() at) #f))
             ((eq? type 'lit)
              `(if ,success
                   ,(cggr for-syntax accum name `(reverse ,body) at2) #f))
             (#t (error-val
                  `(cg-body-ret-error ,type ,accum ,name ,body ,at ,at2)))))))

;; Returns a block of code that sees whether COUNT satisfies the constraints of
;; NUM.
(define (cg-body-success for-syntax num count)
  (cond ((number? num) `(= ,count ,num))
        ((eq? num '+) `(>= ,count 1))
        ((eq? num '*) #t)
        ((eq? num '?) `(<= ,count 1))
        (#t `(cg-body-success-error ,num))))

;; Returns a function that parses a BODY element.
(define (cg-body for-syntax accum type match num)
  (safe-bind
   (str strlen at at2 count body)
   `(lambda (,str ,strlen ,at)
      (let ((,at2 ,at) (,count 0) (,body '()))
        (while (and ,(cg-body-test for-syntax match accum str strlen at2 body)
                    (set! ,count (+ ,count 1))
                    ,(cg-body-more for-syntax num count)))
        (,(cg-body-ret for-syntax accum type 'cg-body body at at2)
         ,(cg-body-success for-syntax num count))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; FOR DEFINING AND USING NONTERMINALS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; The results of parsing using a nonterminal are cached.  Think of it like a
;; hash with no conflict resolution.  Process for deciding on the cache size
;; wasn't very scientific; just ran the benchmarks and stopped a little after
;; the point of diminishing returns on my box.
(define *cache-size* 512)

(define (syntax-for-non-cache-case for-syntax matchf-syn accumsym s-syn)
;  (let ((matchf-syn (datum->syntax for-syntax matchf)))
   #`(lambda (str strlen at)
      (let ((res (#,matchf-syn str strlen at)))
        ;; Try to match the nonterminal.
        (if res
            ;; If we matched, do some post-processing to figure out
            ;; what data to propagate upward.
            (let ((at (car res))
                  (body (cadr res)))
              #,(cond
                 ((eq? accumsym 'name)
                  #`(list at '#,s-syn))
                 ((eq? accumsym 'all)
                  #`(list (car res)
                          (cond
                           ((not (list? body))
                            (list '#,s-syn body))
                           ((null? body) '#,s-syn)
                           ((symbol? (car body))
                            (list '#,s-syn body))
                           (#t (cons '#,s-syn body)))))
                 ((eq? accumsym 'none) #`(list (car res) '()))
                 (#t #`(begin res))))
            ;; If we didn't match, just return false.
            #f))))

;; Defines a new nonterminal symbol accumulating with ACCUM.
(define-syntax define-nonterm
  (lambda (x)
    (syntax-case x ()
      ((_ sym accum match)
       (let ((matchf (peg-sexp-compile x (syntax->datum #'match)
                                       (syntax->datum #'accum)))
             (accumsym (syntax->datum #'accum))
             (c (datum->syntax x (gensym))));; the cache
         ;; CODE is the code to parse the string if the result isn't cached.
         (let ((syn (syntax-for-non-cache-case x matchf accumsym #'sym)))
           #`(begin
               (define #,c (make-vector *cache-size* #f));; the cache
               (define (sym str strlen at)
                 (let* ((vref (vector-ref #,c (modulo at *cache-size*))))
                   ;; Check to see whether the value is cached.
                   (if (and vref (eq? (car vref) str) (= (cadr vref) at))
                       (caddr vref);; If it is return it.
                       (let ((fres ;; Else calculate it and cache it.
                              (#,syn str strlen at)))
                         (vector-set! #,c (modulo at *cache-size*)
                                      (list str at fres))
                         fres))))

               ;; Store the code in case people want to debug.
               (set-symbol-property!
                'sym 'code #,(datum->syntax x (list 'quote (syntax->datum syn))))
               sym)))))))

;; Gets the code corresponding to NONTERM
(define-syntax get-code
  (lambda (x)
    (syntax-case x ()
      ((_ nonterm)
       #`(pretty-print (symbol-property 'nonterm 'code))))))

;; Parses STRING using NONTERM
(define (peg-parse nonterm string)
  ;; We copy the string before using it because it might have been modified
  ;; in-place since the last time it was parsed, which would invalidate the
  ;; cache.  Guile uses copy-on-write for strings, so this is fast.
  (let ((res (nonterm (string-copy string) (string-length string) 0)))
    (if (not res)
        #f
        (make-prec 0 (car res) string (string-collapse (cadr res))))))

;; Searches through STRING for something that parses to PEG-MATCHER.  Think
;; regexp search.
(define-syntax peg-match
  (lambda (x)
    (syntax-case x ()
      ((_ peg-matcher string-uncopied)
       (let ((pmsym (syntax->datum #'peg-matcher)))
         (let ((peg-sexp-compile
                (if (string? pmsym)
                    (peg-string-compile x pmsym 'body)
                    (peg-sexp-compile x pmsym 'body))))
           ;; We copy the string before using it because it might have been
           ;; modified in-place since the last time it was parsed, which would
           ;; invalidate the cache.  Guile uses copy-on-write for strings, so
           ;; this is fast.
           #`(let ((string (string-copy string-uncopied))
                   (strlen (string-length string-uncopied))
                   (at 0))
               (let ((ret ((@@ (ice-9 peg) until-works)
                           (or (>= at strlen)
                               (#,peg-sexp-compile
                                string strlen at))
                           (set! at (+ at 1)))))
                 (if (eq? ret #t) ;; (>= at strlen) succeeded
                     #f
                     (let ((end (car ret))
                           (match (cadr ret)))
                       (make-prec
                        at end string
                        (string-collapse match))))))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; POST-PROCESSING FUNCTIONS (TO CANONICALIZE MATCH TREES)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Is everything in LST true?
(define (andlst lst)
  (or (null? lst)
      (and (car lst) (andlst (cdr lst)))))

;; Is LST a list of strings?
(define (string-list? lst)
  (and (list? lst) (not (null? lst))
       (andlst (map string? lst))))

;; Groups all strings that are next to each other in LST.  Used in
;; STRING-COLLAPSE.
(define (string-group lst)
  (if (not (list? lst))
      lst
      (if (null? lst)
          '()
          (let ((next (string-group (cdr lst))))
            (if (not (string? (car lst)))
                (cons (car lst) next)
                (if (and (not (null? next))
                         (list? (car next))
                         (string? (caar next)))
                    (cons (cons (car lst) (car next)) (cdr next))
                    (cons (list (car lst)) next)))))))


;; Collapses all the string in LST.
;; ("a" "b" (c d) "e" "f") -> ("ab" (c d) "ef")
(define (string-collapse lst)
  (if (list? lst)
      (let ((res (map (lambda (x) (if (string-list? x)
                                      (apply string-append x)
                                      x))
                      (string-group (map string-collapse lst)))))
        (if (single? res) (car res) res))
      lst))

;; If LST is an atom, return (list LST), else return LST.
(define (mklst lst)
  (if (not (list? lst)) (list lst) lst))

;; Takes a list and "flattens" it, using the predicate TST to know when to stop
;; instead of terminating on atoms (see tutorial).
(define (context-flatten tst lst)
  (if (or (not (list? lst)) (null? lst))
      lst
      (if (tst lst)
          (list lst)
          (apply append
                 (map (lambda (x) (mklst (context-flatten tst x)))
                      lst)))))

;; Takes a list and "flattens" it, using the list of keywords KEYWORD-LST to
;; know when to stop at (see tutorial).
(define (keyword-flatten keyword-lst lst)
  (context-flatten
   (lambda (x)
     (if (or (not (list? x)) (null? x))
         #t
         (member (car x) keyword-lst)))
   lst))

;; Gets the left-hand depth of a list.
(define (depth lst)
  (if (or (not (list? lst)) (null? lst))
      0
      (+ 1 (depth (car lst)))))

;; Trims characters off the front and end of STR.
;; (trim-1chars "'ab'") -> "ab"
(define (trim-1chars str) (substring str 1 (- (string-length str) 1)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Parse string PEGs using sexp PEGs.
;; See the variable PEG-AS-PEG for an easier-to-read syntax.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Grammar for PEGs in PEG grammar.
(define peg-as-peg
"grammar <-- (nonterminal ('<--' / '<-' / '<') sp pattern)+
pattern <-- alternative (SLASH sp alternative)*
alternative <-- ([!&]? sp suffix)+
suffix <-- primary ([*+?] sp)*
primary <-- '(' sp pattern ')' sp / '.' sp / literal / charclass / nonterminal !'<'
literal <-- ['] (!['] .)* ['] sp
charclass <-- LB (!']' (CCrange / CCsingle))* RB sp
CCrange <-- . '-' .
CCsingle <-- .
nonterminal <-- [a-zA-Z0-9-]+ sp
sp < [ \t\n]*
SLASH < '/'
LB < '['
RB < ']'
")

(define-nonterm peg-grammar all
  (body lit (and peg-nonterminal (or "<--" "<-" "<") peg-sp peg-pattern) +))
(define-nonterm peg-pattern all
  (and peg-alternative
       (body lit (and (ignore "/") peg-sp peg-alternative) *)))
(define-nonterm peg-alternative all
  (body lit (and (body lit (or "!" "&") ?) peg-sp peg-suffix) +))
(define-nonterm peg-suffix all
  (and peg-primary (body lit (and (or "*" "+" "?") peg-sp) *)))
(define-nonterm peg-primary all
  (or (and "(" peg-sp peg-pattern ")" peg-sp)
      (and "." peg-sp)
      peg-literal
      peg-charclass
      (and peg-nonterminal (body ! "<" 1))))
(define-nonterm peg-literal all
  (and "'" (body lit (and (body ! "'" 1) peg-any) *) "'" peg-sp))
(define-nonterm peg-charclass all
  (and (ignore "[")
       (body lit (and (body ! "]" 1)
                      (or charclass-range charclass-single)) *)
       (ignore "]")
       peg-sp))
(define-nonterm charclass-range all (and peg-any "-" peg-any))
(define-nonterm charclass-single all peg-any)
(define-nonterm peg-nonterminal all
  (and (body lit (or (range #\a #\z) (range #\A #\Z) (range #\0 #\9) "-") +) peg-sp))
(define-nonterm peg-sp none
  (body lit (or " " "\t" "\n") *))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; PARSE STRING PEGS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Pakes a string representing a PEG grammar and defines all the nonterminals in
;; it as the associated PEGs.
(define (peg-parser str)
  (let ((parsed (peg-parse peg-grammar str)))
    (if (not parsed)
        (begin
          ;; (pretty-print "Invalid PEG grammar!\n")
          #f)
        (let ((lst (peg:tree parsed)))
          (cond
           ((or (not (list? lst)) (null? lst))
            lst)
           ((eq? (car lst) 'peg-grammar)
            (cons 'begin (map (lambda (x) (peg-parse-nonterm x))
                              (context-flatten (lambda (lst) (<= (depth lst) 2))
                                          (cdr lst))))))))))

;; Macro wrapper for PEG-PARSER.  Parses PEG grammars expressed as strings and
;; defines all the appropriate nonterminals.
(define-syntax define-grammar
  (lambda (x)
    (syntax-case x ()
      ((_ str)
       (datum->syntax x (peg-parser (syntax->datum #'str)))))))
(define define-grammar-f peg-parser)

;; Parse a nonterminal and pattern listed in LST.
(define (peg-parse-nonterm lst)
  (let ((nonterm (car lst))
        (grabber (cadr lst))
        (pattern (caddr lst)))
    `(define-nonterm ,(string->symbol (cadr nonterm))
       ,(cond
         ((string=? grabber "<--") 'all)
         ((string=? grabber "<-") 'body)
         (#t 'none))
       ,(compressor (peg-parse-pattern pattern)))))

;; Parse a pattern.
(define (peg-parse-pattern lst)
  (cons 'or (map peg-parse-alternative
                 (context-flatten (lambda (x) (eq? (car x) 'peg-alternative))
                             (cdr lst)))))

;; Parse an alternative.
(define (peg-parse-alternative lst)
  (cons 'and (map peg-parse-body
                  (context-flatten (lambda (x) (or (string? (car x))
                                              (eq? (car x) 'peg-suffix)))
                              (cdr lst)))))

;; Parse a body.
(define (peg-parse-body lst)
  (let ((suffix '())
        (front 'lit))
    (cond
     ((eq? (car lst) 'peg-suffix)
      (set! suffix lst))
     ((string? (car lst))
      (begin (set! front (string->symbol (car lst)))
             (set! suffix (cadr lst))))
     (#t `(peg-parse-body-fail ,lst)))
    `(body ,front ,@(peg-parse-suffix suffix))))

;; Parse a suffix.
(define (peg-parse-suffix lst)
  (list (peg-parse-primary (cadr lst))
        (if (null? (cddr lst))
            1
            (string->symbol (caddr lst)))))

;; Parse a primary.
(define (peg-parse-primary lst)
  (let ((el (cadr lst)))
  (cond
   ((list? el)
    (cond
     ((eq? (car el) 'peg-literal)
      (peg-parse-literal el))
     ((eq? (car el) 'peg-charclass)
      (peg-parse-charclass el))
     ((eq? (car el) 'peg-nonterminal)
      (string->symbol (cadr el)))))
   ((string? el)
    (cond
     ((equal? el "(")
      (peg-parse-pattern (caddr lst)))
     ((equal? el ".")
      'peg-any)
     (#t `(peg-parse-any unknown-string ,lst))))
   (#t `(peg-parse-any unknown-el ,lst)))))

;; Parses a literal.
(define (peg-parse-literal lst) (trim-1chars (cadr lst)))

;; Parses a charclass.
(define (peg-parse-charclass lst)
  (cons 'or
        (map
         (lambda (cc)
           (cond
            ((eq? (car cc) 'charclass-range)
             `(range ,(string-ref (cadr cc) 0) ,(string-ref (cadr cc) 2)))
            ((eq? (car cc) 'charclass-single)
             (cadr cc))))
         (context-flatten
          (lambda (x) (or (eq? (car x) 'charclass-range)
                          (eq? (car x) 'charclass-single)))
          (cdr lst)))))

;; Compresses a list to save the optimizer work.
;; e.g. (or (and a)) -> a
(define (compressor lst)
  (if (or (not (list? lst)) (null? lst))
      lst
      (cond
       ((and (or (eq? (car lst) 'or) (eq? (car lst) 'and))
             (null? (cddr lst)))
        (compressor (cadr lst)))
       ((and (eq? (car lst) 'body)
             (eq? (cadr lst) 'lit)
             (eq? (cadddr lst) 1))
        (compressor (caddr lst)))
       (#t (map compressor lst)))))

;; Builds a lambda-expressions for the pattern STR using accum.
(define (peg-string-compile for-syntax str accum)
  (peg-sexp-compile for-syntax
   (compressor (peg-parse-pattern (peg:tree (peg-parse peg-pattern str))))
   accum))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; PMATCH STRUCTURE MUNGING
;; Pretty self-explanatory.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define prec
  (make-record-type "peg" '(start end string tree)))
(define make-prec
  (record-constructor prec '(start end string tree)))
(define (peg:start pm)
  (if pm ((record-accessor prec 'start) pm) #f))
(define (peg:end pm)
  (if pm ((record-accessor prec 'end) pm) #f))
(define (peg:string pm)
  (if pm ((record-accessor prec 'string) pm) #f))
(define (peg:tree pm)
  (if pm ((record-accessor prec 'tree) pm) #f))
(define (peg:substring pm)
  (if pm (substring (peg:string pm) (peg:start pm) (peg:end pm)) #f))
(define peg-record? (record-predicate prec))

)