Class: Tomlrb::Scanner

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

Constant Summary collapse

IDENTIFIER =
/\w+/
SPACES =
/\A\s+/
STRING_SINGLE =
/"[^"]*"/
STRING_MULTI =
/"[^"]*"/
DATETIME =
/(-?\d{4})-(\d{2})-(\d{2})(?:t|\s)(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(z|[-+]\d{2}:\d{2})/i
NUMBER =
/[0-9]+(?:\.[0-9]+)?/
TRUE =
/true/
FALSE =
/false/

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Scanner

Returns a new instance of Scanner.



14
15
16
# File 'lib/tomlrb/scanner.rb', line 14

def initialize(io)
  @ss = StringScanner.new(io.read)
end

Instance Method Details

#next_tokenObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tomlrb/scanner.rb', line 18

def next_token
  return if @ss.eos?

  case
  when @ss.scan(SPACES) then next_token
  when text = @ss.scan(DATETIME) then [:DATETIME, text]
  when text = @ss.scan(STRING_SINGLE) then [:STRING, text[1..-2]]
  when text = @ss.scan(NUMBER) then [:NUMBER, text]
  when text = @ss.scan(TRUE)   then [:TRUE, text]
  when text = @ss.scan(FALSE)  then [:FALSE, text]
  when text = @ss.scan(IDENTIFIER) then [:IDENTIFIER, text]
  else
    x = @ss.getch
    [x, x]
  end
end