Browse Source

Implement comments

master
Dylan Baker 5 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
 class Lexer
1
 class Lexer
2
   def initialize(source)
2
   def initialize(source)
3
-    @source = source.split("\n").join(' ')
3
+    @source = source
4
     @position = 0
4
     @position = 0
5
   end
5
   end
6
 
6
 
7
   def get_token
7
   def get_token
8
     skip_whitespace
8
     skip_whitespace
9
+    skip_comment
9
 
10
 
10
     return Token.new(TokenKinds::EOF) if at_end
11
     return Token.new(TokenKinds::EOF) if at_end
11
 
12
 
189
     end
190
     end
190
   end
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
   def at_end
201
   def at_end
193
     @position == @source.size
202
     @position == @source.size
194
   end
203
   end

+ 12
- 0
spec/lexer_spec.rb View File

166
       ]
166
       ]
167
     )
167
     )
168
   end
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
 end
181
 end

Loading…
Cancel
Save