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

World: r3wp

[Parse] Discussion of PARSE dialect

Oldes
9-Apr-2007
[1680x2]
p:  gets current parse position
:p  sets new position
but it must be in documentation somewhere
Henrik
9-Apr-2007
[1682]
it does, but I still don't see it. does parse read the block at the 
end of the 'any block?
Oldes
9-Apr-2007
[1683x3]
You can use it not just inside any block
but don't know any example why to use it without some rule like 'any
>> parse [1 2 3 4 5] [any [p: (probe p) skip]]
[1 2 3 4 5]
[2 3 4 5]
[3 4 5]
[4 5]
[5]
[]
Henrik
9-Apr-2007
[1686]
gave up. two-pass is easier :-)
Anton
10-Apr-2007
[1687x2]
Here is Oldes example padded with comments:
firsttime?: true 
parse [1 2 3 4 5][
	any [
		set v integer! 
		p:   ; <-- set P to the current parse position.

       ; (This is the input series with the series index at the current 
       position.)
		
		(
			; Here inside the paren is normal rebol code 
			
			prin v 
			if all [firsttime? v = 4][

    p: skip p -2   ; <-- move the index of the series referred to by 
    P
				firsttime?: false
			]
		) 
		
		:p   ; <-- set the current parse position to P
	]
]
Maxim
13-Apr-2007
[1689x5]
I am having a hard time with using REMOVE on a parsed string.
symbol: charset [#"a" - #"z" #"A" - #"Z" #"0" #"9" "_-?!*+~"]
nstr: "...aa.....a.....a.....h." 

parse/all nstr [
	some [
		symbol | end skip | 
		[

   here: ( probe here either none? here/1 [print nstr print "!!"  ][print 
   here/1 print nstr remove here here: back here]) ; :here skip
		]
		;:here
	]
] probe nstr
this just does an infinite loop... this is one of MANY twists I've 
put on the rules... (notice commented out :here)
and nothing works... if I set here to be the previous letter and 
then use skip in the rules... the first dot from nstr will not get 
removed... since we cannot go past the begining.
doh.... was about to give a better example... then I realise the 
error... there is nothing to match in the last rule, just an expression, 
so a no match is always matching nothing !
btiffin
13-Apr-2007
[1694]
It's nice just thinking out loud once in a while...we're here for 
you Maxim.  Cerebration.  :)
Maxim
13-Apr-2007
[1695x5]
it like saying the last rule matches the tail of the string.


Thus parse never gets "passed" the end cause my last rule matches 
the end, and thus prevents the parser from ever reaching it.
well, I was trying to say that I had not realized this was possible... 
and its quite cool... we can actually use that in some ways  ...


 make rules which make parse become an event handler for example ! 
  the moment you feed a string some value, parse will start treating 
 it...
and then fall back to silence... (just inserting a little wait in 
the loop will take care of cpu load)
it would be nice if the result from the expression could be used 
to determine if the rule is a match or not...
thus having (whatever expression TRUE)  would have allowed my previous 
last rule to match "everything else"
btiffin
13-Apr-2007
[1700x2]
Off topic but...that was what intrigued me with SNOBOL and Icon...succeed, 
fail and a result.
If you haven't, take a read through Icon pattern matching...mondo 
powerful.  Off topic...sorry.
Maxim
13-Apr-2007
[1702x2]
here is the solution... complement the valid symbols and match them 
explicitely.

rebol []

symbol: charset [#"a" - #"z" #"A" - #"Z" #"0" #"9" "_-?!*+~"]
invalid: complement symbol
nstr: "...aa.....a.....a.....h." 

end-rule: []

parse/all nstr [
	some [
		symbol | [here: invalid (remove here) ] :here
	]
]
reading? what is that? .... oh yeah... I remember ...  something 
people with time can do ... ;-)
btiffin
13-Apr-2007
[1704x3]
More off topic...I wept a little bit when I heard of Dr. Ralph Griswold 
passing, back in October.  Never met him, much respect.
Final off topic;  Now I'm slowly replacing all my computer heroes...Names 
like Kernighan, Pike, Moore, Griswold, Lovelace... are now Sassenrath, 
DocKimbel, Anton, Cyphre, Graham, Maxim, Ladislav, Henrik, Oldes...et 
al.  Thanks guys.  You are making my world a better place.
Final final...Gregg, Gabriele, Sunanda...I'm missing too many.  :) 
 And the big hook drags me off the stage.
Ladislav
13-Apr-2007
[1707x4]
Max: ...symbol | end skip | ... does not have much sense, since it 
is equivalent to just ...symbol | ...
Max: "it would be nice if the result from the expression could be 
used to determine if the rule is a match or not" - that is of course 
possible as follows:
rule: [(next-rule: unless result-of-expression [[end skip]]) next-rule]
see this:

>> rule: [(next-rule: unless result-of-expression [[end skip]]) next-rule]

== [(next-rule: unless result-of-expression [[end skip]]) next-rule]
>> result-of-expression: false
== false
>> parse "" rule
== false
>> result-of-expression: true
== true
>> parse "" rule
== true
Maxim
13-Apr-2007
[1711x2]
yes... see the supplied fixed parser rules... I had not submitted 
the right one... I saw that after posting it  :-)
ladislav, what you demonstrate is that you can evaluate an expression 
within parse rules... parse itself does not handle the return value 
OF the expression.
Ladislav
13-Apr-2007
[1713x2]
right, but the value of the expression *can be used* to determine 
if a rule is a match
otherwise, I am for addition of a rule which would take the result 
of the paren! expression directly into account without us having 
to resort to this (more complicated) way
Maxim
13-Apr-2007
[1715x2]
that is what I meant... I'd like parse to do it for us .
obviously, using many tricks we can let parse do anything  (including 
become an event parser as I discussed above  ;-)
Ladislav
13-Apr-2007
[1717]
anyway, when you have got the RULE above, you can now always write: 
parse "" [(result-of-expression: compute-something) rule]
Maxim
13-Apr-2007
[1718]
got to go *now*   ;-)   female in the house   ;-)
Ladislav
13-Apr-2007
[1719x2]
if you use a more appropriate rule name like check-result, you have 
got a more readable:
parse "" [(result-of-expression: compute-something) check-result]
btiffin
13-Apr-2007
[1721]
guru question;  Will a utype! definition be allowed to wrap builtins? 
 SNMP MIBs require a fairly heavy weight tuple!  But will a short 
MIB conflict with internal scans of tuple! or do utype! scans take 
some form of precedent?  I've become curious, yet remain dumb enough 
to not know.
Oldes
14-Apr-2007
[1722x5]
Isn't this a bug?
>> parse [a/b] [a/b]
** Script Error: a has no value
** Near: parse [a/b] [a/b]
I don't want the a to be evaluated in the parse rules!
hm... ech.. I'm stupid.. normaly is evaluated as well, so it's not 
a bug.. but is there any way how to parse specific path! ?
I mean:
>> parse [a] ['a]
== true
>> parse [a/b] ['a/b]
== false
Brian: only Carl probably knows that.
ChristianE
14-Apr-2007
[1727]
>> parse [a/b] [(path: 'a/b) path]
== true
>> parse [a/c] [(path: 'a/b) path]
== false
Oldes
14-Apr-2007
[1728]
that works, but it's ugly:(
Gabriele
14-Apr-2007
[1729]
older versions did not evaluate paths. since newer version do, we 
need 'a/b to work. dunno if this is in RAMBO... but it needs to be 
fixed.