Slide 1

Slide 1 text

Perl5ͷ੩తղੳೖ໳ ػցͱਓؒ૒ํͷาΈدΓʹΑΔฏ࿨ฤ YAPC::Tokyo 2019 ໘ന๏ਓΧϠοΫ macopy a.k.a @mackee_w #yapcjapan #yapcjapanRoom1 1

Slide 2

Slide 2 text

͜ͷεϥΠυ 2

Slide 3

Slide 3 text

͜ͷτʔΫ ͷ಺༰ 3

Slide 4

Slide 4 text

ಘΒΕΔ஌ࣝɾମݧ (1) •ਖ਼نදݱͰPerlίʔυΛύʔε͢ΔPPR.pm ͷ࢖͍ํ • ۀ຿Ͱͷ۩ମతͳ࢖༻ྫ •ਓؒͱػցʹ෼͔Γʹ͍͘ίʔυͱ௚͠ํ ͷఏҊ 4

Slide 5

Slide 5 text

ಘΒΕΔ஌ࣝɾମݧ (2) •Perlίʔυͷؔ਺ʹܕγάωνϟΛ෇͚Δ •ؔ਺ͷܕγάωνϟΛPPRͰൈ͖ग़͢ 5

Slide 6

Slide 6 text

࿩ͷྲྀΕ 1. ੩తղੳͱ͸ 2. PPIΛ࢖͏ྫ 3. PPRͷઆ໌ͱྫ 4. ػցͱฏ࿨ͷ࿩ 5. ੩తܕ෇͚ͷೖΓޱ 6

Slide 7

Slide 7 text

ࢲͱ͸ɾɾɾʁ • ໘ന๏ਓΧϠοΫ ιʔγϟ ϧήʔϜࣄۀ෦ • ࢓ࣄݴޠ͸Perl5ͱGo • झຯ͸3DϓϦϯλ࡞Γ • աڈͷτʔΫ: ήʔϜӡ༻ͷ ࿩ͱ͔Ϛελσʔλͷ࿩ͱ͔ WebSocket 7

Slide 8

Slide 8 text

ΠϯλʔωοτͰ͸͜͏͍͏ΞΠ ίϯͰ͢ 8

Slide 9

Slide 9 text

[Ad]͜ͷτʔΫ͸ۀ຿࣌ؒʹॻ͖·ͨ͠ 9

Slide 10

Slide 10 text

੩తղੳͱ͸Ұ ମɾɾɾʁ 10

Slide 11

Slide 11 text

static analysis ੩తղੳ 11

Slide 12

Slide 12 text

ίʔυΛͨͩͷ จࣈྻͱͯ͠ѻ͏ 12

Slide 13

Slide 13 text

΍Γํ 1 ਖ਼نදݱͰҾֻ͚ͬͨΓී௨ͷςΩετͱͯ͠ѻ͏ use Path::Tiny; my $target_package = "Example"; my $script = path("$target_package.pm")->slurp; my $is_valid_package = $script =~ /\Apackage $target_package;/; 13

Slide 14

Slide 14 text

͜͏͍͏ͷ͕ग़ͯ͘Δͱ؆୯ʹ٧Ή use Example; 14

Slide 15

Slide 15 text

ΑΓ࣮֬ͳ΍Γํ͸ͳ͍ͷ͔ʂ 15

Slide 16

Slide 16 text

΍Γํ 2 perl͕΍Δ͜ͱΛ్த·Ͱ΍ͬͯɺந৅ߏจ໦Λར༻͢Δ 16

Slide 17

Slide 17 text

ίϥϜ: ࠷ۙͷݴޠͩͱݴޠຊମͷASTΛར༻Ͱ͖Δ͜ͱ͕͋Δ •Goͷ go/parser go/token go/types go/ast • GoίϯύΠϥ΋͜ΕΒΛ࢖͍ͬͯΔ • ͦ͏Ͱ͸ͳ͍Β͍͠ •Rubyͷ RubyVM::AST • 2.6.0͔Βར༻Մೳ 17

Slide 18

Slide 18 text

੩తղੳͷϝϦοτ (1) •ίʔυͷݟͨ··Λѻ͑Δ • ந৅ߏจ໦ҎલͰ͋Ε͹ίϝϯτͷதΛ ѻ͑Δ͜ͱ΋ •ݴޠػೳΛ௒͑ͨදݱྗ • ޙ͔Β੩తܕ෇͚ͱ͔ 18

Slide 19

Slide 19 text

࣮ߦग़དྷΔঢ়ଶʹͳΔʹͭΕͯιʔείʔυ্ͷ৘ใ͸ൈ͚མ͍ͪͯ͘ 19

Slide 20

Slide 20 text

࣮ߦ࣌ղੳͰ͸ར༻Ͱ͖ͳ͍৘ใ •ۭന, վߦ, ίϝϯτ • ಛघͳॻ͖ํͰར༻Ͱ͖Δ৔߹΋͋Δ (ྫ: Pythonͷdocstring) 20

Slide 21

Slide 21 text

