Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goで軽量マークアップ言語のパーサーを書く / Writing a parser with Go

aereal
April 29, 2017

Goで軽量マークアップ言語のパーサーを書く / Writing a parser with Go

そうだGo、京都 (https://go-kyoto.connpass.com/event/55599/) で発表した資料です。

aereal

April 29, 2017
Tweet

More Decks by aereal

Other Decks in Programming

Transcript

  1. GoͰܰྔϚʔΫΞοϓݴޠ
    ͷύʔαʔΛॻ͘
    id:aereal

    View full-size slide

  2. ܰྔϚʔΫΞοϓݴޠͱ͸
    • ҎԼ LML = Lightweight Markup Language
    • HTML΍XMLͱϓϨʔϯςΩετͷதؒ
    • Markdown, Textile, ͸ͯͳه๏, etc.

    View full-size slide

  3. ܰྔϚʔΫΞοϓݴޠͱ͸
    • ҎԼ LML = Lightweight Markup Language
    • HTML΍XMLͱϓϨʔϯςΩετͷதؒ
    • Markdown, Textile, ͸ͯͳه๏, etc.

    View full-size slide

  4. GoͰ͸ͯͳه๏ͷ
    ύʔαʔΛॻ͘
    id:aereal

    View full-size slide

  5. ࣗݾ঺հ
    • id:aereal
    • Twitter: aereal
    • GitHub: aereal
    • גࣜձࣾ͸ͯͳ

    ΞϓϦέʔγϣϯΤϯδχΞ

    View full-size slide

  6. !͓͜ͱΘΓ!
    • ࠓճ঺հ͢Δ͸ͯͳه๏ύʔαʔ͸

    ϓϥΠϕʔτϫʔΫͰ͢
    • ͸ͯͳͷαʔϏεʹ࠾༻͞ΕΔ͔ͳͲ͸

    ͢΂ͯະఆͰ͢

    View full-size slide

  7. ࢀߟ৘ใ
    • ͜ͷࢿྉͰ঺հ͢Δ৘ใ͸͢΂ͯ 2017gokyoto ͱ
    ͍͏λάΛ͚ͭͯϒοΫϚʔΫ͍ͯ͠·͢
    • http://b.hatena.ne.jp/aereal/2017gokyoto/

    View full-size slide

  8. ͸ͯͳه๏ͱ͸
    • ͸ͯͳ͕ఏڙ͢ΔαʔϏεͷҰ෦Ͱ࢖͑ΔLML
    • ͸ͯͳϒϩάɺ͸ͯͳμΠΞϦʔ
    • org-modeͱͪΐͬͱࣅ͍ͯΔ

    View full-size slide

  9. ͸ͯͳه๏ͷྫ
    * ݟग़͠1
    ** ݟग़͠2
    - Ruby
    - Perl
    - Go
    + ى
    + ঝ
    + స
    + ݁

    View full-size slide

  10. ͳͥύʔαʔΛॻ͘?
    • طʹಈ͍͍ͯΔ΋ͷʹखΛՃ͑ΔͷͰ͸μϝ?
    • ͳͥ͸ͯͳه๏?
    • ͳͥGoͰ?

    View full-size slide

  11. Ϟνϕʔγϣϯ
    • طଘͷ࣮૷ʹखΛՃ͑Δͷ͸Ή͔͍ͣ͠
    • ͸ͯͳه๏Λύʔε͍ͨ͠χʔζ͸͋Δ!
    • Go͕ॻ͖͔ͨͬͨ

    View full-size slide

  12. طଘͷ࣮૷ʹखΛՃ͑Δͷ͸
    Ή͔͍ͣ͠
    • ܗࣜతͳఆ͕ٛͳ͍ = ಈ͘ίʔυ͕࢓༷
    • ਖ਼نදݱ

    View full-size slide

  13. ͸ͯͳه๏Λύʔε͍ͨ͠
    χʔζ͸͋Δ!
    • ͸ͯͳάϧʔϓͰීஈ͔Βॻ͍͍ͯΔ
    • ͸ͯͳϒϩάͰ೔هΛॻ͘ͷ͕झຯ
    • →͸ͯͳه๏ʹ೔ৗతʹ৮Ε͍ͯΔ
    • Markdown΁ͷม׵ (...)

    View full-size slide

  14. Go͕ॻ͖͔ͨͬͨ
    • ීஈ͸Perl΍JavaScriptɺͨ·ʹScala
    • ۀ຿ͱҟͳΔίϯςΩετͰίʔυΛॻ͖͔ͨͬͨ

    View full-size slide

  15. ͪΐ͏Ͳ͍͍໨ඪ
    • Ͱ͖Δ͜ͱɺ΍Γ͍ͨ͜ͱɺؔ৺ͷ͋Δ͜ͱ͕

    ͍͍͔Μ͡ʹόϥϯε͞Ε͍ͯΔ
    • WebͱςΩετϓϩηογϯά
    • খ໨ඪΛੵΈॏͶ͍͚ͯΔ
    • ʮࠓ೔͸͜ͷه๏Λ௥Ճ͠Α͏ʯ

    View full-size slide

  16. 108͋Δ͸ͯͳه๏ύʔαʔ
    • ͸ͯͳμΠΞϦʔ
    • ͸ͯͳάϧʔϓ
    • Text-Hatena (CPAN)
    • Text-Xatena
    • chris4403/WikiTextConverter
    • ...

    View full-size slide

  17. 108͋Δ͸ͯͳه๏ύʔαʔ
    • ͦΕͧΕඍົʹ࢓༷͕ҟͳΔ
    • ৽͘͠࡞ΒΕͨ΋ͷ΄ͲεʔύʔηοτͷఆٛΛ

    ࢼΈΔ܏޲͕͋Δ

    View full-size slide

  18. ܗࣜతͳఆٛΛ༩͍͑ͨ
    • ࣮૷ͱఆٛΛૄʹ͍ͨ͠
    • ఆ͚ٛͩΛࢀর͍ͨ͠
    • ͤΊͯCommonMark͘Β͍ͷ΋ͷ͸……
    • (ͱ͸͍࣮͑૷ͱϦϯΫͤ͞ΒΕΔ΄͏͕Α͍)

    View full-size slide

  19. aereal/go-text-hatena
    https://github.com/aereal/go-text-hatena

    View full-size slide

  20. ࣮૷ͨ͠ه๏
    • ݟग़͠
    • Ϧετ
    • Ҿ༻
    • HTTPه๏ (Ұ෦)

    View full-size slide

  21. ໨ࢦ͢ͱ͜Ζ
    • ϙʔλϒϧͰ͋Δ͜ͱ
    • ݴޠʹґΒͣར༻Ͱ͖Δ
    • ར༻ऀ͕೚ҙͷHTMLม׵ϧʔϧΛద༻Ͱ͖Δ
    • ࣮૷ͱఆٛΛૄʹ
    • ఆٛΛ੩తʹࢀরͰ͖Δ
    • ࣮૷ݴޠ (Go) ͷ஌͕ࣝͳͯ͘΋งғؾ͸௫ΊΔ

    View full-size slide

  22. ࣮૷
    • GoͰೖग़ྗΛΠϯλʔϑΣʔεͱ͢ΔόΠφϦ
    • → ݴޠʹґΒͣར༻Ͱ͖Δ
    • ASTΛJSONͰग़ྗ͢Δ͚ͩ
    • → ར༻ऀ͕೚ҙͷHTMLม׵ϧʔϧΛద༻ (࣮૷)
    Ͱ͖Δ

    View full-size slide

  23. ࣮૷
    • goyaccΛ࢖͍YACCͰจ๏ఆٛ
    • → ఆٛΛ੩తʹࢀরͰ͖Δ (parser.go.y)
    • ࣮૷ݴޠ (Go) ͷ஌͕ࣝͳͯ͘΋งғؾ͸௫ΊΔ

    View full-size slide

  24. HTML΁ͷม׵
    • αʔϏεʹΑͬͯͲΕ͘Β͍HTMLͷೖྗΛ

    ڐ༰͢Δ͔ͷํ਑͕ҟͳΔ
    • ͸ͯͳϒϩά͸ࣗ༝͕ͩɺ͸ͯͳμΠΞϦʔ͸

    ϗϫΠτϦετܗࣜ
    • ηΩϡϦςΟ্ͷࣄ৘

    View full-size slide

  25. ม׵σϞ
    • aereal/sketch-hatena-notation-api

    View full-size slide

  26. GoͰύʔαʔΛॻ͘

    View full-size slide

  27. ࢖ͬͨπʔϧ
    • Go
    • goyacc
    • text/scanner

    View full-size slide

  28. goyacc
    • yaccͷGo൛
    • ࠷ۙ go tool ͔Β֎Εͨ
    • go get golang.org/x/tools/cmd/goyacc

    View full-size slide

  29. yacc
    • ύʔαʔδΣωϨʔλʔ
    • BNF
    • ఆٛʹରͯ͠ίʔϧόοΫΛॻ͍͍ͯ͘
    • ؐݩͱΞΫγϣϯͱݺ͹ΕΔ
    • ଎शyacc͕͓͢͢Ί
    • ࣮૷ྫͱͯ͠ mattn/streeem ΋ࢀߟʹͳΔ

    View full-size slide

  30. yaccͷ͍͍ͱ͜Ζ
    • BNFʹΑΔఆٛͱɺؐݩ͢Δίʔυ͕෼཭Ͱ͖Δ
    • ݴޠ಺DSLΛ࢖͏ύʔαʔίϯϏωʔλʔΑΓ

    ଞͷਓʹݺΜͰ΋Β͍΍͍͢ؾ͕͢Δ

    View full-size slide

  31. yaccͷඍົͳͱ͜Ζ
    • *.go.y ͰΤσΟλͷࢧԉ͕ड͚ΒΕͳ͍
    • ͏·͍͜ͱઃఆͨ͠ΒͰ͖ͦ͏Ͱ͸͋Δ
    • ࣈ۟ղੳ͸ࣗ෼ͰؤுΔඞཁ͕͋Δ
    • GoͰ͸ text/scanner ͱ͍͏ඪ४ϥΠϒϥϦ͕

    ͋ΔͷͰָͰ͖Δ

    View full-size slide

  32. ίʔυΛಡΜͰΈΔ
    • lex.go
    • parser.go.y
    • parser_test.go

    View full-size slide

  33. ·ͱΊ
    • ͸ͯͳه๏ύʔαʔΛGoͰॻ͍ͨ
    • Goʹ͸goyacc΍text/scannerͳͲ

    ߏจղੳ΍ࣈ۟ղੳͷͨΊͷπʔϧ͕ἧ͍ͬͯΔ

    View full-size slide

  34. ࢀߟ৘ใ
    • ͜ͷࢿྉͰ঺հ͢Δ৘ใ͸͢΂ͯ 2017gokyoto ͱ
    ͍͏λάΛ͚ͭͯϒοΫϚʔΫ͍ͯ͠·͢
    • http://b.hatena.ne.jp/aereal/2017gokyoto/

    View full-size slide