@@ -42,6 +42,7 @@ state type parser =
42
42
fn get_str ( token:: str_num ) -> str ;
43
43
fn get_reader ( ) -> lexer:: reader ;
44
44
fn get_filemap ( ) -> codemap:: filemap ;
45
+ fn get_bad_expr_words ( ) -> std:: map:: hashmap [ str, ( ) ] ;
45
46
fn get_chpos ( ) -> uint ;
46
47
fn get_ann ( ) -> ast:: ann ;
47
48
fn next_ann_num ( ) -> uint ;
@@ -63,7 +64,8 @@ fn new_parser(session::session sess,
63
64
ast:: crate_num crate,
64
65
lexer:: reader rdr,
65
66
vec[ op_spec] precs,
66
- mutable uint next_ann_var)
67
+ mutable uint next_ann_var,
68
+ std:: map:: hashmap[ str, ( ) ] bad_words)
67
69
{
68
70
fn peek ( ) -> token:: token {
69
71
ret tok;
@@ -132,6 +134,10 @@ fn new_parser(session::session sess,
132
134
ret rdr. get_filemap ( ) ;
133
135
}
134
136
137
+ fn get_bad_expr_words ( ) -> std:: map:: hashmap [ str, ( ) ] {
138
+ ret bad_words;
139
+ }
140
+
135
141
fn get_chpos ( ) -> uint { ret rdr. get_chpos ( ) ; }
136
142
137
143
fn get_ann ( ) -> ast:: ann {
@@ -156,7 +162,50 @@ fn new_parser(session::session sess,
156
162
auto npos = rdr. get_chpos ( ) ;
157
163
ret stdio_parser ( sess, env, ftype, lexer:: next_token ( rdr) ,
158
164
npos, npos, npos, initial_def. _1 , UNRESTRICTED ,
159
- initial_def. _0 , rdr, prec_table ( ) , next_ann) ;
165
+ initial_def. _0 , rdr, prec_table ( ) , next_ann,
166
+ bad_expr_word_table ( ) ) ;
167
+ }
168
+
169
+ // These are the words that shouldn't be allowed as value identifiers,
170
+ // because, if used at the start of a line, they will cause the line to be
171
+ // interpreted as a specific kind of statement, which would be confusing.
172
+ fn bad_expr_word_table ( ) -> std:: map:: hashmap [ str, ( ) ] {
173
+ auto words = new_str_hash[ ( ) ] ( ) ;
174
+ words. insert ( "mod" , ( ) ) ;
175
+ words. insert ( "if" , ( ) ) ;
176
+ words. insert ( "else" , ( ) ) ;
177
+ words. insert ( "while" , ( ) ) ;
178
+ words. insert ( "do" , ( ) ) ;
179
+ words. insert ( "alt" , ( ) ) ;
180
+ words. insert ( "for" , ( ) ) ;
181
+ words. insert ( "break" , ( ) ) ;
182
+ words. insert ( "cont" , ( ) ) ;
183
+ words. insert ( "put" , ( ) ) ;
184
+ words. insert ( "ret" , ( ) ) ;
185
+ words. insert ( "be" , ( ) ) ;
186
+ words. insert ( "fail" , ( ) ) ;
187
+ words. insert ( "type" , ( ) ) ;
188
+ words. insert ( "check" , ( ) ) ;
189
+ words. insert ( "assert" , ( ) ) ;
190
+ words. insert ( "claim" , ( ) ) ;
191
+ words. insert ( "prove" , ( ) ) ;
192
+ words. insert ( "state" , ( ) ) ;
193
+ words. insert ( "gc" , ( ) ) ;
194
+ words. insert ( "native" , ( ) ) ;
195
+ words. insert ( "auto" , ( ) ) ;
196
+ words. insert ( "fn" , ( ) ) ;
197
+ words. insert ( "pred" , ( ) ) ;
198
+ words. insert ( "iter" , ( ) ) ;
199
+ words. insert ( "import" , ( ) ) ;
200
+ words. insert ( "export" , ( ) ) ;
201
+ words. insert ( "let" , ( ) ) ;
202
+ words. insert ( "const" , ( ) ) ;
203
+ words. insert ( "log" , ( ) ) ;
204
+ words. insert ( "log_err" , ( ) ) ;
205
+ words. insert ( "yield" , ( ) ) ;
206
+ words. insert ( "tag" , ( ) ) ;
207
+ words. insert ( "obj" , ( ) ) ;
208
+ ret words;
160
209
}
161
210
162
211
fn unexpected ( parser p, token:: token t) {
@@ -190,6 +239,10 @@ fn parse_ident(parser p) -> ast::ident {
190
239
}
191
240
}
192
241
}
242
+ fn parse_value_ident ( parser p) -> ast:: ident {
243
+ check_bad_word ( p) ;
244
+ ret parse_ident ( p) ;
245
+ }
193
246
194
247
195
248
/* FIXME: gross hack copied from rustboot to make certain configuration-based
@@ -240,6 +293,17 @@ fn expect_word(&parser p, &str word) {
240
293
token:: to_str ( p. get_reader ( ) , p. peek ( ) ) ) ;
241
294
}
242
295
}
296
+ fn check_bad_word ( & parser p) {
297
+ alt ( p. peek ( ) ) {
298
+ case ( token:: IDENT ( ?sid) ) {
299
+ auto w = p. get_str ( sid) ;
300
+ if ( p. get_bad_expr_words ( ) . contains_key ( w) ) {
301
+ p. err ( "found " + w + " in expression position" ) ;
302
+ }
303
+ }
304
+ case ( _) { }
305
+ }
306
+ }
243
307
244
308
fn parse_ty_fn ( ast:: proto proto, parser p, uint lo)
245
309
-> ast:: ty_ {
@@ -299,7 +363,7 @@ fn parse_ty_obj(parser p, &mutable uint hi) -> ast::ty_ {
299
363
auto flo = p. get_lo_pos ( ) ;
300
364
301
365
let ast:: proto proto = parse_proto ( p) ;
302
- auto ident = parse_ident ( p) ;
366
+ auto ident = parse_value_ident ( p) ;
303
367
auto f = parse_ty_fn ( proto, p, flo) ;
304
368
expect ( p, token:: SEMI ) ;
305
369
alt ( f) {
@@ -338,7 +402,7 @@ fn parse_constr_arg(parser p) -> @ast::constr_arg {
338
402
if ( p. peek ( ) == token:: BINOP ( token:: STAR ) ) {
339
403
p. bump ( ) ;
340
404
} else {
341
- carg = ast:: carg_ident ( parse_ident ( p) ) ;
405
+ carg = ast:: carg_ident ( parse_value_ident ( p) ) ;
342
406
}
343
407
ret @rec( node=carg, span=sp) ;
344
408
}
@@ -504,7 +568,7 @@ fn parse_arg(parser p) -> ast::arg {
504
568
eat_word ( p, "mutable" ) ;
505
569
}
506
570
let @ast:: ty t = parse_ty ( p) ;
507
- let ast:: ident i = parse_ident ( p) ;
571
+ let ast:: ident i = parse_value_ident ( p) ;
508
572
ret rec( mode=m, ty=t, ident=i, id=p. next_def_id ( ) ) ;
509
573
}
510
574
@@ -851,6 +915,7 @@ fn parse_bottom_expr(parser p) -> @ast::expr {
851
915
ex = ast:: expr_call ( f, es. node , p. get_ann ( ) ) ;
852
916
} else if ( is_ident ( p. peek ( ) ) && !is_word ( p, "true" ) &&
853
917
!is_word ( p, "false" ) ) {
918
+ check_bad_word ( p) ;
854
919
auto pth = parse_path ( p) ;
855
920
hi = pth. span . hi ;
856
921
ex = ast:: expr_path ( pth, p. get_ann ( ) ) ;
@@ -1376,7 +1441,7 @@ fn parse_pat(parser p) -> @ast::pat {
1376
1441
1377
1442
fn parse_local_full ( & option:: t[ @ast:: ty] tyopt ,
1378
1443
parser p) -> @ast:: local {
1379
- auto ident = parse_ident ( p) ;
1444
+ auto ident = parse_value_ident ( p) ;
1380
1445
auto init = parse_initializer ( p) ;
1381
1446
ret @rec( ty = tyopt,
1382
1447
infer = false ,
@@ -1622,7 +1687,7 @@ fn parse_fn(parser p, ast::proto proto, ast::purity purity) -> ast::_fn {
1622
1687
1623
1688
fn parse_fn_header ( parser p)
1624
1689
-> tup ( ast:: ident , vec[ ast:: ty_param ] ) {
1625
- auto id = parse_ident ( p) ;
1690
+ auto id = parse_value_ident ( p) ;
1626
1691
auto ty_params = parse_ty_params ( p) ;
1627
1692
ret tup( id, ty_params) ;
1628
1693
}
@@ -1641,14 +1706,14 @@ fn parse_item_fn_or_iter(parser p, ast::purity purity, ast::proto proto)
1641
1706
fn parse_obj_field ( parser p) -> ast:: obj_field {
1642
1707
auto mut = parse_mutability ( p) ; // TODO: store this, use it in typeck
1643
1708
auto ty = parse_ty ( p) ;
1644
- auto ident = parse_ident ( p) ;
1709
+ auto ident = parse_value_ident ( p) ;
1645
1710
ret rec( ty=ty, ident=ident, id=p. next_def_id ( ) , ann=p. get_ann ( ) ) ;
1646
1711
}
1647
1712
1648
1713
fn parse_method ( parser p) -> @ast:: method {
1649
1714
auto lo = p. get_lo_pos ( ) ;
1650
1715
auto proto = parse_proto ( p) ;
1651
- auto ident = parse_ident ( p) ;
1716
+ auto ident = parse_value_ident ( p) ;
1652
1717
auto f = parse_fn ( p, proto, ast:: impure_fn) ;
1653
1718
auto meth = rec ( ident=ident, meth=f,
1654
1719
id=p. next_def_id ( ) , ann=p. get_ann ( ) ) ;
@@ -1675,7 +1740,7 @@ fn parse_dtor(parser p) -> @ast::method {
1675
1740
1676
1741
fn parse_item_obj ( parser p, ast:: layer lyr) -> @ast:: item {
1677
1742
auto lo = p. get_last_lo_pos ( ) ;
1678
- auto ident = parse_ident ( p) ;
1743
+ auto ident = parse_value_ident ( p) ;
1679
1744
auto ty_params = parse_ty_params ( p) ;
1680
1745
auto pf = parse_obj_field;
1681
1746
let util:: common:: spanned[ vec[ ast:: obj_field] ] fields =
@@ -1722,7 +1787,7 @@ fn parse_mod_items(parser p, token::token term) -> ast::_mod {
1722
1787
fn parse_item_const ( parser p) -> @ast:: item {
1723
1788
auto lo = p. get_last_lo_pos ( ) ;
1724
1789
auto ty = parse_ty ( p) ;
1725
- auto id = parse_ident ( p) ;
1790
+ auto id = parse_value_ident ( p) ;
1726
1791
expect ( p, token:: EQ ) ;
1727
1792
auto e = parse_expr ( p) ;
1728
1793
auto hi = p. get_hi_pos ( ) ;
@@ -1871,6 +1936,7 @@ fn parse_item_tag(parser p) -> @ast::item {
1871
1936
auto tok = p. peek ( ) ;
1872
1937
alt ( tok) {
1873
1938
case ( token:: IDENT ( ?name) ) {
1939
+ check_bad_word ( p) ;
1874
1940
auto vlo = p. get_lo_pos ( ) ;
1875
1941
p. bump ( ) ;
1876
1942
@@ -2215,7 +2281,7 @@ fn parse_crate_directive(parser p) -> ast::crate_directive
2215
2281
}
2216
2282
} else if ( eat_word ( p, "let" ) ) {
2217
2283
expect ( p, token:: LPAREN ) ;
2218
- auto id = parse_ident ( p) ;
2284
+ auto id = parse_value_ident ( p) ;
2219
2285
expect ( p, token:: EQ ) ;
2220
2286
auto x = parse_expr ( p) ;
2221
2287
expect ( p, token:: RPAREN ) ;
0 commit comments