Slide 1

Slide 1 text

Converting AST June 25, 2024 After RubyKaigi 2024 LR Parser Night w/ Asakusa.rb @yui-knk Yuichiro Kaneko

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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 !!!

Slide 4

Slide 4 text

May 15th - 17th, 2024 NAHA CULTURAL ARTS THEATER NAHArt, Okinawa, Japan

Slide 5

Slide 5 text

ast_to_prism https://github.com/yui-knk/ast_to_prism Converts RubyVM::AST to Prism::Node Motivations Provide the compatible layer Clarify the difference between current Node and Prism Node Send feedbacks to current struct RNode Writing codes is easier than writing docs It may heavily depend on ruby master branch

Slide 6

Slide 6 text

They are watching me 2 watching and 3 stars !?

Slide 7

Slide 7 text

Who are Watchers and Stargazers

Slide 8

Slide 8 text

The position of ast_to_prism This is a missing piece Source code RubyVM::AST::Node parse.y struct rb_ast_struct struct RNode ast.c Prism::Node ast_to_prism Parser::AST::Node Prism::Translation::Parser RuboCop <>

Slide 9

Slide 9 text

How ast_to_prism works “parse” the source code then “convert” nodes to prism nodes

Slide 10

Slide 10 text

How ast_to_prism works “convert” method is a large case statement Some node types are supported, others are not

Slide 11

Slide 11 text

Milestones 1. Add insufficient information to Node 2. Change AST structures 3. Keep some nodes

Slide 12

Slide 12 text

Insuf fi cient information Location information E.g. “if”, “then”, “end” locations are not supported

Slide 13

Slide 13 text

Insuf fi cient information Simply, add them to nodes RNode is already free from 3 fields restriction

Slide 14

Slide 14 text

Change AST structures NODE_RESCUE wraps begin statement

Slide 15

Slide 15 text

Change AST structures NODE_ENSURE wraps NODE_RESCUE

Slide 16

Slide 16 text

Change AST structures Change it to be more simple structure

Slide 17

Slide 17 text

Change AST structures RNode is already free from 3 fields restriction !!!

Slide 18

Slide 18 text

Another example: UNDEF undef with list is expanded to multiple undef(s)

Slide 19

Slide 19 text

Another example: UNDEF Change NODE_UNDEF to holds all items

Slide 20

Slide 20 text

Keep some nodes NODE_BLOCK is omitted for single line body

Slide 21

Slide 21 text

Keep some nodes Right now, “convert_stmts” checks the node is BLOCK or not

Slide 22

Slide 22 text

Tips: range argument Why range optional argument is needed?

Slide 23

Slide 23 text

How “e” is managed Need to manage “e” variable

Slide 24

Slide 24 text

Prism::RescueNode Manages “e” in “reference“ field

Slide 25

Slide 25 text

NODE_RESBODY “e” is managed as the first line of nd_body

Slide 26

Slide 26 text

Need to split nd_body reference = nd_body.children[0] statements = convert_stmts(nd_body, 1..-1)

Slide 27

Slide 27 text

Expand NODE_RESCUE RNode is already free from 3 fields restriction !!!

Slide 28

Slide 28 text

How to develop Add test cases to “spec/basic_spec.rb” Implement it on “lib/ast_to_prism/parser.rb” If test passes OK. Go next NG. “pending” test case Create PR to ruby/ruby

Slide 29

Slide 29 text

“spec/basic_spec.rb” Add test cases

Slide 30

Slide 30 text

“spec/basic_spec.rb” Remove “not_supported” then implement convert logic

Slide 31

Slide 31 text

If you can not implement pending test case Investigate the reason why it can not Create PR to ruby/ruby Implement the conversion logic after merge

Slide 32

Slide 32 text

Next Steps Cover all simple cases in ast_to_prism Cover “test/*” files under ruby/ruby

Slide 33

Slide 33 text

May 15th - 17th, 2024 NAHA CULTURAL ARTS THEATER NAHArt, Okinawa, Japan Thank you !!!