code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def extract_count_from_options(options) if options.is_a?(Hash) count = options[:count] else count = options options = {} end [count, options] end
options is either a Integer or a Hash with :count
extract_count_from_options
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parallel-1.23.0/lib/parallel.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parallel-1.23.0/lib/parallel.rb
Apache-2.0
def initialize(builder=Parser::Builders::Default.new) @diagnostics = Diagnostic::Engine.new @static_env = StaticEnvironment.new # Stack that holds current parsing context @context = Context.new # Maximum numbered parameters stack @max_numparam_stack = MaxNumparamStack.new # Current argument names stack @current_arg_stack = CurrentArgStack.new # Stack of set of variables used in the current pattern @pattern_variables = VariablesStack.new # Stack of set of keys used in the current hash in pattern matchinig @pattern_hash_keys = VariablesStack.new @lexer = Lexer.new(version) @lexer.diagnostics = @diagnostics @lexer.static_env = @static_env @lexer.context = @context @builder = builder @builder.parser = self # Last emitted token @last_token = nil if self.class::Racc_debug_parser && ENV['RACC_DEBUG'] @yydebug = true end reset end
# @param [Parser::Builders::Default] builder The AST builder to use.
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
Apache-2.0
def reset @source_buffer = nil @lexer.reset @static_env.reset @context.reset @current_arg_stack.reset @pattern_variables.reset @pattern_hash_keys.reset self end
# Resets the state of the parser.
reset
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
Apache-2.0
def parse(source_buffer) @lexer.source_buffer = source_buffer @source_buffer = source_buffer do_parse || nil # Force `false` to `nil`, see https://github.com/ruby/racc/pull/136 ensure # Don't keep references to the source file. @source_buffer = nil @lexer.source_buffer = nil end
# Parses a source buffer and returns the AST, or `nil` in case of a non fatal error. @param [Parser::Source::Buffer] source_buffer The source buffer to parse. @return [Parser::AST::Node, nil]
parse
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
Apache-2.0
def parse_with_comments(source_buffer) @lexer.comments = [] [ parse(source_buffer), @lexer.comments ] ensure @lexer.comments = nil end
# Parses a source buffer and returns the AST and the source code comments. @see #parse @see Parser::Source::Comment#associate @return [Array]
parse_with_comments
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/base.rb
Apache-2.0
def initialize(level, reason, arguments, location, highlights=[]) unless LEVELS.include?(level) raise ArgumentError, "Diagnostic#level must be one of #{LEVELS.join(', ')}; " \ "#{level.inspect} provided." end raise 'Expected a location' unless location @level = level @reason = reason @arguments = (arguments || {}).dup.freeze @location = location @highlights = highlights.dup.freeze freeze end
# @param [Symbol] level @param [Symbol] reason @param [Hash] arguments @param [Parser::Source::Range] location @param [Array<Parser::Source::Range>] highlights
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
Apache-2.0
def render if @location.line == @location.last_line || @location.is?("\n") ["#{@location}: #{@level}: #{message}"] + render_line(@location) else # multi-line diagnostic first_line = first_line_only(@location) last_line = last_line_only(@location) num_lines = (@location.last_line - @location.line) + 1 buffer = @location.source_buffer last_lineno, last_column = buffer.decompose_position(@location.end_pos) ["#{@location}-#{last_lineno}:#{last_column}: #{@level}: #{message}"] + render_line(first_line, num_lines > 2, false) + render_line(last_line, false, true) end end
# Renders the diagnostic message as a clang-like diagnostic. @example diagnostic.render # => # [ # "(fragment:0):1:5: error: unexpected token $end", # "foo +", # " ^" # ] @return [Array<String>]
render
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
Apache-2.0
def render_line(range, ellipsis=false, range_end=false) source_line = range.source_line highlight_line = ' ' * source_line.length @highlights.each do |highlight| line_range = range.source_buffer.line_range(range.line) if highlight = highlight.intersect(line_range) highlight_line[highlight.column_range] = '~' * highlight.size end end if range.is?("\n") highlight_line += "^" else if !range_end && range.size >= 1 highlight_line[range.column_range] = '^' + '~' * (range.size - 1) else highlight_line[range.column_range] = '~' * range.size end end highlight_line += '...' if ellipsis [source_line, highlight_line]. map { |line| "#{range.source_buffer.name}:#{range.line}: #{line}" } end
# Renders one source line in clang diagnostic style, with highlights. @return [Array<String>]
render_line
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
Apache-2.0
def first_line_only(range) if range.line != range.last_line range.resize(range.source =~ /\n/) else range end end
# If necessary, shrink a `Range` so as to include only the first line. @return [Parser::Source::Range]
first_line_only
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
Apache-2.0
def last_line_only(range) if range.line != range.last_line range.adjust(begin_pos: range.source =~ /[^\n]*\z/) else range end end
# If necessary, shrink a `Range` so as to include only the last line. @return [Parser::Source::Range]
last_line_only
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic.rb
Apache-2.0
def on_newline(p) # After every heredoc was parsed, @herebody_s contains the # position of next token after all heredocs. if @herebody_s p = @herebody_s @herebody_s = nil end p end
This hook is triggered by "main" lexer on every newline character
on_newline
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/lexer-strings.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/lexer-strings.rb
Apache-2.0
def rewrite(source_buffer, ast) @source_rewriter = Source::Rewriter.new(source_buffer) process(ast) @source_rewriter.process end
# Rewrites the AST/source buffer and returns a String containing the new version. @param [Parser::Source::Buffer] source_buffer @param [Parser::AST::Node] ast @return [String]
rewrite
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
Apache-2.0
def assignment?(node) [:lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn].include?(node.type) end
# Returns `true` if the specified node is an assignment node, returns false otherwise. @param [Parser::AST::Node] node @return [Boolean]
assignment?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
Apache-2.0
def wrap(range, before, after) @source_rewriter.wrap(range, before, after) end
# Wraps the given source range with the given values. @param [Parser::Source::Range] range @param [String] content
wrap
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
Apache-2.0
def insert_before(range, content) @source_rewriter.insert_before(range, content) end
# Inserts new code before the given source range. @param [Parser::Source::Range] range @param [String] content
insert_before
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
Apache-2.0
def insert_after(range, content) @source_rewriter.insert_after(range, content) end
# Inserts new code after the given source range. @param [Parser::Source::Range] range @param [String] content
insert_after
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
Apache-2.0
def replace(range, content) @source_rewriter.replace(range, content) end
# Replaces the code of the source range `range` with `content`. @param [Parser::Source::Range] range @param [String] content
replace
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/rewriter.rb
Apache-2.0
def rewrite(source_buffer, ast, **policy) @source_rewriter = Parser::Source::TreeRewriter.new(source_buffer, **policy) process(ast) @source_rewriter.process end
# Rewrites the AST/source buffer and returns a String containing the new version. @param [Parser::Source::Buffer] source_buffer @param [Parser::AST::Node] ast @param [Symbol] crossing_deletions:, different_replacements:, swallowed_insertions: policy arguments for TreeRewriter (optional) @return [String]
rewrite
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
Apache-2.0
def assignment?(node) [:lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn].include?(node.type) end
# Returns `true` if the specified node is an assignment node, returns false otherwise. @param [Parser::AST::Node] node @return [Boolean]
assignment?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
Apache-2.0
def wrap(range, before, after) @source_rewriter.wrap(range, before, after) end
# Wraps the given source range with the given values. @param [Parser::Source::Range] range @param [String] content
wrap
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
Apache-2.0
def insert_before(range, content) @source_rewriter.insert_before(range, content) end
# Inserts new code before the given source range. @param [Parser::Source::Range] range @param [String] content
insert_before
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
Apache-2.0
def insert_after(range, content) @source_rewriter.insert_after(range, content) end
# Inserts new code after the given source range. @param [Parser::Source::Range] range @param [String] content
insert_after
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
Apache-2.0
def replace(range, content) @source_rewriter.replace(range, content) end
# Replaces the code of the source range `range` with `content`. @param [Parser::Source::Range] range @param [String] content
replace
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/tree_rewriter.rb
Apache-2.0
def assign_properties(properties) if (location = properties[:location]) location = location.dup if location.frozen? location.node = self @location = location end end
# Assigns various properties to this AST node. Currently only the location can be set. @param [Hash] properties @option properties [Parser::Source::Map] :location Location information of the node.
assign_properties
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/ast/node.rb
Apache-2.0
def logical_op(type, lhs, op_t, rhs) n(type, [ lhs, rhs ], binary_op_map(lhs, op_t, rhs)) end
Control flow Logical operations: and, or
logical_op
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/builders/default.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/builders/default.rb
Apache-2.0
def static_string(nodes) nodes.map do |node| case node.type when :str node.children[0] when :begin if (string = static_string(node.children)) string else return nil end else return nil end end.join end
HELPERS Extract a static string from e.g. a regular expression, honoring the fact that MRI expands interpolations like #{""} at parse time.
static_string
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/builders/default.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/builders/default.rb
Apache-2.0
def process(diagnostic) if ignore?(diagnostic) # do nothing elsif @consumer @consumer.call(diagnostic) end if raise?(diagnostic) raise Parser::SyntaxError, diagnostic end self end
# Processes a `diagnostic`: * Passes the diagnostic to the consumer, if it's not a warning when `ignore_warnings` is set. * After that, raises {Parser::SyntaxError} when `all_errors_are_fatal` is set to true. @param [Parser::Diagnostic] diagnostic @return [Parser::Diagnostic::Engine] @see ignore? @see raise?
process
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic/engine.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic/engine.rb
Apache-2.0
def ignore?(diagnostic) @ignore_warnings && diagnostic.level == :warning end
# Checks whether `diagnostic` should be ignored. @param [Parser::Diagnostic] diagnostic @return [Boolean]
ignore?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic/engine.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic/engine.rb
Apache-2.0
def raise?(diagnostic) (@all_errors_are_fatal && diagnostic.level == :error) || diagnostic.level == :fatal end
# Checks whether `diagnostic` should be raised as an exception. @param [Parser::Diagnostic] diagnostic @return [Boolean]
raise?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic/engine.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/diagnostic/engine.rb
Apache-2.0
def dedent(string) original_encoding = string.encoding # Prevent the following error when processing binary encoded source. # "\xC0".split # => ArgumentError (invalid byte sequence in UTF-8) lines = string.force_encoding(Encoding::BINARY).split("\\\n") if lines.length == 1 # If the line continuation sequence was found but there is no second # line, it was not really a line continuation and must be ignored. lines = [string.force_encoding(original_encoding)] else lines.map! {|s| s.force_encoding(original_encoding) } end if @at_line_begin lines_to_dedent = lines else _first, *lines_to_dedent = lines end lines_to_dedent.each do |line| left_to_remove = @dedent_level remove = 0 line.each_char do |char| break if left_to_remove <= 0 case char when ?\s remove += 1 left_to_remove -= 1 when ?\t break if TAB_WIDTH * (remove / TAB_WIDTH + 1) > @dedent_level remove += 1 left_to_remove -= TAB_WIDTH else # no more spaces or tabs break end end line.slice!(0, remove) end string.replace(lines.join) @at_line_begin = string.end_with?("\n") end
For a heredoc like <<-HERE a b HERE this method gets called with " a\n" and " b\n" However, the following heredoc: <<-HERE a\ b HERE calls this method only once with a string " a\\\n b\n" This is important because technically it's a single line, but it has to be concatenated __after__ dedenting. It has no effect for non-squiggly heredocs, i.e. it simply removes "\\\n" Of course, lexer could do it but once again: it's all because of dedenting.
dedent
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/lexer/dedenter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/lexer/dedenter.rb
Apache-2.0
def advance type, (val, range) = advance_before_explanation more = "(in-kwarg)" if @context.in_kwarg puts decorate(range, Color.green("#{type} #{val.inspect}"), "#{state.to_s.ljust(12)} #{@cond} #{@cmdarg} #{more}") [ type, [val, range] ] end
Like #advance, but also pretty-print the token and its position in the stream to `stdout`.
advance
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/lexer/explanation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/lexer/explanation.rb
Apache-2.0
def read File.open(@name, 'rb') do |io| self.source = io.read end self end
# Populate this buffer from correspondingly named file. @example Parser::Source::Buffer.new('foo/bar.rb').read @return [Buffer] self @raise [ArgumentError] if already populated
read
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def source if @source.nil? raise RuntimeError, 'Cannot extract source from uninitialized Source::Buffer' end @source end
# Source code contained in this buffer. @return [String] source code @raise [RuntimeError] if buffer is not populated yet
source
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def decompose_position(position) line_index = line_index_for_position(position) line_begin = line_begins[line_index] [ @first_line + line_index , position - line_begin ] end
# Convert a character index into the source to a `[line, column]` tuple. @param [Integer] position @return [[Integer, Integer]] `[line, column]`
decompose_position
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def line_for_position(position) line_index_for_position(position) + @first_line end
# Convert a character index into the source to a line number. @param [Integer] position @return [Integer] line @api private
line_for_position
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def column_for_position(position) line_index = line_index_for_position(position) position - line_begins[line_index] end
# Convert a character index into the source to a column number. @param [Integer] position @return [Integer] column @api private
column_for_position
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def source_lines @lines ||= begin lines = @source.lines.to_a lines << ''.dup if @source.end_with?("\n".freeze) lines.each do |line| line.chomp!("\n".freeze) line.freeze end lines.freeze end end
# Return an `Array` of source code lines. @return [Array<String>]
source_lines
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def source_line(lineno) source_lines.fetch(lineno - @first_line).dup end
# Extract line `lineno` from source, taking `first_line` into account. @param [Integer] lineno @return [String] @raise [IndexError] if `lineno` is out of bounds
source_line
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def line_range(lineno) index = lineno - @first_line if index < 0 || index + 1 >= line_begins.size raise IndexError, 'Parser::Source::Buffer: range for line ' \ "#{lineno} requested, valid line numbers are #{@first_line}.." \ "#{@first_line + line_begins.size - 2}" else Range.new(self, line_begins[index], line_begins[index + 1] - 1) end end
# Extract line `lineno` as a new `Range`, taking `first_line` into account. @param [Integer] lineno @return [Range] @raise [IndexError] if `lineno` is out of bounds
line_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def source_range @source_range ||= Range.new(self, 0, source.size) end
# @return [Range] A range covering the whole source
source_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def last_line line_begins.size + @first_line - 2 end
# Number of last line in the buffer @return [Integer]
last_line
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def line_index_for_position(position) @line_index_for_position[position] || begin index = bsearch(line_begins, position) - 1 @line_index_for_position[position] = index unless @line_index_for_position.frozen? index end end
@returns 0-based line index of position
line_index_for_position
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/buffer.rb
Apache-2.0
def type if text.start_with?("#".freeze) :inline elsif text.start_with?("=begin".freeze) :document end end
# Type of this comment. * Inline comments correspond to `:inline`: # whatever * Block comments correspond to `:document`: =begin hi i am a document =end @return [Symbol]
type
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
Apache-2.0
def inline? type == :inline end
# @see #type @return [Boolean] true if this is an inline comment.
inline?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
Apache-2.0
def document? type == :document end
# @see #type @return [Boolean] true if this is a block comment.
document?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
Apache-2.0
def inspect "#<Parser::Source::Comment #{@location.expression.to_s} #{text.inspect}>" end
# @return [String] a human-readable representation of this comment
inspect
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment.rb
Apache-2.0
def to_hash instance_variables.inject({}) do |hash, ivar| next hash if ivar.to_sym == :@node hash[ivar[1..-1].to_sym] = instance_variable_get(ivar) hash end end
# Converts this source map to a hash with keys corresponding to ranges. For example, if called on an instance of {Collection}, which adds the `begin` and `end` ranges, the resulting hash will contain keys `:expression`, `:begin` and `:end`. @example require 'parser/current' p Parser::CurrentRuby.parse('[1, 2]').loc.to_hash # => { # :begin => #<Source::Range (string) 0...1>, # :end => #<Source::Range (string) 5...6>, # :expression => #<Source::Range (string) 0...6> # } @return [Hash<Symbol, Parser::Source::Range>]
to_hash
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/map.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/map.rb
Apache-2.0
def initialize(source_buffer, begin_pos, end_pos) if end_pos < begin_pos raise ArgumentError, 'Parser::Source::Range: end_pos must not be less than begin_pos' end if source_buffer.nil? raise ArgumentError, 'Parser::Source::Range: source_buffer must not be nil' end @source_buffer = source_buffer @begin_pos, @end_pos = begin_pos, end_pos freeze end
# @param [Buffer] source_buffer @param [Integer] begin_pos @param [Integer] end_pos
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def begin with(end_pos: @begin_pos) end
# @return [Range] a zero-length range located just before the beginning of this range.
begin
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def end with(begin_pos: @end_pos) end
# @return [Range] a zero-length range located just after the end of this range.
end
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def size @end_pos - @begin_pos end
# @return [Integer] amount of characters included in this range.
size
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def column_range if line != last_line raise RangeError, "#{self.inspect} spans more than one line" end column...last_column end
# @return [::Range] a range of columns spanned by this range. @raise RangeError
column_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def source @source_buffer.slice(@begin_pos, @end_pos - @begin_pos) end
# @return [String] all source code covered by this range.
source
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def to_s line, column = @source_buffer.decompose_position(@begin_pos) [@source_buffer.name, line, column + 1].join(':') end
# Composes a GNU/Clang-style string representation of the beginning of this range. For example, for the following range in file `foo.rb`, def foo ^^^ `to_s` will return `foo.rb:1:5`. Note that the column index is one-based. @return [String]
to_s
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def with(begin_pos: @begin_pos, end_pos: @end_pos) Range.new(@source_buffer, begin_pos, end_pos) end
# @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos @return [Range] the same range as this range but with the given end point(s) changed to the given value(s).
with
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def adjust(begin_pos: 0, end_pos: 0) Range.new(@source_buffer, @begin_pos + begin_pos, @end_pos + end_pos) end
# @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos @return [Range] the same range as this range but with the given end point(s) adjusted by the given amount(s)
adjust
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def resize(new_size) with(end_pos: @begin_pos + new_size) end
# @param [Integer] new_size @return [Range] a range beginning at the same point as this range and length `new_size`.
resize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def join(other) Range.new(@source_buffer, [@begin_pos, other.begin_pos].min, [@end_pos, other.end_pos].max) end
# @param [Range] other @return [Range] smallest possible range spanning both this range and `other`.
join
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def intersect(other) unless disjoint?(other) Range.new(@source_buffer, [@begin_pos, other.begin_pos].max, [@end_pos, other.end_pos].min) end end
# @param [Range] other @return [Range] overlapping region of this range and `other`, or `nil` if they do not overlap
intersect
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def disjoint?(other) if empty? && other.empty? @begin_pos != other.begin_pos else @begin_pos >= other.end_pos || other.begin_pos >= @end_pos end end
# Return `true` iff this range and `other` are disjoint. Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing? @param [Range] other @return [Boolean]
disjoint?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def contains?(other) (other.begin_pos <=> @begin_pos) + (@end_pos <=> other.end_pos) >= (other.empty? ? 2 : 1) end
# Returns true iff this range contains (strictly) `other`. Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing? @param [Range] other @return [Boolean]
contains?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def crossing?(other) return false unless overlaps?(other) (@begin_pos <=> other.begin_pos) * (@end_pos <=> other.end_pos) == 1 end
# Returns true iff both ranges intersect and also have different elements from one another. Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing? @param [Range] other @return [Boolean]
crossing?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def empty? @begin_pos == @end_pos end
# Checks if a range is empty; if it contains no characters @return [Boolean]
empty?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def hash [@source_buffer, @begin_pos, @end_pos].hash end
# Support for Ranges be used in as Hash indices and in Sets.
hash
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def inspect "#<Parser::Source::Range #{@source_buffer.name} #{@begin_pos}...#{@end_pos}>" end
# @return [String] a human-readable representation of this range.
inspect
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/range.rb
Apache-2.0
def initialize(source_buffer) self.class.warn_of_deprecation @diagnostics = Diagnostic::Engine.new @diagnostics.consumer = lambda do |diag| $stderr.puts diag.render end @source_buffer = source_buffer @queue = [] @clobber = 0 @insertions = 0 # clobbered zero-length positions; index 0 is the far left @insert_before_multi_order = 0 @insert_after_multi_order = 0 @pending_queue = nil @pending_clobber = nil @pending_insertions = nil end
# @param [Source::Buffer] source_buffer @deprecated Use {TreeRewriter}
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def remove(range) append Rewriter::Action.new(range, ''.freeze) end
# Removes the source range. @param [Range] range @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#remove}
remove
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def insert_before(range, content) append Rewriter::Action.new(range.begin, content) end
# Inserts new code before the given source range. @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#insert_before}
insert_before
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def wrap(range, before, after) append Rewriter::Action.new(range.begin, before) append Rewriter::Action.new(range.end, after) end
# Inserts new code before and after the given source range. @param [Range] range @param [String] before @param [String] after @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#wrap}
wrap
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def insert_before_multi(range, content) @insert_before_multi_order -= 1 append Rewriter::Action.new(range.begin, content, true, @insert_before_multi_order) end
# Inserts new code before the given source range by allowing other insertions at the same position. Note that an insertion with latter invocation comes _before_ earlier insertion at the same position in the rewritten source. @example Inserting '[(' rewriter. insert_before_multi(range, '('). insert_before_multi(range, '['). process @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#insert_before}
insert_before_multi
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def insert_after(range, content) append Rewriter::Action.new(range.end, content) end
# Inserts new code after the given source range. @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#insert_after}
insert_after
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def insert_after_multi(range, content) @insert_after_multi_order += 1 append Rewriter::Action.new(range.end, content, true, @insert_after_multi_order) end
# Inserts new code after the given source range by allowing other insertions at the same position. Note that an insertion with latter invocation comes _after_ earlier insertion at the same position in the rewritten source. @example Inserting ')]' rewriter. insert_after_multi(range, ')'). insert_after_multi(range, ']'). process @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#insert_after}
insert_after_multi
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def replace(range, content) append Rewriter::Action.new(range, content) end
# Replaces the code of the source range `range` with `content`. @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected @deprecated Use {TreeRewriter#replace}
replace
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def process if in_transaction? raise "Do not call #{self.class}##{__method__} inside a transaction" end adjustment = 0 source = @source_buffer.source.dup @queue.sort.each do |action| begin_pos = action.range.begin_pos + adjustment end_pos = begin_pos + action.range.length source[begin_pos...end_pos] = action.replacement adjustment += (action.replacement.length - action.range.length) end source end
# Applies all scheduled changes to the `source_buffer` and returns modified source as a new string. @return [String] @deprecated Use {TreeRewriter#process}
process
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def transaction unless block_given? raise "#{self.class}##{__method__} requires block" end if in_transaction? raise 'Nested transaction is not supported' end @pending_queue = @queue.dup @pending_clobber = @clobber @pending_insertions = @insertions yield @queue = @pending_queue @clobber = @pending_clobber @insertions = @pending_insertions self ensure @pending_queue = nil @pending_clobber = nil @pending_insertions = nil end
# Provides a protected block where a sequence of multiple rewrite actions are handled atomically. If any of the actions failed by clobbering, all the actions are rolled back. @example begin rewriter.transaction do rewriter.insert_before(range_of_something, '(') rewriter.insert_after(range_of_something, ')') end rescue Parser::ClobberingError end @raise [RuntimeError] when no block is passed @raise [RuntimeError] when already in a transaction @deprecated Use {TreeRewriter#transaction}
transaction
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def append(action) range = action.range # Is this an insertion? if range.empty? # Replacing nothing with... nothing? return self if action.replacement.empty? if !action.allow_multiple_insertions? && (conflicting = clobbered_insertion?(range)) raise_clobber_error(action, [conflicting]) end record_insertion(range) if (adjacent = adjacent_updates?(range)) conflicting = adjacent.find do |a| a.range.overlaps?(range) && !replace_compatible_with_insertion?(a, action) end raise_clobber_error(action, [conflicting]) if conflicting merge_actions!(action, adjacent) else active_queue << action end else # It's a replace or remove operation. if (insertions = adjacent_insertions?(range)) insertions.each do |insertion| if range.overlaps?(insertion.range) && !replace_compatible_with_insertion?(action, insertion) raise_clobber_error(action, [insertion]) else action = merge_actions(action, [insertion]) active_queue.delete(insertion) end end end if (adjacent = adjacent_updates?(range)) if can_merge?(action, adjacent) record_replace(range) merge_actions!(action, adjacent) else raise_clobber_error(action, adjacent) end else record_replace(range) active_queue << action end end self end
Schedule a code update. If it overlaps with another update, check whether they conflict, and raise a clobbering error if they do. (As a special case, zero-length ranges at the same position are considered to "overlap".) Otherwise, merge them. Updates which are adjacent to each other, but do not overlap, are also merged. RULES: - Insertion ("replacing" a zero-length range): - Two insertions at the same point conflict. This is true even if the earlier insertion has already been merged with an adjacent update, and even if they are both inserting the same text. - An insertion never conflicts with a replace or remove operation on its right or left side, which does not overlap it (in other words, which does not update BOTH its right and left sides). - An insertion always conflicts with a remove operation which spans both its sides. - An insertion conflicts with a replace operation which spans both its sides, unless the replacement text is longer than the replaced text by the size of the insertion (or more), and the portion of replacement text immediately after the insertion position is identical to the inserted text. - Removal operations never conflict with each other. - Replacement operations: - Take the portion of each replacement text which falls within: - The other operation's replaced region - The other operation's replacement text, if it extends past the end of its own replaced region (in other words, if the replacement text is longer than the text it replaces) - If and only if the taken texts are identical for both operations, they do not conflict.
append
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/rewriter.rb
Apache-2.0
def merge!(with) raise 'TreeRewriter are not for the same source_buffer' unless source_buffer == with.source_buffer @action_root = @action_root.combine(with.action_root) self end
# Merges the updates of argument with the receiver. Policies of the receiver are used. This action is atomic in that it won't change the receiver unless it succeeds. @param [Rewriter] with @return [Rewriter] self @raise [ClobberingError] when clobbering is detected
merge!
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def import!(foreign_rewriter, offset: 0) return self if foreign_rewriter.empty? contracted = foreign_rewriter.action_root.contract merge_effective_range = ::Parser::Source::Range.new( @source_buffer, contracted.range.begin_pos + offset, contracted.range.end_pos + offset, ) check_range_validity(merge_effective_range) merge_with = contracted.moved(@source_buffer, offset) @action_root = @action_root.combine(merge_with) self end
# For special cases where one needs to merge a rewriter attached to a different source_buffer or that needs to be offset. Policies of the receiver are used. @param [TreeRewriter] rewriter from different source_buffer @param [Integer] offset @return [Rewriter] self @raise [IndexError] if action ranges (once offset) don't fit the current buffer
import!
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def replace(range, content) combine(range, replacement: content) end
# Replaces the code of the source range `range` with `content`. @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected
replace
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def wrap(range, insert_before, insert_after) combine(range, insert_before: insert_before.to_s, insert_after: insert_after.to_s) end
# Inserts the given strings before and after the given range. @param [Range] range @param [String, nil] insert_before @param [String, nil] insert_after @return [Rewriter] self @raise [ClobberingError] when clobbering is detected
wrap
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def remove(range) replace(range, ''.freeze) end
# Shortcut for `replace(range, '')` @param [Range] range @return [Rewriter] self @raise [ClobberingError] when clobbering is detected
remove
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def insert_before(range, content) wrap(range, content, nil) end
# Shortcut for `wrap(range, content, nil)` @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected
insert_before
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def insert_after(range, content) wrap(range, nil, content) end
# Shortcut for `wrap(range, nil, content)` @param [Range] range @param [String] content @return [Rewriter] self @raise [ClobberingError] when clobbering is detected
insert_after
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def process source = @source_buffer.source chunks = [] last_end = 0 @action_root.ordered_replacements.each do |range, replacement| chunks << source[last_end...range.begin_pos] << replacement last_end = range.end_pos end chunks << source[last_end...source.length] chunks.join end
# Applies all scheduled changes to the `source_buffer` and returns modified source as a new string. @return [String]
process
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def transaction unless block_given? raise "#{self.class}##{__method__} requires block" end previous = @in_transaction @in_transaction = true restore_root = @action_root yield restore_root = nil self ensure @action_root = restore_root if restore_root @in_transaction = previous end
# Provides a protected block where a sequence of multiple rewrite actions are handled atomically. If any of the actions failed by clobbering, all the actions are rolled back. Transactions can be nested. @raise [RuntimeError] when no block is passed
transaction
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def insert_before_multi(range, text) self.class.warn_of_deprecation insert_before(range, text) end
# @api private @deprecated Use insert_after or wrap
insert_before_multi
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def insert_after_multi(range, text) self.class.warn_of_deprecation insert_after(range, text) end
# @api private @deprecated Use insert_after or wrap
insert_after_multi
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter.rb
Apache-2.0
def initialize(ast, comments) @ast = ast @comments = comments @skip_directives = true end
# @param [Parser::AST::Node] ast @param [Array<Parser::Source::Comment>] comments
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
Apache-2.0
def associate @map_using = :eql do_associate end
# Compute a mapping between AST nodes and comments. Comment is associated with the node, if it is one of the following types: - preceding comment, it ends before the node start - sparse comment, it is located inside the node, after all child nodes - decorating comment, it starts at the same line, where the node ends This rule is unambiguous and produces the result one could reasonably expect; for example, this code # foo hoge # bar + fuga will result in the following association: { (send (lvar :hoge) :+ (lvar :fuga)) => [#<Parser::Source::Comment (string):2:1 "# foo">], (lvar :fuga) => [#<Parser::Source::Comment (string):3:8 "# bar">] } Note that comments after the end of the end of a passed tree range are ignored (except root decorating comment). Note that {associate} produces unexpected result for nodes which are equal but have distinct locations; comments for these nodes are merged. You may prefer using {associate_by_identity} or {associate_locations}. @return [Hash<Parser::AST::Node, Array<Parser::Source::Comment>>] @deprecated Use {associate_locations}.
associate
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
Apache-2.0
def associate_locations @map_using = :location do_associate end
# Same as {associate}, but compares by identity, thus producing an unambiguous result even in presence of equal nodes. @return [Hash<Parser::Source::Node, Array<Parser::Source::Comment>>]
associate_locations
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
Apache-2.0
def associate_by_identity @map_using = :identity do_associate end
# Same as {associate}, but uses `node.loc` instead of `node` as the hash key, thus producing an unambiguous result even in presence of equal nodes. @return [Hash<Parser::Source::Map, Array<Parser::Source::Comment>>]
associate_by_identity
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/comment/associator.rb
Apache-2.0
def contract raise 'Empty actions can not be contracted' if empty? return self if insertion? range = @range.with( begin_pos: children.first.range.begin_pos, end_pos: children.last.range.end_pos, ) with(range: range) end
# A root action has its range set to the whole source range, even though it typically do not act on that range. This method returns the action as if it was a child action with its range contracted. @return [Action]
contract
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
Apache-2.0
def moved(source_buffer, offset) moved_range = ::Parser::Source::Range.new( source_buffer, @range.begin_pos + offset, @range.end_pos + offset ) with( range: moved_range, children: children.map { |child| child.moved(source_buffer, offset) } ) end
# @return [Action] that has been moved to the given source_buffer and with the given offset No check is done on validity of resulting range.
moved
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
Apache-2.0
def do_combine(action) if action.range == @range merge(action) else place_in_hierarchy(action) end end
Assumes range.contains?(action.range) && action.children.empty?
do_combine
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
Apache-2.0
def bsearch_child_index(from = 0) size = @children.size (from...size).bsearch { |i| yield @children[i] } || size end
Similar to @children.bsearch_index || size except allows for a starting point and `bsearch_index` is only Ruby 2.3+
bsearch_child_index
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
Apache-2.0
def analyse_hierarchy(action) r = action.range # left_index is the index of the first child that isn't completely to the left of action left_index = bsearch_child_index { |child| child.range.end_pos > r.begin_pos } # right_index is the index of the first child that is completely on the right of action start = left_index == 0 ? 0 : left_index - 1 # See "corner case" below for reason of -1 right_index = bsearch_child_index(start) { |child| child.range.begin_pos >= r.end_pos } center = right_index - left_index case center when 0 # All children are disjoint from action, nothing else to do when -1 # Corner case: if a child has empty range == action's range # then it will appear to be both disjoint and to the left of action, # as well as disjoint and to the right of action. # Since ranges are equal, we return it as parent left_index -= 1 # Fix indices, as otherwise this child would be right_index += 1 # considered as a sibbling (both left and right!) parent = @children[left_index] else overlap_left = @children[left_index].range.begin_pos <=> r.begin_pos overlap_right = @children[right_index-1].range.end_pos <=> r.end_pos # For one child to be the parent of action, we must have: if center == 1 && overlap_left <= 0 && overlap_right >= 0 parent = @children[left_index] else # Otherwise consider all non disjoint elements (center) to be contained... contained = @children[left_index...right_index] fusible = check_fusible(action, (contained.shift if overlap_left < 0), # ... but check first and last one (contained.pop if overlap_right > 0) # ... for overlaps ) end end { parent: parent, sibbling_left: @children[0...left_index], sibbling_right: @children[[email protected]], fusible: fusible, child: contained, } end
Returns the children in a hierarchy with respect to `action`: :sibbling_left, sibbling_right (for those that are disjoint from `action`) :parent (in case one of our children contains `action`) :child (in case `action` strictly contains some of our children) :fusible (in case `action` overlaps some children but they can be fused in one deletion) or raises a `CloberingError` In case a child has equal range to `action`, it is returned as `:parent` Reminder: an empty range 1...1 is considered disjoint from 1...10
analyse_hierarchy
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
Apache-2.0
def merge(action) call_enforcer_for_merge(action) with( insert_before: "#{action.insert_before}#{insert_before}", replacement: action.replacement || @replacement, insert_after: "#{insert_after}#{action.insert_after}", ).combine_children(action.children) end
Assumes action.range == range && action.children.empty?
merge
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/parser-3.2.2.1/lib/parser/source/tree_rewriter/action.rb
Apache-2.0
def initialize(path) return @path = path if path.is_a?(String) return @path = path.to_path if path.respond_to?(:to_path) return @path = path.to_s end
-- @note A lot of this class can be compatible with Pathname. Initialize a new instance. @return Pathutil --
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/pathutil-0.16.2/lib/pathutil.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/pathutil-0.16.2/lib/pathutil.rb
Apache-2.0
def cleanpath(symlink = false) symlink ? conservative_cleanpath : aggressive_cleanpath end
-- @see Pathname#cleanpath. @note This is a wholesale rip and cleanup of Pathname#cleanpath @return Pathutil --
cleanpath
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/pathutil-0.16.2/lib/pathutil.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/pathutil-0.16.2/lib/pathutil.rb
Apache-2.0
def search_backwards(file, backwards: Float::INFINITY) ary = [] ascend.with_index(1).each do |path, index| if index > backwards break else Dir.chdir path do if block_given? file = self.class.new(file) if yield(file) ary.push( file ) end elsif File.exist?(file) ary.push(self.class.new( path.join(file) )) end end end end ary end
-- @yield Pathutil @note It will return all results that it finds across all ascending paths. @example Pathutil.new("~/").expand_path.search_backwards(".bashrc") => [#<Pathutil:/home/user/.bashrc>] Search backwards for a file (like Rakefile, _config.yml, opts.yml). @return Enum --
search_backwards
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/pathutil-0.16.2/lib/pathutil.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/pathutil-0.16.2/lib/pathutil.rb
Apache-2.0