class SheepAst::Stage

Handle order delegation of analyze to Ast Maanger

@api private

rubocop: disable all

Attributes

ast[RW]
info[RW]
match_id_array[RW]
match_symbol_array[RW]

Public Class Methods

new(ast) click to toggle source
Calls superclass method SheepAst::Log::new
# File lib/sheep_ast/stage_manager.rb, line 36
def initialize(ast)
  super()
  @ast = ast
  init
  @committed_node_id = 0
end

Public Instance Methods

analyze(data) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 66
def analyze(data) # rubocop: disable all
  node = @ast.node_factory.from_id(T.must(info).node_id)
  ldebug? and ldebug "analyze start, node_id = #{T.must(info).node_id}, object_id =  #{node.object_id}, data = #{data.inspect}"
  ret_node_info = @ast.find_next_node(data, node)
  ldebug? and ldebug "#{name} matched! => info = #{ret_node_info.inspect} for data = #{data.inspect}"

  put_stack(ret_node_info)
  data.stack = @match_id_array.dup
  data.stack_symbol = @match_symbol_array.dup

  after_action = MatchAction::Abort

  case ret_node_info.status
  when MatchStatus::NotFound
    if match_id_array.length.zero?
      after_action = @ast.not_found(data, node)
    else
      after_action = @ast.not_found_in_progress(data, node)
    end
  when MatchStatus::ConditionMatchingStart
    after_action = @ast.condition_start(data, node)
  when MatchStatus::ConditionMatchingProgress
    after_action = @ast.condition_in_progress(data, node)
  when MatchStatus::MatchingProgress
    after_action = @ast.in_progress(data, node)
  when MatchStatus::ConditionEndButMatchingProgress
    after_action = @ast.condition_end_but_in_progress(data, node)
  when MatchStatus::AtEnd, MatchStatus::ConditionMatchingAtEnd
    node = @ast.node_factory.from_id(T.must(ret_node_info).node_id)
    application_error 'node is not got' if node.nil?

    ldebug? and ldebug "Invoking action my node id = #{node.my_id}"
    after_action = @ast.at_end(data, node)
  else
    application_error "Unknown status #{ret_node_info.status}"
  end

  return handle_after_action(after_action, data, ret_node_info)
end
commit_node() click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 117
def commit_node
  @committed_node_id = T.must(@info).node_id
end
copy(other) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 192
def copy(other)
  T.must(@info).copy(T.must(other.info))
  @match_id_array = other.match_id_array.dup
  @match_symbol_array = other.match_symbol_array.dup
  return self
end
current_node() click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 44
def current_node
  node = @ast.node_factory.from_id(T.must(info).node_id)
  if node.nil?
    application_error 'current node is not found. Bug?'
  end
  return node
end
dump_stack(logs) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 175
def dump_stack(logs)
  logf = method(logs)
  str = ''.dup
  match_id_array.each do |id|
    a_match = @ast.match_factory.from_id(id)
    application_error "match from id=#{id} not found" if a_match.nil?

    str += "#{a_match.matched_expr.inspect} => "
  end
  4.times { str.chop! }
  if str.empty? || str.nil?
    str = 'None'
  end
  logf.call str, :yellow
end
dump_tree(logs) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 170
def dump_tree(logs)
  ast.dump_tree(logs)
end
handle_after_action(after_action, data, info) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 123
def handle_after_action(after_action, data, info) # rubocop: disable all
  ldebug? and ldebug "#{name} decided to #{after_action.inspect} for the '#{data.expr.inspect}'"
  case after_action
  when MatchAction::Abort
    expression_not_found "'#{data.expr.inspect}'"
  when MatchAction::LazyAbort
    return MatchResult::NotFound
  when MatchAction::StayNode
    return MatchResult::GetNext
  when MatchAction::Next
    move_node(info)
    return MatchResult::GetNext
  when MatchAction::Continue
    init
    return MatchResult::Continue
  when MatchAction::Finish
    move_node(info)
    save_req = nil
    if !data.save_request.nil?
      save_req = data.save_request
      data.save_request = nil
    end
    init
    handle_save_request(data, save_req) unless save_req.nil?

    return MatchResult::Finish
  else
    application_error "Match action #{after_action} is not defined"
  end
end
handle_save_request(data, save_req) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 155
def handle_save_request(data, save_req)
  ldebug? and ldebug "handle_save_request save_req = #{save_req.inspect}"
  T.must(data.file_manager).register_next_chunk(T.must(save_req.chunk)) unless save_req.chunk.nil?
  T.must(data.file_manager).register_next_file(T.must(save_req.file)) unless save_req.file.nil?
  T.must(data.file_manager).ast_include_set(save_req.ast_include)
  T.must(data.file_manager).ast_exclude_set(save_req.ast_exclude)
  T.must(data.file_manager).put_namespace(save_req.namespace)
end
init() click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 215
def init
  ldebug? and ldebug "#{name.inspect} init the node_info now. the info was #{@info.inspect}, match_id_array =>"\
    " #{@match_id_array.inspect}, match_stack => #{@match_symbol_array.inspect}"
  @info = NodeInfo.new if @info.nil?
  @info.init
  @match_id_array = []
  @match_symbol_array = []
end
inspect() click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 208
def inspect
  "custom inspect <#{self.class.name} object_id = #{object_id}, ast = #{@ast.inspect},"\
    " info = #{@info.inspect}, match_id_array = #{@match_id_array.inspect},"\
    " match_symbol_array = #{@match_symbol_array.inspect} >"
end
move_committed_node() click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 111
def move_committed_node
  node_info = NodeInfo.new
  node_info.node_id = @committed_node_id
  move_node(node_info)
end
move_node(info) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 107
def move_node(info)
  T.must(@info).copy(info)
end
name() click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 165
def name
  return ast.full_name
end
put_stack(info) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 53
def put_stack(info)
  if info.status != MatchStatus::NotFound &&
     info.status != MatchStatus::ConditionMatchingProgress &&
     info.status != MatchStatus::ConditionMatchingAtEnd &&
     info.status != MatchStatus::ConditionEndButMatchingProgress
    @match_id_array << info.match_id
    @match_symbol_array << info.store_symbol
    ldebug? and ldebug "match_id_array = #{@match_id_array.inspect}"
    ldebug? and ldebug "match_symbol = #{@match_symbol_array.inspect}"
  end
end
save(other) click to toggle source
# File lib/sheep_ast/stage_manager.rb, line 200
def save(other)
  T.must(@info).copy(T.must(other.info))
  @match_id_array = []
  @match_symbol_array = []
  return self
end