Class: RuboCop::AST::NodePattern
- Inherits:
-
Object
- Object
- RuboCop::AST::NodePattern
- Extended by:
- Forwardable
- Includes:
- MethodDefiner
- Defined in:
- lib/rubocop/ast/node_pattern.rb,
lib/rubocop/ast/node_pattern/node.rb,
lib/rubocop/ast/node_pattern/sets.rb,
lib/rubocop/ast/node_pattern/lexer.rb,
lib/rubocop/ast/node_pattern/parser.rb,
lib/rubocop/ast/node_pattern/builder.rb,
lib/rubocop/ast/node_pattern/comment.rb,
lib/rubocop/ast/node_pattern/compiler.rb,
lib/rubocop/ast/node_pattern/with_meta.rb,
lib/rubocop/ast/node_pattern/parser.racc.rb,
lib/rubocop/ast/node_pattern/compiler/debug.rb,
lib/rubocop/ast/node_pattern/method_definer.rb,
lib/rubocop/ast/node_pattern/compiler/binding.rb,
lib/rubocop/ast/node_pattern/compiler/subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/atom_subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb
Overview
This class performs a pattern-matching operation on an AST node.
Detailed syntax: /docs/modules/ROOT/pages/node_pattern.adoc
Initialize a new ‘NodePattern` with `NodePattern.new(pattern_string)`, then pass an AST node to `NodePattern#match`. Alternatively, use one of the class macros in `NodePattern::Macros` to define your own pattern-matching method.
If the match fails, ‘nil` will be returned. If the match succeeds, the return value depends on whether a block was provided to `#match`, and whether the pattern contained any “captures” (values which are extracted from a matching AST.)
-
With block: #match yields the captures (if any) and passes the return
value of the block through.
-
With no block, but one capture: the capture is returned.
-
With no block, but multiple captures: captures are returned as an array.
-
With no block and no captures: #match returns ‘true`.
Defined Under Namespace
Modules: Macros, MethodDefiner, Sets Classes: Builder, Comment, Compiler, Lexer, LexerRex, Node, Parser
Constant Summary collapse
- Invalid =
Class.new(StandardError)
- VAR =
'node'
Instance Attribute Summary collapse
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
-
#match_code ⇒ Object
readonly
Returns the value of attribute match_code.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Class Method Summary collapse
-
.descend(element) {|element| ... } ⇒ Object
Yields its argument and any descendants, depth-first.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#as_json(_options = nil) ⇒ Object
:nodoc:.
-
#encode_with(coder) ⇒ Object
:nodoc:.
- #freeze ⇒ Object
-
#init_with(coder) ⇒ Object
:nodoc:.
-
#initialize(str, compiler: Compiler.new) ⇒ NodePattern
constructor
A new instance of NodePattern.
-
#marshal_dump ⇒ Object
:nodoc:.
-
#marshal_load(pattern) ⇒ Object
:nodoc:.
- #match(*args, **rest, &block) ⇒ Object
- #to_s ⇒ Object
Methods included from MethodDefiner
#as_lambda, #compile_as_lambda, #def_node_matcher, #def_node_search
Constructor Details
#initialize(str, compiler: Compiler.new) ⇒ NodePattern
Returns a new instance of NodePattern.
61 62 63 64 65 66 67 |
# File 'lib/rubocop/ast/node_pattern.rb', line 61 def initialize(str, compiler: Compiler.new) @pattern = str @ast = compiler.parser.parse(str) @compiler = compiler @match_code = @compiler.compile_as_node_pattern(@ast, var: VAR) @cache = {} end |
Instance Attribute Details
#ast ⇒ Object (readonly)
Returns the value of attribute ast.
57 58 59 |
# File 'lib/rubocop/ast/node_pattern.rb', line 57 def ast @ast end |
#match_code ⇒ Object (readonly)
Returns the value of attribute match_code.
57 58 59 |
# File 'lib/rubocop/ast/node_pattern.rb', line 57 def match_code @match_code end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
57 58 59 |
# File 'lib/rubocop/ast/node_pattern.rb', line 57 def pattern @pattern end |
Class Method Details
.descend(element) {|element| ... } ⇒ Object
Yields its argument and any descendants, depth-first.
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubocop/ast/node_pattern.rb', line 105 def self.descend(element, &block) return to_enum(__method__, element) unless block yield element if element.is_a?(::RuboCop::AST::Node) element.children.each do |child| descend(child, &block) end end nil end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
74 75 76 |
# File 'lib/rubocop/ast/node_pattern.rb', line 74 def ==(other) other.is_a?(NodePattern) && other.ast == ast end |
#as_json(_options = nil) ⇒ Object
:nodoc:
91 92 93 |
# File 'lib/rubocop/ast/node_pattern.rb', line 91 def as_json( = nil) # :nodoc: pattern end |
#encode_with(coder) ⇒ Object
:nodoc:
95 96 97 |
# File 'lib/rubocop/ast/node_pattern.rb', line 95 def encode_with(coder) # :nodoc: coder['pattern'] = pattern end |
#freeze ⇒ Object
119 120 121 122 123 |
# File 'lib/rubocop/ast/node_pattern.rb', line 119 def freeze @match_code.freeze @compiler.freeze super end |
#init_with(coder) ⇒ Object
:nodoc:
99 100 101 |
# File 'lib/rubocop/ast/node_pattern.rb', line 99 def init_with(coder) # :nodoc: initialize(coder['pattern']) end |
#marshal_dump ⇒ Object
:nodoc:
87 88 89 |
# File 'lib/rubocop/ast/node_pattern.rb', line 87 def marshal_dump # :nodoc: pattern end |
#marshal_load(pattern) ⇒ Object
:nodoc:
83 84 85 |
# File 'lib/rubocop/ast/node_pattern.rb', line 83 def marshal_load(pattern) # :nodoc: initialize pattern end |
#match(*args, **rest, &block) ⇒ Object
69 70 71 72 |
# File 'lib/rubocop/ast/node_pattern.rb', line 69 def match(*args, **rest, &block) @cache[:lambda] ||= as_lambda @cache[:lambda].call(*args, block: block, **rest) end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/rubocop/ast/node_pattern.rb', line 79 def to_s "#<#{self.class} #{pattern}>" end |