in reply to Modifying Parse::RecDescent Grammar to deal with multiline property file entries

This seems to work... (your LastProp, in this context, was superflous)

Update Kind of works, but I realized it allows continuation lines before a prop

use strict; use Parse::RecDescent; $::TestGrammar = <<'TG'; Output: PropLine(s) /\Z/ PropLine: CommentLine | SimpleProp | ContinuationLine CommentLine: /\#.*\n/ { print "RULE: $item{__RULE__}\n"; print "MATCH: $item{__PATTERN1__}\n"; } SimpleProp: VAR EQ VAL { print "RULE: $item{__RULE__}\n"; print "VAR: $item{VAR}\n"; print "EQ: $item{EQ}\n"; print "VAL: $item{VAL}\n\n"; } VAR: /^[^=\n]+/ EQ: '=' VAL: /.+/ ContinuationLine: VAL { print "RULE: $item{__RULE__}\n"; print "VAL: $item{VAL}\n\n"; } TG undef $/; my $foo = <DATA>; my $parser = Parse::RecDescent->new($::TestGrammar); defined $parser->Output($foo) or die "FAILURE"; __DATA__ # Comment Line # Comment #2 foo=this is property one but bar=does it grab this one too? baz=snark

                - Ant
                - Some of my best work - (1 2 3)

  • Comment on Re: Modifying Parse::RecDescent Grammar to deal with multiline property file entries
  • Download Code

Replies are listed 'Best First'.
Re^2: Modifying Parse::RecDescent Grammar to deal with multiline property file entries
by chahn (Beadle) on Aug 16, 2007 at 16:58 UTC
    Thank you for the suggestion.

    I need to work on each property as a whole, but could still piece together your ContinuationLine with its corresponding SimpleProp....

    In any case, what I was hoping to express was that a property is everything from a line beginning with Var=Val and continuing up to, but not including, the next line beginning with Var=Val. This seems a likely spot for a look-ahead.

    I am going to brute force the situation, using regexps and flags to indicate when in a Property, but do hope to poke on this when I can. I will certainly post back any useful conclusions.
      yeah you could do something like
      /.*(?=(^[^=]+=|\Z))/ # not tested, but should be about right
      Or you could use the code... set a flag when you find a propname and unset only except continuation lines when it is set. If you need to look at the prop as one piece just build a parse tree... that usually is the better way to go unless you are working on a huge dataset or a real time stream. And, as you say, you could pop it back together in simpleprop.

      Of course.. unless you need them it is often easier to pre-process out the comments.

                      - Ant
                      - Some of my best work - (1 2 3)