On 14/07/2017 15:23, Lucas Buchala wrote:
Alternatively, the «...» builtin operator already does some kind of
word splitting respecting quotes, if that fits your needs:


Thanks. I take it you are referring to the "hyper" operator (?) It looks good, but it doesn't quite work:
 my $d = "hello \"there world\"";
my @f = << $d >>;
 @f[1] ; # "there


As it happens, I did manage to create a grammar that respects quoting, and even escaped quoting:

grammar Shlex {
        token TOP {<.ws>? (<word>|<qstring>)* <.ws>?}
        token ws { \s+ }
        rule word { <[a..zA..Z0..9\\-]>+ }
        token ascii_char { <-["\\]> } # anything not a " or \
        token escaped_char { "\\\"" } # literal \"
        token qstring { '"' [<escaped_char>|<ascii_char>]* '"' <.ws>?  }
}

my $m = Shlex.parse(Q[goodbye "\"cruel\" world"  ]);
say "First  component:$m[0][0].Str()"; # OUTPUT: First  component:goodbye
say "Second component:$m[0][1].Str()"; # OUTPUT: Second component:"\"cruel\" 
world"

Reply via email to