Movatterモバイル変換


[0]ホーム

URL:


Project

General

Profile

Ruby

Ruby

Custom queries

Actions

Feature #21795

open

Methods for retrieving ASTs

Feature #21795:Methods for retrieving ASTs
1

Added bykddnewton (Kevin Newton)2 months ago. Updated6 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:124311]

Description

I would like to propose a handful of methods for retrieving ASTs from various objects that correspond to locations in code. This includes:

  • Proc#ast
  • Method#ast
  • UnboundMethod#ast
  • Thread::Backtrace::Location#ast
  • TracePoint#ast (on call/return events)

The purpose of this is to make tooling easier to write and maintain. Specifically, this would be able to be used in irb, power_assert, error_highlight, and various other tools both in core and not that make use of source code.

There have been many previous discussions of retrieving node_id, source_location, source, etc. All of these use cases are covered by returning the AST for some entity. In this case node_id becomes an implementation detail, invisible to the user. Source location can be derived from the information on the AST itself. Similarly, source can be derived from the AST.

Internally, I do not think we have to store any more information than we already do (since we have node_id for the first four of these, it becomes rather trivial). For TracePoint we can have a larger discussion about it, but I think it should not be too much work. In terms of implementation, the only caveat I would put is that if the ISEQ were compiled through the old parser/compiler, this should returnnil, as the node ids do not match up and we do not want to further propagate the RubyVM::AST API.

The reason I am opening up this ticket with 5 different methods requested in it is to get approval first for the direction, then I can open individual tickets or just PRs for each method. I believe this feature would ease the maintenance burden of many core libraries, and unify otherwise disparate efforts to achieve the same thing.


Related issues1 (1 open0 closed)

Blocks Ruby -Feature #21826: Deprecating RubyVM::AbstractSyntaxTreeOpenActions

Issue #Delay: daysCancel

Updated bymame (Yusuke Endoh)2 months agoActions#1[ruby-core:124321]

I anticipated that we would consider this eventually, but incorporating it into the core presents significant challenges.

Here are two major issues regarding feasibility.

(Based on chats with@ko1 (Koichi Sasada),@tompng (tomoya ishida), and@yui-knk (Kaneko Yuichiro), though these are my personal views.)

The Implementation Approach

CRuby currently discards source code and ASTs after ISeq generation. The proposed#ast method would have to re-read and re-parse the source, which causes two problems:

  1. If the file is modified after loading,#ast may return the wrong node.
  2. It does not work foreval strings.

error_highlight accepts this fragility because it displays just "hints". But I don't think that it is allowed for a built-in method. At least, we must avoid returning an incorrect node, and clarify when failures occur.

I propose two approaches:

  1. Keep loaded source in memory (e.g.,RubyVM.keep_script_lines = true by default). This supportseval but increase memory usage.
  2. Validate source hash. Store a hash in the ISeq and check it to ensure the file hasn't changed.

The Parser Switching Problem

What is the node definition returned by#ast?

As noted in#21618, built-in Prism is not exposed as a Ruby API. IfGemfile.lock specifies an older version of prism gem, evenrequire "prism" won't provide the expected definition.

IMO, it would be good to have a node definition that does not depend on prism gem (maybeRuby::Node?). I am not sure how much effort is needed for this. We would also need to consider where to place what in the ruby/prism and ruby/ruby repositories for development.

We also need to decide if#ast should returnRubyVM::AST::Node when--parser=parse.y is specified.

Updated byEregon (Benoit Daloze)about 1 month ago· EditedActions#2

I think this would be great to have, and abstract over implementation details likenode_id.
It's also very powerful as e.g.Thread::Backtrace::Location#ast would be able to return aPrism::CallNode with all the relevant information, whicherror_highlight and others could then use very conveniently.

mame (Yusuke Endoh) wrote in#note-1:

The Implementation Approach

error_highlight accepts this fragility because it displays just "hints".
But I don't think that it is allowed for a built-in method. At least, we must avoid returning an incorrect node, and clarify when failures occur.

I think a built-in method doesn't imply it must work perfectly, it's e.g. not really possible to succeed when the file is modified (without keeping the source in memory).

I propose two approaches:

  1. Keep loaded source in memory (e.g.,RubyVM.keep_script_lines = true by default). This supportseval but increase memory usage.

I think we could keep onlyeval code (IOW, non-file-based code) in memory, then the memory increase wouldn't be so big, and it would work as long as the files aren't modified.
Keeping all code in memory would be convenient, and interesting to measure how much an overhead it is (source code is often quite a compact representation actually), but I suspect given the general focus of CRuby on memory footprint it would be considered only as last resort.

  1. Validate source hash. Store a hash in the ISeq and check it to ensure the file hasn't changed.

This is a great idea, it should be reasonably fast and avoid the pitfall about modified files.
Although modified files are probably very rare in practice, so I'm not sure how much this matters, but it does seem nicer to fail properly than potentially return the wrong node.

The Parser Switching Problem

What is the node definition returned by#ast?

APrism::Node becauseMatz has agreed that going forward the official parser API for Ruby will be the Prism API.

Actually more specific nodes where known:

  • Proc#ast:LambdaNode | CallNode | ForNode | DefNode (DefNode becauseMethod#to_proc)
  • Method#ast &UnboundMethod#ast:DefNode | LambdaNode | CallNode | ForNode (block-related nodes becausedefine_method(&Proc))
  • Thread::Backtrace::Location#ast &TracePoint#ast:Call*Node | Index*Node | YieldNode, and maybe a few more.

As noted in#21618, built-in Prism is not exposed as a Ruby API. IfGemfile.lock specifies an older version of prism gem, evenrequire "prism" won't provide the expected definition.

