Re: lookahead ?
[email protected] (Aaron J Mackey)
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <Pine.OSF.4.33.0104251922350.2199-100000@alpha10.bioch.virginia.edu> |
On Thu, 26 Apr 2001, Damian Conway wrote:
> You really do want to use a regex here (to get the lookahead/backtracking
> that RecDescent doesn't do).
>
> line: m/^(\s* # leading whitespace
> (.*?) # the title
> (?:\s+(?=\d+\.\d+\s*$)) # the space preceeding the numbers
> )
> (\d+) # the major version number
> \.
> (\d+) # the minor version number
> /x
> <reject: length $1 != 47>
> { @{$return}{title major minor)} = ($2,$3,$4) }
Ok, but do I need to insert a <skip: ''>, so that the leading whitespace
gets captured (and not skipped)?
I'm still wrapping my mind around all this: when you say that RD doesn't
backtrack .. isn't that what trying alternative rules does, effectively
(as long as there hasn't been a <commit>)? In other words, how about
something like this:
line: title '' <reject: $thiscolumn != 47> version
title: titleword(s)
titleword: ...!version word
version: minor '.' major
minor: digits
major: digits
word: /\S+/
digits: /\d+/
Hmm, I guess that would fail in an instance where a substring that looked
like "version" occured prior to column 47, even if a valid "version" also
occurred at column 47, something like this:
01 11 21 31 41 47
Here is a title with 4.3 in it 5.6
where the title here should be "Here is a title with 4.3 in it"
Hmm, how about a rule that grabs words until $thiscolumn >= 47 ?
title: titleword(s)
titleword: word <reject: do { $thiscolumn >= 47 }>
And then I start thinking about <leftop> and <rightop>, because it seems
like what I really want is:
title: word '' <reject: $thiscolumn != 47> |
word word '' <reject: $thiscolumn != 47> |
word word word '' <reject: $thiscolumn != 47> |
... etc
since I could easily insert '' between each word there, perhaps I could do
this:
title: <leftop: word '' word> '' <reject: $thiscolumn != 47>
I guess that's still a "greedy" match that would pass column 47 to include
the version, and then be rejected in it's entirety.
So how about:
title: <leftop: word '' <reject: $thiscolumn == 47> word>
Sorry for the perhaps random musings, and thank you for your assistance in
this most academic matter.
-Aaron