Class: MixTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/tokenizer.rb,
lib/ruby/new_tokenizer.rb

Overview

Copyright © 2011-2012 Jesse Sielaff

Instance Method Summary collapse

Instance Method Details

#access!Object



11
12
13
# File 'lib/ruby/new_tokenizer.rb', line 11

def access!
  @state = :access
end

#access?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/ruby/new_tokenizer.rb', line 7

def access?
  @state == :access
end

#access_tokenObject



15
16
17
# File 'lib/ruby/new_tokenizer.rb', line 15

def access_token
  add_token '·' + @token
end

#add_token(token, value = token) ⇒ Object



19
20
21
# File 'lib/ruby/new_tokenizer.rb', line 19

def add_token (token, value = token)
  @token_stream << [token, [value, [@filename, @line]]]
end

#adjust_indentObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby/new_tokenizer.rb', line 23

def adjust_indent
  current_indent = @token.length
  previous_indent = @indent_stack.last
  
  if current_indent > previous_indent
    indent_token
  elsif current_indent < previous_indent
    unless @indent_stack.include?(current_indent)
      raise SyntaxError, "Indent error: #{@filename}:#{@line}"
    end
    
    outdent_token until @indent_stack.last == current_indent
  end
end

#comparison_tokenObject



38
39
40
# File 'lib/ruby/new_tokenizer.rb', line 38

def comparison_token
  add_token @token + '='
end

#double_token?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/ruby/new_tokenizer.rb', line 42

def double_token?
  lookahead?(@token) do
    @token *= 2
    yield
  end
end

#getchObject



7
8
9
# File 'lib/ruby/tokenizer.rb', line 7

def getch
  @script[@pointer += 1]
end

#handle_tokenObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ruby/new_tokenizer.rb', line 53

