Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Parse limitation ?

From: petr:krenzelok:trz:cz at: 8-Oct-2003 16:31

Petr Krenzelok wrote:
>patrick la poste wrote: > >>Hi List, >> >>I'd like to parse a string searching for two things at the same time. >>it seems to me that this is impossible. >> >>For example, a text from which I want to extract the HREF and the SRC target. >> >>myText: {<A HREF="#section1"><IMG SRC="foobar.gif"><A HREF="#section1">} >> >>parse myText [ >> any [ thru "HREF=" copy target to ">" (print target) | >> thru "SRC=" copy target to ">" (print target) >> ] ; any >>] ; parse >> >>"#section1" >>"#section1" >> >>parse myText [ >> any [ thru "SRC=" copy target to ">" (print target) | >> thru "HREF=" copy target to ">" (print target) >> ] ; any >>] ; parse >> >>"foobar.gif" >>"#section1" >> >>The result is different depending which rule comes first. The only way I see as a workaround is to parse the text twice. Is there a better (smarter) way? >> >> >> >> >> >I would just like to point out, that 'first directive or tu/thru [a | b >| c] was proposed for parse enahncement some time ago, but then some >parse gurus (e.g. Gabriele) admitted, that parse would have to work >other way internally and that it is not easy achievable (am I right, >Gabriele?) > >OTOH - your example is just one of those which we often enough meet in >real life, but have no easy/elegant solution for, at least not for >novice being able to solve it .... >
Well, I just played a bit and following hack appeared in my notepad :-) reposition: func [str blk /local res tmp][ res: copy [] foreach item blk [ if not none? tmp: find str item [append res reduce [index? tmp item]] ] sort/skip res 2 either empty? res [str][at str (first res) - (index? str) + 1] ] myText: { <A HREF="#section1"><IMG SRC="foobar.gif"><A HREF="#section2"> <A HREF="#section1"><IMG SRC="foobar.gif"><A HREF="#section2"> <A HREF="#section1"><IMG SRC="foobar.gif"><A HREF="#section2"> <A HREF="#section1"><IMG SRC="foobar.gif"><A HREF="#section2"> } src-rule: ["SRC=" copy target to ">" (print target)] href-rule: ["HREF=" copy target to ">" (print target)] parse/all mytext [ any [ mark: (mark: reposition mark ["HREF=" "SRC="]) :mark [src-rule | href-rule] ] to end ] You can call 'reposition function with block containing any number of options you want to decide upon which is coming first. It will just do plain search, analyze its postion, sort resulting block and "reposition" your parse input string so that the parser pointer points to first of the options, so you can directly apply "HREF=", "SRC=" etc and you can be sure one of them is there ... Well, I don't know how it is robust, but tried with mytext: read http://www.rebol.com and it seems it needs further tuning :-) .... following might get you better results: mytext: read http://www.rebol.com src-rule: [{SRC="} copy target to {"} (print target)] href-rule: [{HREF="} copy target to {"} (print target)] parse/all mytext [ any [ mark: (mark: reposition mark [{HREF="} {SRC="}]) :mark [src-rule | href-rule] ] to end ] Anyway ... you've got some inspiration ... -pekr-