| 1 | package Config::Format::Ini::Grammar; | ||
| 2 | 10 | ▶ | use Parse::RecDescent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 10 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 10 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 10 | my $gram = <<'END_GRAMMAR' ; | |
| 7 | { my $h ; | ||
| 8 | use Data::Dumper; | ||
| 9 | my %esc = map {$_ => pack 'H2', $_} 0..255; | ||
| 10 | @esc{ '\n', '\t', '\r', '\a' } = ( "\n", "\t", "\r", "\a" ); | ||
| 11 | sub merge2hash { my $aref = shift; my $h; | ||
| 12 | ref eq 'HASH' and @{$h}{keys %$_} = values %$_ for @$aref; $h; | ||
| 13 | } | ||
| 14 | sub postprocess { | ||
| 15 | local $_ = shift ||return; | ||
| 16 | s/ \\x([\d]{1,3})/ $esc{ "$1"+0 } /egx; | ||
| 17 | s/ (\\[ntra]) / $esc{$1} /xge; | ||
| 18 | $_ ; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | startrule : <skip:'[ \t]*'> | ||
| 24 | Section(s) | ||
| 25 | { $return = merge2hash( $item[2]) } | ||
| 26 | |||
| 27 | Section: Title "\n" Pair(s?) | ||
| 28 | { $return = { $item[1]=> merge2hash( $item[3])||{} } } | ||
| 29 | | COMMENT(s) | ||
| 30 | | BLANK(s) | ||
| 31 | | <resync> | ||
| 32 | |||
| 33 | Title: '[' /\w+/ ']' COM(?) | ||
| 34 | { $item[2] } | ||
| 35 | |||
| 36 | Pair: KEY '=' VAL(s?) COM(?) "\n" | ||
| 37 | {$return = {$item[1]=>$item[3]} } | ||
| 38 | Pair: COMMENT(s){$return = 0} | ||
| 39 | | BLANK(s) {$return = 0} | ||
| 40 | Pair: <resync:[^[\n]+\n> | ||
| 41 | |||
| 42 | KEY: /\w+(?=\s*=)/ | ||
| 43 | |||
| 44 | VAL: <perl_quotelike> | ||
| 45 | <reject: ${item[1]}[1] ne '"' > | ||
| 46 | { $item[1]->[2] =~ s/\\\n//g } | ||
| 47 | { $item[1]->[2] } | ||
| 48 | VAL: | ||
| 49 | /.*? (?<!\\)(?=[,;#\n]) /sx | ||
| 50 | { $item[1] =~ s/\\\n//g } | ||
| 51 | { $item[1] =~ s/[;#].*\Z//m } | ||
| 52 | { $item[1] =~ s/^\s+|\s+$// } | ||
| 53 | { postprocess( $item[1] ) } | ||
| 54 | VAL: ',' VAL | ||
| 55 | {$item[2]} | ||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | BLANK: "\n" | ||
| 60 | COM: /^(?:[#;].*)/ | ||
| 61 | COMMENT: COM "\n" | ||
| 62 | |||
| 63 | END_GRAMMAR | ||
| 64 | |||
| 65 | 24 | ▶ | sub new { Parse::RecDescent->new( $gram ) } |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 66 | |||
| 67 | 10 | 1; | |
| 68 | __END__ | ||