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

My favorite script, "dsl.rb"

My favorite script, "dsl.rb"

RubyKaigi 2024事後勉強会 https://smarthr.connpass.com/event/319010/

yui-knk

May 31, 2024
Tweet

More Decks by yui-knk

Other Decks in Programming

Transcript

  1. My favorite script, "dsl.rb" May 31, 2024 in RubyKaigi 2024

    事後勉強会 @yui-knk Yuichiro Kaneko
  2. About me Yuichiro Kaneko yui-knk (GitHub) / spikeolaf (Twitter) Treasure

    Data Engineering Manager of Applications Backend CRuby committer, mainly develop parser generator and parser Lrama LALR (1) parser generator (2023, Ruby 3.3) Love LR parser
  3. May 15th - 17th, 2024 NAHA CULTURAL ARTS THEATER NAHArt,

    Okinawa, Japan The Bison Slayer The parser monster Parser界の黎明卿 The world is now in the great age of parsers. People are setting sail into the vast sea of parsers. - RubyKaigi 2023 LT- Yuichiro Kaneko https://twitter.com/kakutani/status/1657762294431105025/ NEW !!! NEW !!!
  4. May 15th - 17th, 2024 NAHA CULTURAL ARTS THEATER NAHArt,

    Okinawa, Japan EXTRA STAGE “Ripper” CHALLENGE 19
  5. May 15th - 17th, 2024 NAHA CULTURAL ARTS THEATER NAHArt,

    Okinawa, Japan How Ripper is implemented?
  6. How parse.y is used parse.y is a source of ripper.y

    parse.y parse.c parse.h Lrama ripper.y tool/id2token.rb tools/preproc.rb Lrama ripper.c
  7. Comments in parse.y is transformed to C codes in ripper.y

    Comments are not comments !! parse.y is two-faced parse.y ripper.y
  8. May 15th - 17th, 2024 NAHA CULTURAL ARTS THEATER NAHArt,

    Okinawa, Japan Today’s main topic
  9. How parse.y is used parse.y is a source of ripper.y

    parse.y parse.c parse.h Lrama ripper.y tool/id2token.rb tools/preproc.rb Lrama ripper.c tools/dsl.rb
  10. preproc.rb preproc.rb processes input file line by line /*% ripper:

    stmts_add!($:1, $:3) %*/ match /*% ripper: stmts_add!($:1, $:3) %*/ processed
  11. In parse.y, C codes and Ruby codes in C’s comment

    exist In ripper.y, Ruby codes are transformed to C codes Mixture of C and Ruby parse.y ripper.y C codes Ruby codes C codes
  12. #1. Split the code Split a comment into options and

    “@code” Generate DSL instance class DSL options @error @brace @ fi nal @code
  13. #2. eval @code Eval @code, then method_missing and/or const_missing will

    be called const_missing method_missing method_missing
  14. #3. method_missing method_missing generates C code #stmts_new! call with no

    arguments then translated to dispatch0 function call
  15. #3. method_missing #stmts_add! call with 2 arguments then translated to

    variable setup codes and dispatch2 function call
  16. Tips #1 p = p = "p" As you know,

    "p" is "struct parser_params *p” What is Kernel#p method? Double assignments is for warning suppression "warning: assigned but unused variable - p"
  17. Tips #2 undef is needed for lambda, hash, class events

    Otherwise method_missing is not called
  18. PR #10766 PR was created by nobu on May 12

    RubyKaigi was May 15-17 I wan in Okinawa https://github.com/ruby/ruby/pull/10766
  19. $:n[i] and $:n[i..j] to splat head in ripper is an

    array with 3 elements Want to splat the array when invoke defs event
  20. p = p = "p" is improved Remove double assignments

    by using default value of argument Intention is more clear than previous codes
  21. Summary “ext/ripper/tools/dsl.rb” Is less than 200 lines but very powerful

    Contains real world examples of #eval #method_missing #const_missing #binding Refinements (<- New!)