Class: NewickTokenizer

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

Overview

Splits a Newick tree string into tokens that NewickTree uses

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ NewickTokenizer

Returns a new instance of NewickTokenizer.



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

def initialize(str)
  @str = str
  @pos = 0
end

Instance Method Details

#nextCharObject

returns the next character in the string and updates position



29
30
31
32
33
34
35
36
37
# File 'lib/Newick.rb', line 29

def nextChar
  if (@pos < @str.size)
    c = @str[@pos].chr
    @pos += 1
    return c
  else
    return nil
  end
end

#nextTokenObject

returns the next token in the string and updates position



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/Newick.rb', line 40

def nextToken
  c = nextChar
  if (c == " " || c == "\n" || c == "\r")
    return nextToken
  elsif (c == "(" || c == ")" || c == ',')
    return NewickToken.new("SYMBOL", c)
  elsif (c == ":")
    if (@str.index((/([0-9|\.|\-|e|E]+)/), @pos) == @pos)
	@pos += $1.length
	return NewickToken.new("WEIGHT", $1)
    else
	raise NewickParseError, "Illegal weight at pos #{@pos} of #{@str}"
    end
  elsif (c == "'")
    if (@str.index(/(\'[^\']*\')/, @pos - 1) == @pos - 1)
	@pos += $1.length - 1
	return NewickToken.new("LABEL", $1)
    else
	raise NewickParseError, "Illegal label at pos #{@pos} of #{@str}"
    end
  elsif (@str.index(/([^,():]+)/, @pos - 1) == @pos - 1)
    @pos += $1.length - 1
    return NewickToken.new("LABEL", $1)
  end
end

#peekTokenObject

returms the next token in the string without changing position



67
68
69
70
71
72
# File 'lib/Newick.rb', line 67

def peekToken
  origPos = @pos
  token = nextToken
  @pos = origPos
  return token
end