123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern::Parser

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::SimpleForwardable, Racc::Parser
Instance Chain:
self, Racc::Parser
Inherits: Racc::Parser
  • Object
Defined in: lib/rubocop/ast/node_pattern/parser.rb,
lib/rubocop/ast/node_pattern/with_meta.rb

Overview

Parser for NodePattern Note: class reopened in parser.racc

Doc on how this fits in the compiling process: /docs/modules/ROOT/pages/node_pattern.adoc

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(builder = self.class::Builder.new) ⇒ Parser

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 19

def initialize(builder = self.class::Builder.new)
  super()
  @builder = builder
end

Instance Method Details

#enforce_unary(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 50

def enforce_unary(node)
  return node if node.arity == 1

  detail = node.loc&.expression&.source || node.to_s
  raise NodePattern::Invalid, 'parse error, expected unary node pattern ' \
                              "but got expression matching multiple elements: #{detail}"
end

#inspect

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 40

def inspect
  "<##{self.class}>"
end

#on_error(token, val, _vstack) (private)

Overrides Racc::Parser’s method:

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 59

def on_error(token, val, _vstack)
  detail = token_to_str(token) || '?'
  raise NodePattern::Invalid, "parse error on value #{val.inspect} (#{detail})"
end

#parse(source) ⇒ NodePattern::Node

(Similar API to parser gem) Parses a source and returns the AST.

Parameters:

  • source_buffer (Parser::Source::Buffer, String)

    The source buffer to parse.

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 31

def parse(source)
  @lexer = self.class::Lexer.new(source)
  do_parse
rescue Lexer::Error => e
  raise NodePattern::Invalid, e.message
ensure
  @lexer = nil # Don't keep references
end