
EN: rebol.com -
rebol.org -
rebol.net -
FR: Rebol Documentation Project -
forum RebelBB -
rebol-france.org -
forum codeur -
The main goal of this documentation is to create a refering point concerning parse. Parse is a central technology in rebol. But it’s the most ununderstoud and low documented technology in rebol.
Parse allow the coder to bring another level in their applications.
Parse is what makes rebol a complete different language. So having a good understanding of it allow us to show and use rebol at it’s full power.
This work is mainly inspired by the wikibook about parse
http://en.wikibooks.org/wiki/REBOL_Programming/Language_Features/Parse
This documentation is open to anyone. You have some experience using parse or creating dialects we want you!
To contribute the rules are the following:
1) please don’t erase the work of the other
2) create a header with a resume of what you want to say
3) explain like if you where talking to a 5 years child explain each step of your example
4) Boring text without example is not helping so even if you don’t have a complicated example to show what you are saying bring some easy to understand and commented example.
5) Say about parse what other have not already said.
6) For some specific problems, we can work in the ask/answer mode too.(cookbook)
keep those rules in mind and lets work!
Side note: if you want to submit bunch of code you can unse the flags <code rebol> and </code>
Parse is a string analyst we will explain further how it works.
Mainly parse can be used in place of a
foreach line document [ ;this is an example of a if matcher string block if any [ find in_string "keyword1" [ what_to_do] find in_string "keyword2" [ what_to_do] find in_string "keyword3" [ what_to_do] ]]
A dialect is a string or document countaining keywords that will be identified by parse and parse will then run the associated functions and fill arguments.
parse is a string analyst. That means it will identify schemes in the submited strng (one or more line rebol is not type length defined so if it works with a line it works with several lines too)
The 2 main ways to use parse are:
- Data format control
- Keyword matching.
To be able to work parse should recieve the bunch of ascii/unicode text to be analysed and the matching rules or the keywords mataching rules.
To be simple you say to parse on what to work (input string) and how to work (rule).
Keyword matching type of rules are what are called dialects in rebol. When a keyword is found in the input string then the related action is done by parse. In tags oriented format like MakeDoc or HTML or XML that’s a pretty usefull tool.
Dialects will be explained in full details in the second part of this document. Please refere to the appropriated section using the table of content.
In this area we will speak about data matching after presenting how parse is implemented.
USAGE:
PARSE input rules /all /case
DESCRIPTION:
Parses a series according to rules.
PARSE is a native value.
ARGUMENTS:
input -- Input series to parse (Type: series!)
rules -- Rules to parse by (Type: block! string! none!)
REFINEMENTS:
/all -- Parses all chars including spaces.
/case -- Uses case-sensitive comparison.
This is the usual definition of parse but that’s not very clear.
Parse takes a sting in first argument and the second argument, the “rule”, can be a block! a string! or none!.
All the work is about understanding what kind of rule we need and how to build it.
We will see now the most easy kind of rule.
We saw in the definition that a rule can be none. Since none is nothing then this rule should be the most easy rule to exist in the parse word.
Don’t make the mistake to think that a “none” rule means nothing and can be avoided!
In fact a “none” rule is a rule and have an effect while not providing second argument to parse only produce an error.
>> parse "a b c" none == ["a" "b" "c"]
We notice here that the none rule doesn’t produce an error and have a really intresting effect!
Parse with a none rule split and store the alpha numerical items into séparated string in a block!
The space ascii caracter appears therefore as the delimiter character to say where we want the spliting of the string to be done and what items to isolate.
That means so if I read a file using read/lines, I will store in a block each lines of my document and then i will be able to split the content of each line using the none rule!!!
Doing so I can imagine having a software storing information to a file using a simple data organisation like this: name<space>phone number<space>city
Then I load this file in memory using read/line and for each of the lines in the created block! I will be able using the none! rule to split my content and why not to store the splited information into a rebol object! and those filled object!.
; I load my document content into memory >> my-doc: read/lines %/C/test.txt == ["andrew 555-00000 losangels" "Thomas 666-00000 pitsburg" "Jane 777-00000 seatle" " "] ; I test to see if my-doc contains all my informations from the document >> probe my-doc ["andrew 555-00000 losangels" "Thomas 666-00000 pitsburg" "Jane 777-00000 seatle" " "] == ["andrew 555-00000 losangels" "Thomas 666-00000 pitsburg" "Jane 777-00000 seatle" " "] ; I print on screen the data loaded in a better organised way >> foreach line my-doc [ my-tmp: parse line none print rejoin [ my-tmp/1 newline my-tmp/2 newline my-tmp/3 newline] ] andrew 555-00000 losangels Thomas 666-00000 pitsburg Jane: 777-00000 seatle
From that example we can understand that the none rule will parse something like: “I need to isolated groups of ascii characters which are not none” and so space is the equivalent to a not a A to Z or a to z and 0 to 9 characters. So space will be the none alphanumerical symbol in parse?
Is it true?
It’s partly true since we can optain the same splitting effect with newlines and comma or semi-comma as separators.
The rule involving a string! type base second argument for par have almost the same effet than with the none rule.
By giving a letter or a word as second argument to parse function we obtain a “locate and cut” effect.
But that’s not all of it.
The String rule is then the first brick to get into the blok rule with is where things get really tough and interresting.
In this area send your questions about parse
Or post detailled explainations to the questions.
Q: I want to retreive the name of a rebol fucntion in a string.
string: {my-function: func [ ^M} string: {my-function: function [ ^M} string: {set 'my-function [ ^M}
The end of the string could be end or newline
the parse sentence should be able to match any of those 3 examples.
A:
As a starter for a solution to this question Steeve propose:
>>t: {toto: func [ a b ] [ ; etc... ^/ } >> parse t [ some [ copy str thru ":" "func" (print str) | thru newline ]]