| 
									
										
										
										
											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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  | CONSTANT: just-pattern [ | 
					
						
							| 
									
										
										
										
											2009-03-23 20:23:18 -04:00
										 |  |  |     dup [ | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |         dup remaining>> empty? [ drop f ] unless
 | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  |     ] when
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  | ] | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 10:05:21 -04:00
										 |  |  | M: just-parser (compile) ( parser -- quot )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     p1>> compile-parser-quot just-pattern compose ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 17:45:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : just ( parser -- parser )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05: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 )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ over 2seq ] dip [ repeat1 ] [ repeat0 ] if
 | 
					
						
							|  |  |  |     [ concat ] action 2seq | 
					
						
							|  |  |  |     [ unclip 1vector swap first append ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : list-of ( items separator -- parser )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     hide f (list-of) ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : list-of-many ( items separator -- parser )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     hide t (list-of) ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     first2 append! ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : exactly-n ( parser n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     swap <repetition> seq ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : at-most-n ( parser n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         drop epsilon | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ exactly-n ] [ 1 - at-most-n ] 2bi 2choice | 
					
						
							|  |  |  |     ] if-zero ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : at-least-n ( parser n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     dupd exactly-n swap repeat0 2seq | 
					
						
							|  |  |  |     [ flatten-vectors ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : from-m-to-n ( parser m n -- parser' )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ [ exactly-n ] 2keep ] dip swap - at-most-n 2seq | 
					
						
							|  |  |  |     [ flatten-vectors ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 22:20:19 -04:00
										 |  |  | : pack ( begin body end -- parser )
 | 
					
						
							| 
									
										
										
										
											2015-08-15 21:10:13 -04:00
										 |  |  |     [ hide ] [ ] [ hide ] tri* 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' )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ token ] bi@ swapd pack ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 21:10:13 -04:00
										 |  |  | : digit-parser ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ digit? ] satisfy [ digit> ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 21:10:13 -04:00
										 |  |  | : integer-parser ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2015-07-19 21:06:35 -04:00
										 |  |  |     [ digit? ] satisfy repeat1 [ string>number ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-03 14:28:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 21:10:13 -04:00
										 |  |  | : string-parser ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ CHAR: " = ] satisfy hide , | 
					
						
							|  |  |  |         [ CHAR: " = not ] satisfy repeat0 , | 
					
						
							|  |  |  |         [ CHAR: " = ] satisfy hide , | 
					
						
							|  |  |  |     ] seq* [ first >string ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-03-19 20:40:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (range-pattern) ( pattern -- string )
 | 
					
						
							| 
									
										
										
										
											2015-09-08 19:15:10 -04:00
										 |  |  |     ! Given a range pattern, produce a string containing | 
					
						
							|  |  |  |     ! all characters within that range. | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         any-char , | 
					
						
							|  |  |  |         [ CHAR: - = ] satisfy hide , | 
					
						
							|  |  |  |         any-char , | 
					
						
							|  |  |  |     ] seq* [ | 
					
						
							|  |  |  |         first2 [a,b] >string
 | 
					
						
							|  |  |  |     ] action | 
					
						
							|  |  |  |     replace ;
 | 
					
						
							| 
									
										
										
										
											2008-03-19 20:40:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 08:25:45 -04:00
										 |  |  | : range-pattern ( pattern -- parser )
 | 
					
						
							| 
									
										
										
										
											2015-09-08 19:15:10 -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. | 
					
						
							| 
									
										
										
										
											2014-12-12 17:19:39 -05:00
										 |  |  |     dup first CHAR: ^ = [ | 
					
						
							|  |  |  |         rest (range-pattern) [ member? not ] curry satisfy | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         (range-pattern) [ member? ] curry satisfy | 
					
						
							|  |  |  |     ] if ;
 |