Class: Habaki::FormalSyntax::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/habaki/formal_syntax.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, value = nil, children = [], occurence = 1..1) ⇒ Node

Returns a new instance of Node.



16
17
18
19
20
21
# File 'lib/habaki/formal_syntax.rb', line 16

def initialize(type = nil, value = nil, children = [], occurence = 1..1)
  @type = type
  @value = value
  @children = children
  @occurence = occurence
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/habaki/formal_syntax.rb', line 9

def children
  @children
end

#occurenceObject

Returns the value of attribute occurence.



12
13
14
# File 'lib/habaki/formal_syntax.rb', line 12

def occurence
  @occurence
end

#origObject

Returns the value of attribute orig.



14
15
16
# File 'lib/habaki/formal_syntax.rb', line 14

def orig
  @orig
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/habaki/formal_syntax.rb', line 8

def parent
  @parent
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/habaki/formal_syntax.rb', line 10

def type
  @type
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/habaki/formal_syntax.rb', line 11

def value
  @value
end

Instance Method Details

#nObject



50
51
52
# File 'lib/habaki/formal_syntax.rb', line 50

def n
  Float::INFINITY
end

#occurence_from_children!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/habaki/formal_syntax.rb', line 35

def occurence_from_children!
  case @type
  when :and
    occ_min = 0
    occ_max = 0
    @children.each do |child|
      occ_min += child.occurence.begin
      occ_max += child.occurence.end
    end
    @occurence = Range.new(occ_min, occ_max)
  when :or_and
    @occurence = Range.new(1, @children.length)
  end
end

#push_children(child) ⇒ Object



23
24
25
26
# File 'lib/habaki/formal_syntax.rb', line 23

def push_children(child)
  child.parent = self
  @children << child
end

#to_sObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/habaki/formal_syntax.rb', line 54

def to_s
  str = ""
  case @type
  when :and
    str += "[#{@children.map(&:to_s).join(" ")}]"
  when :or
    str += "[#{@children.map(&:to_s).join(" | ")}]"
  when :or_and
    str += "[#{@children.map(&:to_s).join(" || ")}]"
  when :function
    str += "#{@value}(#{@children.map(&:to_s).join(" ")})"
  when :number
    str += @value.to_s
  when :token
    str += @value
  when :ref
    str += "<'#{@value}'>"
  when :type
    str += "<#{@value}>"
  else
    str += @value
  end

  case @occurence.begin
  when 0
    case @occurence.end
    when 1
      str += "?"
    when n
      str += "*"
    end
  when 1
    case @occurence.end
    when 1
    when n
      str += "+"
    else
      str += "{#{@occurence.begin},#{@occurence.end}}"
    end
  end

  str
end

#traverse(&block) ⇒ Object



28
29
30
31
32
33
# File 'lib/habaki/formal_syntax.rb', line 28

def traverse(&block)
  block.call self
  @children.each do |child|
    child.traverse(&block)
  end
end