Browse Source

Use \A instead of ^

^ matches the beginning of every line. Given the string
function() {
}
using /^\{/ will match the final } because it's at the beginning of the
line
master
Dylan Baker 5 years ago
parent
commit
858d7a095b
1 changed files with 46 additions and 45 deletions
  1. 46
    45
      lib/ahem/lexer.rb

+ 46
- 45
lib/ahem/lexer.rb View File

@@ -10,26 +10,27 @@ class Lexer
10 10
     @position += 1 while !at_end && @source.slice(@position).match(/\s/)
11 11
 
12 12
     source = @source.slice(@position..-1)
13
-    if source.match(/^null/)
13
+
14
+    if source.match(/\Anull/)
14 15
       @position += 4
15 16
       Token.new(TokenKinds::NULL)
16
-    elsif source.match(/^true/)
17
+    elsif source.match(/\Atrue/)
17 18
       @position += 4
18 19
       Token.new(TokenKinds::BOOLEAN, true)
19
-    elsif source.match(/^false/)
20
+    elsif source.match(/\Afalse/)
20 21
       @position += 5
21 22
       Token.new(TokenKinds::BOOLEAN, false)
22
-    elsif source.match(/^if/)
23
+    elsif source.match(/\Aif/)
23 24
       @position += 2
24 25
       Token.new(TokenKinds::IF)
25
-    elsif source.match(/^elseif/)
26
+    elsif source.match(/\Aelseif/)
26 27
       @position += 6
27 28
       Token.new(TokenKinds::ELSEIF)
28
-    elsif source.match(/^else/)
29
+    elsif source.match(/\Aelse/)
29 30
       @position += 4
30 31
       Token.new(TokenKinds::ELSE)
31
-    elsif source.match(/^\d+(\.\d+)?/)
32
-      number = source.match(/^\d+(\.\d+)?/)[0]
32
+    elsif source.match(/\A\d+(\.\d+)?/)
33
+      number = source.match(/\A\d+(\.\d+)?/)[0]
33 34
       @position += number.size
34 35
       Token.new(TokenKinds::NUMBER, number.to_f)
35 36
     elsif source[0] == '"'
@@ -62,109 +63,109 @@ class Lexer
62 63
       end
63 64
 
64 65
       Token.new(TokenKinds::STRING, string)
65
-    elsif source.match(/^\:([a-z][a-zA-Z0-9_]*)/)
66
-      atom = source.match(/^\:([a-z][a-zA-Z0-9_]*)/)[1]
66
+    elsif source.match(/\A\:([a-z][a-zA-Z0-9_]*)/)
67
+      atom = source.match(/\A\:([a-z][a-zA-Z0-9_]*)/)[1]
67 68
       @position += atom.size + 1
68 69
       Token.new(TokenKinds::ATOM, atom.to_sym)
69
-    elsif source.match(/^\+/)
70
+    elsif source.match(/\A\+/)
70 71
       @position += 1
71 72
       Token.new(TokenKinds::OPERATOR, :+)
72
-    elsif source.match(/^\-/)
73
+    elsif source.match(/\A\-/)
73 74
       @position += 1
74 75
       Token.new(TokenKinds::OPERATOR, :-)
75
-    elsif source.match(/^\*/)
76
+    elsif source.match(/\A\*/)
76 77
       @position += 1
77 78
       Token.new(TokenKinds::OPERATOR, :*)
78
-    elsif source.match(%r{^\/})
79
+    elsif source.match(%r{\A\/})
79 80
       @position += 1
80 81
       Token.new(TokenKinds::OPERATOR, :/)
81
-    elsif source.match(/^\{/)
82
+    elsif source.match(/\A\{/)
82 83
       @position += 1
83 84
       Token.new(TokenKinds::LBRACE)
84
-    elsif source.match(/^\}/)
85
+    elsif source.match(/\A\}/)
85 86
       @position += 1
86 87
       Token.new(TokenKinds::RBRACE)
87
-    elsif source.match(/^\(/)
88
+    elsif source.match(/\A\(/)
88 89
       @position += 1
89 90
       Token.new(TokenKinds::LPAREN)
90
-    elsif source.match(/^\)/)
91
+    elsif source.match(/\A\)/)
91 92
       @position += 1
92 93
       Token.new(TokenKinds::RPAREN)
93
-    elsif source.match(/^\[/)
94
+    elsif source.match(/\A\[/)
94 95
       @position += 1
95 96
       Token.new(TokenKinds::LBRACKET)
96
-    elsif source.match(/^\]/)
97
+    elsif source.match(/\A\]/)
97 98
       @position += 1
98 99
       Token.new(TokenKinds::RBRACKET)
99
-    elsif source.match(/^\;/)
100
+    elsif source.match(/\A\;/)
100 101
       @position += 1