This is basically a solved problem, as discussed there.
In that case,Prism.parse(foo, version: "current") fails with a clear exception explaining one needs to use a newer prism gem.
This only happens if one explicitly downgrades the Prism gem, which is expected to be pretty rare.

IMO, it would be good to have a node definition that does not depend on prism gem (maybeRuby::Node?). I am not sure how much effort is needed for this. We would also need to consider where to place what in the ruby/prism and ruby/ruby repositories for development.

IMO there should only bePrism::Node, otherwise tools would have to switch between two APIs whether they want to use the current-running syntax or another syntax.
From the discussion in#21618 my take away is it's unnecessary to have a different API.

We also need to decide if#ast should returnRubyVM::AST::Node when--parser=parse.y is specified.

It must not, the users of these new methods expect aPrism::Node.
Matz has said the official parser API for Ruby is the Prism API, so it doesn't make sense to returnRubyVM::AST::Node there.
Also that wouldn't be reasonable when considering alternative Ruby implementations.

This does mean these methods wouldn't work with--parser=parse.y, untilparse.y can be used to create aPrism::Node AST.
Since that's the official Ruby parsing API, it's already a goal anyway forparse.y to do that (#21825), so that shouldn't be a blocker.

Updated byEregon (Benoit Daloze)about 1 month agoActions#3[ruby-core:124427]

One idea to make it work with--parser=parse.y until universal parser supports the Prism API (#21825) would be to:

  • Get theRubyVM::AST::Node of that object, then extract the start/end line & start/end columns. Or do the same internally without needing aRubyVM::AST::Node, it's just converting from parse.y node_id to "bounds".
  • With those values, use something similar toPrism.node_for to find a node base on those bounds.
  • There should be a single node matching those bounds because we are only looking for specific nodes.

Updated bykddnewton (Kevin Newton)about 1 month agoActions#4[ruby-core:124437]

Thanks@mame (Yusuke Endoh) for the detailed reply! I appreciate your thoughtfulness here.

With regard to the implementation approach problem, I love your solution of keeping a source hash on the iseq. I think that makes a lot of sense, and could be used in error highlight today as well. That could potentially even be used by other tools in the case of code reloading. I think wecould potentially store the code for eval, but I would be tempted to say let us not change anything for now and returnnil or raise an error in that case. (In the same way we would need to returnnil or raise an error for C methods.)

For the parser switching problem, I think I would like to introduce a Prism ABI version (alongside the Prism gem version). I would update this version whenever a structural change is made (field added/renamed/removed/etc.). Then, if we could store the Prism ABI version on the ISEQ as well, we could require prism and check if the ABI version matches before attempting to re-parse. We could be clear through the error message that the Prism ABI version is a mismatch and therefore we cannot re-parse.

I am not sure if we should return RubyVM::AST nodes in the case the ISEQ was compiled with parse.y/compile.c, but I am okay with it if that's the direction you would like to go.

Updated byEregon (Benoit Daloze)about 1 month agoActions#5

  • Related toFeature #21826: Deprecating RubyVM::AbstractSyntaxTree added

Updated byEregon (Benoit Daloze)about 1 month agoActions#6

  • Related to deleted (Feature #21826: Deprecating RubyVM::AbstractSyntaxTree)

Updated byEregon (Benoit Daloze)about 1 month agoActions#7

Updated byEregon (Benoit Daloze)about 1 month agoActions#8[ruby-core:124578]

Rails'_callable_to_source_string would be a good use case for this, seehttps://github.com/rails/rails/pull/56624

Updated bymame (Yusuke Endoh)7 days agoActions#9[ruby-core:124809]

Eregon (Benoit Daloze) wrote in#note-2:

As noted in#21618, built-in Prism is not exposed as a Ruby API. IfGemfile.lock specifies an older version of prism gem, evenrequire "prism" won't provide the expected definition.

This is basically a solved problem, as discussed there.
In that case,Prism.parse(foo, version: "current") fails with a clear exception explaining one needs to use a newer prism gem.

I believe#21618 primarily discusses released Ruby versions. My concern is specifically about the behavior on the master branch.

When new syntax is introduced to the Ruby master branch, the built-inprism.c is updated immediately. In this scenario, if we attempt to retrieve#ast using the node definitions from a released prism gem, I am concerned that we will not get a correct AST due to the node definition mismatch.

kddnewton (Kevin Newton) wrote in#note-4:

For the parser switching problem, I think I would like to introduce a Prism ABI version (alongside the Prism gem version). I would update this version whenever a structural change is made (field added/renamed/removed/etc.). Then, if we could store the Prism ABI version on the ISEQ as well, we could require prism and check if the ABI version matches before attempting to re-parse. We could be clear through the error message that the Prism ABI version is a mismatch and therefore we cannot re-parse.

While this is certainly a feasible solution, I don't feel it is the optimal one.
I acknowledge the engineering challenges involved, but ideally, I believe having a built-in node definition (likeRuby::Node) within Ruby core itself would be the simplest and best approach.

Updated bykddnewton (Kevin Newton)6 days agoActions#10[ruby-core:124822]

Would a Ruby::Node be the same thing as a Prism::Node? As in, would it basically be a Ruby API that duplicates the Prism interface?

I'm not sure about how to maintain it. For example, if we add more features to Prism's Ruby API (for example the work we've been doing on the translation layers to Ripper recently) would we also duplicate it to the various live branches of the Ruby::Node API? Or would it just be a trimmed down version? Either way, I'm not sure when I would recommend using Ruby::Node, because it seems like it would always be an out-of-date version of Prism::Node.

Actions

Also available in:PDFAtom

Powered byRedmine © 2006-2026 Jean-Philippe Lang
Loading...

[8]ページ先頭

©2009-2026 Movatter.jp