Class: JSGF::Tokenizer

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

Constant Summary collapse

TOKENS =
{
    /grammar/       => :GRAMMAR,
    /#JSGF/       => :HEADER,
    /import/        => :IMPORT,
    /public/        => :PUBLIC,
    /\{(\\.|[^\}]+)*\}/     => :TAG,
    /[^ \t\n;=|*+<>\(\)\[\]{}*\/]+/ => :TOKEN,
    %r{/\d*(\.\d+)?/}     => :WEIGHT,
}

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Tokenizer



16
17
18
19
# File 'lib/jsgf/tokenizer.rb', line 16

def initialize(io)
    io = io.read unless io.is_a?(String)
    @scanner = StringScanner.new(io)
end

Instance Method Details

#next_tokenObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jsgf/tokenizer.rb', line 21

def next_token
    return if @scanner.eos?

    @scanner.skip(/\s+/)  # Skip all leading whitespace
    @scanner.skip(%r{//.*\n}) # Skip single-line comments

    TOKENS.each do |(pattern, token)|
  text = @scanner.scan(pattern)
  return [token, text] if text
    end
    x = @scanner.getch
    [x, x] unless x.nil?
end