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

World: r3wp

[Parse] Discussion of PARSE dialect

Ladislav
11-Oct-2005
[463]
example of what you get and what want?
Graham
11-Oct-2005
[464x2]
split-text "this is a sentence" 11
[]
I want [ "this is a sentence" ]
Ladislav
11-Oct-2005
[466]
how about:

    copy frag [to #" " | to end]
Graham
11-Oct-2005
[467x2]
yah! that works.
I tried putting in an alternate rule of " | copy frag to end " but 
that hung the parser.
Ladislav
11-Oct-2005
[469x2]
this should work too:

    [copy frag #" " | copy frag to end]
rather:  [copy frag to #" " | copy frag to end]
Graham
11-Oct-2005
[471]
Hmm.  It's failing when the text is less than n.
Ladislav
11-Oct-2005
[472]
strange
Graham
11-Oct-2005
[473x2]
[ copy frag n | copy frag to end ]
at the beginning I guess
Ladislav
11-Oct-2005
[475]
aha, this one, yes, right
Graham
11-Oct-2005
[476]
[ copy frag n skip | copy frag to end ]
Ladislav
11-Oct-2005
[477]
or copy frag [n skip | to end]
Graham
11-Oct-2005
[478]
nope.
Ladislav
11-Oct-2005
[479]
if append "" none works as it does, then it is a problem with the 
none value
Tomc
11-Oct-2005
[480x2]
split-text: func [txt [string!] n [integer!]
    /local frag fraglet bl frag-rule bs ws
][  ws: charset [#" " #"^-" #"^/"]
    bs: complement ws
    bl: copy []
    frag-rule: [
        any ws
        copy frag [n skip] (print frag)
        opt[copy fraglet some bs  (print fraglet)]
        (insert tail bl join frag fraglet)
    ]
    parse/all txt [some frag-rule]
    bl
]
will prolly fail on tezt < n as well
Graham
11-Oct-2005
[482x2]
it does fail.
simpler to just check the size of the string before it gets worked 
on.
Tomc
11-Oct-2005
[484x2]
yep
but the last  line being less than n is the same thing
Ladislav
11-Oct-2005
[486]
I must say, that the NONE issue is quite annoying/inconsistent (IMO)
Graham
11-Oct-2005
[487]
yeah .. it is.
Ladislav
11-Oct-2005
[488x2]
how about this one?: copy frag [n skip | to end] (frag: any [frag 
""])
maybe we should put it to RAMBO as an enhancement request?
Tomc
11-Oct-2005
[490x2]
split-text: func [txt [string!] n [integer!]
    /local frag fraglet bl frag-rule bs ws
][  ws: charset [#" " #"^-" #"^/"]
    bs: complement ws
    bl: copy []
    frag-rule: [
        any ws
        [copy frag [n skip]
         [opt[copy fraglet some bs  (insert tail frag fraglet)]
        ]
       |[copy frag to end]
       (insert tail bl frag)
    ]
    parse/all txt [some frag-rule]
    bl
]
untries
Graham
11-Oct-2005
[492x3]
Ladislav, that doesn't work.
tom, untried fails :)
this is being done to try and format and wrap text of course.
Ladislav
11-Oct-2005
[495]
>> n: 5 parse "" [copy frag [n skip | to end] (frag: any [frag ""])]
== true
>> frag
== ""
Graham
11-Oct-2005
[496]
this is a little tricky for such a simple function.
Ladislav
11-Oct-2005
[497]
that is why I am saying that the NONE issue is annoying
Graham
11-Oct-2005
[498]
Did you get a chance to talk to Carl?
Ladislav
11-Oct-2005
[499]
yes, I did, he accepted all my proposals - more than 600 lines of 
text when written
Pekr
11-Oct-2005
[500x2]
Ladislav - could you post it, please? :-)
or at least pointers to some older articles already written, which 
were accepted by Carl?
Ladislav
11-Oct-2005
[502]
(but this one deserves to be put to Rambo too - do you want me to 
put it there?)
Graham
11-Oct-2005
[503x3]
yes.
Do we have a final function that works yet?
it might be easier to to a parse/all txt #" " and then build up the 
lines one at a time.
Tomc
11-Oct-2005
[506x2]
I can get rid of the infinate nones by making an error ...
but that dosent really count
Ladislav
11-Oct-2005
[508]
split-text: func [
	txt n 
	/local frag result bl stop-rule
][
    bl: copy []
    result: copy ""
    stop-rule: none
    end-rule: [to end (stop-rule: [end skip])]
    frag-rule: [

  copy frag [n skip | end-rule] (frag: any [frag ""] print frag append 
  result frag)

  copy frag [to #" " | end-rule] (frag: any [frag ""] print frag append 
  result frag append bl copy result clear result)]
    parse/all txt [some [frag-rule stop-rule]]
    bl
]
Tomc
11-Oct-2005
[509]
split-text: func [txt [string!] n [integer!]
    /local frag fraglet bl frag-rule bs ws
][  ws: charset [#" " #"^-" #"^/"]
    bs: complement ws
    bl: copy []
    frag-rule: [
        any ws
        copy frag [
            [1 n skip 
                opt[copy fraglet some bs]
            ]
            | to end skip
        ]
        (all [fraglet join frag fraglet]
         insert tail bl frag 
         print frag
        )
    ]
    parse/all txt [some frag-rule]
    bl
]
Graham
11-Oct-2005
[510]
I have to say Tomc is a winner !
Tomc
11-Oct-2005
[511]
it was the copy frag 1 n skip
Graham
11-Oct-2005
[512]
Unspecified .. but tom's function removes leading white space as 
well.  Ladislav's preserves whitespace.