Class: Riemann::Tools::Utils::StringTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/riemann/tools/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ StringTokenizer

Returns a new instance of StringTokenizer.



11
12
13
14
15
16
17
18
# File 'lib/riemann/tools/utils.rb', line 11

def initialize(text)
  @scanner = StringScanner.new(text)

  @lineno = 1
  @pos = 0
  @line = next_line
  @tokens = []
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



9
10
11
# File 'lib/riemann/tools/utils.rb', line 9

def tokens
  @tokens
end

Instance Method Details

#eos?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/riemann/tools/utils.rb', line 24

def eos?
  @scanner.eos?
end

#matchedObject



28
29
30
# File 'lib/riemann/tools/utils.rb', line 28

def matched
  @scanner.matched
end

#next_lineObject



32
33
34
# File 'lib/riemann/tools/utils.rb', line 32

def next_line
  (@scanner.check_until(/\n/) || @scanner.rest).chomp
end

#push_token(token, value = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/riemann/tools/utils.rb', line 36

def push_token(token, value = nil)
  value ||= @scanner.matched

  if value == "\n"
    @lineno += 1
    @line = next_line
    @pos = pos = 0
  else
    pos = @pos
    @pos += @scanner.matched.length
  end

  @tokens << [token, { value: value, line: @line, lineno: @lineno, pos: pos }] if token
end

#scan(expression) ⇒ Object



20
21
22
# File 'lib/riemann/tools/utils.rb', line 20

def scan(expression)
  @scanner.scan(expression)
end

#unexpected_tokenObject

Raises:

  • (Racc::ParseError)


51
52
53
# File 'lib/riemann/tools/utils.rb', line 51

def unexpected_token
  raise(Racc::ParseError, "unexpected data on line #{@lineno}:\n#{@line}\n#{' ' * @pos}^")
end