࣮ߦ࣌ղੳͰ͸ར༻Ͱ͖ͳ͍৘ใ •τʔΫϯͷҐஔ΍લޙͷτʔΫϯ • ར༻͞Ε͍ͯΔม਺͕࣮ࡍʹ͸Ͳ͜Ͱએ ݴ͞Ε͍ͯΔ͔ͱ͔ • ؔ਺΍จͷҐஔͳͲ͸ελοΫτϨʔε Ͱར༻͢ΔͨΊʹ෇͍͍ͯΔ 21

Slide 22

Slide 22 text

੩తղੳͷϝϦοτ (2) •࣮ߦͤͣʹίʔυΛѻ͑Δ • BEGINͰϠόΠ͜ͱ͍ͯͯ͠΋େৎ෉ • ٯʹݴ͏ͱBEGINͰେࣄͳ͜ͱ͍ͯͯ͠ ΋ݕ஌͸೉͍͠(ޙड़) 22

Slide 23

Slide 23 text

੩తղੳͷϝϦοτ (3) •͢΂ͯͷ࣮ߦύεʹ͍ͭͯௐ΂্͛ΒΕΔ ৔߹͕͋Δ • ࣮ߦ࣌ʹܕόϦσʔγϣϯ͢Δ৔߹ɺ࣮ ߦ͢Δ·Ͱ੒ޭ͢Δ͔Ͳ͏͔෼͔Βͳ͍ 23

Slide 24

Slide 24 text

੩తղੳͷσϝϦοτ •PerlͷύʔεΛࣗલ(Ϟδϡʔϧ࢖͏͕)Ͱ΍ Βͳ͍ͱ͍͚ͳ͍͔Βେม •ͦ΋ͦ΋໦ߏ଄ΛಡΜͰ͍ͬͯͲ͏ʹ͔͢ Δͷେม •perlͷؾ࣋ͪʹͳΒͳ͍ͱ͍͚ͳͯ͘େม 24

Slide 25

Slide 25 text

ࢲ͕੩తղੳ͍ͨ͠ಈػ ػցͷྗΛआΓͯ ؒҧ͍ͷͳ͍ίʔυΛ ॻ͖͍ͨʂ 25

Slide 26

Slide 26 text

Perl5Ͱ੩తղੳ ͢ΔͨΊͷπʔϧ 26

Slide 27

Slide 27 text

Perl5੩తղੳքͷୈҰਓऀ PPI 27

Slide 28

Slide 28 text

PPI Parse, Analyze and Manipulate Perl (without perl) •Pure Perl͚ͩͰPerlίʔυΛύʔεͯ͠੩ తղੳͰར༻Ͱ͖Δܗʹ͢ΔϞδϡʔϧ 28

Slide 29

Slide 29 text

࢖༻ྫ (1) use PPI; use PPI::Dumper; my $document = PPI::Document->new(\'my $v1 = $v2;'); PPI::Dumper->new($document)->print; 29

Slide 30

Slide 30 text

࢖༻ྫ (2) PPI::Document PPI::Statement::Variable PPI::Token::Word 'my' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$v1' PPI::Token::Whitespace ' ' PPI::Token::Operator '=' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$v2' PPI::Token::Structure ';' 30

Slide 31

Slide 31 text

࢖༻ྫ (3) my ($stmt) = $document->children; my $op = $stmt->find_first("PPI::Token::Operator"); $op->content; # => "=" 31

Slide 32

Slide 32 text

PPIͷಛ௃ (1) •PDOM(ύʔεޙͷ໦ߏ଄ͷ͜ͱ)͔Βίʔυ ʹॻ͖໭͢͜ͱ͕ग़དྷΔ $document->prune("PPI::Token::Whitespace"); $document->serialize; # => "my$v1=$v2;" 32

Slide 33

Slide 33 text

PPIͷಛ௃ (2) •ॻ͖໭ͨ͢ΊʹτʔΫϯ΍εϖʔεɺ;ͳͲ ͸อ࣋ͨ͠·· •͋͘·Ͱղੳ͢ΔͨΊͷߏ଄Ͱ͋ͬͯɺ࣮ ߦ͢ΔͨΊͷߏ଄Ͱ͸ͳ͍ 33

Slide 34

Slide 34 text

PPIͷσϝϦοτ •݁ߏ஗͍ • 1175ߦͷ࣮ࡍͷۀ຿ͷίʔυΛύʔε͠ ͨ݁Ռ • 6.80/s1 1 Macbook Pro 15inch mid 2015 Ͱܭଌ 34

Slide 35

Slide 35 text

ͦͷଞͷϞδϡʔϧ •Compiler::Lexer ಠࣗͷCݴޠͰॻ͔Εͨ τʔΧφΠβΛ࢖༻ •Perl::Lexer perlຊମͷ಺෦APIΛୟ͍ͯτʔ ΫϯྻΛऔΓग़͢ 35

Slide 36

Slide 36 text

PPRͷ঺հͱ࢖ ͍ํ 36

Slide 37

Slide 37 text

