Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Last active January 26, 2020 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusukebe/252b9b438430bbaed43605347fcda5a3 to your computer and use it in GitHub Desktop.
Save yusukebe/252b9b438430bbaed43605347fcda5a3 to your computer and use it in GitHub Desktop.
Rakuのgrammarを使ってREST風URLをパースする
# https://docs.perl6.org/language/grammar_tutorial このチュートリアルのコードです
grammar REST {
token TOP { <slash><subject><slash><command>[<slash><data>]? }
proto token command {*}
token command:sym<create> { <sym> }
token command:sym<retrieve> { <sym> }
token command:sym<update> { <sym> }
token command:sym<delete> { <sym> }
token subject { \w+ }
token data { .* }
token slash { \s* '/' \s* }
}
class REST-actions {
method TOP($/) {
make {
subject => $<subject>.Str,
command => $<command>.Str,
data => $<data>.made,
subject-id => $<data>.made[0]
}
}
method data($/) { make $/.split('/') }
}
my $uri = '/product/update/7/notify';
my $rest = REST.parse($uri, actions => REST-actions.new).made;
say $rest<command>; # update say $rest<subject>; # product
say $rest<subject-id>; # 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment