r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Parse] Discussion of PARSE dialect

Henrik
9-Jul-2006
[1327x3]
the block is stored in 'attr.

parse attr [

  (txt: 123 print txt) 'image any [set txt string! (print txt) | set 
  img word! | set action block!] (print txt) | 'face
]

gives:

123
search
none
== true
argh... forgot to show you attr
attr: [image test-image "Search" [print "test"]]
Oldes
9-Jul-2006
[1330]
Don't you want to use rather:

parse/all attr [(txt: 123 print txt) 'image opt [set txt string! 
(print txt)] opt [ set img word!] opt [ set action block! (print 
txt)] (print txt) | 'face]
Henrik
9-Jul-2006
[1331]
it seems the variable is stored in the last iteration, so it's definitely 
lost after one loop.
Anton
9-Jul-2006
[1332]
Doesn't Henrik want to be able to process the attributes in any order 
?
Henrik
9-Jul-2006
[1333x3]
that's what I want. I'm catching fish :-) or rather information to 
store them safely for processing later.
but let me try Oldes thing to see what it does
it works the exact opposite :-) Only the outer 'txt is set, and I 
can't reach the variable inside the block
Anton
9-Jul-2006
[1336x2]
Henrik, I parse your test attr block successfully:

>> parse [image test-image "Search" [print "action"]] ['image any 
[set txt string! (?? txt) | set img word! | set action block!]]
txt: "Search"
== true
Need more test data to parse.
Henrik
9-Jul-2006
[1338x2]
anton: yes, but what is txt after processing?
I don't want to handle it inside the parse block but after it
Anton
9-Jul-2006
[1340]
it is none.
Henrik
9-Jul-2006
[1341]
right. that's the problem
Anton
9-Jul-2006
[1342x2]
add a block to control the evaluation.
>> parse [image test-image "Search" [print "action"]] ['image any 
[[set txt string! (?? txt)] | set img word! | set action block!]]
txt: "Search"
== true
>> txt
== "Search"
Oldes
9-Jul-2006
[1344]
it always set the variables to none, if it fails to match
Anton
9-Jul-2006
[1345]
You have to be careful in your interpretation of  |
Oldes
9-Jul-2006
[1346]
interesting, this is the solution:

 txt: 123 parse/all attr ['image any [[set txt string! (print txt)] 
 | [set img word!] | [set action block! (print txt)]] (print txt) 
 | 'face]
Henrik
9-Jul-2006
[1347x2]
oldes solution seems to work
the wonders of parse... :-)
Oldes
9-Jul-2006
[1349]
it's just improved anton's solution:-)
Anton
9-Jul-2006
[1350]
I'm trying to figure out a simple example to show why.
Henrik
9-Jul-2006
[1351]
I wonder what the difference is? If it's only for controlling how 
global a variable is, it seems a little backwards to me
Oldes
9-Jul-2006
[1352]
without the extra brackets, the parse set the variables to none if 
it failes
Henrik
9-Jul-2006
[1353]
the brackets would make it a "real" rule, wouldn't it? it would be 
possible to replace the rule with a variable and have the rule block 
placed elsewhere in your code
Anton
9-Jul-2006
[1354]
You have to think of a rule like this:
	[ integer! | ]
as equivalent to
	[ integer! | none ]
or
	opt [ integer! ]
Oldes
9-Jul-2006
[1355]
I don;t know how much complex will be the source to parse, but I 
would use the OPT settings as used above
Anton
9-Jul-2006
[1356x2]
But Henrick wants it in any order, like LAYOUT code. (Thus, it's 
worth to look how LAYOUT works. :)
>> parse ["hi"] [[set num integer! | ] string!]   ?? num
num: none
Oldes
9-Jul-2006
[1358x2]
I just think he can end with endless loop easily, as he will never 
be able to catch the second 'image
>> parse ["hi"] [[set num [integer! | string!]] ] ?? num
num: "hi"
Anton
9-Jul-2006
[1360]
Note the | .     "set NUM to an integer or NONE, then... oh. it's 
the end of this block..."  but in Henrick's rule above he continues 
to set other variables, which is of no concern to parse, it still 
has successfully set NUM to NONE.
Henrik
9-Jul-2006
[1361]
there is only one 'image in the block fortunately
Oldes
9-Jul-2006
[1362]
ok, so you will not catch the 'face word:-) as it end up in the img 
variable:-)
Anton
9-Jul-2006
[1363]
I think he might be using 'test-image in place of a real image! for 
this example ?
Henrik
9-Jul-2006
[1364x2]
oldes, nope, because I have many different blocks. they either start 
with 'face or with 'image
never both
Anton
9-Jul-2006
[1366x2]
That's good.
So you can use ANY in an unlimited fashion.
Henrik
9-Jul-2006
[1368x2]
and it makes the parse scalable, so I can add options later
thanks for your help, everyone :-)
Anton
9-Jul-2006
[1370]
cool :)
Henrik
9-Jul-2006
[1371]
It's also a good thing with these discussions. I've never really 
grown 100% comfortable with parse.
DideC
10-Jul-2006
[1372]
About Layout : parse handles only the layout words (origin, space, 
at...), see source layout.

The face description is handled by a loop, not by parse. See system/view/vid/grow-facets
Pekr
19-Jul-2006
[1373x4]
Hi, need a bit of help ....
I tried doing myself small template "engine", which will simply look-up 
for some marks, and replace values accordingly. I decided to look 
for the end of the marks and my friend suggested me, that I should 
name even ending marks, to be clear there is not an error. My parse 
looks like this:
REBOL []

template: {

<b><!--[mark-x]-->Hello x!<!--/[mark-y]--></b>
<b><!--[mark-y]-->Hello y!<!--/[mark-y]--></b>
<b><!--[mark-z]-->Hello z!<!--/[mark-z]--></b>
<b><!--[mark-w]-->Hello w!<!--/[mark-w]--></b>

}

parse/all template [
   some [
         thru "<!--["
         copy mark to "]-->"
         "]-->" 
         start:
         copy text to "<!--/["
         end:
         "<!--/[" mark "]-->"
         (print text)
         |
         skip
    ]
]           
         
halt
I now can create simply a func, which will accept mark name, and 
do some code-block accordingly - sql query, simple replace of value, 
whatever (well, it will not work for cases like img tags, so it is 
not as flexible as full html parser in temple for e.g., but hey, 
it is meant being simple)