| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | ! Copyright (C) 2007, 2008 Chris Double, Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | USING: kernel sequences strings namespaces make math assocs | 
					
						
							| 
									
										
										
										
											2008-11-23 02:00:12 -05:00
										 |  |  | vectors arrays math.parser accessors unicode.categories | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | sequences.deep peg peg.private peg.search math.ranges words ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | IN: peg.parsers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  | TUPLE: just-parser p1 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : just-pattern | 
					
						
							|  |  |  |   [ | 
					
						
							| 
									
										
										
										
											2008-03-25 22:26:54 -04:00
										 |  |  |     execute dup [ | 
					
						
							| 
									
										
										
										
											2008-08-29 18:00:19 -04:00
										 |  |  |       dup remaining>> empty? [ drop f ] unless
 | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  |     ] when
 | 
					
						
							|  |  |  |   ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 10:05:21 -04:00
										 |  |  | M: just-parser (compile) ( parser -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 08:45:33 -04:00
										 |  |  |   p1>> compile-parser just-pattern curry ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : just ( parser -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-07-08 00:56:12 -04:00
										 |  |  |   just-parser boa wrap-peg ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : 1token ( ch -- parser ) 1string token ;
 | 
					
						
							| 
									
										
										
										
											2008-03-12 11:43:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : (list-of) ( items separator repeat1? -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |   [ over 2seq ] dip [ repeat1 ] [ repeat0 ] if [ concat ] action 2seq | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   [ unclip 1vector swap first append ] action ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : list-of ( items separator -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   hide f (list-of) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : list-of-many ( items separator -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   hide t (list-of) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : epsilon ( -- parser ) V{ } token ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : any-char ( -- parser ) [ drop t ] satisfy ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : flatten-vectors ( pair -- vector )
 | 
					
						
							|  |  |  |   first2 over push-all ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : exactly-n ( parser n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   swap <repetition> seq ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : at-most-n ( parser n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   dup zero? [ | 
					
						
							|  |  |  |     2drop epsilon | 
					
						
							|  |  |  |   ] [ | 
					
						
							| 
									
										
										
										
											2008-12-22 06:41:01 -05:00
										 |  |  |     [ exactly-n ] [ 1- at-most-n ] 2bi 2choice | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : at-least-n ( parser n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   dupd exactly-n swap repeat0 2seq | 
					
						
							|  |  |  |   [ flatten-vectors ] action ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : from-m-to-n ( parser m n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |   [ [ exactly-n ] 2keep ] dip swap - at-most-n 2seq | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   [ flatten-vectors ] action ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : pack ( begin body end -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |   [ hide ] 2dip hide 3seq [ first ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : surrounded-by ( parser begin end -- parser' )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |   [ token ] bi@ swapd pack ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : 'digit' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   [ digit? ] satisfy [ digit> ] action ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : 'integer' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   'digit' repeat1 [ 10 digits>integer ] action ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : 'string' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  |   [ | 
					
						
							|  |  |  |     [ CHAR: " = ] satisfy hide , | 
					
						
							|  |  |  |     [ CHAR: " = not ] satisfy repeat0 , | 
					
						
							|  |  |  |     [ CHAR: " = ] satisfy hide , | 
					
						
							| 
									
										
										
										
											2008-03-26 22:50:27 -04:00
										 |  |  |   ] seq* [ first >string ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-19 20:40:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (range-pattern) ( pattern -- string )
 | 
					
						
							|  |  |  |   #! Given a range pattern, produce a string containing | 
					
						
							|  |  |  |   #! all characters within that range. | 
					
						
							|  |  |  |   [  | 
					
						
							|  |  |  |     any-char ,  | 
					
						
							|  |  |  |     [ CHAR: - = ] satisfy hide ,  | 
					
						
							|  |  |  |     any-char ,  | 
					
						
							|  |  |  |   ] seq* [ | 
					
						
							|  |  |  |     first2 [a,b] >string     | 
					
						
							|  |  |  |   ] action | 
					
						
							|  |  |  |   replace ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : range-pattern ( pattern -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-03-19 20:40:22 -04:00
										 |  |  |   #! 'pattern' is a set of characters describing the | 
					
						
							|  |  |  |   #! parser to be produced. Any single character in | 
					
						
							|  |  |  |   #! the pattern matches that character. If the pattern | 
					
						
							|  |  |  |   #! begins with a ^ then the set is negated (the element | 
					
						
							|  |  |  |   #! matches any character not in the set). Any pair of | 
					
						
							|  |  |  |   #! characters separated with a dash (-) represents the | 
					
						
							|  |  |  |   #! range of characters from the first to the second, | 
					
						
							|  |  |  |   #! inclusive. | 
					
						
							|  |  |  |   dup first CHAR: ^ = [ | 
					
						
							| 
									
										
										
										
											2008-04-26 03:01:43 -04:00
										 |  |  |     rest (range-pattern) [ member? not ] curry satisfy  | 
					
						
							| 
									
										
										
										
											2008-03-19 20:40:22 -04:00
										 |  |  |   ] [ | 
					
						
							|  |  |  |     (range-pattern) [ member? ] curry satisfy | 
					
						
							|  |  |  |   ] if ;
 |