-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
742 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rainbow' | ||
|
||
module RuboCop | ||
module AST | ||
class NodePattern | ||
class Compiler | ||
# Variant of the Compiler with tracing information for nodes | ||
class Debug < Compiler | ||
# Compiled node pattern requires a named parameter `trace`, | ||
# which should be an instance of this class | ||
class Trace | ||
def initialize | ||
@visit = {} | ||
end | ||
|
||
def enter(node_id) | ||
@visit[node_id] = false | ||
true | ||
end | ||
|
||
def success(node_id) | ||
@visit[node_id] = true | ||
end | ||
|
||
# return nil (not visited), false (not matched) or true (matched) | ||
def matched?(node_id) | ||
@visit[node_id] | ||
end | ||
end | ||
|
||
attr_reader :node_ids | ||
|
||
# @api private | ||
class Colorizer | ||
# Result of a NodePattern run against a particular AST | ||
# Consider constructor is private | ||
Result = Struct.new(:colorizer, :trace, :returned) do # rubocop:disable Metrics/BlockLength | ||
# @return [String] a Rainbow colorized version of ruby | ||
def colorize | ||
ast.loc.expression.source_buffer.source.chars.map.with_index do |char, i| | ||
Rainbow(char).color((color_map[i] || COLORS[:not_visitable])) | ||
end.join | ||
end | ||
|
||
# @return [Hash] a map for {character_position => color} | ||
def color_map | ||
@color_map ||= | ||
match_map | ||
.map { |node, matched| color_map_for(node, matched) } | ||
.inject(:merge) | ||
end | ||
|
||
# @return [Hash] a map for {node => matched?}, depth-first | ||
def match_map | ||
@match_map ||= | ||
ast | ||
.each_descendant | ||
.to_a | ||
.prepend(ast) | ||
.to_h { |node| [node, matched?(node)] } | ||
end | ||
|
||
# @return a value of `Trace#matched?` or `:not_visitable` | ||
def matched?(node) | ||
id = colorizer.compiler.node_ids.fetch(node) { return :not_visitable } | ||
trace.matched?(id) | ||
end | ||
|
||
private | ||
|
||
COLORS = { | ||
not_visitable: :lightseagreen, | ||
nil => :yellow, | ||
false => :red, | ||
true => :green | ||
}.freeze | ||
|
||
def color_map_for(node, matched = matched?(node)) | ||
return {} unless (range = node.loc&.expression) | ||
|
||
color = COLORS.fetch(matched) | ||
range.to_a.to_h { |char| [char, color] } | ||
end | ||
|
||
def ast | ||
colorizer.node_pattern.ast | ||
end | ||
end | ||
|
||
attr_reader :pattern, :compiler, :node_pattern | ||
|
||
def initialize(pattern) | ||
@pattern = pattern | ||
@compiler = ::RuboCop::AST::NodePattern::Compiler::Debug.new | ||
@node_pattern = ::RuboCop::AST::NodePattern.new(pattern, compiler: @compiler) | ||
end | ||
|
||
# @return [Node] the Ruby AST | ||
def test(ruby) | ||
ruby = ruby_ast(ruby) if ruby.is_a?(String) | ||
trace = Trace.new | ||
returned = @node_pattern.as_lambda.call(ruby, trace: trace) | ||
Result.new(self, trace, returned) | ||
end | ||
|
||
private | ||
|
||
def ruby_ast(ruby) | ||
buffer = ::Parser::Source::Buffer.new('(ruby)', source: ruby) | ||
ruby_parser.parse(buffer) | ||
end | ||
|
||
def ruby_parser | ||
require 'parser/current' | ||
builder = ::RuboCop::AST::Builder.new | ||
::Parser::CurrentRuby.new(builder) | ||
end | ||
end | ||
|
||
def initialize | ||
super | ||
@node_ids = Hash.new { |h, k| h[k] = h.size }.compare_by_identity | ||
end | ||
|
||
def named_parameters | ||
super << :trace | ||
end | ||
|
||
def parser | ||
@parser ||= Parser::WithMeta.new | ||
end | ||
|
||
def_delegators :parser, :comments, :tokens | ||
|
||
# @api private | ||
module InstrumentationSubcompiler | ||
def do_compile | ||
"#{tracer(:enter)} && #{super} && #{tracer(:success)}" | ||
end | ||
|
||
private | ||
|
||
def tracer(kind) | ||
id = compiler.node_ids[node] | ||
"trace.#{kind}(#{id})" | ||
end | ||
end | ||
|
||
# @api private | ||
class NodePatternSubcompiler < Compiler::NodePatternSubcompiler | ||
include InstrumentationSubcompiler | ||
end | ||
|
||
# @api private | ||
class SequenceSubcompiler < Compiler::SequenceSubcompiler | ||
include InstrumentationSubcompiler | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
class NodePattern | ||
class Parser | ||
# Overrides Parser to use `WithMeta` variants and provide additional methods | ||
class WithMeta < Parser | ||
# Overrides Lexer to token locations and comments | ||
class Lexer < NodePattern::Lexer | ||
attr_reader :source_buffer | ||
|
||
def initialize(str_or_buffer) | ||
@source_buffer = if str_or_buffer.respond_to?(:source) | ||
str_or_buffer | ||
else | ||
::Parser::Source::Buffer.new('(string)', source: str_or_buffer) | ||
end | ||
@comments = [] | ||
super(@source_buffer.source) | ||
end | ||
|
||
def token(type, value) | ||
super(type, [value, pos]) | ||
end | ||
|
||
def emit_comment | ||
@comments << Comment.new(pos) | ||
super | ||
end | ||
|
||
# @return [::Parser::Source::Range] last match's position | ||
def pos | ||
::Parser::Source::Range.new(source_buffer, ss.pos - ss.matched_size, ss.pos) | ||
end | ||
end | ||
|
||
# Overrides Builder to emit nodes with locations | ||
class Builder < NodePattern::Builder | ||
def emit_atom(type, token) | ||
value, loc = token | ||
begin_l = loc.resize(1) | ||
end_l = loc.end.adjust(begin_pos: -1) | ||
begin_l = nil if begin_l.source.match?(/\w/) | ||
end_l = nil if end_l.source.match?(/\w/) | ||
n(type, [value], source_map(token, begin_t: begin_l, end_t: end_l)) | ||
end | ||
|
||
def emit_unary_op(type, operator_t = nil, *children) | ||
children[-1] = children[-1].first if children[-1].is_a?(Array) # token? | ||
map = source_map(children.first.loc.expression, operator_t: operator_t) | ||
n(type, children, map) | ||
end | ||
|
||
def emit_list(type, begin_t, children, end_t) | ||
expr = children.first.loc.expression.join(children.last.loc.expression) | ||
map = source_map(expr, begin_t: begin_t, end_t: end_t) | ||
n(type, children, map) | ||
end | ||
|
||
def emit_call(type, selector_t, args = nil) | ||
selector, = selector_t | ||
begin_t, arg_nodes, end_t = args | ||
|
||
map = source_map(selector_t, begin_t: begin_t, end_t: end_t, selector_t: selector_t) | ||
n(type, [selector, *arg_nodes], map) | ||
end | ||
|
||
private | ||
|
||
def n(type, children, source_map) | ||
super(type, children, { location: source_map }) | ||
end | ||
|
||
def loc(token_or_range) | ||
return token_or_range[1] if token_or_range.is_a?(Array) | ||
|
||
token_or_range | ||
end | ||
|
||
def join_exprs(left_expr, right_expr) | ||
left_expr.loc.expression | ||
.join(right_expr.loc.expression) | ||
end | ||
|
||
def source_map(token_or_range, begin_t: nil, end_t: nil, operator_t: nil, selector_t: nil) | ||
expression_l = loc(token_or_range) | ||
expression_l = expression_l.expression if expression_l.respond_to?(:expression) | ||
locs = [begin_t, end_t, operator_t, selector_t].map { |token| loc(token) } | ||
begin_l, end_l, operator_l, selector_l = locs | ||
|
||
expression_l = locs.compact.inject(expression_l, :join) | ||
|
||
::Parser::Source::Map::Send.new(_dot_l = nil, selector_l, begin_l, end_l, expression_l) | ||
.with_operator(operator_l) | ||
end | ||
end | ||
|
||
attr_reader :comments, :tokens | ||
|
||
def do_parse | ||
r = super | ||
@comments = @lexer.comments | ||
@tokens = @lexer.tokens | ||
r | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'parse_helper' | ||
|
||
Failure = Struct.new(:expected, :actual) | ||
|
||
module NodePatternHelper | ||
include ParseHelper | ||
|
||
def assert_equal(expected, actual, mess = nil) | ||
expect(actual).to eq(expected), *mess | ||
end | ||
|
||
def assert(test, mess = nil) | ||
expect(test).to eq(true), *mess | ||
end | ||
|
||
def expect_parsing(ast, source, source_maps) | ||
version = '-' | ||
try_parsing(ast, source, parser, source_maps, version) | ||
end | ||
end | ||
|
||
RSpec.shared_context 'parser' do | ||
let(:parser) { RuboCop::AST::NodePattern::Parser.new } | ||
include NodePatternHelper | ||
|
||
let(:parser) { RuboCop::AST::NodePattern::Parser::WithMeta.new } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.