| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::Grammar::Attribute; | ||
| 4 | |||
| 5 | sub opt_attribute { | ||
| 6 | my $str = $_[0]; | ||
| 7 | my $pos = $_[1]; | ||
| 8 | |||
| 9 | my @attributes; | ||
| 10 | my $ws = Perlito5::Grammar::Space::opt_ws( $str, $pos ); | ||
| 11 | if ($str->[ $ws->{to} ] ne ':') { | ||
| 12 | # no colon, return an empty list | ||
| 13 | return { to => $pos, capture => [] } | ||
| 14 | } | ||
| 15 | $ws = Perlito5::Grammar::Space::opt_ws( $str, $ws->{to} + 1 ); | ||
| 16 | my $p = $ws->{to}; | ||
| 17 | my $m = Perlito5::Grammar::ident($str, $p); | ||
| 18 | Perlito5::Compiler::error( "syntax error" ) if !$m; | ||
| 19 | |||
| 20 | my $to; | ||
| 21 | while (1) { | ||
| 22 | |||
| 23 | my $attr = [ Perlito5::Match::flat($m), undef ]; | ||
| 24 | $to = $m->{to}; | ||
| 25 | my $delimiter = $str->[$to]; | ||
| 26 | if ($delimiter eq '(') { | ||
| 27 | # "ident(params)" | ||
| 28 | my $params = Perlito5::Grammar::String::string_interpolation_parse($str, $m->{to} + 1, '(', ')', 0); | ||
| 29 | Perlito5::Compiler::error( "syntax error" ) if !$params; | ||
| 30 | $attr->[1] = Perlito5::Match::flat($params)->{buf}; | ||
| 31 | $to = $params->{to}; | ||
| 32 | } | ||
| 33 | push @attributes, $attr; | ||
| 34 | |||
| 35 | # check if the attribute list continues | ||
| 36 | $ws = Perlito5::Grammar::Space::opt_ws( $str, $to ); | ||
| 37 | if ($str->[$ws->{to}] eq ':') { | ||
| 38 | $ws = Perlito5::Grammar::Space::opt_ws( $str, $ws->{to} + 1 ); | ||
| 39 | } | ||
| 40 | $p = $ws->{to}; | ||
| 41 | $m = Perlito5::Grammar::ident($str, $p); | ||
| 42 | |||
| 43 | return { to => $to, capture => \@attributes } | ||
| 44 | if ! $m; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 |