Class: Kumi::Parser::SmartTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/parser/smart_tokenizer.rb

Overview

Context-aware tokenizer that produces tokens with embedded semantic metadata

Instance Method Summary collapse

Constructor Details

#initialize(source, source_file: '<input>') ⇒ SmartTokenizer

Returns a new instance of SmartTokenizer.



11
12
13
14
15
16
17
18
19
# File 'lib/kumi/parser/smart_tokenizer.rb', line 11

def initialize(source, source_file: '<input>')
  @source = source
  @source_file = source_file
  @pos = 0
  @line = 1
  @column = 1
  @context_stack = [:global]
  @tokens = []
end

Instance Method Details

#tokenizeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kumi/parser/smart_tokenizer.rb', line 21

def tokenize
  while @pos < @source.length
    skip_whitespace_except_newlines

    case current_char
    when nil then break
    when "\n" then handle_newline
    when '#' then consume_comment
    when '"', "'" then consume_string
    when /\d/ then consume_number
    when '-'
      if peek_char && peek_char.match?(/\d/)
        consume_number
      else
        consume_operator_or_punctuation
      end
    when /[a-zA-Z_]/ then consume_identifier_or_label_or_keyword
    when ':' then consume_symbol_or_colon
    else
      consume_operator_or_punctuation
    end
  end

  add_token(:eof, nil, {})
  @tokens
end