Browse Source

Make hashes their own token type

master
Dylan Baker 5 years ago
parent
commit
8da2f2da25
4 changed files with 14 additions and 6 deletions
  1. 0
    1
      lib/ahem/ast/operators.rb
  2. 3
    3
      lib/ahem/lexer.rb
  3. 1
    0
      lib/ahem/token_kinds.rb
  4. 10
    2
      spec/lexer_spec.rb

+ 0
- 1
lib/ahem/ast/operators.rb View File

@@ -11,5 +11,4 @@ module AST::Operators
11 11
   OR = :or
12 12
   AND = :and
13 13
   NOT = :not
14
-  HASH = :'#'
15 14
 end

+ 3
- 3
lib/ahem/lexer.rb View File

@@ -110,6 +110,9 @@ class Lexer
110 110
     elsif source.match(/\A=>/)
111 111
       @position += 2
112 112
       Token.new(TokenKinds::ROCKET)
113
+    elsif source.match(/\A\#/)
114
+      @position += 1
115
+      Token.new(TokenKinds::HASH)      
113 116
     elsif source.match(/\A!/)
114 117
       @position += 1
115 118
       Token.new(TokenKinds::OPERATOR, :!)
@@ -128,9 +131,6 @@ class Lexer
128 131
     elsif source.match(/\A\>/)
129 132
       @position += 1
130 133
       Token.new(TokenKinds::OPERATOR, :>)
131
-    elsif source.match(/\A\#/)
132
-      @position += 1
133
-      Token.new(TokenKinds::OPERATOR, :'#')
134 134
     elsif source.match(/\Aand/)
135 135
       @position += 3
136 136
       Token.new(TokenKinds::OPERATOR, :and)

+ 1
- 0
lib/ahem/token_kinds.rb View File

@@ -11,6 +11,7 @@ module TokenKinds
11 11
   EQUALS = :equals
12 12
   FOR = :for
13 13
   FUNCTION = :function
14
+  HASH = :hash
14 15
   IDENTIFIER = :identifier
15 16
   IF = :if
16 17
   IN = :in

+ 10
- 2
spec/lexer_spec.rb View File

@@ -32,7 +32,7 @@ RSpec.describe Lexer do
32 32
   end
33 33
 
34 34
   it 'lexes operators' do
35
-    expect(Lexer.new('+-*/<<=>>===# and or not').scan_all).to eq(
35
+    expect(Lexer.new('+-*/<<=>>=== and or not').scan_all).to eq(
36 36
       [
37 37
         Token.new(TokenKinds::OPERATOR, :+),
38 38
         Token.new(TokenKinds::OPERATOR, :-),
@@ -43,7 +43,6 @@ RSpec.describe Lexer do
43 43
         Token.new(TokenKinds::OPERATOR, :>),
44 44
         Token.new(TokenKinds::OPERATOR, :>=),
45 45
         Token.new(TokenKinds::OPERATOR, :==),
46
-        Token.new(TokenKinds::OPERATOR, :'#'),
47 46
         Token.new(TokenKinds::OPERATOR, :and),
48 47
         Token.new(TokenKinds::OPERATOR, :or),
49 48
         Token.new(TokenKinds::OPERATOR, :not),
@@ -179,4 +178,13 @@ RSpec.describe Lexer do
179 178
       ]
180 179
     )
181 180
   end
181
+
182
+  it 'lexes hashes' do
183
+    expect(Lexer.new("#").scan_all).to eq(
184
+      [
185
+        Token.new(TokenKinds::HASH),
186
+        Token.new(TokenKinds::EOF)
187
+      ]
188
+    )
189
+  end
182 190
 end

Loading…
Cancel
Save