| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  | ! Copyright (C) 2007 Chris Double. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: help.markup help.syntax ;
 | 
					
						
							|  |  |  | IN: peg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: parse | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "input" "a string" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							| 
									
										
										
										
											2008-07-14 21:51:30 -04:00
										 |  |  |   { "ast" "an object" }  | 
					
						
							| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							| 
									
										
										
										
											2008-07-14 21:51:30 -04:00
										 |  |  |     "Given the input string, parse it using the given parser. The result is the abstract " | 
					
						
							|  |  |  |     "syntax tree returned by the parser." }  | 
					
						
							| 
									
										
										
										
											2008-03-28 06:20:43 -04:00
										 |  |  | { $see-also compile } ;
 | 
					
						
							| 
									
										
										
										
											2008-03-26 22:50:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | HELP: compile | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  |   { "word" "a word" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							| 
									
										
										
										
											2008-07-14 21:51:30 -04:00
										 |  |  |     "Compile the parser to a word. The word will have stack effect ( -- ast )." | 
					
						
							| 
									
										
										
										
											2008-03-28 06:20:43 -04:00
										 |  |  | }  | 
					
						
							|  |  |  | { $see-also parse } ;
 | 
					
						
							| 
									
										
										
										
											2008-03-26 22:50:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  | HELP: token | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "string" "a string" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that matches the given string." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: satisfy | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "quot" "a quotation" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that calls the quotation on the first character of the input string, " | 
					
						
							|  |  |  |     "succeeding if that quotation returns true. The AST is the character from the string." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: range | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "min" "a character" }  | 
					
						
							|  |  |  |   { "max" "a character" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that matches a single character that lies within the range of characters given, inclusive." } | 
					
						
							|  |  |  | { $examples { $code ": digit ( -- parser ) CHAR: 0 CHAR: 9 range ;" } } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: seq | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "seq" "a sequence of parsers" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that calls all parsers in the given sequence, in order. The parser succeeds if " | 
					
						
							|  |  |  |     "all the parsers succeed, otherwise it fails. The AST produced is a sequence of the AST produced by " | 
					
						
							|  |  |  |     "the individual parsers." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: choice | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "seq" "a sequence of parsers" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that will try all the parsers in the sequence, in order, until one succeeds. " | 
					
						
							|  |  |  |     "The resulting AST is that produced by the successful parser." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: repeat0 | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that parses 0 or more instances of the 'p1' parser. The AST produced is " | 
					
						
							|  |  |  |     "an array of the AST produced by the 'p1' parser. An empty array indicates 0 instances were " | 
					
						
							|  |  |  |     "parsed." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: repeat1 | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that parses 1 or more instances of the 'p1' parser. The AST produced is " | 
					
						
							|  |  |  |     "an array of the AST produced by the 'p1' parser." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: optional | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that parses 0 or 1 instances of the 'p1' parser. The AST produced is " | 
					
						
							|  |  |  |     "'f' if 0 instances are parsed the AST produced is 'f', otherwise it is the AST produced by 'p1'." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 23:34:59 -04:00
										 |  |  | HELP: semantic | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							| 
									
										
										
										
											2008-11-16 10:08:38 -05:00
										 |  |  |   { "quot" { $quotation "( object -- ? )" } }  | 
					
						
							| 
									
										
										
										
											2008-03-30 23:34:59 -04:00
										 |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that succeeds if the 'p1' parser succeeds and the quotation called with " | 
					
						
							|  |  |  |     "the AST produced by 'p1' on the stack returns true." } | 
					
						
							|  |  |  | { $examples  | 
					
						
							| 
									
										
										
										
											2008-07-14 21:51:30 -04:00
										 |  |  |   { $example "USING: kernel math peg prettyprint ;" "\"C\" [ drop t ] satisfy [ 66 > ] semantic parse ." "67" }  | 
					
						
							| 
									
										
										
										
											2008-03-30 23:34:59 -04:00
										 |  |  | } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  | HELP: ensure | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that succeeds if the 'p1' parser succeeds but does not add anything to the " | 
					
						
							|  |  |  |     "AST and does not move the location in the input string. This can be used for lookahead and " | 
					
						
							|  |  |  |     "disambiguation, along with the " { $link ensure-not } " word." } | 
					
						
							|  |  |  | { $examples { $code "\"0\" token ensure octal-parser" } } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: ensure-not | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that succeeds if the 'p1' parser fails but does not add anything to the " | 
					
						
							|  |  |  |     "AST and does not move the location in the input string. This can be used for lookahead and " | 
					
						
							|  |  |  |     "disambiguation, along with the " { $link ensure } " word." } | 
					
						
							|  |  |  | { $code "\"+\" token \"=\" token ensure-not \"+=\" token 3array seq" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: action | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							| 
									
										
										
										
											2008-11-16 10:08:38 -05:00
										 |  |  |   { "quot" { $quotation "( ast -- ast )" } }  | 
					
						
							| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that calls the 'p1' parser and applies the quotation to the AST resulting " | 
					
						
							|  |  |  |     "from that parse. The result of the quotation is then used as the final AST. This can be used " | 
					
						
							|  |  |  |     "for manipulating the parse tree to produce a AST better suited for the task at hand rather than " | 
					
						
							| 
									
										
										
										
											2008-03-31 00:26:42 -04:00
										 |  |  |     "the default AST. If the quotation returns " { $link fail } " then the parser fails." } | 
					
						
							| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  | { $code "CHAR: 0 CHAR: 9 range [ to-digit ] action" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: sp | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that calls the original parser 'p1' after stripping any whitespace " | 
					
						
							|  |  |  |     " from the left of the input string." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: hide | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Returns a parser that succeeds if the original parser succeeds, but does not "  | 
					
						
							|  |  |  |     "put any result in the AST. Useful for ignoring 'syntax' in the AST." } | 
					
						
							|  |  |  | { $code "\"[\" token hide number \"]\" token hide 3array seq" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: delay | 
					
						
							|  |  |  | { $values  | 
					
						
							| 
									
										
										
										
											2008-03-05 19:15:28 -05:00
										 |  |  |   { "quot" "a quotation" }  | 
					
						
							| 
									
										
										
										
											2008-01-13 04:31:31 -05:00
										 |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Delays the construction of a parser until it is actually required to parse. This "  | 
					
						
							|  |  |  |     "allows for calling a parser that results in a recursive call to itself. The quotation " | 
					
						
							| 
									
										
										
										
											2008-03-29 00:45:21 -04:00
										 |  |  |     "should return the constructed parser and is called the first time the parser is run." | 
					
						
							|  |  |  |     "The compiled result is memoized for future runs. See " { $link box } " for a word " | 
					
						
							|  |  |  |     "that calls the quotation at compile time." } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HELP: box | 
					
						
							|  |  |  | { $values  | 
					
						
							|  |  |  |   { "quot" "a quotation" }  | 
					
						
							|  |  |  |   { "parser" "a parser" }  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | { $description  | 
					
						
							|  |  |  |     "Delays the construction of a parser until the parser is compiled. The quotation " | 
					
						
							|  |  |  |     "should return the constructed parser and is called when the parser is compiled." | 
					
						
							|  |  |  |     "The compiled result is memoized for future runs. See " { $link delay } " for a word " | 
					
						
							|  |  |  |     "that calls the quotation at runtime." } ;
 |