Computer Science Canada Tutorial: REBOL parse |
Author: | btiffin [ Fri Apr 04, 2008 10:35 pm ] | ||||||||||||||||||
Post subject: | Tutorial: REBOL parse | ||||||||||||||||||
An introduction to REBOL PARSE By Brian Tiffin April 2008 PARSE REBOL, the Relative Expression Based Object Language has many features. One of the key elements of this scripting language is PARSE. PARSE opens the door to Domain Specific Languages or Dialects using REBOL terminology. Unlike Regular Expressions, PARSE works more like the pattern matching made famous by SNOBOL and Icon and can resemble Backus-Naur Form. Code examples use >> to signify the console prompt with [ used during line continuations and == to signify the result display or as complete scripts, with a REBOL header suitable for >> do %script.r Major Modes PARSE has two major modes
block parsing String parsing At its simplest, PARSE is used to pull strings apart, using default delimiters
REBOL ate the space and returned a block! with two elements
and the string! "World!" This is using the default parse rules; specified by none. none is a REBOL word that has a type of none! and a value of none. Other delimiters can be specified, such as comma
or dash
or null
Here the /all refinement was used so that spaces, tabs and newlines are included. Grammar Rules String splitting is all well and good, but parse rules can be far more sophisticated. The rules for PARSE use a dialect of REBOL and as part of the Relative Expression acronym, symbols can have different meanings in different contexts. Alternates The vertical bar allows alternates in a pattern match
The above would be true for "a radio", "the radio", "a phone" or "the phone". The rules can also be in variables, looking a little more BNF.
REBOL datatype parsing This is where things can get fun. The script
==false will display the HTML title line from the compsi.ca website. REBOL knows about the tag! datatype. Lexically, anything within angle brackets is treated as a tag! So; the thru keyword asks the parser to scan through the title tag, leaving the parse scanner pointing just past the title open tag. copy title informs the parser to keep a copy of the next match that follow in the REBOL variable title. That pattern match happens to be to </title>. Unlike thru, to asks the parser to match up to the tag, leaving the scanner just before the closing </title> tag. And the REBOL variable title, will end up having the text that is between the <title> and </title> tags of the page that is returned by reading http://compsci.ca, be that index.html, index.php or whatever the webserver delivers. Note, this example will return false from the parse. There is more text beyond the </title> so the parse expression actually fails, but we still get our title. A proper parse expression returning true would be
More to it than what is shown here This is just a sample of what can be done with PARSE. There are more keywords, more patterns, more features. But being REBOL; not hundreds, just a dozen or so. With that toolkit of features, a lot of power exists. Last example To end this little tutorial; evaluated parsing. After each successful match, the parse dialect allows expression evaluation. This is where the power of PARSE becomes more apparent, (though perhaps not in this trivial example). This example includes parse looping, REBOL value parsing and expression evaluation. Note that the input is not a string!, but a block! of loadable REBOL values. The some keyword causes a one or more repeat to occur inside the parse, and skip advances the parse scanner (used here to skip over non integers).
1 squared is 1 2 squared is 4 3 squared is 9 1024 squared is 1048576 == true Part of the reason for this example is also to raise curiosity so that you will want to visit the online manual at rebol.com to find out more. Please note; I froth at the mouth a little bit when I evangelize REBOL, out on the lunatic fringe. Cooler heads do prevail in the majority of the REBOL community and others can do a better job of explaining things than I. Think of me as the nut case sports fan all dressed in body paint with a flame spewing top hat, far too excited to actually make any sense, but meaning well, and hopefully adding to the fun of the event. And I just realized, I didn't even show a keyword Dialect or DSL. Maybe next time if anyone is curious for more. Cheers |
Author: | A.J [ Sat Apr 05, 2008 1:04 pm ] |
Post subject: | Re: Tutorial: REBOL parse |
Nice! You should ask Tony if you could make a REBOL Forum ![]() |
Author: | btiffin [ Mon Apr 07, 2008 9:35 am ] |
Post subject: | Re: Tutorial: REBOL parse |
Thanks A.J. I'd do my best to populate a REBOL forum. ![]() Next Tutorial will either be PARSE and DSL or something on the ease of Client / Server when using the message passing features of REBOL. Cheers, Brian |
Author: | Mackie [ Mon Apr 07, 2008 9:44 am ] |
Post subject: | RE:Tutorial: REBOL parse |
If you want to start a Rebol forum, you should make some more, introductory tutorials. If people start leanring, and posting the creation, a forum would probably follow, if you want REBOL forum that is. |
Author: | btiffin [ Mon Apr 07, 2008 12:23 pm ] |
Post subject: | Re: Tutorial: REBOL parse |
Mackie; I was thinking the same thing. Let things build up until it seems reasonable to have a full forum. One thing that happened on RebolTalk ... too many forum divisions. There is definitely a balance; one that is easily broken, but hard to define. I've been plopping recent REBOL related posts in this Functional Programming group. A small misnomer, as REBOL isn't clearly defined as Functional ... nor is it OO, nor declarative, nor ... but it has features of many of these paradigms. I won't be pestering Tony anytime soon. ![]() Cheers |
Author: | Mackie [ Mon Apr 07, 2008 6:42 pm ] |
Post subject: | RE:Tutorial: REBOL parse |
I'll be glad to check out REBOL, once I have more experience. For now I'm sticking with Ruby. ![]() |
Author: | reboltutorial [ Sun Jun 28, 2009 11:44 pm ] |
Post subject: | RE:Tutorial: REBOL parse |
I read chapter 15: http://www.rebol.com/docs/core23/rebolcore-15.html#section-8" Code: spacer: charset reduce [tab newline #" "] spaces: [some spacer] rule: ["a" spaces "b" spaces "c"] parse/all "a b c" rule is OK but if I change rule to just Code: rule: ["a" spaces copy varb to spaces "c"] parse/all "a b c" rule Rebol Console outputs error: Code: ** Script Error: Invalid argument: some spacer ** Where: halt-view ** Near: parse/all "a b c" rule >> Why ? |
Author: | btiffin [ Tue Jul 21, 2009 7:13 pm ] | ||
Post subject: | Re: Tutorial: REBOL parse | ||
TO sadly only accepts datatype! or value, not a pattern or dialect command. http://www.rebol.com/docs/core23/rebolcore-15.html#section-10.3 If you'd like to work out alternative parse mechanics, we can talk more. Something like
Cheers |
Author: | tbrown01 [ Thu Jan 14, 2010 12:57 am ] |
Post subject: | RE:Tutorial: REBOL parse |
Great! You've done your assignment well! Keep it up.... *removed links* |
Author: | chiropractic marketing [ Sat Sep 25, 2010 2:14 am ] |
Post subject: | Re: Tutorial: REBOL parse |
Is there anything anymore that's considered a general programming language? Kind of like C used to be, or is it still? *removed link* |
Author: | DtY [ Sat Sep 25, 2010 6:54 pm ] |
Post subject: | Re: Tutorial: REBOL parse |
chiropractic marketing @ Sat Sep 25, 2010 2:14 am wrote: Is there anything anymore that's considered a general programming language? Kind of like C used to be, or is it still?
I love how this message is *almost* passable as something a human would say.*removed link* |