Class: Token

Inherits:
Array show all
Defined in:
lib/magic_parser.rb

Direct Known Subclasses

Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#assert_all_types, #contains?, #find, #prep_insert

Constructor Details

#initialize(n = nil, t = nil) ⇒ Token


18
19
20
21
22
23
24
# File 'lib/magic_parser.rb', line 18

def initialize n = nil, t = nil
  check_args [n, t], [[String, NilClass], [String, NilClass]]

  @name, @text, @@tab = n, t, 0

  update
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


16
17
18
# File 'lib/magic_parser.rb', line 16

def name
  @name
end

#textObject

Returns the value of attribute text


16
17
18
# File 'lib/magic_parser.rb', line 16

def text
  @text
end

Instance Method Details


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/magic_parser.rb', line 30

def print verbose = false
  out = ''

  if name || verbose
    val = @@tab
    if !verbose then val -= 1 end
    out += "\t" * val
    out += "#{name} |#{text}|\n"
  end

  @@tab += 1
  each { |s|
    out += s.print verbose
  }
  @@tab -= 1

  out
end

26
27
28
# File 'lib/magic_parser.rb', line 26

def print_verbose
  print true
end

#to_sObject


49
50
51
# File 'lib/magic_parser.rb', line 49

def to_s
  update
end

#updateObject


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/magic_parser.rb', line 53

def update
  if length == 0
    return text
  end

  text = ''

  each { |t|
    text += t.update
  }

  text
end

#write(filename = nil) ⇒ Object


67
68
69
# File 'lib/magic_parser.rb', line 67

def write filename = nil
  write_file( filename ){ to_s }
end