Ver código fonte

Fix lexer whitespace issues

master
Dylan Baker 5 anos atrás
pai
commit
bd283f0913
1 arquivos alterados com 8 adições e 7 exclusões
  1. 8
    7
      lib/chervil/lexer.rb

+ 8
- 7
lib/chervil/lexer.rb Ver arquivo

14
     end
14
     end
15
 
15
 
16
     def get_next_token
16
     def get_next_token
17
+      while @source.slice(@position..-1).match(/^\s/)
18
+        advance
19
+      end
20
+
17
       case current_char
21
       case current_char
18
       when nil
22
       when nil
19
         Token.new(:eof, "eof")
23
         Token.new(:eof, "eof")
41
         Token.new(:string, string)
45
         Token.new(:string, string)
42
       else
46
       else
43
         source = @source.slice(@position..-1)
47
         source = @source.slice(@position..-1)
44
-        while source.match(/^\s/)
45
-          advance
46
-          source = @source.slice(@position..-1)
47
-        end
48
-
49
         if match = source.match(/^[0-9]+(\.[0-9]+)?/)
48
         if match = source.match(/^[0-9]+(\.[0-9]+)?/)
50
           advance(match[0].size)
49
           advance(match[0].size)
51
           Token.new(:number, match[0])
50
           Token.new(:number, match[0])
52
-        elsif match = source.match(/^[a-z!$%&*\/:<=>?~_^+][a-z0-9@!$%&*\/:<=>?~_^,\-]*/)
51
+        elsif match = source.match(/^[a-z!$%&*\/:<=>?~_^+\-][a-z0-9@!$%&*\/:<=>?~_^+\-]*/)
53
           advance(match[0].size)
52
           advance(match[0].size)
54
           Token.new(:identifier, match[0])
53
           Token.new(:identifier, match[0])
54
+        else
55
+          raise "Unrecognized character #{current_char}"
55
         end
56
         end
56
       end
57
       end
57
     end
58
     end
60
       tokens = Array.new
61
       tokens = Array.new
61
       loop do
62
       loop do
62
         token = get_next_token
63
         token = get_next_token
63
-        tokens << token
64
+        tokens << token unless token.nil?
64
         break if token.type == :eof
65
         break if token.type == :eof
65
       end
66
       end
66
       tokens
67
       tokens

Carregando…
Cancelar
Salvar