Class: Packcr::Node::RootNode

Inherits:
Packcr::Node show all
Defined in:
lib/packcr/node/root_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

#alt, #link_references, #nodes, #reversible?, #seq, #sequence?, #setup_rule, #verify_captures, #verify_variables

Constructor Details

#initializeRootNode

Returns a new instance of RootNode.



7
8
9
10
11
# File 'lib/packcr/node/root_node.rb', line 7

def initialize
  @rules = []
  @rulehash = {}
  @implicit_rules = []
end

Instance Attribute Details

#rulehashObject (readonly)

Returns the value of attribute rulehash.



5
6
7
# File 'lib/packcr/node/root_node.rb', line 5

def rulehash
  @rulehash
end

#rulesObject

Returns the value of attribute rules.



4
5
6
# File 'lib/packcr/node/root_node.rb', line 4

def rules
  @rules
end

Instance Method Details

#debug_dumpObject



13
14
15
# File 'lib/packcr/node/root_node.rb', line 13

def debug_dump
  @rules.each(&:debug_dump)
end

#make_rulehash(ctx) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/packcr/node/root_node.rb', line 17

def make_rulehash(ctx)
  @rules.each do |rule|
    if @rulehash[rule.name]
      ctx.error rule.line + 1, rule.col + 1, "Multiple definition of rule '#{rule.name}'"
    else
      @rulehash[rule.name] = rule
    end
  end
end

#rule(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/packcr/node/root_node.rb', line 38

def rule(name)
  rule = @rulehash[name]
  return rule if rule

  case name
  when "EOF"
    expr = Packcr::Node::EofNode.new
  else
    return nil
  end
  rule = Packcr::Node::RuleNode.new(expr, name)
  @rules << rule
  @rulehash[name] = rule
end

#setup(ctx) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/packcr/node/root_node.rb', line 27

def setup(ctx)
  make_rulehash(ctx)
  @rules.first&.top = true
  @rules.each do |rule|
    rule.setup(ctx)
  end
  @rules.each do |rule|
    rule.verify(ctx)
  end
end

#to_hObject



53
54
55
56
57
58
# File 'lib/packcr/node/root_node.rb', line 53

def to_h
  {
    type: :root,
    rules: rules.map(&:to_h),
  }
end