class SheepAst::MatchBase

Matcher base class

Attributes

command[RW]
description[RW]
end_index[RW]
end_line[RW]
key[R]
kind_name[R]
matched_expr[RW]
my_chain_num[RW]
my_tag[RW]
node_id[RW]
node_tag[RW]
parent_tag[RW]
start_index[RW]
start_line[RW]
store_sym[RW]

Public Class Methods

new(key = '', sym = nil, **options) click to toggle source
Calls superclass method SheepAst::Log::new
# File lib/sheep_ast/match/match_base.rb, line 78
def initialize(key = '', sym = nil, **options)
  @key = key
  @store_sym = sym
  @options = options
  @debug = options[:debug]
  @extract = options[:extract]
  @start_add_cond = options[:index_cond]
  @end_add_cond = options[:end_cond]
  @command = options[:command] || key
  @description = options[:description]
  @my_tag = options[:tag]
  super()
end

Public Instance Methods

additional_cond(data) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 149
def additional_cond(data)
  if @options[:at_head]
    if data.file_info.index != 1
      ldebug? and ldebug 'at head : false'
      return false
    end
    ldebug? and ldebug 'at head : true'
  end

  ret = iterate_cond(data, @start_add_cond)
  ldebug? and ldebug "additional_cond : #{ret.inspect}"
  return ret
end
additional_end_cond(data) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 163
def additional_end_cond(data)
  ret = iterate_cond(data, @end_add_cond)
  ldebug? and ldebug "additional_end_cond : #{ret.inspect}"
  return ret
end
dump() click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 197
def dump
  ldebug? and ldebug "matched_expr => #{@matched_expr.inspect}"
end
end_info_set(line, index) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 123
def end_info_set(line, index)
  @end_line = line
  @end_index = index
end
init() click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 186
def init; end
inspect() click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 93
def inspect
  "custom inspect: <#{self.class.name} object_id = #{object_id}, kind_name = #{@kind_name},"\
    " key = #{@key}, matched_expr = #{@matched_expr.inspect} >"
end
iterate_cond(data, cond) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 169
def iterate_cond(data, cond)
  return true if cond.nil?

  if cond.is_a? Enumerable
    cond.each do |c|
      ret = c.validate(data)
      return false if !ret
    end
  else
    ret = cond.validate(data)
    return false if !ret
  end

  return true
end
kind?() click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 189
def kind?; end
kind_name_set(name) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 67
def kind_name_set(name)
  @kind_name = name
end
match(data) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 129
def match(data)
  reg_match(@key, T.must(data.expr))
end
matched(data) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 99
def matched(data)
  expr_ = data.expr
  if @extract
    expr_ = T.must(expr_)[@extract]
  end

  if @store_sym.nil?
    @store_sym = "_#{my_chain_num}".to_sym
  end

  if @matched_expr.instance_of?(Array)
    @matched_expr.push(expr_)
  else
    @matched_expr = expr_
  end
  start_info_set(data.file_info&.line, data.file_info&.index)
  end_info_set(data.file_info&.line, data.file_info&.index)
end
node_info() click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 192
def node_info
  return NodeInfo.new(node_id: node_id, match_id: my_id, kind: kind?, store_symbol: store_sym)
end
reg_match(expr_, target_) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 134
def reg_match(expr_, target_)
  # expr = expr_.gsub(/\||\?|\*|\(|\)|\{|\}|\[|\]|\+|\./) { |word| "\\#{word}" }
  # target = target_.gsub(/\||\?|\*|\(|\)|\{|\}|\[|\]|\+|\./) { |word| "\\#{word}" }
  rg = Regexp.new expr_
  @md = rg.match(target_)
  ldebug? and ldebug @md.inspect
  if !@md.nil?
    ldebug? and ldebug 'Found'
    return true
  else
    ldebug? and ldebug 'Not Found'
    return nil
  end
end
start_info_set(line, index) click to toggle source
# File lib/sheep_ast/match/match_base.rb, line 118
def start_info_set(line, index)
  @start_line = line
  @start_index = index
end