Skip to content

Commit d51973a

Browse files
committed
syntax: Move some functions from parser to token
1 parent 476d5a0 commit d51973a

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

src/librustsyntax/parse/parser.rs

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import result::result;
22
import either::{either, left, right};
33
import std::map::{hashmap, str_hash};
4-
import token::can_begin_expr;
4+
import token::{can_begin_expr, is_ident, is_plain_ident};
55
import codemap::{span,fss_none};
66
import util::interner;
77
import ast::{node_id, spanned};
@@ -160,24 +160,7 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader,
160160
mut restriction: UNRESTRICTED,
161161
reader: rdr,
162162
binop_precs: binop_prec_table(),
163-
bad_expr_words: bad_expr_word_table()}
164-
}
165-
166-
// These are the words that shouldn't be allowed as value identifiers,
167-
// because, if used at the start of a line, they will cause the line to be
168-
// interpreted as a specific kind of statement, which would be confusing.
169-
fn bad_expr_word_table() -> hashmap<str, ()> {
170-
let words = str_hash();
171-
let keys = ["alt", "assert", "be", "break", "check", "claim",
172-
"class", "const", "cont", "copy", "crust", "do", "else",
173-
"enum", "export", "fail", "fn", "for", "if", "iface",
174-
"impl", "import", "let", "log", "loop", "mod",
175-
"mut", "native", "pure", "resource", "ret", "trait",
176-
"type", "unchecked", "unsafe", "while", "new"];
177-
for keys.each {|word|
178-
words.insert(word, ());
179-
}
180-
words
163+
bad_expr_words: token::bad_expr_word_table()}
181164
}
182165

