summaryrefslogtreecommitdiff
path: root/ast.rb
AgeCommit message (Collapse)Author
2023-11-05ast.rb: Fix bug for source of multibyte charactersalpaca-tc
first_column and last_column return byte positions, but existing implementations did not consider multibyte.
2023-08-25Move SCRIPT_LINES__ away from parse.yNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/8289
2023-02-27Fix spelling (#7389)John Bampton
Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2022-12-24[DOC] Document AST.parse's keyword argumentszverok
Notes: Merged: https://github.com/ruby/ruby/pull/6996
2022-11-25[DOC] Fix AST documentsNobuyoshi Nakada
- Fix indents of `tokens`, to make the contents of Token a list - Move the example of `tokens` to separate from the above list - Markup keyword argument and method name
2022-11-21Enhance keep_tokens option for RubyVM::AbstractSyntaxTree parsing methodsyui-knk
Implementation for Language Server Protocol (LSP) sometimes needs token information. For example both `m(1)` and `m(1, )` has same AST structure other than node locations then it's impossible to check the existence of `,` from AST. However in later case, it might be better to suggest variables list for the second argument. Token information is important for such case. This commit adds these methods. * Add `keep_tokens` option for `RubyVM::AbstractSyntaxTree.parse`, `.parse_file` and `.of` * Add `RubyVM::AbstractSyntaxTree::Node#tokens` which returns tokens for the node including tokens for descendants nodes. * Add `RubyVM::AbstractSyntaxTree::Node#all_tokens` which returns all tokens for the input script regardless the receiver node. [Feature #19070] Impacts on memory usage and performance are below: Memory usage: ``` $ cat test.rb root = RubyVM::AbstractSyntaxTree.parse_file(File.expand_path('../test/ruby/test_keyword.rb', __FILE__), keep_tokens: true) $ /usr/bin/time -f %Mkb /usr/local/bin/ruby -v ruby 3.2.0dev (2022-11-19T09:41:54Z 19070-keep_tokens d3af1b8057) [x86_64-linux] 11408kb # keep_tokens :false $ /usr/bin/time -f %Mkb /usr/local/bin/ruby test.rb 17508kb # keep_tokens :true $ /usr/bin/time -f %Mkb /usr/local/bin/ruby test.rb 30960kb ``` Performance: ``` $ cat ../ast_keep_tokens.yml prelude: | src = <<~SRC module M class C def m1(a, b) 1 + a + b end end end SRC benchmark: without_keep_tokens: | RubyVM::AbstractSyntaxTree.parse(src, keep_tokens: false) with_keep_tokens: | RubyVM::AbstractSyntaxTree.parse(src, keep_tokens: true) $ make benchmark COMPARE_RUBY="./ruby" ARGS=../ast_keep_tokens.yml /home/kaneko.y/.rbenv/shims/ruby --disable=gems -rrubygems -I../benchmark/lib ../benchmark/benchmark-driver/exe/benchmark-driver \ --executables="compare-ruby::./ruby -I.ext/common --disable-gem" \ --executables="built-ruby::./miniruby -I../lib -I. -I.ext/common ../tool/runruby.rb --extout=.ext -- --disable-gems --disable-gem" \ --output=markdown --output-compare -v ../ast_keep_tokens.yml compare-ruby: ruby 3.2.0dev (2022-11-19T09:41:54Z 19070-keep_tokens d3af1b8057) [x86_64-linux] built-ruby: ruby 3.2.0dev (2022-11-19T09:41:54Z 19070-keep_tokens d3af1b8057) [x86_64-linux] warming up.. | |compare-ruby|built-ruby| |:--------------------|-----------:|---------:| |without_keep_tokens | 21.659k| 21.303k| | | 1.02x| -| |with_keep_tokens | 6.220k| 5.691k| | | 1.09x| -| ``` Notes: Merged: https://github.com/ruby/ruby/pull/6770
2022-10-31Add `node_id_for_backtrace_location` functioneileencodes
We want to use error highlight with eval'd code, specifically ERB templates. We're able to recover the generated code for eval'd templates and can get a parse tree for the ERB generated code, but we don't have a way to get the node id from the backtrace location. So we can't pass the right node into error highlight. This patch gives us an API to get the node id from the backtrace location so we can find the node in the AST. Error Highlight PR: https://github.com/ruby/error_highlight/pull/26 Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/6593
2022-10-08Add error_tolerant option to RubyVM::ASTyui-knk
If this option is enabled, SyntaxError is not raised and Node is returned even if passed script is broken. [Feature #19013] Notes: Merged: https://github.com/ruby/ruby/pull/6512
2021-08-20ast.c: Rename "save_script_lines" to "keep_script_lines"Yusuke Endoh
... as per ko1's preference. He is preparing to extend this feature to ISeq for his new debugger. He prefers "keep" to "save" for this wording. This API is internal and not included in any released version, so I change it in advance.
2021-06-21Experimentally expose RubyVM::AST::Node#node_idYusuke Endoh
Now ISeq#to_a includes the node_id list for each bytecode instruction. I want a way to retrieve the AST::Node instance corresponding to an instruction for a research purpose including TypeProf-based LSP server.
2021-06-18ast.rb: RubyVM::AST.parse and .of accepts `save_script_lines: true`Yusuke Endoh
This option makes the parser keep the original source as an array of the original code lines. This feature exploits the mechanism of `SCRIPT_LINES__` but records only the specified code that is passed to RubyVM::AST.of or .parse, instead of recording all parsed program texts. Notes: Merged: https://github.com/ruby/ruby/pull/4581
2021-06-06ast.rb: reduce nestingNobuyoshi Nakada
2020-06-24Fix RubyVM::AbstractSyntaxTree documentMasataka Pocke Kuwabara
RubyVM::AbstractSyntaxTree is a module actually, but the document says "class". Notes: Merged: https://github.com/ruby/ruby/pull/3251
2020-06-19[Feature #16254] Use `Primitive.func` styleNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/3165
2020-06-19[Feature #16254] Use `__builtin.func` styleNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/3165
2020-01-03Fix variable name and add more example [ci skip]Kazuhiro NISHIYAMA
2020-01-03Fix example of node.type [ci skip]Kazuhiro NISHIYAMA
``` % docker run -it --rm rubylang/all-ruby env ALL_RUBY_SINCE=ruby-2.6 ./all-ruby -e 'root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") p [root, root.type] call = root.children[2] p [call, call.type] ' ruby-2.6.0 [#<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>, :SCOPE] [#<RubyVM::AbstractSyntaxTree::Node:LASGN@1:0-1:9>, :LASGN] ... ruby-2.7.0 [#<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>, :SCOPE] [#<RubyVM::AbstractSyntaxTree::Node:LASGN@1:0-1:9>, :LASGN] ```
2019-12-14Improve highlighting in RubyVM::AbstractSyntaxTree docs when using `ri`Benoit Daloze
2019-12-14Clarify in the documentation that RubyVM::AbstractSyntaxTree is not stable APIBenoit Daloze
* See [Feature #14844].
2019-11-08fix line break code (fix to LF)Koichi Sasada
2019-11-08use builtin for RubyVM::AbstractSyntaxTree.Koichi Sasada
Define RubyVM::AbstractSyntaxTree in ast.rb with __builtin functions. Notes: Merged: https://github.com/ruby/ruby/pull/2655