@@ -14,6 +14,7 @@ use ast;
14
14
use codemap:: { BytePos , spanned} ;
15
15
use parse:: lexer:: reader;
16
16
use parse:: parser:: Parser ;
17
+ use parse:: token:: keywords;
17
18
use parse:: token;
18
19
19
20
use opt_vec;
@@ -133,54 +134,15 @@ pub impl Parser {
133
134
return if * self . token == * tok { self . bump ( ) ; true } else { false } ;
134
135
}
135
136
136
- // Storing keywords as interned idents instead of strings would be nifty.
137
-
138
- // A sanity check that the word we are asking for is a known keyword
139
- // NOTE: this could be done statically....
140
- fn require_keyword ( & self , word : & str ) {
141
- if !self . keywords . contains_equiv ( & word) {
142
- self . bug ( fmt ! ( "unknown keyword: %s" , word) ) ;
143
- }
144
- }
145
-
146
- // return true when this token represents the given string, and is not
147
- // followed immediately by :: .
148
- fn token_is_word ( & self , word : & str , tok : & token:: Token ) -> bool {
149
- match * tok {
150
- token:: IDENT ( sid, false ) => { word == * self . id_to_str ( sid) }
151
- _ => { false }
152
- }
153
- }
154
-
155
- fn token_is_keyword ( & self , word : & str , tok : & token:: Token ) -> bool {
156
- self . require_keyword ( word) ;
157
- self . token_is_word ( word, tok)
158
- }
159
-
160
- fn is_keyword ( & self , word : & str ) -> bool {
161
- self . token_is_keyword ( word, & copy * self . token )
162
- }
163
-
164
- fn id_is_any_keyword ( & self , id : ast:: ident ) -> bool {
165
- self . keywords . contains ( self . id_to_str ( id) )
166
- }
167
-
168
- fn is_any_keyword ( & self , tok : & token:: Token ) -> bool {
169
- match * tok {
170
- token:: IDENT ( sid, false ) => {
171
- self . keywords . contains ( self . id_to_str ( sid) )
172
- }
173
- _ => false
174
- }
137
+ fn is_keyword ( & self , kw : keywords:: Keyword ) -> bool {
138
+ token:: is_keyword ( kw, self . token )
175
139
}
176
140
177
- // if the given word is not a keyword, signal an error.
178
141
// if the next token is the given keyword, eat it and return
179
142
// true. Otherwise, return false.
180
- fn eat_keyword ( & self , word : & str ) -> bool {
181
- self . require_keyword ( word) ;
143
+ fn eat_keyword ( & self , kw : keywords:: Keyword ) -> bool {
182
144
let is_kw = match * self . token {
183
- token:: IDENT ( sid, false ) => word == * self . id_to_str ( sid) ,
145
+ token:: IDENT ( sid, false ) => kw . to_ident ( ) . repr == sid. repr ,
184
146
_ => false
185
147
} ;
186
148
if is_kw { self . bump ( ) }
@@ -190,63 +152,30 @@ pub impl Parser {
190
152
// if the given word is not a keyword, signal an error.
191
153
// if the next token is not the given word, signal an error.
192
154
// otherwise, eat it.
193
- fn expect_keyword ( & self , word : & str ) {
194
- self . require_keyword ( word) ;
195
- if !self . eat_keyword ( word) {
155
+ fn expect_keyword ( & self , kw : keywords:: Keyword ) {
156
+ if !self . eat_keyword ( kw) {
196
157
self . fatal (
197
158
fmt ! (
198
159
"expected `%s`, found `%s`" ,
199
- word ,
160
+ * self . id_to_str ( kw . to_ident ( ) ) ,
200
161
self . this_token_to_str( )
201
162
)
202
163
) ;
203
164
}
204
165
}
205
166
206
- // return true if the given string is a strict keyword
207
- fn is_strict_keyword ( & self , word : & str ) -> bool {
208
- self . strict_keywords . contains_equiv ( & word)
209
- }
210
-
211
- // signal an error if the current token is a strict keyword
212
- fn check_strict_keywords ( & self ) {
213
- match * self . token {
214
- token:: IDENT ( _, false ) => {
215
- let w = token_to_str ( self . reader , & copy * self . token ) ;
216
- self . check_strict_keywords_ ( w) ;
217
- }
218
- _ => ( )
219
- }
220
- }
221
-
222
167
// signal an error if the given string is a strict keyword
223
- fn check_strict_keywords_ ( & self , w : & str ) {
224
- if self . is_strict_keyword ( w ) {
168
+ fn check_strict_keywords ( & self ) {
169
+ if token :: is_strict_keyword ( self . token ) {
225
170
self . span_err ( * self . last_span ,
226
- fmt ! ( "found `%s` in ident position" , w ) ) ;
171
+ fmt ! ( "found `%s` in ident position" , self . this_token_to_str ( ) ) ) ;
227
172
}
228
173
}
229
174
230
- // return true if this is a reserved keyword
231
- fn is_reserved_keyword ( & self , word : & str ) -> bool {
232
- self . reserved_keywords . contains_equiv ( & word)
233
- }
234
-
235
175
// signal an error if the current token is a reserved keyword
236
176
fn check_reserved_keywords ( & self ) {
237
- match * self . token {
238
- token:: IDENT ( _, false ) => {
239
- let w = token_to_str ( self . reader , & copy * self . token ) ;
240
- self . check_reserved_keywords_ ( w) ;
241
- }
242
- _ => ( )
243
- }
244
- }
245
-
246
- // signal an error if the given string is a reserved keyword
247
- fn check_reserved_keywords_ ( & self , w : & str ) {
248
- if self . is_reserved_keyword ( w) {
249
- self . fatal ( fmt ! ( "`%s` is a reserved keyword" , w) ) ;
177
+ if token:: is_reserved_keyword ( self . token ) {
178
+ self . fatal ( fmt ! ( "`%s` is a reserved keyword" , self . this_token_to_str( ) ) ) ;
250
179
}
251
180
}
252
181
0 commit comments