1 ! Copyright (C) 2007, 2008 Chris Double.
2 ! See https://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs classes combinators
5 combinators.short-circuit compiler.units effects.parser kernel
6 literals make math math.order memoize namespaces quotations
7 sequences sets splitting strings unicode vectors vocabs.loader
12 TUPLE: parse-result remaining ast ;
13 TUPLE: parse-error position got messages ;
14 TUPLE: parser peg compiled id ;
16 M: parser equal? { [ [ class-of ] same? ] [ [ id>> ] same? ] } 2&& ;
17 M: parser hashcode* id>> hashcode* ;
19 C: <parse-result> parse-result
20 C: <parse-error> parse-error
22 GENERIC: parser-quot ( peg -- quot )
31 : merge-overlapping-errors ( a b -- c )
32 dupd [ messages>> ] bi@ union [ [ position>> ] [ got>> ] bi ] dip
35 : (merge-errors) ( a b -- c )
37 { [ over position>> not ] [ nip ] }
38 { [ dup position>> not ] [ drop ] }
40 2dup [ position>> ] compare {
43 { +eq+ [ merge-overlapping-errors ] }
49 error-stack get dup length 1 > [
50 [ pop ] [ pop swap (merge-errors) ] [ ] tri push
55 : add-error ( position got message -- )
56 <parse-error> error-stack get push ;
58 : packrat ( id -- cache )
59 ! The packrat cache is a mapping of parser-id->cache.
60 ! For each parser it maps to a cache holding a mapping
61 ! of position->result. The packrat cache therefore keeps
62 ! track of all parses that have occurred at each position
63 ! of the input string and the results obtained from that
65 \ packrat get [ drop H{ } clone ] cache ;
72 ! A mapping from position->peg-head. It maps a
73 ! position in the input string being parsed to
74 ! the head of the left recursion which is currently
75 ! being grown. It is 'f' at any position where
76 ! left recursion growth is not underway.
79 : peg-cache ( -- cache )
80 ! Holds a hashtable mapping a peg tuple to
81 ! the parser tuple for that peg. The parser tuple
82 ! holds a unique id and the compiled form of that peg.
83 \ peg-cache get-global [
84 H{ } clone dup \ peg-cache set-global
88 H{ } clone \ peg-cache set-global ;
93 ! Return the next unique id for a parser
96 : wrap-peg ( peg -- parser )
97 ! Wrap a parser tuple around the peg object.
98 ! Look for an existing parser tuple for that
100 peg-cache [ f next-id parser boa ] cache ;
102 ! An entry in the table of memoized parse results
103 ! ast = an AST produced from the parse
104 ! or the symbol 'fail'
105 ! or a left-recursion object
106 ! pos = the position in the input string of this entry
107 TUPLE: memo-entry ans pos ;
109 TUPLE: left-recursion seed rule-id head next ;
111 TUPLE: peg-head rule-id involved-set eval-set ;
113 : rule-id ( word -- id )
114 ! A rule is the parser compiled down to a word. It has
115 ! a "peg-id" property containing the id of the original parser.
118 : input-slice ( -- slice )
119 ! Return a slice of the input from the current parse position
120 input get pos get tail-slice ;
122 : input-from ( input -- n )
123 ! Return the index from the original string that the
124 ! input slice is based on.
125 dup slice? [ from>> ] [ drop 0 ] if ;
127 : process-rule-result ( p result -- result )
129 nip [ ast>> ] [ remaining>> ] bi input-from pos namespaces:set
131 pos namespaces:set fail
134 : eval-rule ( rule -- ast )
135 ! Evaluate a rule, return an ast resulting from it.
136 ! Return fail if the rule failed. The rule has
137 ! stack effect ( -- parse-result )
138 pos get swap execute( -- parse-result ) process-rule-result ; inline
140 : memo ( pos id -- memo-entry )
141 ! Return the result from the memo cache.
144 : set-memo ( memo-entry pos id -- )
145 ! Store an entry in the cache
148 : update-m ( ast m -- )
149 swap >>ans pos get >>pos drop ;
151 : stop-growth? ( ast m -- ? )
152 [ fail = pos get ] dip pos>> <= or ;
154 : setup-growth ( h p -- )
155 pos namespaces:set dup involved-set>> clone >>eval-set drop ;
157 : (grow-lr) ( h p r: ( -- result ) m -- )
158 [ [ setup-growth ] 2keep ] 2dip
159 [ dup eval-rule ] dip swap
160 dup pick stop-growth? [
165 ] if ; inline recursive
167 : grow-lr ( h p r m -- ast )
168 [ [ heads set-at ] 2keep ] 2dip
169 pick over [ (grow-lr) ] 2dip
171 dup pos>> pos namespaces:set ans>>
174 :: (setup-lr) ( l s -- )
176 s left-recursion? [ s throw ] unless
177 s head>> l head>> eq? [
179 l head>> [ s rule-id>> suffix ] change-involved-set drop
180 l s next>> (setup-lr)
184 :: setup-lr ( r l -- )
186 r rule-id V{ } clone V{ } clone peg-head boa l head<<
188 l lrstack get (setup-lr) ;
190 :: lr-answer ( r p m -- ast )
192 h rule-id>> r rule-id eq? [
193 m ans>> seed>> m ans<<
203 :: recall ( r p -- memo-entry )
204 p r rule-id memo :> m
207 m r rule-id h involved-set>> h rule-id>> suffix member? not and [
208 fail p memo-entry boa
210 r rule-id h eval-set>> member? [
211 h [ r rule-id swap remove ] change-eval-set drop
223 :: apply-non-memo-rule ( r p -- ast )
224 fail r rule-id f lrstack get left-recursion boa :> lr
225 lr lrstack namespaces:set lr p memo-entry boa dup p r rule-id set-memo :> m
227 lrstack get next>> lrstack namespaces:set
230 m ans>> left-recursion? [
239 : apply-memo-rule ( r m -- ast )
240 [ ans>> ] [ pos>> ] bi pos namespaces:set
241 dup left-recursion? [
242 [ setup-lr ] keep seed>>
247 : apply-rule ( r p -- ast )
254 : with-packrat ( input quot -- result )
255 ! Run the quotation with a packrat cache active.
260 V{ } clone error-stack ,,
261 H{ } clone \ heads ,,
262 H{ } clone \ packrat ,,
263 ] H{ } make swap with-variables ; inline
265 : process-parser-result ( result -- result )
269 input-slice swap <parse-result>
272 : execute-parser ( word -- result )
273 pos get apply-rule process-parser-result ;
275 : preset-parser-word ( parser -- word parser )
276 gensym tuck >>compiled ;
278 : define-parser-word ( word parser -- )
279 ! Return the body of the word that is the compiled version
281 [ peg>> parser-quot ( -- result ) define-declared ]
282 [ id>> "peg-id" set-word-prop ] 2bi ;
284 : compile-parser-word ( parser -- word )
285 ! Look to see if the given parser has been compiled.
286 ! If not, compile it to a temporary word, cache it,
287 ! and return it. Otherwise return the existing one.
288 ! Circular parsers are supported by getting the word
289 ! name and storing it in the cache, before compiling,
290 ! so it is picked up when re-entered.
294 preset-parser-word dupd define-parser-word
297 : execute-parser-quot ( parser -- quot )
298 compile-parser-word '[ _ execute-parser ] ;
300 : execute-parsers-quots ( parsers -- quots )
301 [ execute-parser-quot ] map dup rest-slice
302 [ '[ @ merge-errors ] ] map! drop ;
306 : fixup-delayed ( -- )
307 ! Work through all delayed parsers and recompile their
308 ! words to have the correct bodies.
310 call( -- parser ) execute-parser-quot ( -- result ) define-declared
313 : compile-parser ( parser -- word )
316 compile-parser-word fixup-delayed
318 ] with-compilation-unit ;
320 : perform-parse ( input word -- result )
323 error-stack get ?first [ throw ] [
324 pos get input get f <parse-error> throw
331 : (parse) ( input parser -- result )
332 compile-parser perform-parse ;
334 : parse ( input parser -- ast )
337 ERROR: unable-to-fully-parse remaining ;
339 ERROR: could-not-parse ;
341 : check-parse-result ( result -- result )
343 dup remaining>> [ blank? ] trim [
344 unable-to-fully-parse
350 : parse-fully ( input parser -- ast )
351 (parse) check-parse-result ast>> ;
355 TUPLE: token-parser symbol ;
357 : parse-token ( input string -- result )
358 ! Parse the string, returning a parse result
359 [ ?head-slice ] keep swap [
362 [ seq>> pos get swap ] dip "'" "'" surround 1vector add-error f
365 M: token-parser parser-quot
366 symbol>> '[ input-slice _ parse-token ] ;
368 TUPLE: satisfy-parser quot ;
370 :: parse-satisfy ( input quot -- result/f )
372 unclip-slice dup quot call [
379 M: satisfy-parser parser-quot
380 quot>> '[ input-slice _ parse-satisfy ] ;
382 TUPLE: range-parser min max ;
384 :: parse-range ( input min max -- result/f )
386 dup first min max between? [
387 unclip-slice <parse-result>
393 M: range-parser parser-quot
394 [ min>> ] [ max>> ] bi '[ input-slice _ _ parse-range ] ;
396 TUPLE: seq-parser parsers ;
398 : calc-seq-result ( prev-result current-result -- next-result )
400 [ remaining>> >>remaining ] [ ast>> ] bi
401 dup ignore = [ drop ] [ over ast>> push ] if
406 : parse-seq-element ( result quot -- result )
407 '[ @ calc-seq-result ] [ f ] if* ; inline
409 M: seq-parser parser-quot
410 parsers>> execute-parsers-quots
411 [ '[ _ parse-seq-element ] ] map
412 '[ input-slice V{ } clone <parse-result> _ 1&& ] ;
414 TUPLE: choice-parser parsers ;
416 M: choice-parser parser-quot
417 parsers>> execute-parsers-quots '[ _ 0|| ] ;
419 TUPLE: repeat0-parser parser ;
421 : repeat-loop ( quot: ( -- result/f ) result -- result )
423 [ remaining>> >>remaining ] [ ast>> ] bi
424 over ast>> push repeat-loop
427 ] if* ; inline recursive
429 M: repeat0-parser parser-quot
430 parser>> execute-parser-quot '[
431 input-slice V{ } clone <parse-result> _ swap repeat-loop
434 TUPLE: repeat1-parser parser ;
436 : repeat1-empty-check ( result -- result )
437 [ dup ast>> empty? [ drop f ] when ] [ f ] if* ;
439 M: repeat1-parser parser-quot
440 parser>> execute-parser-quot '[
441 input-slice V{ } clone <parse-result> _ swap repeat-loop
445 TUPLE: optional-parser parser ;
447 : check-optional ( result -- result )
448 [ input-slice f <parse-result> ] unless* ;
450 M: optional-parser parser-quot
451 parser>> execute-parser-quot '[ @ check-optional ] ;
453 TUPLE: semantic-parser parser quot ;
455 : check-semantic ( result quot -- result )
456 dupd '[ dup ast>> @ [ drop f ] unless ] when ; inline
458 M: semantic-parser parser-quot
459 [ parser>> execute-parser-quot ] [ quot>> ] bi
460 '[ @ _ check-semantic ] ;
462 TUPLE: ensure-parser parser ;
464 : check-ensure ( old-input result -- result )
465 [ ignore <parse-result> ] [ drop f ] if ;
467 M: ensure-parser parser-quot
468 parser>> execute-parser-quot '[ input-slice @ check-ensure ] ;
470 TUPLE: ensure-not-parser parser ;
472 : check-ensure-not ( old-input result -- result )
473 [ drop f ] [ ignore <parse-result> ] if ;
475 M: ensure-not-parser parser-quot
476 parser>> execute-parser-quot '[ input-slice @ check-ensure-not ] ;
478 TUPLE: action-parser parser quot ;
480 : check-action ( result quot -- result )
481 dupd '[ [ _ call( ast -- ast ) ] change-ast ] when ;
483 M: action-parser parser-quot
484 [ parser>> execute-parser-quot ] [ quot>> ] bi
485 '[ @ _ check-action ] ;
487 TUPLE: sp-parser parser ;
489 M: sp-parser parser-quot
490 parser>> execute-parser-quot '[
491 input-slice [ blank? ] trim-head-slice input-from pos namespaces:set @
494 TUPLE: delay-parser quot ;
496 M: delay-parser parser-quot
497 ! For efficiency we memoize the quotation.
498 ! This way it is run only once and the
499 ! parser constructed once at run time.
500 quot>> gensym [ delayed get set-at ] keep 1quotation ;
502 TUPLE: box-parser quot ;
504 M: box-parser parser-quot
505 ! Calls the quotation at compile time
506 ! to produce the parser to be compiled.
507 ! This differs from 'delay' which calls
509 quot>> call( -- parser ) execute-parser-quot ;
513 : token ( string -- parser )
514 token-parser boa wrap-peg ;
516 : satisfy ( quot -- parser )
517 satisfy-parser boa wrap-peg ;
519 : range ( min max -- parser )
520 range-parser boa wrap-peg ;
522 : seq ( seq -- parser )
523 seq-parser boa wrap-peg ;
525 : 2seq ( parser1 parser2 -- parser )
528 : 3seq ( parser1 parser2 parser3 -- parser )
531 : 4seq ( parser1 parser2 parser3 parser4 -- parser )
534 : seq* ( quot -- parser )
535 { } make seq ; inline
537 : choice ( seq -- parser )
538 choice-parser boa wrap-peg ;
540 : 2choice ( parser1 parser2 -- parser )
543 : 3choice ( parser1 parser2 parser3 -- parser )
546 : 4choice ( parser1 parser2 parser3 parser4 -- parser )
549 : choice* ( quot -- parser )
550 { } make choice ; inline
552 : repeat0 ( parser -- parser )
553 repeat0-parser boa wrap-peg ;
555 : repeat1 ( parser -- parser )
556 repeat1-parser boa wrap-peg ;
558 : optional ( parser -- parser )
559 optional-parser boa wrap-peg ;
561 : semantic ( parser quot -- parser )
562 semantic-parser boa wrap-peg ;
564 : ensure ( parser -- parser )
565 ensure-parser boa wrap-peg ;
567 : ensure-not ( parser -- parser )
568 ensure-not-parser boa wrap-peg ;
570 : action ( parser quot -- parser )
571 action-parser boa wrap-peg ;
573 : sp ( parser -- parser )
574 sp-parser boa wrap-peg ;
576 : hide ( parser -- parser )
577 [ drop ignore ] action ;
579 : delay ( quot -- parser )
580 delay-parser boa wrap-peg ;
582 : box ( quot -- parser )
583 ! because a box has its quotation run at compile time
584 ! it must always have a new parser wrapper created,
585 ! not a cached one. This is because the same box,
586 ! compiled twice can have a different compiled word
587 ! due to running at compile time.
588 ! Why the [ ] action at the end? Box parsers don't get
589 ! memoized during parsing due to all box parsers being
590 ! unique. This breaks left recursion detection during the
591 ! parse. The action adds an indirection with a parser type
592 ! that gets memoized and fixes this. Need to rethink how
593 ! to fix boxes so this isn't needed...
594 box-parser boa f next-id parser boa [ ] action ;
600 _ call( -- parser ) compile-parser
601 '[ _ perform-parse ast>> ]
603 ] with-compilation-unit
610 _ call( -- parser ) compile-parser
611 '[ _ perform-parse check-parse-result ast>> ]
613 ] with-compilation-unit
616 { "debugger" "peg" } "peg.debugger" require-when