Browse Source

Lex strings

master
Dylan Baker 6 years ago
parent
commit
64cc88f9a8
2 changed files with 25 additions and 0 deletions
  1. 16
    0
      lib/chervil/lexer.rb
  2. 9
    0
      spec/lexer_spec.rb

+ 16
- 0
lib/chervil/lexer.rb View File

@@ -23,6 +23,22 @@ module Chervil
23 23
       when ')'
24 24
         advance
25 25
         Token.new(:rparen, ")")
26
+      when '"', '\''
27
+        delimiter = current_char
28
+        advance
29
+        string = String.new
30
+        until current_char == delimiter
31
+          string << current_char
32
+          advance
33
+        end
34
+
35
+        if current_char.nil?
36
+          raise "Unterminated string"
37
+        end
38
+
39
+        advance
40
+
41
+        Token.new(:string, string)
26 42
       else
27 43
         source = @source.slice(@position..-1)
28 44
         while source.match(/^\s/)

+ 9
- 0
spec/lexer_spec.rb View File

@@ -34,5 +34,14 @@ module Chervil
34 34
         Token.new(:identifier, 'hello')
35 35
       )
36 36
     end
37
+
38
+    it 'lexes strings' do
39
+      expect(Lexer.new('"hello"').get_next_token).to eq(
40
+        Token.new(:string, 'hello')
41
+      )
42
+      expect(Lexer.new("'world'").get_next_token).to eq(
43
+        Token.new(:string, 'world')
44
+      )
45
+    end
37 46
   end
38 47
 end

Loading…
Cancel
Save