Refactor seq peg parser
							parent
							
								
									7b73d2734f
								
							
						
					
					
						commit
						102178f787
					
				| 
						 | 
					@ -313,34 +313,38 @@ M: range-parser (compile) ( parser -- quot )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TUPLE: seq-parser parsers ;
 | 
					TUPLE: seq-parser parsers ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MATCH-VARS: ?quot ;
 | 
					: ignore? ( ast -- bool )
 | 
				
			||||||
 | 
					  ignore = ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: seq-pattern ( -- quot )
 | 
					: calc-seq-result ( prev-result current-result -- next-result )
 | 
				
			||||||
  [
 | 
					  [
 | 
				
			||||||
    dup [
 | 
					    [ remaining>> swap (>>remaining) ] 2keep
 | 
				
			||||||
      ?quot [
 | 
					    ast>> dup ignore? [  
 | 
				
			||||||
        [ remaining>> swap (>>remaining) ] 2keep
 | 
					      drop
 | 
				
			||||||
        ast>> dup ignore = [ 
 | 
					 | 
				
			||||||
          drop  
 | 
					 | 
				
			||||||
        ] [ 
 | 
					 | 
				
			||||||
          swap [ ast>> push ] keep 
 | 
					 | 
				
			||||||
        ] if
 | 
					 | 
				
			||||||
      ] [
 | 
					 | 
				
			||||||
        drop f 
 | 
					 | 
				
			||||||
      ] if*
 | 
					 | 
				
			||||||
    ] [
 | 
					    ] [
 | 
				
			||||||
      drop f
 | 
					      swap [ ast>> push ] keep
 | 
				
			||||||
    ] if  
 | 
					    ] if
 | 
				
			||||||
  ] ;
 | 
					  ] [
 | 
				
			||||||
 | 
					    drop f
 | 
				
			||||||
 | 
					  ] if* ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: parse-seq-element ( result quot -- result )
 | 
				
			||||||
 | 
					  over [
 | 
				
			||||||
 | 
					    call calc-seq-result
 | 
				
			||||||
 | 
					  ] [
 | 
				
			||||||
 | 
					    2drop f
 | 
				
			||||||
 | 
					  ] if ; inline
 | 
				
			||||||
 | 
					
 | 
				
			||||||
M: seq-parser (compile) ( parser -- quot )
 | 
					M: seq-parser (compile) ( parser -- quot )
 | 
				
			||||||
  [
 | 
					  [
 | 
				
			||||||
    [ input-slice V{ } clone <parse-result> ] %
 | 
					    [ input-slice V{ } clone <parse-result> ] %
 | 
				
			||||||
    parsers>> [ compiled-parser \ ?quot seq-pattern match-replace % ] each 
 | 
					    parsers>> [ compiled-parser 1quotation , \ parse-seq-element , ] each 
 | 
				
			||||||
  ] [ ] make ;
 | 
					  ] [ ] make ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TUPLE: choice-parser parsers ;
 | 
					TUPLE: choice-parser parsers ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					MATCH-VARS: ?quot ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: choice-pattern ( -- quot )
 | 
					: choice-pattern ( -- quot )
 | 
				
			||||||
  [
 | 
					  [
 | 
				
			||||||
    [ ?quot ] unless* 
 | 
					    [ ?quot ] unless* 
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue