Class: RuboCop::AST::NodePattern
- Inherits:
-
Object
- Object
- RuboCop::AST::NodePattern
- Extended by:
- SimpleForwardable
- 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 SimpleForwardable
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.
78 79 80 81 82 83 84 |
# File 'lib/rubocop/ast/node_pattern.rb', line 78 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.
74 75 76 |
# File 'lib/rubocop/ast/node_pattern.rb', line 74 def ast @ast end |
#match_code ⇒ Object (readonly)
Returns the value of attribute match_code.
74 75 76 |
# File 'lib/rubocop/ast/node_pattern.rb', line 74 def match_code @match_code end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
74 75 76 |
# File 'lib/rubocop/ast/node_pattern.rb', line 74 def pattern @pattern end |
Class Method Details
.descend(element) {|element| ... } ⇒ Object
Yields its argument and any descendants, depth-first.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rubocop/ast/node_pattern.rb', line 60 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?
91 92 93 |
# File 'lib/rubocop/ast/node_pattern.rb', line 91 def ==(other) other.is_a?(NodePattern) && other.ast == ast end |
#as_json(_options = nil) ⇒ Object
:nodoc:
108 109 110 |
# File 'lib/rubocop/ast/node_pattern.rb', line 108 def as_json( = nil) # :nodoc: pattern end |
#encode_with(coder) ⇒ Object
:nodoc:
112 113 114 |
# File 'lib/rubocop/ast/node_pattern.rb', line 112 def encode_with(coder) # :nodoc: coder['pattern'] = pattern end |
#freeze ⇒ Object
120 121 122 123 124 |
# File 'lib/rubocop/ast/node_pattern.rb', line 120 def freeze @match_code.freeze @compiler.freeze super end |
#init_with(coder) ⇒ Object
:nodoc:
116 117 118 |
# File 'lib/rubocop/ast/node_pattern.rb', line 116 def init_with(coder) # :nodoc: initialize(coder['pattern']) end |
#marshal_dump ⇒ Object
:nodoc:
104 105 106 |
# File 'lib/rubocop/ast/node_pattern.rb', line 104 def marshal_dump # :nodoc: pattern end |
#marshal_load(pattern) ⇒ Object
:nodoc:
100 101 102 |
# File 'lib/rubocop/ast/node_pattern.rb', line 100 def marshal_load(pattern) # :nodoc: initialize pattern end |
#match(*args, **rest, &block) ⇒ Object
86 87 88 89 |
# File 'lib/rubocop/ast/node_pattern.rb', line 86 def match(*args, **rest, &block) @cache[:lambda] ||= as_lambda @cache[:lambda].call(*args, block: block, **rest) end |
#to_s ⇒ Object
96 97 98 |
# File 'lib/rubocop/ast/node_pattern.rb', line 96 def to_s "#<#{self.class} #{pattern}>" end |