]> gitweb.factorcode.org Git - factor.git/blob - basis/peg/peg.factor
peg: reuse code in compile-parsers-quots.
[factor.git] / basis / peg / peg.factor
1 ! Copyright (C) 2007, 2008 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3
4 USING: accessors arrays assocs classes combinators
5 combinators.short-circuit compiler.units effects.parser fry
6 generalizations kernel locals make math math.order namespaces
7 quotations sequences sets splitting unicode vectors
8 vocabs.loader words ;
9
10 IN: peg
11
12 TUPLE: parse-result remaining ast ;
13 TUPLE: parse-error position got messages ;
14 TUPLE: parser peg compiled id ;
15
16 M: parser equal? { [ [ class-of ] same? ] [ [ id>> ] same? ] } 2&& ;
17 M: parser hashcode* id>> hashcode* ;
18
19 C: <parse-result> parse-result
20 C: <parse-error>  parse-error
21
22 SYMBOL: error-stack
23
24 : merge-overlapping-errors ( a b -- c )
25     dupd [ messages>> ] bi@ union [ [ position>> ] [ got>> ] bi ] dip
26     <parse-error> ;
27
28 : (merge-errors) ( a b -- c )
29     {
30         { [ over position>> not ] [ nip ] }
31         { [ dup  position>> not ] [ drop ] }
32         [
33             2dup [ position>> ] compare {
34                 { +lt+ [ nip ] }
35                 { +gt+ [ drop ] }
36                 { +eq+ [ merge-overlapping-errors ] }
37             } case
38         ]
39     } cond ;
40
41 : merge-errors ( -- )
42     error-stack get dup length 1 > [
43         [ pop ] [ pop swap (merge-errors) ] [ ] tri push
44     ] [
45         drop
46     ] if ;
47
48 : add-error ( position got message -- )
49     <parse-error> error-stack get push ;
50
51 SYMBOL: ignore
52
53 : ignore? ( obj -- ? )
54     ignore = ;
55
56 : packrat ( id -- cache )
57     ! The packrat cache is a mapping of parser-id->cache.
58     ! For each parser it maps to a cache holding a mapping
59     ! of position->result. The packrat cache therefore keeps
60     ! track of all parses that have occurred at each position
61     ! of the input string and the results obtained from that
62     ! parser.
63     \ packrat get [ drop H{ } clone ] cache ;
64
65 SYMBOL: pos
66 SYMBOL: input
67 SYMBOL: fail
68 SYMBOL: lrstack
69
70 : heads ( -- cache )
71     ! A mapping from position->peg-head. It maps a
72     ! position in the input string being parsed to
73     ! the head of the left recursion which is currently
74     ! being grown. It is 'f' at any position where
75     ! left recursion growth is not underway.
76     \ heads get ;
77
78 : failed? ( obj -- ? )
79     fail = ;
80
81 : peg-cache ( -- cache )
82     ! Holds a hashtable mapping a peg tuple to
83     ! the parser tuple for that peg. The parser tuple
84     ! holds a unique id and the compiled form of that peg.
85     \ peg-cache get-global [
86         H{ } clone dup \ peg-cache set-global
87     ] unless* ;
88
89 : reset-pegs ( -- )
90     H{ } clone \ peg-cache set-global ;
91
92 reset-pegs
93
94 ! An entry in the table of memoized parse results
95 ! ast = an AST produced from the parse
96 !       or the symbol 'fail'
97 !       or a left-recursion object
98 ! pos = the position in the input string of this entry
99 TUPLE: memo-entry ans pos ;
100
101 TUPLE: left-recursion seed rule-id head next ;
102 TUPLE: peg-head rule-id involved-set eval-set ;
103
104 : rule-id ( word -- id )
105     ! A rule is the parser compiled down to a word. It has
106     ! a "peg-id" property containing the id of the original parser.
107     "peg-id" word-prop ;
108
109 : input-slice ( -- slice )
110     ! Return a slice of the input from the current parse position
111     input get pos get tail-slice ;
112
113 : input-from ( input -- n )
114     ! Return the index from the original string that the
115     ! input slice is based on.
116     dup slice? [ from>> ] [ drop 0 ] if ;
117
118 : process-rule-result ( p result -- result )
119     [
120         nip [ ast>> ] [ remaining>> ] bi input-from pos namespaces:set
121     ] [
122         pos namespaces:set fail
123     ] if* ;
124
125 : eval-rule ( rule -- ast )
126     ! Evaluate a rule, return an ast resulting from it.
127     ! Return fail if the rule failed. The rule has
128     ! stack effect ( -- parse-result )
129     pos get swap execute( -- parse-result ) process-rule-result ; inline
130
131 : memo ( pos id -- memo-entry )
132     ! Return the result from the memo cache.
133     packrat at ;
134
135 : set-memo ( memo-entry pos id -- )
136     ! Store an entry in the cache
137     packrat set-at ;
138
139 : update-m ( ast m -- )
140     swap >>ans pos get >>pos drop ;
141
142 : stop-growth? ( ast m -- ? )
143     [ failed? pos get ] dip pos>> <= or ;
144
145 : setup-growth ( h p -- )
146     pos namespaces:set dup involved-set>> clone >>eval-set drop ;
147
148 : (grow-lr) ( h p r: ( -- result ) m -- )
149     [ [ setup-growth ] 2keep ] 2dip
150     [ dup eval-rule ] dip swap
151         dup pick stop-growth? [
152         5drop
153     ] [
154         over update-m
155         (grow-lr)
156     ] if ; inline recursive
157
158 : grow-lr ( h p r m -- ast )
159     [ [ heads set-at ] 2keep ] 2dip
160     pick over [ (grow-lr) ] 2dip
161     swap heads delete-at
162     dup pos>> pos namespaces:set ans>>
163     ; inline
164
165 :: (setup-lr) ( l s -- )
166     s [
167         s left-recursion? [ s throw ] unless
168         s head>> l head>> eq? [
169             l head>> s head<<
170             l head>> [ s rule-id>> suffix ] change-involved-set drop
171             l s next>> (setup-lr)
172         ] unless
173     ] when ;
174
175 :: setup-lr ( r l -- )
176     l head>> [
177         r rule-id V{ } clone V{ } clone peg-head boa l head<<
178     ] unless
179     l lrstack get (setup-lr) ;
180
181 :: lr-answer ( r p m -- ast )
182     m ans>> head>> :> h
183     h rule-id>> r rule-id eq? [
184         m ans>> seed>> m ans<<
185         m ans>> failed? [
186             fail
187         ] [
188             h p r m grow-lr
189         ] if
190     ] [
191         m ans>> seed>>
192     ] if ; inline
193
194 :: recall ( r p -- memo-entry )
195     p r rule-id memo :> m
196     p heads at :> h
197     h [
198         m r rule-id h involved-set>> h rule-id>> suffix member? not and [
199             fail p memo-entry boa
200         ] [
201             r rule-id h eval-set>> member? [
202                 h [ r rule-id swap remove ] change-eval-set drop
203                 r eval-rule
204                 m update-m
205                 m
206             ] [
207                 m
208             ] if
209         ] if
210     ] [
211         m
212     ] if ; inline
213
214 :: apply-non-memo-rule ( r p -- ast )
215     fail r rule-id f lrstack get left-recursion boa :> lr
216     lr lrstack namespaces:set lr p memo-entry boa dup p r rule-id set-memo :> m
217     r eval-rule :> ans
218     lrstack get next>> lrstack namespaces:set
219     pos get m pos<<
220     lr head>> [
221         m ans>> left-recursion? [
222             ans lr seed<<
223             r p m lr-answer
224         ] [ ans ] if
225     ] [
226         ans m ans<<
227         ans
228     ] if ; inline
229
230 : apply-memo-rule ( r m -- ast )
231     [ ans>> ] [ pos>> ] bi pos namespaces:set
232     dup left-recursion? [
233         [ setup-lr ] keep seed>>
234     ] [
235         nip
236     ] if ;
237
238 : apply-rule ( r p -- ast )
239     2dup recall [
240         nip apply-memo-rule
241     ] [
242         apply-non-memo-rule
243     ] if* ; inline
244
245 : with-packrat ( input quot -- result )
246     ! Run the quotation with a packrat cache active.
247     [
248         swap input ,,
249         0 pos ,,
250         f lrstack ,,
251         V{ } clone error-stack ,,
252         H{ } clone \ heads ,,
253         H{ } clone \ packrat ,,
254     ] H{ } make swap with-variables ; inline
255
256 GENERIC: (compile) ( peg -- quot )
257
258 : process-parser-result ( result -- result )
259     dup failed? [
260         drop f
261     ] [
262         input-slice swap <parse-result>
263     ] if ;
264
265 : execute-parser ( word -- result )
266     pos get apply-rule process-parser-result ;
267
268 : preset-parser-word ( parser -- parser word )
269     gensym [ >>compiled ] keep ;
270
271 : define-parser-word ( parser word -- )
272     ! Return the body of the word that is the compiled version
273     ! of the parser.
274
275     2dup swap peg>> (compile) ( -- result ) define-declared
276     swap id>> "peg-id" set-word-prop ;
277
278 : compile-parser ( parser -- word )
279     ! Look to see if the given parser has been compiled.
280     ! If not, compile it to a temporary word, cache it,
281     ! and return it. Otherwise return the existing one.
282     ! Circular parsers are supported by getting the word
283     ! name and storing it in the cache, before compiling,
284     ! so it is picked up when re-entered.
285     dup compiled>> [
286         nip
287     ] [
288         preset-parser-word [ define-parser-word ] keep
289     ] if* ;
290
291 : compile-parser-quot ( parser -- quot )
292     compile-parser '[ _ execute-parser ] ;
293
294 : compile-parsers-quots ( parsers -- quots )
295     [ compile-parser-quot ] map dup rest-slice
296     [ '[ @ merge-errors ] ] map! drop ;
297
298 SYMBOL: delayed
299
300 : fixup-delayed ( -- )
301     ! Work through all delayed parsers and recompile their
302     ! words to have the correct bodies.
303     delayed get [
304         call( -- parser ) compile-parser-quot ( -- result ) define-declared
305     ] assoc-each ;
306
307 : compile ( parser -- word )
308     [
309         H{ } clone delayed [
310             compile-parser-quot ( -- result ) define-temp fixup-delayed
311         ] with-variable
312     ] with-compilation-unit ;
313
314 : compiled-parse ( state word -- result )
315     swap [
316         execute( -- result )
317         [ error-stack get ?first [ throw ]
318         [ pos get input get f <parse-error> throw ] if* ] unless*
319     ] with-packrat ;
320
321 : (parse) ( input parser -- result )
322     dup word? [ compile ] unless compiled-parse ;
323
324 : parse ( input parser -- ast )
325     (parse) ast>> ;
326
327 <PRIVATE
328
329 : next-id ( -- n )
330     ! Return the next unique id for a parser
331     \ next-id counter ;
332
333 : wrap-peg ( peg -- parser )
334     ! Wrap a parser tuple around the peg object.
335     ! Look for an existing parser tuple for that
336     ! peg object.
337     peg-cache [ f next-id parser boa ] cache ;
338
339 TUPLE: token-parser symbol ;
340
341 : parse-token ( input string -- result )
342     ! Parse the string, returning a parse result
343     [ ?head-slice ] keep swap [
344         <parse-result>
345     ] [
346         [ seq>> pos get swap ] dip "'" "'" surround 1vector add-error f
347     ] if ;
348
349 M: token-parser (compile) ( peg -- quot )
350     symbol>> '[ input-slice _ parse-token ] ;
351
352 TUPLE: satisfy-parser quot ;
353
354 :: parse-satisfy ( input quot -- result/f )
355     input [ f ] [
356         unclip-slice dup quot call [
357             <parse-result>
358         ] [
359             2drop f
360         ] if
361     ] if-empty ; inline
362
363 M: satisfy-parser (compile)
364     quot>> '[ input-slice _ parse-satisfy ] ;
365
366 TUPLE: range-parser min max ;
367
368 :: parse-range ( input min max -- result/f )
369     input [ f ] [
370         dup first min max between? [
371             unclip-slice <parse-result>
372         ] [
373             drop f
374         ] if
375     ] if-empty ;
376
377 M: range-parser (compile)
378     [ min>> ] [ max>> ] bi '[ input-slice _ _ parse-range ] ;
379
380 TUPLE: seq-parser parsers ;
381
382 : calc-seq-result ( prev-result current-result -- next-result )
383     [
384         [ remaining>> >>remaining ] [ ast>> ] bi
385         dup ignore? [ drop ] [ over ast>> push ] if
386     ] [
387         drop f
388     ] if* ;
389
390 : parse-seq-element ( result quot -- result )
391     '[ @ calc-seq-result ] [ f ] if* ; inline
392
393 M: seq-parser (compile)
394     parsers>> compile-parsers-quots
395     [ '[ _ parse-seq-element ] ] map
396     '[ input-slice V{ } clone <parse-result> _ 1&& ] ;
397
398 TUPLE: choice-parser parsers ;
399
400 M: choice-parser (compile)
401     parsers>> compile-parsers-quots '[ _ 0|| ] ;
402
403 TUPLE: repeat0-parser parser ;
404
405 : (repeat) ( quot: ( -- result ) result -- result )
406     over call [
407         [ remaining>> >>remaining ] [ ast>> ] bi
408         over ast>> push (repeat)
409     ] [
410         nip
411     ] if* ; inline recursive
412
413 M: repeat0-parser (compile)
414     parser>> compile-parser-quot '[
415         input-slice V{ } clone <parse-result> _ swap (repeat)
416     ] ;
417
418 TUPLE: repeat1-parser parser ;
419
420 : repeat1-empty-check ( result -- result )
421     [ dup ast>> empty? [ drop f ] when ] [ f ] if* ;
422
423 M: repeat1-parser (compile)
424     parser>> compile-parser-quot '[
425         input-slice V{ } clone <parse-result> _ swap (repeat)
426         repeat1-empty-check
427     ] ;
428
429 TUPLE: optional-parser parser ;
430
431 : check-optional ( result -- result )
432     [ input-slice f <parse-result> ] unless* ;
433
434 M: optional-parser (compile)
435     parser>> compile-parser-quot '[ @ check-optional ] ;
436
437 TUPLE: semantic-parser parser quot ;
438
439 : check-semantic ( result quot -- result )
440     dupd '[ dup ast>> @ [ drop f ] unless ] when ; inline
441
442 M: semantic-parser (compile)
443     [ parser>> compile-parser-quot ] [ quot>> ] bi
444     '[ @ _ check-semantic ] ;
445
446 TUPLE: ensure-parser parser ;
447
448 : check-ensure ( old-input result -- result )
449     [ ignore <parse-result> ] [ drop f ] if ;
450
451 M: ensure-parser (compile)
452     parser>> compile-parser-quot '[ input-slice @ check-ensure ] ;
453
454 TUPLE: ensure-not-parser parser ;
455
456 : check-ensure-not ( old-input result -- result )
457     [ drop f ] [ ignore <parse-result> ] if ;
458
459 M: ensure-not-parser (compile)
460     parser>> compile-parser-quot '[ input-slice @ check-ensure-not ] ;
461
462 TUPLE: action-parser parser quot ;
463
464 : check-action ( result quot -- result )
465     dupd '[ [ _ call( ast -- ast ) ] change-ast ] when ;
466
467 M: action-parser (compile)
468     [ parser>> compile-parser-quot ] [ quot>> ] bi
469     '[ @ _ check-action ] ;
470
471 TUPLE: sp-parser parser ;
472
473 M: sp-parser (compile)
474     parser>> compile-parser-quot '[
475         input-slice [ blank? ] trim-head-slice input-from pos namespaces:set @
476     ] ;
477
478 TUPLE: delay-parser quot ;
479
480 M: delay-parser (compile)
481     ! For efficiency we memoize the quotation.
482     ! This way it is run only once and the
483     ! parser constructed once at run time.
484     quot>> gensym [ delayed get set-at ] keep 1quotation ;
485
486 TUPLE: box-parser quot ;
487
488 M: box-parser (compile)
489     ! Calls the quotation at compile time
490     ! to produce the parser to be compiled.
491     ! This differs from 'delay' which calls
492     ! it at run time.
493     quot>> call( -- parser ) compile-parser-quot ;
494
495 PRIVATE>
496
497 : token ( string -- parser )
498     token-parser boa wrap-peg ;
499
500 : satisfy ( quot -- parser )
501     satisfy-parser boa wrap-peg ;
502
503 : range ( min max -- parser )
504     range-parser boa wrap-peg ;
505
506 : seq ( seq -- parser )
507     seq-parser boa wrap-peg ;
508
509 : 2seq ( parser1 parser2 -- parser )
510     2array seq ;
511
512 : 3seq ( parser1 parser2 parser3 -- parser )
513     3array seq ;
514
515 : 4seq ( parser1 parser2 parser3 parser4 -- parser )
516     4array seq ;
517
518 : seq* ( quot -- paser )
519     { } make seq ; inline
520
521 : choice ( seq -- parser )
522     choice-parser boa wrap-peg ;
523
524 : 2choice ( parser1 parser2 -- parser )
525     2array choice ;
526
527 : 3choice ( parser1 parser2 parser3 -- parser )
528     3array choice ;
529
530 : 4choice ( parser1 parser2 parser3 parser4 -- parser )
531     4array choice ;
532
533 : choice* ( quot -- paser )
534     { } make choice ; inline
535
536 : repeat0 ( parser -- parser )
537     repeat0-parser boa wrap-peg ;
538
539 : repeat1 ( parser -- parser )
540     repeat1-parser boa wrap-peg ;
541
542 : optional ( parser -- parser )
543     optional-parser boa wrap-peg ;
544
545 : semantic ( parser quot -- parser )
546     semantic-parser boa wrap-peg ;
547
548 : ensure ( parser -- parser )
549     ensure-parser boa wrap-peg ;
550
551 : ensure-not ( parser -- parser )
552     ensure-not-parser boa wrap-peg ;
553
554 : action ( parser quot -- parser )
555     action-parser boa wrap-peg ;
556
557 : sp ( parser -- parser )
558     sp-parser boa wrap-peg ;
559
560 : hide ( parser -- parser )
561     [ drop ignore ] action ;
562
563 : delay ( quot -- parser )
564     delay-parser boa wrap-peg ;
565
566 : box ( quot -- parser )
567     ! because a box has its quotation run at compile time
568     ! it must always have a new parser wrapper created,
569     ! not a cached one. This is because the same box,
570     ! compiled twice can have a different compiled word
571     ! due to running at compile time.
572     ! Why the [ ] action at the end? Box parsers don't get
573     ! memoized during parsing due to all box parsers being
574     ! unique. This breaks left recursion detection during the
575     ! parse. The action adds an indirection with a parser type
576     ! that gets memoized and fixes this. Need to rethink how
577     ! to fix boxes so this isn't needed...
578     box-parser boa f next-id parser boa [ ] action ;
579
580 ERROR: parse-failed input word ;
581
582 SYNTAX: PEG:
583     [let
584         (:) :> ( word def effect )
585         [
586             [
587                 def call compile :> compiled-def
588                 word [
589                     dup compiled-def compiled-parse
590                     [ ast>> ] [ word parse-failed ] ?if
591                 ] effect define-declared
592             ] with-compilation-unit
593         ] append!
594     ] ;
595
596 { "debugger" "peg" } "peg.debugger" require-when