def handle_token
  case @token
    when ?~
      lookahead?(?~) { skip_to_end_of_line } || skip_whitespace
    
    when ' ', ?\t
      no_access!
    
    when ?\n
      return if line_empty?
      
      @token = ''
      scan_token(/ /)
      return if lookahead?(?\n) { @empty_lines += 1; unscan }
      
      newline_token
      adjust_indent
    
    when ??, ?., ?;, ?,, ?(, ?{
      no_access!
      plain_token
    
    when ?], ?}, ?)
      access!
      plain_token
    
    when ?[, ?:, ?#
      (access?) ? access_token : plain_token
      no_access!
    
    when ?!, ?=
      no_access!
      lookahead?(?=) { comparison_token } || plain_token
    
    when ?+, ?*, ?/, ?%
      no_access!
      lookahead?(?=) { operator_assignment_token } || plain_token
    
    when ?-
      return if lookahead?(?=) { operator_assignment_token }
      return if lookahead?(/[0-9]/) { negative_token; unscan }
      
      (unary_minus_possible?) ? unary_minus_token : plain_token
      no_access!
    
    when ?>, ?<
      no_access!
      return if double_token? { lookahead?(?=) { operator_assignment_token } || plain_token }
      lookahead?(?=) { comparison_token } || plain_token
    
    when ?&, ?|
      no_access!
      return if double_token? { lookahead?(?=) { operator_assignment_token } || plain_token }
      plain_token
    
    when /[0-9]/
      access!
      scan_token(/\./)
      scan_token(/[0-9]/)
      number_token
    
    when /[A-Z]/
      no_access!
      scan_token(/[_a-zA-Z0-9]/)
      mixin_token
    
    when ?'
      access!
      @token = ""
      scan_token(/[^']/)
      getch
      
      lookahead?(?:) do
        return if lookahead?(/[^a-z]/) do
          no_access!
          key_token
        end
      end
      
      string_token
    
    when /[_a-z]/
      access!
      scan_token(/[_a-zA-Z0-9]/)
      scan_token(/[?!]/)
      
      lookahead?(?:) do
        return if lookahead?(/[^a-z]/) do
          no_access!
          key_token
        end
      end
      
      (keyword?) ? keyword_token : identifier_token
      
      case @token = getch
        when ?:, ?#
          access_token
        when ?[
          access_token
          no_access!
        else
          unscan
      end
    
    else
      raise SyntaxError, "Invalid token `#{@token}': #{@filename}:#{@line}"
  end
end

#identifier_tokenObject



163
164
165
# File 'lib/ruby/new_tokenizer.rb', line 163

def identifier_token
  add_token :IDENTIFIER, @token.to_sym
end

#indent_tokenObject



167
168
169
170
# File 'lib/ruby/new_tokenizer.rb', line 167

def indent_token
  @indent_stack << @token.length
  add_token :INDENT
end

#key_tokenObject



172
173
174
# File 'lib/ruby/new_tokenizer.rb', line 172

def key_token
  add_token :KEY, @token.to_sym
end

#keyword?Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
182
# File 'lib/ruby/new_tokenizer.rb', line 176

def keyword?
  %w|app break case
    elsif else false
    if null return
    self switch true
    unless until while|.include? @token
end

#keyword_tokenObject



184
185
186
# File 'lib/ruby/new_tokenizer.rb', line 184

def keyword_token
  add_token @token.upcase.to_sym
end

#line_empty?Boolean

Returns:

  • (Boolean)


188
189
190
191
192
193
# File 'lib/ruby/new_tokenizer.rb', line 188

def line_empty?
  if @state == :newline
    @empty_lines += 1
    return true
  end
end

#lookahead?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
219
# File 'lib/ruby/new_tokenizer.rb', line 211

def lookahead? (pattern)
  if getch =~ Regexp.new(pattern)
    yield
    true
  else
    unscan
    false
  end
end

#mixin_tokenObject



195
196
197
# File 'lib/ruby/new_tokenizer.rb', line 195

def mixin_token
  add_token :MIXIN, @token.to_sym
end

#negative_tokenObject



199
200
201
# File 'lib/ruby/new_tokenizer.rb', line 199

def negative_token
  add_token :NEGATIVE
end

#newline_tokenObject



203
204
205
206
207
208
209
# File 'lib/ruby/new_tokenizer.rb', line 203

def newline_token
  add_token :NEWLINE
  
  @line += (@empty_lines + 1)
  @empty_lines = 0
  @state = :newline
end

#no_access!Object



221
222
223
# File 'lib/ruby/new_tokenizer.rb', line 221

def no_access!
  @state = :no_access
end

#number_tokenObject



225
226
227
# File 'lib/ruby/new_tokenizer.rb', line 225

def number_token
  add_token :NUMBER, @token.to_i
end

#operator_assignment_tokenObject



229
230
231
# File 'lib/ruby/new_tokenizer.rb', line 229

def operator_assignment_token
  add_token '·=', @token
end

#outdent_tokenObject



233
234
235
236
# File 'lib/ruby/new_tokenizer.rb', line 233

def outdent_token
  @indent_stack.pop
  add_token :OUTDENT
end

#plain_tokenObject



238
239
240
# File 'lib/ruby/new_tokenizer.rb', line 238

def plain_token
  add_token @token
end

#scan_token(pattern) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/ruby/new_tokenizer.rb', line 242

def scan_token (pattern)
  while (c = getch) =~ pattern
    @token << c
  end
  
  unscan
end

#skip_to_end_of_lineObject



250
251
252
253
# File 'lib/ruby/new_tokenizer.rb', line 250

def skip_to_end_of_line
  :skip until getch =~ /\n/
  unscan
end

#skip_whitespaceObject



255
256
257
258
# File 'lib/ruby/new_tokenizer.rb', line 255

def skip_whitespace
  :skip while getch =~ /\s/
  unscan
end

#string_tokenObject



260
261
262
# File 'lib/ruby/new_tokenizer.rb', line 260

def string_token
  add_token :STRING, @token
end

#token(token, value = token) ⇒ Object



11
12
13
# File 'lib/ruby/tokenizer.rb', line 11

def token (token, value = token)
  @tokens << [token, [value, [@filename, @line]]]
end

#tokenize(filename, script, token_stream) ⇒ Object



15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/ruby/tokenizer.rb', line 15

def tokenize (filename, script, tokens)
  @filename = filename
  @script = script
  @tokens = tokens
  
  @line = 1
  @pointer = -1
  @indent_stack = [0]
  @state = :newline
  
  empty_lines = 0
  
  while c = getch
    case c
      when ?~
        if getch == ?~
          :skip until getch =~ /\n/
          unscan
          next
        end
        
        unscan
        :skip while getch =~ /\s/
        unscan
      
      when ' ', ?\t
        @state = :space if @state == :reference
      
      when ?\n
        if @state == :newline
          empty_lines += 1
          next
        end
        
        current_indent = 0
        previous_indent = @indent_stack.last
        
        while (c = getch) == ' '
          current_indent += 1
        end
        
        unscan
        
        if c == ?\n
          empty_lines += 1
          next
        end
        
        @state = :newline
        token :NEWLINE
        
        @line += (empty_lines + 1)
        empty_lines = 0
        
        if current_indent > previous_indent
          @indent_stack << current_indent
          token :INDENT
        elsif current_indent < previous_indent
          unless @indent_stack.include?(current_indent)
            raise(SyntaxError, "Indent error: #{@filename}:#{@line}")
          end
          
          until @indent_stack.last == current_indent
            @indent_stack.pop
            token :OUTDENT
          end
        end
      
      when ?!, ?=
        @state = :begin
        
        if getch == ?=
          token c + '='
        else
          unscan
          token c
        end
      
      when ?+, ?*, ?/, ?%
        @state = :begin
        
        if getch == ?=
          token '·=', c
        else
          unscan
          token c
        end
      
      when ?-
        if getch == ?=
          token '·=', c
        elsif c =~ /[0-9]/
          token :NEGATIVE
          unscan
        else
          unscan
          
          if @state == :begin || @state == :newline
            token '-·'
          else
            token '-'
          end
        end
        
        @state = :begin
      
      when ??, ?., ?;, ?,, ?(, ?{
        @state = :begin
        token c
      
      when ?'
        @state = :reference
        
        s = ""
        
        until (c = getch) == ?'
          s << c
        end
        
        token :STRING, s
      
      when ?>, ?<
        @state = :begin
        
        if getch == c
          if getch == ?=
            token '·=', c * 2
          else
            unscan
            token c * 2
          end
        else
          unscan
          
          if getch == ?=
            token c + '='
          else
            unscan
            token c
          end
        end
      
      when ?&, ?|
        @state = :begin
        
        if getch == c
          if getch == ?=
            token c + c + '='
          else
            unscan
            token c + c
          end
        else
          unscan
          token c
        end
      
      when /[0-9]/
        @state = :reference
        value = c
        
        while (c = getch) =~ /[0-9]/
          value << c
        end
        
        unscan
        
        token :NUMBER, value.to_i
      
      when ?], ?}, ?)
        @state = :reference
        token c
      
      when ?:, ?[, ?#
        if @state == :reference
          token '·' + c
        else
          token c
        end
        
        @state = :begin
      
      when /[A-Z]/
        @state = :begin
        value = c
        
        while (c = getch) =~ /[_a-zA-Z0-9]/
          value << c
        end
        
        unscan
        
        token :MIXIN, value.to_sym
      
      when /[_a-z]/
        @state = :reference
        value = c
        
        while (c = getch) =~ /[_a-zA-Z0-9]/
          value << c
        end
        
        if c == ?:
          if getch =~ /[a-z]/
            unscan
          else
            @state = :begin
            token :KEY, value.to_sym
            unscan
            next
          end
          
          unscan
        elsif c == ?? || c == ?!
          value << c
        else
          unscan
        end
        
        if %w:
          app break case
          elsif else false if
          null return self
          switch true unless
          until while
        :
        .include?(value)
          token value.upcase.to_sym
        else
          token :IDENTIFIER, value.to_sym
        end
        
        case c = getch
          when ?:, ?#
            token '·' + c
          when ?[
            token '·['
            @state = :begin
          else
            unscan
        end
      
      else
        raise SyntaxError, "Invalid token `#{c}': #{@filename}:#{@line}"
    end
  end
  
  @tokens
end

#unary_minus_possible?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/ruby/new_tokenizer.rb', line 279

def unary_minus_possible?
  @state == :begin || @state == :newline
end

#unary_minus_tokenObject



283
284
285
# File 'lib/ruby/new_tokenizer.rb', line 283

def unary_minus_token
  add_token '-·'
end

#unscanObject



265
266
267
# File 'lib/ruby/tokenizer.rb', line 265

def unscan
  @pointer -= 1
end