Parse
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:
Or range:
Matching
skip
Skips (jumps) one element:
thru
Skips elements until if finds a match. Input is set past the match.
to
Skips elements until if finds a match, but the match is checked by the rule.
ahead
Checks if the next element (ahead) matches a rule.
end
Returns true if all input items have been checked by parse.
none
Always returns sucess. It is a catch-all rule
opt
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.
Iteration
some, any
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 :
Storing input
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 .
collect set
parse
will return a logical true
or false
, and insert all the successes preceded by the command keep in a new block.
collect into
parse
will return a logical true
or false
, and insert all the successes preceded by the command keep
in a block you previously created.
Modifying input
insert
Inserts a value at the current input position and advance input after the value.
remove
Removes the matched input.
Useful links:
http://www.michaelsydenham.com/reds-parse-dialect/
http://www.red-lang.org/2013/11/041-introducing-parse.html
The following links refer to Parse in Rebol which is somewhat different than in Red:
http://www.rebol.com/docs/core23/rebolcore-15.html
http://www.codeconscious.com/rebol/parse-tutorial.html
http://www.rebol.com/r3/docs/concepts/parsing-summary.html
http://www.rebol.com/r3/docs/functions/parse.html
http://blog.hostilefork.com/why-rebol-red-parse-cool/
https://en.wikibooks.org/wiki/Rebol_Programming/Language_Features/Parse/Parse_expressions
For future reference, I'll write down a list of commands taken from http://www.red-lang.org/2013/11/041-introducing-parse.html
Matching
ahead rule
Look-ahead rule, match the rule, but do not advance input.
end
Return success if current input position is at end.
none
Always return success (catch-all rule).
not rule
Invert the result of the sub-rule.
opt rule
Look-ahead rule, optionally match the rule.
quote value
Match next value literally (for dialect escaping needs).
skip
Advance the input by one element (a character or a value).
thru rule
Advance input until rule matches, input is set past the match.
to rule
Advance input until rule matches, input is set at head of the match.
Control flow
break
Break out of a matching loop, returning success.
if (expr
)
Evaluate the Red expression, if false or none, fail and backtrack.
into rule
Switch input to matched series (string or block) and parse it with rule.
fail
Force current rule to fail and backtrack.
then
Regardless of failure or success of what follows, skip the next alternate rule.
reject
Break out of a matching loop, returning failure.
Iteration
any rule
Repeat rule zero or more times until failure or if input does not advance.
some rule
Repeat rule one or more times until failure or if input does not advance.
while rule
Repeat rule zero or more times until failure regardless of input advancing.
Extraction
collect [rule]
Return a block of collected values from the matched rule.
collect set word [rule]
Collect values from the matched rule, in a block and set the word to it.
collect into word [rule]
Collect values from the matched rule and insert them in the block referred by word.
copy word rule
Set the word to a copy of the matched input.
keep rule
Append a copy of the matched input to the collecting block.
keep (expr)
Append the last value from the Red expression to the collecting block.
set word rule
Set the word to the first value of the matched input.
Modification
insert only value
Insert[/only] a value at the current input position and advance input after the value.
remove rule
Remove the matched input.