Last active
January 26, 2020 06:15
-
-
Save yusukebe/252b9b438430bbaed43605347fcda5a3 to your computer and use it in GitHub Desktop.
Rakuのgrammarを使ってREST風URLをパースする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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