Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 New REBOL 3 PARSE dialect. Pretty cool.
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
btiffin




PostPosted: Sun Nov 01, 2009 11:32 am   Post subject: New REBOL 3 PARSE dialect. Pretty cool.

See http://www.rebol.com/r3/docs/concepts/parsing-summary.html for more details.

A small sample of what the new RETURN keywords allow
code:

>> ; more likely read from a configuration file
>> conf: {homebase: /etc/rebol^/dates: rejoin [now/year "-" now/month "-" now/day]}
== {homebase: /etc/rebol
dates: rejoin [now/year "-" now/month "-" now/day]}

>> ; parse out the date formatting preference
>> parse conf [thru "dates" any #" " ":" any #" " return [to newline | to end]]
== {rejoin [now/year "-" now/month "-" now/day]}

>> ; do that return value
>> do parse conf [thru "dates" any #" " ":" any #" " return [to newline | to end]]
== "2009-11-1"

Explained:
conf: sets the word conf to a sample configuration string. The ^/ is the REBOL escape for newline.
parse then scans through the string and pattern matches passed "dates" any amount of spaces, a literal ":" and then any amount of spaces. After that it RETURNs the next pattern match, here it scans upto a newline or upto the the end of input.

do parse, is the same thing, but it evaluates the returned string. REJOIN is short for REDUCE JOIN and evaluates the data in a block joining all the results together.

There is no validation of the input; but that could be a simple test for false on the parse.

code:

>> parse conf [thru "fates" any #" " ":" any #" " return [to newline | to end]]
== false

>> ; do that return value
>> do parse conf [thru "mates" any #" " ":" any #" " return [to newline | to end]]
== false

There being no mates or fates in our conf.

Worth a look, imho.
Cheers
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: