diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2023-10-13 12:16:11 -0400 |
|---|---|---|
| committer | Jemma Issroff <jemmaissroff@gmail.com> | 2023-10-16 15:40:19 -0700 |
| commit | 5523a23469987f92e38d52d4332bde09bdd8896c (patch) | |
| tree | 01c4fcef6dea113e55ab1eedb59742a5ebad2b36 /lib | |
| parent | 39dd3343d8672a70ebb0990c166d99a8b29ee19e (diff) | |
[ruby/prism] Attach magic comments to the parse result
https://github.com/ruby/prism/commit/c7ef25a79a
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/prism/ffi.rb | 4 | ||||
| -rw-r--r-- | lib/prism/lex_compat.rb | 2 | ||||
| -rw-r--r-- | lib/prism/parse_result.rb | 35 |
3 files changed, 34 insertions, 7 deletions
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb index 69b2b35bbb..cc7d94fb3f 100644 --- a/lib/prism/ffi.rb +++ b/lib/prism/ffi.rb @@ -234,11 +234,11 @@ module Prism loader = Serialize::Loader.new(source, buffer.read) tokens = loader.load_tokens - node, comments, errors, warnings = loader.load_nodes + node, comments, magic_comments, errors, warnings = loader.load_nodes tokens.each { |token,| token.value.force_encoding(loader.encoding) } - ParseResult.new([node, tokens], comments, errors, warnings, source) + ParseResult.new([node, tokens], comments, magic_comments, errors, warnings, source) end end diff --git a/lib/prism/lex_compat.rb b/lib/prism/lex_compat.rb index d0679db669..a17d4eaadd 100644 --- a/lib/prism/lex_compat.rb +++ b/lib/prism/lex_compat.rb @@ -818,7 +818,7 @@ module Prism # We sort by location to compare against Ripper's output tokens.sort_by!(&:location) - ParseResult.new(tokens, result.comments, result.errors, result.warnings, []) + ParseResult.new(tokens, result.comments, result.magic_comments, result.errors, result.warnings, []) end end diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index b5e6643e6d..fd2737b20c 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -137,7 +137,7 @@ module Prism end def self.null - new(0, 0) + new(nil, 0, 0) end end @@ -166,6 +166,32 @@ module Prism end end + # This represents a magic comment that was encountered during parsing. + class MagicComment + attr_reader :key_loc, :value_loc + + def initialize(key_loc, value_loc) + @key_loc = key_loc + @value_loc = value_loc + end + + def key + key_loc.slice + end + + def value + value_loc.slice + end + + def deconstruct_keys(keys) + { key_loc: key_loc, value_loc: value_loc } + end + + def inspect + "#<Prism::MagicComment @key=#{key.inspect} @value=#{value.inspect}>" + end + end + # This represents an error that was encountered during parsing. class ParseError attr_reader :message, :location @@ -206,18 +232,19 @@ module Prism # the AST, any comments that were encounters, and any errors that were # encountered. class ParseResult - attr_reader :value, :comments, :errors, :warnings, :source + attr_reader :value, :comments, :magic_comments, :errors, :warnings, :source - def initialize(value, comments, errors, warnings, source) + def initialize(value, comments, magic_comments, errors, warnings, source) @value = value @comments = comments + @magic_comments = magic_comments @errors = errors @warnings = warnings @source = source end def deconstruct_keys(keys) - { value: value, comments: comments, errors: errors, warnings: warnings } + { value: value, comments: comments, magic_comments: magic_comments, errors: errors, warnings: warnings } end def success? |