PPR Pattern-based Perl Recognizer •PerlίʔυͷύλʔϯΛఆٛͯ͠ݕ஌Ͱ͖ Δਖ਼نදݱΛ࡞ΕΔϞδϡʔϧ 37

Slide 38

Slide 38 text

࢖༻ྫ # my $hoge = "fuga"; ͷม਺໊ΛҾֻ͚ͬΔʹ͸ my $matcher = qr{ my (?&PerlOWS) ((?&PerlVariableScalar)) (?&PerlOWS) (?&PerlAssignmentOperator) (?&PerlOWS) (?&PerlString) (?&PerlOWS) ; $PPR::GRAMMAR }x 38

Slide 39

Slide 39 text

࢖༻ྫ my ($var) = grep { defined } $script =~ $matcher; ͜ΕͰ $var ʹ "$hoge" ͕ೖΔ 39

Slide 40

Slide 40 text

ศརʂʂʂ 40

Slide 41

Slide 41 text

ʮΘʔʂ͍͖ͳΓ ϠόΠਖ਼نදݱ͕ ग़͖ͯͨͧʂʂʯ 41

Slide 42

Slide 42 text

଴͍ͬͯͩ͘͞ ղઆ͠·͢ 42

Slide 43

Slide 43 text

PPR͸PerlίʔυͷτʔΫϯʹϚον͢Δਖ਼نදݱͷύλʔϯू Perlίʔυྫ PPRͷύλʔϯ my (?&PerlBuiltinFunction) $var (?&PerlVariableScalar) = (?&PerlAssignmentOperator) "hogehoge" (?&PerlString) 43

Slide 44

Slide 44 text

සग़ύλʔϯ •(?&PerlOWS) • θϩจࣈҎ্ͷPerl্ͷۭനจࣈ • ۭന΍վߦΛؚΉ 44

Slide 45

Slide 45 text

࣮༻ྫ 45

Slide 46

Slide 46 text

