Slide 1

Slide 1 text

XPathͰιʔείʔυݕࡧ ҋPHPษڧձ (2016/12/11)

Slide 2

Slide 2 text

ࣗݾ঺հ ߴࢁԹ @edvakf ϐΫγϒגࣜձࣾ ࣥߦ໾һ CTO ISUCON6ຊબग़୊͠·ͨ͠ JavaScript, PHP, Ruby, Go, Scala ͋ͨΓ͕कඋൣғ

Slide 3

Slide 3 text

ྫ͑͹͜͏͍͏έʔε ϓϩδΣΫτͷίʔσΟϯάن໿Ͱ
 in_arrayͷୈ3Ҿ਺ʹtrueΛ౉͢͜ͱ͕ඞਢʹͳͬͨ ୈ3Ҿ਺͕trueͰͳ͍ՕॴΛ͢΂ͯݕग़͍ͨ͠

Slide 4

Slide 4 text

grep git grep in_array | grep -v true in_array(1, [1,2]); ←ݕग़Ͱ͖Δ in_array(1, [
 1,
 2,
 ], true); ←͜ͷํ๏Ͱ͸ݕग़Ͱ͖ͳ͍

Slide 5

Slide 5 text

खܰʹݕग़Ͱ͖Δ πʔϧ͸ແ͍

Slide 6

Slide 6 text

token_get_all % php -r 'var_export(token_get_all(" array ( 0 => 379, 1 => ' 1, ), 1 => array ( 0 => 319, 1 => 'in_array', 2 => 1, ), 2 => '(', 3 => array ( 0 => 317, 1 => '1', 2 => 1, ), 4 => ',', 5 => array ( 0 => 382, 1 => ' ', 2 => 1, ), 6 => '[', 7 => array ( 0 => 317, 1 => '1', 2 => 1, ), 8 => ',', 9 => array ( 0 => 382, 1 => ' ', 2 => 1, ), 10 => array ( 0 => 317, 1 => '2', 2 => 1, ), 11 => ']', 12 => ')', 13 => ';', )

Slide 7

Slide 7 text

τʔΫϯྻ͔Β ݕࡧ͢Δͷ͸ҙ֎ͱ໘౗ εςʔτϚγϯΛ࡞Δ ಛఆͷจ຺ʢεςʔτʣͰ ಛఆͷτʔΫϯΛൃݟͨ͠ Βܯࠂ จ຺ͷωετ͕͋ΓಘΔͷ Ͱɺ࣮ࡍʹ͸ӈͷྫΑΓԿ ഒ΋ෳࡶʹͳΔ

Slide 8

Slide 8 text

ast\Node::__set_state(array( 'kind' => 133, 'flags' => 0, 'lineno' => 1, 'children' => array ( 0 => ast\Node::__set_state(array( 'kind' => 515, 'flags' => 0, 'lineno' => 1, 'children' => array ( 'expr' => ast\Node::__set_state(array( 'kind' => 2048, 'flags' => 1, 'lineno' => 1, 'children' => array ( 'name' => 'in_array', ), )), 'args' => ast\Node::__set_state(array( 'kind' => 128, 'flags' => 0, 'lineno' => 1, 'children' => array ( 0 => 1, 1 => ast\Node::__set_state(array( 'kind' => 130, 'flags' => 0, 'lineno' => 1, 'children' => array ( 0 => ast\Node::__set_state(array( 'kind' => 525, 'flags' => 0, $code = "

Slide 9

Slide 9 text

ASTʹର͢Δݕࡧ τʔΫϯྻʹର͢Δݕࡧʹ ൺ΂Ε͹·ͩ؆୯͕ͩɺ࠶ ؼͳͲ͕ग़͖ͯͯ໘౗ ΍ͬͺΓπϦʔʹର͢Δݕ ࡧΫΤϦͱ͍͑͹…

Slide 10

Slide 10 text

XPath ASTΛXMLʹม׵͢Ε͹XPath͕࢖͑Δʂ XPath͸XMLʹ͓͚Δਖ਼نදݱ πϦʔʹର͢Δݕࡧͱ͍͑͹XPath ࢓༷͕͔ͬ͠Γ͍࣮ͯͯ͠૷΋ଟ͘ɺղઆจॻ΋ଟ͍ DSLΛ֮͑Δͷ͸ਏ͍ͷͰɺطʹ஌ͬͯΔDSL͕࢖͑Δͷ͕خ͍͠ ؆୯ͳΫΤϦ͸؆୯ʹॻ͚ɺͪΐͬͱෳࡶͳΫΤϦ΋ॻ͚Δ

Slide 11

Slide 11 text

php-ast-to-xml https://github.com/edvakf/php-ast-to-xml

Slide 12

Slide 12 text

php-ast-xpath ୈࡾҾ਺ͷ༩͑ΒΕ͍ͯͳ͍in_arrayΛݕࡧ $ php-ast-xpath some.php '//AST_CALL[./expr/ AST_NAME/name/scalar[@value="in_array"] and not(./args/AST_ARG_LIST/*[3])]'
 some.php:10
 some.php:12

Slide 13

Slide 13 text

σϞ

Slide 14

Slide 14 text

·ͱΊ ϊʔυ୳ࡧϓϩάϥϜΛॻ͘ͷ͸େม php-astͷग़ྗΛػցతʹXMLʹม׵Ͱ͖Δ XPathΛ࢖͑͹؆୯ͳΫΤϦͰgrepΑΓਖ਼֬ʹ
 ιʔείʔυΛݕࡧͰ͖Δ

Slide 15

Slide 15 text

͔͜͜Β͸ໝ૝

Slide 16

Slide 16 text

php-astͰASTʹͯ͠͠·͏ͱɺAST͔Βݩͷιʔε ίʔυʹ໭͢͜ͱ͕Ͱ͖ͳ͍ token_get_allͰऔಘͨ͠τʔΫϯྻͳΒ࿈݁͢Ε͹ݩ ͷιʔείʔυʹ໭ͤΔ token_get_allͷ৘ใΛอͬͨ··XMLʹม׵Ͱ͖Ε ͹ɺXSLTͰπϦʔม׵ͯ͠PHPʹ໭͢ϦϑΝΫλϦϯ άπʔϧ͕Ͱ͖ͦ͏ →LISPͷϚΫϩͩ