183166
fn token_to_str(reader: reader, token: token::token) -> str {
@@ -276,21 +259,19 @@ fn expect_word(p: parser, word: str) {
276259
}
277260

278261
fn check_bad_word(p: parser) {
279-
alt p.token {
280-
token::IDENT(sid, false) {
281-
let w = p.get_str(sid);
282-
if p.bad_expr_words.contains_key(w) {
283-
p.fatal("found " + w + " in expression position");
284-
}
285-
}
286-
_ { }
262+
if token::is_bad_expr_word(p.token, p.bad_expr_words,
263+
*p.reader.interner) {
264+
let w = token_to_str(p.reader, p.token);
265+
p.fatal("found " + w + " in expression position");
287266
}
288267
}
289268

290269
fn parse_ty_fn(p: parser) -> ast::fn_decl {
291270
fn parse_fn_input_ty(p: parser) -> ast::arg {
292271
let mode = parse_arg_mode(p);
293-
let name = if is_plain_ident(p) && p.look_ahead(1u) == token::COLON {
272+
let name = if is_plain_ident(p.token)
273+
&& p.look_ahead(1u) == token::COLON {
274+
294275
let name = parse_value_ident(p);
295276
p.bump();
296277
name
@@ -783,15 +764,6 @@ fn parse_lit(p: parser) -> ast::lit {
783764
ret {node: lit, span: ast_util::mk_sp(lo, p.last_span.hi)};
784765
}
785766

786-
fn is_ident(t: token::token) -> bool {
787-
alt t { token::IDENT(_, _) { ret true; } _ { } }
788-
ret false;
789-
}
790-
791-
fn is_plain_ident(p: parser) -> bool {
792-
ret alt p.token { token::IDENT(_, false) { true } _ { false } };
793-
}
794-
795767
fn parse_path(p: parser) -> @ast::path {
796768
let lo = p.span.lo;
797769
let global = eat(p, token::MOD_SEP);
@@ -858,10 +830,6 @@ fn mk_mac_expr(p: parser, lo: uint, hi: uint, m: ast::mac_) -> @ast::expr {
858830
span: ast_util::mk_sp(lo, hi)};
859831
}
860832

861-
fn is_bar(t: token::token) -> bool {
862-
alt t { token::BINOP(token::OR) | token::OROR { true } _ { false } }
863-
}
864-
865833
fn mk_lit_u32(p: parser, i: u32) -> @ast::expr {
866834
let span = p.span;
867835
let lv_lit = @{node: ast::lit_uint(i as u64, ast::ty_u32),
@@ -925,7 +893,7 @@ fn parse_bottom_expr(p: parser) -> pexpr {
925893
} else if p.token == token::LBRACE {
926894
p.bump();
927895
if is_word(p, "mut") ||
928-
is_plain_ident(p) && p.look_ahead(1u) == token::COLON {
896+
is_plain_ident(p.token) && p.look_ahead(1u) == token::COLON {
929897
let mut fields = [parse_field(p, token::COLON)];
930898
let mut base = none;
931899
while p.token != token::RBRACE {
@@ -940,7 +908,7 @@ fn parse_bottom_expr(p: parser) -> pexpr {
940908
hi = p.span.hi;
941909
expect(p, token::RBRACE);
942910
ex = ast::expr_rec(fields, base);
943-
} else if is_bar(p.token) {
911+
} else if token::is_bar(p.token) {
944912
ret pexpr(parse_fn_block_expr(p));
945913
} else {
946914
let blk = parse_block_tail(p, lo, ast::default_blk);
@@ -1211,7 +1179,8 @@ fn parse_dot_or_call_expr_with(p: parser, e0: pexpr) -> pexpr {
12111179
}
12121180

12131181
// expr {|| ... }
1214-
token::LBRACE if is_bar(p.look_ahead(1u)) && permits_call(p) {
1182+
token::LBRACE if (token::is_bar(p.look_ahead(1u))
1183+
&& permits_call(p)) {
12151184
p.bump();
12161185
let blk = parse_fn_block_expr(p);
12171186
alt e.node {
@@ -1683,7 +1652,7 @@ fn parse_pat(p: parser) -> @ast::pat {
16831652
hi = val.span.hi;
16841653
pat = ast::pat_lit(val);
16851654
}
1686-
} else if is_plain_ident(p) &&
1655+
} else if is_plain_ident(p.token) &&
16871656
alt p.look_ahead(1u) {
16881657
token::LPAREN | token::LBRACKET | token::LT { false }
16891658
_ { true }
@@ -1751,7 +1720,7 @@ fn parse_instance_var(p:parser, pr: ast::privacy) -> @ast::class_member {
17511720
if eat_word(p, "mut") || eat_word(p, "mutable") {
17521721
is_mutbl = ast::class_mutable;
17531722
}
1754-
if !is_plain_ident(p) {
1723+
if !is_plain_ident(p.token) {
17551724
p.fatal("expecting ident");
17561725
}
17571726
let name = parse_ident(p);

src/librustsyntax/parse/token.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import util::interner;
33
import util::interner::interner;
4+
import std::map::{hashmap, str_hash};
45

56
type str_num = uint;
67

@@ -192,6 +193,49 @@ pure fn can_begin_expr(t: token) -> bool {
192193
}
193194
}
194195

196+
fn is_ident(t: token::token) -> bool {
197+
alt t { token::IDENT(_, _) { ret true; } _ { } }
198+
ret false;
199+
}
200+
201+
fn is_plain_ident(t: token::token) -> bool {
202+
ret alt t { token::IDENT(_, false) { true } _ { false } };
203+
}
204+
205+
fn is_bar(t: token::token) -> bool {
206+
alt t { token::BINOP(token::OR) | token::OROR { true } _ { false } }
207+
}
208+
209+
fn is_bad_expr_word(t: token,
210+
bad_expr_words: hashmap<str, ()>,
211+
in: interner<str>) -> bool {
212+
alt t {
213+
token::IDENT(_, false) {
214+
bad_expr_words.contains_key(to_str(in, t))
215+
}
216+
_ { false }
217+
}
218+
}
219+
220+
#[doc = "
221+
These are the words that shouldn't be allowed as value identifiers,
222+
because, if used at the start of a line, they will cause the line to be
223+
interpreted as a specific kind of statement, which would be confusing.
224+
"]
225+
fn bad_expr_word_table() -> hashmap<str, ()> {
226+
let words = str_hash();
227+
let keys = ["alt", "assert", "be", "break", "check", "claim",
228+
"class", "const", "cont", "copy", "crust", "do", "else",
229+
"enum", "export", "fail", "fn", "for", "if", "iface",
230+
"impl", "import", "let", "log", "loop", "mod",
231+
"mut", "native", "pure", "resource", "ret", "trait",
232+
"type", "unchecked", "unsafe", "while", "new"];
233+
for keys.each {|word|
234+
words.insert(word, ());
235+
}
236+
words
237+
}
238+
195239
// Local Variables:
196240
// fill-column: 78;
197241
// indent-tabs-mode: nil

0 commit comments

Comments
 (0)