101 102
       Token.new(TokenKinds::SEMICOLON)
102
-    elsif source.match(/^,/)
103
+    elsif source.match(/\A,/)
103 104
       @position += 1
104 105
       Token.new(TokenKinds::COMMA)
105
-    elsif source.match(/^\./)
106
+    elsif source.match(/\A\./)
106 107
       @position += 1
107 108
       Token.new(TokenKinds::DOT)
108
-    elsif source.match(/^=>/)
109
+    elsif source.match(/\A=>/)
109 110
       @position += 2
110 111
       Token.new(TokenKinds::ROCKET)
111
-    elsif source.match(/^!/)
112
+    elsif source.match(/\A!/)
112 113
       @position += 1
113 114
       Token.new(TokenKinds::OPERATOR, :!)
114
-    elsif source.match(/^==/)
115
+    elsif source.match(/\A==/)
115 116
       @position += 2
116 117
       Token.new(TokenKinds::OPERATOR, :==)
117
-    elsif source.match(/^\<=/)
118
+    elsif source.match(/\A\<=/)
118 119
       @position += 2
119 120
       Token.new(TokenKinds::OPERATOR, :<=)
120
-    elsif source.match(/^\>\=/)
121
+    elsif source.match(/\A\>\=/)
121 122
       @position += 2
122 123
       Token.new(TokenKinds::OPERATOR, :>=)
123
-    elsif source.match(/^\</)
124
+    elsif source.match(/\A\</)
124 125
       @position += 1
125 126
       Token.new(TokenKinds::OPERATOR, :<)
126
-    elsif source.match(/^\>/)
127
+    elsif source.match(/\A\>/)
127 128
       @position += 1
128 129
       Token.new(TokenKinds::OPERATOR, :>)
129
-    elsif source.match(/^and/)
130
+    elsif source.match(/\Aand/)
130 131
       @position += 3
131 132
       Token.new(TokenKinds::OPERATOR, :and)
132
-    elsif source.match(/^or/)
133
+    elsif source.match(/\Aor/)
133 134
       @position += 2
134 135
       Token.new(TokenKinds::OPERATOR, :or)
135
-    elsif source.match(/^not/)
136
+    elsif source.match(/\Anot/)
136 137
       @position += 3
137 138
       Token.new(TokenKinds::OPERATOR, :not)
138
-    elsif source.match(/^\=/)
139
+    elsif source.match(/\A\=/)
139 140
       @position += 1
140 141
       Token.new(TokenKinds::EQUALS)
141
-    elsif source.match(/^let/)
142
+    elsif source.match(/\Alet/)
142 143
       @position += 3
143 144
       Token.new(TokenKinds::LET)
144
-    elsif source.match(/^function/)
145
+    elsif source.match(/\Afunction/)
145 146
       @position += 8
146 147
       Token.new(TokenKinds::FUNCTION)
147
-    elsif source.match(/^class/)
148
+    elsif source.match(/\Aclass/)
148 149
       @position += 5
149 150
       Token.new(TokenKinds::CLASS)
150
-    elsif source.match(/^public/)
151
+    elsif source.match(/\Apublic/)
151 152
       @position += 6
152 153
       Token.new(TokenKinds::PUBLIC)
153
-    elsif source.match(/^private/)
154
+    elsif source.match(/\Aprivate/)
154 155
       @position += 7
155 156
       Token.new(TokenKinds::PRIVATE)
156
-    elsif source.match(/^for/)
157
+    elsif source.match(/\Afor/)
157 158
       @position += 3
158 159
       Token.new(TokenKinds::FOR)
159
-    elsif source.match(/^in/)
160
+    elsif source.match(/\Ain/)
160 161
       @position += 2
161 162
       Token.new(TokenKinds::IN)
162
-    elsif source.match(/^[a-z][a-zA-Z0-9_]*/)
163
-      identifier = source.match(/^[a-z][a-zA-Z0-9_]*/)[0]
163
+    elsif source.match(/\A[a-z][a-zA-Z0-9_]*/)
164
+      identifier = source.match(/\A[a-z][a-zA-Z0-9_]*/)[0]
164 165
       @position += identifier.size
165 166
       Token.new(TokenKinds::IDENTIFIER, identifier)
166
-    elsif source.match(/^[A-Z][a-zA-Z0-9_]*/)
167
-      class_name = source.match(/^[A-Z][a-zA-Z0-9_]*/)[0]
167
+    elsif source.match(/\A[A-Z][a-zA-Z0-9_]*/)
168
+      class_name = source.match(/\A[A-Z][a-zA-Z0-9_]*/)[0]
168 169
       @position += class_name.size
169 170
       Token.new(TokenKinds::CLASS_NAME, class_name)
170 171
     else

Loading…
Cancel
Save