NewsCrawler.org

Steadfast Update I: Laying the Baseline for a Ruby AST Toolkit

The post is the first update on Steadfast, an open-source Ruby project the author has been working on. The author's recent work laid the project's foundations, starting with converting a Prism AST[1] into source code, a feature he had built earlier for the Papercraft compiler and now improved.

Ruby cannot currently pass an AST to eval[2], so Steadfast generates source code from the AST and feeds that to eval instead. Exceptions raised in code built from composed ASTs can produce nonsensical backtraces[3], so the author added filename information to the Prism::Source class and maps each line of generated code back to its original location.

Steadfast currently provides three tools: to_ast, which uses the Proc#syntax_tree and Method#syntax_tree APIs from Ruby 4.1; to_source, which generates source code designed for correctness and testing rather than formatting; and translate_backtrace, which points each backtrace entry to the correct source location. The author plans to add mutate, walk, find and find_all, and is considering an alternative to #syntax_tree for older Ruby versions.

Read the original noteflakes.comDiscussion

Sources

Published: First seen: Читать по-русски

Open as a page