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.
77 78 79 80 81 82 83 |
# File 'lib/rubocop/ast/node_pattern.rb', line 77 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.
73 74 75 |
# File 'lib/rubocop/ast/node_pattern.rb', line 73 def ast @ast end |
#match_code ⇒ Object (readonly)
Returns the value of attribute match_code.
73 74 75 |
# File 'lib/rubocop/ast/node_pattern.rb', line 73 def match_code @match_code end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
73 74 75 |
# File 'lib/rubocop/ast/node_pattern.rb', line 73 def pattern @pattern end |
Class Method Details
.descend(element) {|element| ... } ⇒ Object
Yields its argument and any descendants, depth-first.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rubocop/ast/node_pattern.rb', line 59 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?
90 91 92 |
# File 'lib/rubocop/ast/node_pattern.rb', line 90 def ==(other) other.is_a?(NodePattern) && other.ast == ast end |
#as_json(_options = nil) ⇒ Object
:nodoc:
107 108 109 |
# File 'lib/rubocop/ast/node_pattern.rb', line 107 def as_json( = nil) # :nodoc: pattern end |
#encode_with(coder) ⇒ Object
:nodoc:
111 112 113 |
# File 'lib/rubocop/ast/node_pattern.rb', line 111 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:
115 116 117 |
# File 'lib/rubocop/ast/node_pattern.rb', line 115 def init_with(coder) # :nodoc: initialize(coder['pattern']) end |
#marshal_dump ⇒ Object
:nodoc:
103 104 105 |
# File 'lib/rubocop/ast/node_pattern.rb', line 103 def marshal_dump # :nodoc: pattern end |
#marshal_load(pattern) ⇒ Object
:nodoc:
99 100 101 |
# File 'lib/rubocop/ast/node_pattern.rb', line 99 def marshal_load(pattern) # :nodoc: initialize pattern end |
#match(*args, **rest, &block) ⇒ Object
85 86 87 88 |
# File 'lib/rubocop/ast/node_pattern.rb', line 85 def match(*args, **rest, &block) @cache[:lambda] ||= as_lambda @cache[:lambda].call(*args, block: block, **rest) end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/rubocop/ast/node_pattern.rb', line 95 def to_s "#<#{self.class} #{pattern}>" end |