Browse Source

Implement comments

master
Dylan Baker 4 years ago
parent
commit
9d9548404f
2 changed files with 22 additions and 1 deletions
  1. 10
    1
      lib/ahem/lexer.rb
  2. 12
    0
      spec/lexer_spec.rb

+ 10
- 1
lib/ahem/lexer.rb View File

@@ -1,11 +1,12 @@
1 1
 class Lexer
2 2
   def initialize(source)
3
-    @source = source.split("\n").join(' ')
3
+    @source = source
4 4
     @position = 0
5 5
   end
6 6
 
7 7
   def get_token
8 8
     skip_whitespace
9
+    skip_comment
9 10
 
10 11
     return Token.new(TokenKinds::EOF) if at_end
11 12
 
@@ -189,6 +190,14 @@ class Lexer
189 190
     end
190 191
   end
191 192
 
193
+  def skip_comment
194
+    if @source.slice(@position..@position + 1) == '//'
195
+      @position += 2
196
+      @position += 1 until @source[@position] == "\n" || at_end
197
+      @position += 1 unless at_end
198
+    end
199
+  end
200
+
192 201
   def at_end
193 202
     @position == @source.size
194 203
   end

+ 12
- 0
spec/lexer_spec.rb View File

@@ -166,4 +166,16 @@ RSpec.describe Lexer do
166 166
       ]
167 167
     )
168 168
   end
169
+
170
+  it 'ignores comments' do
171
+    expect(Lexer.new("// prints 5\nprint(5)").scan_all).to eq(
172
+      [
173
+        Token.new(TokenKinds::IDENTIFIER, 'print'),
174
+        Token.new(TokenKinds::LPAREN),
175
+        Token.new(TokenKinds::NUMBER, 5),
176
+        Token.new(TokenKinds::RPAREN),
177
+        Token.new(TokenKinds::EOF),
178
+      ]
179
+    )
180
+  end
169 181
 end

Loading…
Cancel
Save