Class: Riemann::Tools::Utils::StringTokenizer
- Inherits:
-
Object
- Object
- Riemann::Tools::Utils::StringTokenizer
- Defined in:
- lib/riemann/tools/utils.rb
Instance Attribute Summary collapse
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #eos? ⇒ Boolean
-
#initialize(text) ⇒ StringTokenizer
constructor
A new instance of StringTokenizer.
- #matched ⇒ Object
- #next_line ⇒ Object
- #push_token(token, value = nil) ⇒ Object
- #scan(expression) ⇒ Object
- #unexpected_token ⇒ Object
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
#tokens ⇒ Object (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
24 25 26 |
# File 'lib/riemann/tools/utils.rb', line 24 def eos? @scanner.eos? end |
#matched ⇒ Object
28 29 30 |
# File 'lib/riemann/tools/utils.rb', line 28 def matched @scanner.matched end |
#next_line ⇒ Object
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_token ⇒ Object
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 |