ฐࣾͩͱ͜͏͍͏όϦσʔγϣϯΛΑ͘΍͍ͬͯΔ͚ΕͲ sub do_something { my $self = shift; state $rule = Data::Validator->new( a1 => "Str", b1 => "UInt", ); my $args = $rule->validate(@_); my ($a1, $b1) = $args->{qw/a1 b1/}; # do something... } 46

Slide 47

Slide 47 text

ϓϥΠϕʔτϝιου ͡Όͳ͔ͬͨΒ ͪΌΜͱόϦσʔγϣϯ ͯ͠ΔΑͶνΣοΫ 47

Slide 48

Slide 48 text

εΫϦϓτ͔Βؔ਺ఆٛΛൈ͖ग़͢ my $script = ...; my $sub_matcher = qr{ ((?&PerlSubroutineDeclaration)) $PPR::GRAMMAR }x; my @decls = grep { defined } $script =~ m{$sub_matcher}gx; 48

Slide 49

Slide 49 text

ؔ਺ఆ͔ٛΒؔ਺໊ͱϒϩοΫΛൈ͖ग़͢ my $subname_matcher = qr{ \Asub (?&PerlOWS) ((?&PerlQualifiedIdentifier)) (?&PerlOWS) ((?&PerlBlock)) $PPR::GRAMMAR }x; for my $decl (@decls) { my ($subname, $block) = grep { defined } $decl =~ $subname_matcher; 49

Slide 50

Slide 50 text

ؔ਺໊͕ΞϯμʔείΞ࢝·ΓͳΒεΩοϓ͢Δ for my $decl (@decls) { my ($subname, $block) = grep { defined } $decl =~ $subname_matcher; next if $subname =~ /\A_/; 50

Slide 51

Slide 51 text

validatorͷఆٛ my $validator_matcher = qr{ ( (state) (?&PerlOWS) (?&PerlVariableScalar) (?&PerlOWS) (?&PerlAssignmentOperator) (?&PerlOWS) Data::Validator->new (?&PerlOWS) (?&PerlParenthesesList) (?&PerlOWS) ) $PPR::GRAMMAR }x; 51

Slide 52

Slide 52 text

validatorͷఆ͕ٛ͋Δ͔Λௐ΂Δ for my $decl (@decls) { ... next if $subname =~ /\A_/; my ($validator) = grep { defined } $block =~ $validator_matcher; 52

Slide 53

Slide 53 text

͋ͱ͸ࣽΔͳΓম͘ͳΓ # warningΛग़͢ͳΓ if (!defined $validator) { warn "$subname Ͱ Data::Validator ͕࢖ΘΕͯͳ͍Αʂ"; } # ςετͰ͚ͤ͜͞ΔͳΓ use Test::More; fail "$subname Ͱ Data::Validator ͕࢖ΘΕͯͳ͍Αʂ"; 53

Slide 54

Slide 54 text

͜ͷνΣοΫͰൈ͚͍ͯΔͱ͜Ζ •Ҿ਺ΛऔΒͳ͍ؔ਺Λޡݕ஌ •Attribute΍Subroutine signature •Ҿ਺όϦσʔγϣϯҎ֎ͷvalidator 54

Slide 55

Slide 55 text

׬ᘳΛ໨ࢦͣ͞ʹࣗ෼ͷ࣋ͭίʔυ͚ͩͰ௨Ε͹ྑ͍ ͋Δఔ౓͸αϘΔ 55

Slide 56

Slide 56 text

͞ΒʹԠ༻Ҋ •Smart::Args Λ࢖͏Α͏ʹஔ׵͢Δ • όϦσʔλͷதͷݸʑͷఆٛ·Ͱόϥ͠ ͯൈ͖ग़ͤΔ •LSPͳͲͰҾ਺ϦετΛग़͢ 56

Slide 57

Slide 57 text

PPR͸ਖ਼نදݱͰίʔυ ͷҰ෦Λൈ͖ग़ͯ͠ ݕࠪ΍ஔ׵ʹ࢖͑Δ 57

Slide 58

Slide 58 text

ͦ͏͍͏໨తͷطଘͷϞδϡʔϧ •Perl::Critic • PPIΛ࢖ͬͨνΣοΧʔ •Perl::Lint • Compiler::LexerΛ࢖ͬͨνΣοΧʔ 58

Slide 59

Slide 59 text

੩తղੳͰ ίʔυͷ࣏҆Λ Α͍ͯ͘͘͠ʹ͸ʁ 59

Slide 60

Slide 60 text

ػցͱਓؒʹྑ͍ ίʔυͱ ཆ੒Ϊϓεͱͯ͠ ͷ੩తղੳ 60

Slide 61

Slide 61 text

perl͸ೲಘ͕Ͱ͖Δڍಈ΋͢Δ͠ ڻ͖MAXͷڍಈΛى͜͢͜ͱ΋͋Δ 61

Slide 62

Slide 62 text

Only perl can parse Perl (1) •Only perl can parse Perl ͳ෦෼ •PPIͷυΩϡϝϯτΑΓ @result = (dothis $foo, $bar); # Which of the following is it equivalent to? @result = (dothis($foo), $bar); @result = dothis($foo, $bar); 62

Slide 63

Slide 63 text

Only perl can parse Perl (2) $ perl -MO=Deparse -e '@result = (dothis $foo, $bar);' @result = ($foo->dothis, $bar); -e syntax OK $ perl -MO=Deparse -e 'sub dothis {} @result = (dothis $foo, $bar);' sub dothis { } @result = dothis($foo, $bar); -e syntax OK 63

Slide 64

Slide 64 text

΍Γ͍ͨํʹ໌ࣔ͢Δʂʂʂ @result = dothis($foo, $bar); •͍ΖΜͳॻ͖ํ͕Ͱ͖Δݴޠ͔ͩΒͦ͜ɺ ΍Γ͍ͨ͜ͱΛओு͢Δ 64

Slide 65

Slide 65 text

Only perl can parse Perl (3) sub hoge { "hoge" } sub f { { hoge() => "fuga" } } # hashref or list ? my @result = f(); 65

Slide 66

Slide 66 text

Only perl can parse Perl (4) use DDP; p @result; # [ # [0] "hoge", # [1] "fuga" # ] ϦετʹͳΔͷ͕ਖ਼ղ 66

Slide 67

Slide 67 text

Only perl can parse Perl (5) hashrefʹ͢Δʹ͸ʁ •return Λ෇͚Δ •ϑΝοτΧϯϚͷࠨล஋Λจࣈྻʹ͢Δ •+{} ʹ͢Δ 67

Slide 68

Slide 68 text

ͭ·ΓͪΌΜͱreturnॻ͚͹ղܾ͢Δ sub hoge { "hoge" } sub f { return { hoge() => "fuga" }; } # hashref desu!!! my @result = f(); 68

Slide 69

Slide 69 text

Only perl can parse Perl (6) •1ͭͷه߸͕ෳ਺ͷҙຯʹ࢖ΘΕ͍ͯͯ೉ ͍͠ • είʔϓͱϋογϡϦϑΝϨϯεͷ {} • আࢉͱਖ਼نදݱͷ / 69

Slide 70

Slide 70 text

ʮ͜Ε͔ͩΒPerl͸ʂʯ 70

Slide 71

Slide 71 text

! ͚ΕͲ ίʔυͷ໎͍͸ࢥߟͷ໎͍ Ұݺٵ͓͍ͯɺ΋͏গ͠ߟ͑Α͏ 71

Slide 72

Slide 72 text

άϨούϏϦςΟ greppability 72

Slide 73

Slide 73 text

greppability •ૂͬͨίʔυΛgrepͰݕ஌Ͱ͖Δ͔Ͳ͏͔ • ΄͔ͷఆ͕ٛ͋ͬͨΒڭ͍͑ͯͩ͘͞ $ hw 'Greeter->say_hello' lib | \ xargs perl -pie 's/Greeter->say_hello/Greeter->send_hello' 73

Slide 74

Slide 74 text

greppability͕௿͍ίʔυ •ϝιου໊Λಈతʹ૊Έཱ͍ͯͯΔ my @params = qr/str vit dex int/; my $total = sum( map { my $method = $_ . "_factor"; $person->$method; } @params ); 74

Slide 75

Slide 75 text

ϝιου໊Λಈతʹ૊Έཱͯͨͱ͖ͷฐ֐ •ʮ͜ͷϝιουͲ͜Ͱ࢖͍ͬͯΔʁʯ͕೉ ͘͠ͳΔ •ϝιουݺͼग़͠Λ੩తղੳͰநग़͢Δͱ ͖ʹ൑ఆͰ͖ͳ͘ͳΔ ͱʹ͔͘ػցʹ༏͘͠ͳ͍ʂʂʂ 75

Slide 76

Slide 76 text

ͤΊͯจࣈྻ࿈݁Λ΍ΊΔ my @methods = qr/ str_factor vit_factor dex_factor int_factor /; my $total = sum(map { $person->$_ } @methods); •ϝιου໊͕ϢχʔΫͰ͋Ε͹grepʹҾͬ ͔͔ͬͯ͘Δ 76

Slide 77

Slide 77 text

ಈతܕ෇͚ݴޠͰ΋৺ʹ੩తܕ෇͚ίϯύΠϥΛ࣋ͭ •ίʔυΛॻ͍ͯܕ͕੩తʹܾఆ͢Δ͔Λߟ ͑Δ •Πϛϡʔλϒϧม਺ͳͲϞμϯݴޠʹ͋Δ ػೳ΍੍ݶΛಋೖ͢Δͱྑ͍ 77

Slide 78

Slide 78 text

ͦͷଞ੩తղੳͰࠔΔ΋ͷ •࣮ߦ࣌ʹuse͢Δ΋ͷ͕ܾ·Δͱ͔ BEGIN ಈ͔͞ͳ͍ͱ֬ఆ͠ͳ͍ͱ͔ •Mo+([uo]se)? ͷ has ΍ method ͸ͦΕΛղऍ ͢Δ΋ͷΛ࡞Ε͹·ͩܕͷ੩తղܾ͕Ͱ͖ Δ৔߹͕͋Δ 78

Slide 79

Slide 79 text

ػցʹ༏͚͠Ε͹ಡΉਓؒʹ΋͍͍ͩͨ༏͍͠ •ॻ͘ਓؒʹ͸ݫ͘͠ͳΔ͜ͱ͕͋Δ • ॻ͘ͷ͸Ұճ͔ͭҰਓɺಡΉͷ͸n(n>=1) ճ͔ͭmਓ •ػցʹ༏͚͠Ε͹ػցͷࢧԉΛड͚Δ͜ͱ ͕ग़དྷΔ 79

Slide 80

Slide 80 text

ػցʹ΋ਓؒʹ΋༏͍͠Perlίʔυ͸PerlͷαϒηοτʹͳΔ => ੩తղੳͰྑ͘ͳ͍ίʔυΛݕ஌Ͱ͖Δ 80

Slide 81

Slide 81 text

ਓؒʹ͸ཉ΍ࡶ೦͕͋ΔͨΊɺࣗݾΛ཯͢Δͷ͸೉͍͠ •ʮλΠϓ਺ଟ͘ͳΔ͔ΒखΛൈ͜͏ʯͬͯ ͳΔ͜ͱ͕͋Δ •ػցʹ஫ҙͯ͠΋Β͏ • returnෆ଍ݕ஌ͷϙϦγʔ͸ Perl::Critic ʹ͋Δ 81

Slide 82

Slide 82 text

ϋ΢εϧʔϧ •Rubyͷࣄྫ Querly •ʮ͜͏͍͏ॻ͖ํ͏ͪͩͱڻ͖͕͍ͬͺ͍ ͔ͩΒνΣοΫ͍ͨ͠ΑͶʯ •PerlͰ΋PPRͰݕ஌Մೳ 82

Slide 83

Slide 83 text

ͦͷଞϋ΢εϧʔϧͷྫ •Plack::RequestͰ $c->req->param(...)͸࢖Θ ͳ͍ •DBIx::Class Ͱ ResultSet->search Λ arrayί ϯςΩετͰड͚ͳ͍ ͳͲͳͲ 83

Slide 84

Slide 84 text

Ұ୴࣭໰λΠϜ 84

Slide 85

Slide 85 text

͔͜͜Β͸͍࣌ؒͬͺ͍·Ͱ ૸Γൈ͚·͢ 85

Slide 86

Slide 86 text

PPRΛ࢖͍ͬͯͨ͘Ίʹ PPRΛ΋ͬͱਂ͘஌Ζ ͏ʂ 86

Slide 87

Slide 87 text

PPRͷυΩϡϝϯτʹ͸࢖͑Δਖ਼نදݱͷϧʔϧ͕ྻڍ͞Ε͍ͯΔ͕... 87

Slide 88

Slide 88 text

υΩϡϝϯτಡΜͰ΋ ۩ମతʹԿʹϚον ͢Δ͔Θ͔ΒΜͱ͜ΖΛ ίʔυͰಡΜͰΈΔ 88

Slide 89

Slide 89 text

$PPR::GRAMMAR ΛݟͯΈΔ my $stmt_matcher = qr{ \G (?&PerlOWS) ((?&PerlStatement)) $PPR::GRAMMAR # <= ίϨ }x; my @stmts = grep { defined } $script =~ m{$stmt_matcher}gcx; 89

Slide 90

Slide 90 text

90

Slide 91

Slide 91 text

! 1761ߦ ໨ͱ೴ʹ௚઀μϝʔδ͕དྷΔߦͷਖ਼نදݱ 91

Slide 92

Slide 92 text

ਖ਼نදݱ͓͹͚ ! ʹཱͪ޲͔͏ 92

Slide 93

Slide 93 text

ಡΈํ (1) •(? ... ) ϧʔϧఆٛ •(?&PerlStatementSequence) ϧʔϧݺͼग़͠ 93

Slide 94

Slide 94 text

ಡΈํ (2) (? \x{FEFF}?+ # Optional BOM marker (?&PerlStatementSequence) ) # End of rule Ͷɺ؆୯Ͱ͠ΐʁ 94

Slide 95

Slide 95 text

ಡΈํ (3) ͱ͖Ͳ͖͜͏͍͏পʹग़͘Θ͚͢ΕͲ (? # Optimized to match any Perl builtin name, without backtracking... (?=[^\W\d]) # Skip if possible (?> s(?>e(?>t(?>(?>(?>(?>hos|ne)t|gr)en|s(?>erven|ockop))t|p(?>r(?>iority|otoent)|went|grp)) | g(?>et(?>p(?>r(?>oto(?>byn(?>umber|ame)|ent)|iority)|w(?>ent|nam|uid)|eername|grp|pid)| | r(?>e(?>ad(?>lin[ek]|pipe|dir)?|(?>quir|vers|nam)e|winddir|turn|set|cv|do|f)|index|mdir | c(?>h(?>o(?>m?p|wn)|r(?>oot)?|dir|mod)|o(?>n(?>tinue|nect)|s)|lose(?>dir)?|aller|rypt) | e(?>nd(?>(?>hos|ne)t|p(?>roto|w)|serv|gr)ent|x(?>i(?>sts|t)|ec|p)|ach|val(?>bytes)?+|of | l(?>o(?>c(?>al(?>time)?|k)|g)|i(?>sten|nk)|(?>sta|as)t|c(?>first)?|ength) | u(?>n(?>(?>lin|pac)k|shift|def|tie)|c(?>first)?|mask|time) | p(?>r(?>ototype|intf?)|ack(?>age)?|o[ps]|ipe|ush) | d(?>bm(?>close|open)|e(?>fined|lete)|ump|ie|o) | f(?>or(?>m(?>line|at)|k)|ileno|cntl|c|lock) | t(?>i(?>mes?|ed?)|ell(?>dir)?|runcate) 95

Slide 96

Slide 96 text

Regexp::OptimizerͰੜ੒͞ΕͯΔΈ͍͔ͨͩ Βେৎ෉ʂ 96

Slide 97

Slide 97 text

͍͍ײ͡ͷࢹ֮ԽΛ͢Δ •ఆ͕ٛผͷͲΕΛࢀর͍ͯ͠Δ͔ • (?&PerlVariable) ͸ • (?&PerlScalarAccess) ͱ • (?&PerlHashAccess) ͱ • (?&PerlArrayAccess) ͷ OR ͩΑ Έ͍ͨͳ 97

Slide 98

Slide 98 text

͍͍ײ͡ͷՄࢹԽ (1) •PPRͷਖ਼نදݱͷจࣈྻΛൈ͖ग़͢ • ਖ਼نදݱϦϑΝϨϯεͩͱ͍Ζ͍Ζͬ͘ ͍ͭͯ͘ΔͷͰίʔυ্ͷςΩετͦͷ ··͕΄͍͠ • PPRͰൈ͖ग़͢ 98

Slide 99

Slide 99 text

our $GRAMMAR = qr{ ... }; # ίί ^^^^^^^^^ 99

Slide 100

Slide 100 text

͍͍ײ͡ͷՄࢹԽ (2) my $stmt_regexp = qr{ our (?&PerlOWS) \$GRAMMAR (?&PerlOWS) = (?&PerlOWS) ((?&PerlRegex)) $PPR::GRAMMAR }x; my ($regexp) = grep { defined } $ppr_script =~ /$stmt_regexp/gcx; # => "qr{ ... }" 100

Slide 101

Slide 101 text

͍͍ײ͡ͷՄࢹԽ (3) •PPIx::Regexp Ͱ ਖ਼نදݱͷந৅ߏจ໦ʹ͢ Δ use PPIx::Regexp; my $tree = PPIx::Regexp->new($regexp); 101

Slide 102

Slide 102 text

͍͍ײ͡ͷՄࢹԽ (4) •͋ͱ͸࠶ؼͰτʔΫϯΛऩू͍ͯ͘͠ sub traverse { my ($tree, $bucket, $name) = @_; my $literal = ""; for my $child ($tree->children) { if (ref $child eq "PPIx::Regexp::Token::Literal") { $literal .= $child->content; } elsif (ref $child eq "PPIx::Regexp::Token::Whitespace") { # skip } else { if (length($literal) > 1 && $literal ne '\n') { $bucket->{$name}->{'"' . $literal . '"'} = 1; } $literal = ""; } 102

Slide 103

Slide 103 text

͍͍ײ͡ͷՄࢹԽ (5) if (ref $child eq "PPIx::Regexp::Token::Recursion") { next if $child->name eq "PerlOWS"; $bucket->{$name}->{$child->content} = 1; } if ($child->can("children")) { my $next_name = $name; if (ref $child eq "PPIx::Regexp::Structure::NamedCapture") { next if $child->name =~ /^PPR_/; $next_name = $name ? $name . " > (?&" . $child->name . ")" : "(?&" . $child->name . ")"; #PPIx::Regexp::Dumper->new($child)->print; } traverse($child, $bucket, $next_name); } } } 103

Slide 104

Slide 104 text

͍͍ײ͡ͷՄࢹԽ (6) (?&PerlPackageDeclaration) |-- "\}" |-- "package" |-- (?&PerlBlock) |-- (?&PerlNWS) |-- (?&PerlQualifiedIdentifier) `-- (?&PerlVersionNumber) (?&PerlParenthesesList) |-- "\(" `-- (?&PerlExpression) (?&PerlPod) `-- "=cut" 104

Slide 105

Slide 105 text

ࣗ༝ࣗࡏʹτʔΫϯΛൈ͖ग़ͤΔʂ PPRͷத਎΋Θ͔ͬͨʂ 105

Slide 106

Slide 106 text

͜͜·ͰདྷΔͱܕΛॻ͍ͯ ࢖͍ͨ͘ͳΓ·ͤΜ͔ʁ 106

Slide 107

Slide 107 text

઴ਐత੩తܕ෇͚ •ಈతܕ෇͚ݴޠʹ෦෼తʹ੩తܕ෇͚Λಋ ೖ͢Δ • TypeScript(JavaScript) ΍ Hack(PHP) ͳ Ͳ 107

Slide 108

Slide 108 text

•ޙ෇͢Δʹ͸ҎԼͷཁૉ͕ඞཁ • ܕ͕ॻ͚ΔΑ͏ʹͳΔ (PythonͷPEP 484 Type Hints) • ੩తղੳͰܕΛௐ΂Δ͜ͱ͕ग़དྷΔ • ܕγεςϜΛ࣮૷͢Δ 108

Slide 109

Slide 109 text

ܕ͸͢Ͱʹॻ͚Δ package Greeter { use Function::Parameters; use Function::Return; use Types::Standard qw/Str Int Bool/; method say_hello(Str :$name, Int :$times) :Return(Bool) { say $self . ": Hello " . $name for 1..$times; return 1; } } Greeter->say_hello(name => "shinpei0213", times => 10); 109

Slide 110

Slide 110 text

͜ͷؔ਺ͷ γάωνϟΛ PPRͰղੳ͢Δ 110

Slide 111

Slide 111 text

ίʔυͱͯ͠ಡΜͰ͍Ε͹͜͏ग़དྷΔ͚ΕͲ my $pinfo = Function::Parameters::info \&Greeter::say_hello; my $rinfo = Function::Return::info \&Greeter::say_hello; 111

Slide 112

Slide 112 text

ޙʑͷ͜ͱΛߟ͑Δͱ੩తղੳͰऔΓ͍ͨ 112

Slide 113

Slide 113 text

΍ͬͯΈΑ͏ͱ͢Δ͕ͦ΋ͦ΋Perlͷίʔυͱͯ͠͏·͘ ೝࣝͯ͘͠Εͳ͍ my $doc_matcher = qr{ \A (?&PerlDocument) \Z $PPR::GRAMMAR }x; say !!($script =~ $doc_matcher); # => ""(false) 113

Slide 114

Slide 114 text

໰୊͸ keyword plugin method say_hello(Str :$name, Int :$times) :Return(Bool) { say $self . ": Hello " . $name for 1..$times; return 1; } keyword plugin ͸ perl 5.12.0 ͔Β࢖͑Δจ ๏֦ுػೳ 114

Slide 115

Slide 115 text

PPR͸ͦ͏͍͏ͷ΋ߟྀग़དྷΔ࡞Γʹͳ͍ͬͯΔ •ͦ΋ͦ΋keyword pluginΛ؆୯ʹॻ͚Δ Keyword::Declare ͷͨΊʹ࡞ΒΕͨϞ δϡʔϧ •keywordͷϧʔϧΛ֦ுͰ͖Δ 115

Slide 116

Slide 116 text

Function::ParametersʹରԠ͢Δ (1) my $FP_GRAMMAR = qr{ (?(DEFINE) (? method (?&PerlOWS) (?&PerlIdentifier) (?&PerlOWS) # => say_hello (?&kw_balanced_parens) (?&PerlOWS) # => (Str :$name, Int :$times) (?: (?&PerlAttributes) (?&PerlOWS) # => :Return(Bool) )?+ (?&PerlBlock) # => { ... } ) (? (?&PerlFunctionParametersMethod) ) (? \( (?: [^()]++ | (?&kw_balanced_parens) )*+ \) ) ) $PPR::GRAMMAR }x; 116

Slide 117

Slide 117 text

Function::ParametersʹରԠ͢Δ (2) qr{ (? method (?&PerlOWS) (?&PerlIdentifier) (?&PerlOWS) # => say_hello (?&kw_balanced_parens) (?&PerlOWS) # => (Str :$name, Int :$times) (?: (?&PerlAttributes) (?&PerlOWS) # => :Return(Bool) )?+ (?&PerlBlock) # => { ... } ) } 117

Slide 118

Slide 118 text

Function::ParametersʹରԠ͢Δ (3) qr{ (? # <= keyword֦ுͱͯ͠ొ࿥ (?&PerlFunctionParametersMethod) ) (? \( (?: [^()]++ | (?&kw_balanced_parens) )*+ \) ) } 118

Slide 119

Slide 119 text

Function::ParametersʹରԠ͢Δ (4) my $doc_matcher = qr{ \A (?&PerlDocument) \Z $FP_GRAMMAR }x; say !!($script =~ $doc_matcher); # => 1 Perlίʔυͱͯ͠ೝࣝͯ͘͠Εͨʂ ͜ΕͰউ ͭΔʂ 119

Slide 120

Slide 120 text

DEMO 120

Slide 121

Slide 121 text

ܕ͕ݟΕͨ͋ͱͷ࢖͍Έͪ •ܕγεςϜΛ࣮૷ͯ͠઴ਐత੩తܕ෇͚ •Language Server Protocolʹར༻͢Δ • ΤσΟλ/IDEͰิ׬΍ϝιου৘ใ͕ར ༻Ͱ͖ΔΑ͏ʹͳΔ 121

Slide 122

Slide 122 text

PPI౳ͱPPRͷ࢖͍෼͚ •ίʔυͷ͢΂ͯΛར༻͢ΔͳΒPPI •ίʔυͷҰ෦෼Λൈ͖ग़ͨ͠Γ༗Δແ͠Λ ݟΔͳΒPPR • ਖ਼نදݱͳͷͰϦϑΝΫλϦϯάʹ΋ར ༻Մೳ 122

Slide 123

Slide 123 text

·ͱΊ •Perl5͸े෼ʹ੩తղੳ͕Ͱ͖Δ͜ͱΛࣔ͠ ·ͨ͠ •۩ମతͳ΍Γํ΍࢖͍ಓʹ͍ͭͯ࿩͠·͠ ͨ •ػցͱਓؒͷڞଘʹ͍ͭͯ࿩͠·ͨ͠ 123

Slide 124

Slide 124 text

Any Questions? 124

Slide 125

Slide 125 text

Q: ࣏҆Λྑ͘͢ΔͨΊͷίʔυ͕࣏͕҆ѱ͍ •A1: ਖ਼نදݱ͸ίʔυͷ࣏͕҆ѱ͘ͳΔҰ ͭͷཁૉͰ͕͢ɺίϝϯτ΍ϧʔϧΛ׆༻ ͠·͠ΐ͏ 125

Slide 126

Slide 126 text

Q: ࣏҆Λྑ͘͢ΔͨΊͷίʔυ͕࣏͕҆ѱ͍ my $matcher = qr{ # ͜͜ʹίϝϯτ͕ॻ͚·͢ (?(DEFINE) (? # ͜͜ʹ޷͖ͳύλʔϯΛ࡞ͬͯύλʔϯʹ໊લΛ͚ͭΑ͏! ) ) }x; 126

Slide 127

Slide 127 text

Q: ͳΜͰͦ͜·Ͱͯ͠PerlΛ࢖͏ΜͰ͔͢ (1) •A1: 10ສߦҎ্͋ΔPerlίʔυ͕͋Γɺࠓ ͪΌΜͱಈ͍͍ͯͯɺͪΌΜͱอक΋͞Ε ͍ͯΔ৔߹ʹଞͷݴޠʹஔ͖׵͑Δ͔Ͳ͏ ͔ • ଞͷݴޠͷ઴ਐతܕ෇͚ͷ࣮૷͢Δಈػ 127

Slide 128

Slide 128 text

Q: ͳΜͰͦ͜·Ͱͯ͠PerlΛ࢖͏ΜͰ͔͢ (2) •A2: Perl5͸͔ͳΓջͷਂ͍ݴޠ • hackable • จ๏͕͔ͳΓࣗ༝ͰɺϞμϯͬΆ͍ॻ͖ ํ͕Ͱ͖Δ • keyword plugin 128