Parse is a "dialect" of Red (a DSL - domain specific language to be precise), that is, a mini-language embedded inside Red. The Red interpreter you download comes with a few of these languages.Two of them are aimed at creating GUI and drawings, and seem to me so seamlessly blend into the programming flow, that I chose to treat them as regular Red commands in the GUI chapters.
Parse, on the other hand, really feels like you are making a program within a program, so I think it should be studied as a small programming language.
parse picks each element of the input and submits it to the corresponding rule in the rules block. It returns
true if all rules are matched or
false, if one fails to match its corresponding rule.
A most basic example would be to simply check if each element in the input block is equal to the corresponding rule in the rules' block:
For the sake of clarity in the description of parse, lets rewrite the example above with a different format:
The match may be done with datatypes:
Regular code may be inserted in the rules' block by enclosing it with parenthesis:
Rules accept the logical "or" operator, represented by "|":
Rules can be repeated by adding the number of repetitions (or range) before it:
Skips (jumps) one element:
Skips elements until if finds a match. Input is set past the match.
Skips elements until if finds a match, but the match is checked by the rule.
Checks if the next element (ahead) matches a rule.
Returns true if all input items have been checked by parse.
Always returns sucess. It is a catch-all rule
If it finds a match, it returns sucess, and parse follows to the next input. If the input does not match the opt rule, parse skips (ignores) this opt rule and checks the same input with the next rule.
Both return success for as long as they find matches in the input, the difference is that some requires at least one occurrence of the input (match), while any will return success even with no match :
set and copy
Both get the input of the next parse rule, if successful. set sets a variable to it, copy sets a copy of the input to the variable.
collect and keep
If you have a collect block inside your rules' block, parse will no longer return a logical true or false, instead it will return a block with all the successes preceded by the command keep .
parse will return a logical
false, and insert all the successes preceded by the command keep in a new block.
parse will return a logical
false, and insert all the successes preceded by the command
keep in a block you previously created.
Inserts a value at the current input position and advance input after the value.
Removes the matched input.
The following links refer to Parse in Rebol which is somewhat different than in Red:
For future reference, I'll write down a list of commands taken from http://www.red-lang.org/2013/11/041-introducing-parse.html
Look-ahead rule, match the rule, but do not advance input.
Return success if current input position is at end.
Always return success (catch-all rule).
Invert the result of the sub-rule.
Look-ahead rule, optionally match the rule.
Match next value literally (for dialect escaping needs).
Advance the input by one element (a character or a value).
Advance input until rule matches, input is set past the match.
Advance input until rule matches, input is set at head of the match.
Break out of a matching loop, returning success.
Evaluate the Red expression, if false or none, fail and backtrack.
Switch input to matched series (string or block) and parse it with rule.
Force current rule to fail and backtrack.
Regardless of failure or success of what follows, skip the next alternate rule.
Break out of a matching loop, returning failure.
Repeat rule zero or more times until failure or if input does not advance.
Repeat rule one or more times until failure or if input does not advance.
Repeat rule zero or more times until failure regardless of input advancing.
Return a block of collected values from the matched rule.
Collect values from the matched rule, in a block and set the word to it.
Collect values from the matched rule and insert them in the block referred by word.
Set the word to a copy of the matched input.
Append a copy of the matched input to the collecting block.
Append the last value from the Red expression to the collecting block.
Set the word to the first value of the matched input.
Insert[/only] a value at the current input position and advance input after the value.
Remove the matched input.