Home   Archive   Permalink



R3 Parse: Is there any lazy quantifiers in parse dialect?

In parse dialect, it seems that all quantifiers (repetitions) are greedy, and I am wondering whether there are any lazy quantifiers?


posted by:   Rex       3-Mar-2014/6:49:30-8:00



Can you give an example of what you'd like to do?

posted by:   Nick       3-Mar-2014/12:01:20-8:00



Greedy quantifiers work like this:
If you match "aaaaa" with [some "a"], the position after the match should come to the end of the sequence. In other words the quantifier 'some eats up all the sequence.
But I want the quantifiers to stop as soon as it captures the minimal amount of data which make it true. In the example above, the lazy version of 'some should stop as soon as it reaches the first "a" , because that's already enough for the condition to be true (at least one "a").
I am wondering whether there are quantifiers work this way. And if not, whether it is possible to make one on my own.

posted by:   Rex       5-Mar-2014/10:06:37-8:00



I think this is what you're looking for. Using "thru" and "to end" returns a true value for the entire parse function:
    
>> parse "aaaaaa" [thru "a" to end]
== true
    
And also allows you to perform an operation with the input string, at the point where the first matching value is found:
    
>> parse "aaaaaa" [thru "a" copy x to end (print x)]
aaaaa
== true
    
parse "aaaaaabab" [thru "a" copy x to end (print x)]
aaaaabab
== true
    
>> parse "aaaaaabab" [thru "b" copy x to end (print x)]
ab
== true

posted by:   Nick       5-Mar-2014/10:37:05-8:00



If you want to capture the input string, including the matching character, use "to" instead of "thru":
    
>> parse "aaaaaabab" [to "b" copy x to end (print x)]
bab
== true

posted by:   Nick       5-Mar-2014/10:39:54-8:00



And that's not limited to single characters. You can match a string:
    
>> parse "aaaaaabab" [to "ba" copy x to end (print x)]
bab
== true
    
>> parse "aaaaaabab" [thru "ba" copy x to end (print x)]
b
== true

posted by:   Nick       5-Mar-2014/10:41:24-8:00