Skip to content

Commit e4c3d80

Browse files
committed
syntax: Removing uses of HashMap
1 parent fa77728 commit e4c3d80

File tree

8 files changed

+61
-54
lines changed

8 files changed

+61
-54
lines changed

src/libcore/hashmap.rs

+5
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,11 @@ pub mod linear {
656656
fn reserve_at_least(&mut self, n: uint) {
657657
self.map.reserve_at_least(n)
658658
}
659+
660+
/// Consumes all of the elements in the set, emptying it out
661+
fn consume(&mut self, f: &fn(T)) {
662+
self.map.consume(|k, _| f(k))
663+
}
659664
}
660665

661666
#[test]

src/libsyntax/ext/tt/macro_parser.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ pub enum named_match {
186186
pub type earley_item = ~MatcherPos;
187187

188188
pub fn nameize(p_s: @mut ParseSess, ms: ~[matcher], res: ~[@named_match])
189-
-> HashMap<ident,@named_match> {
189+
-> LinearMap<ident,@named_match> {
190190
fn n_rec(p_s: @mut ParseSess, m: matcher, res: ~[@named_match],
191-
ret_val: HashMap<ident, @named_match>) {
191+
ret_val: &mut LinearMap<ident, @named_match>) {
192192
match m {
193193
codemap::spanned {node: match_tok(_), _} => (),
194194
codemap::spanned {node: match_seq(ref more_ms, _, _, _, _), _} => {
@@ -207,13 +207,13 @@ pub fn nameize(p_s: @mut ParseSess, ms: ~[matcher], res: ~[@named_match])
207207
}
208208
}
209209
}
210-
let ret_val = HashMap();
211-
for ms.each() |m| { n_rec(p_s, *m, res, ret_val) }
210+
let mut ret_val = LinearMap::new();
211+
for ms.each() |m| { n_rec(p_s, *m, res, &mut ret_val) }
212212
return ret_val;
213213
}
214214
215215
pub enum parse_result {
216-
success(HashMap<ident, @named_match>),
216+
success(LinearMap<ident, @named_match>),
217217
failure(codemap::span, ~str),
218218
error(codemap::span, ~str)
219219
}
@@ -223,11 +223,11 @@ pub fn parse_or_else(
223223
+cfg: ast::crate_cfg,
224224
rdr: @reader,
225225
ms: ~[matcher]
226-
) -> HashMap<ident, @named_match> {
226+
) -> LinearMap<ident, @named_match> {
227227
match parse(sess, cfg, rdr, ms) {
228228
success(m) => m,
229-
failure(sp, ref str) => sess.span_diagnostic.span_fatal(sp, (*str)),
230-
error(sp, ref str) => sess.span_diagnostic.span_fatal(sp, (*str))
229+
failure(sp, str) => sess.span_diagnostic.span_fatal(sp, str),
230+
error(sp, str) => sess.span_diagnostic.span_fatal(sp, str)
231231
}
232232
}
233233

src/libsyntax/ext/tt/macro_rules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ pub fn add_new_extension(cx: @ext_ctxt,
6363
argument_gram);
6464

6565
// Extract the arguments:
66-
let lhses = match argument_map.get(&lhs_nm) {
66+
let lhses = match *argument_map.get(&lhs_nm) {
6767
@matched_seq(ref s, _) => /* FIXME (#2543) */ copy *s,
6868
_ => cx.span_bug(sp, ~"wrong-structured lhs")
6969
};
7070

71-
let rhses = match argument_map.get(&rhs_nm) {
71+
let rhses = match *argument_map.get(&rhs_nm) {
7272
@matched_seq(ref s, _) => /* FIXME (#2543) */ copy *s,
7373
_ => cx.span_bug(sp, ~"wrong-structured rhs")
7474
};

src/libsyntax/ext/tt/transcribe.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use ext::tt::macro_parser::{named_match, matched_seq, matched_nonterminal};
1818
use parse::token::{EOF, INTERPOLATED, IDENT, Token, nt_ident, ident_interner};
1919
use parse::lexer::TokenAndSpan;
2020

21+
use core::hashmap::linear::LinearMap;
2122
use core::option;
2223
use core::vec;
2324

@@ -38,7 +39,7 @@ pub struct TtReader {
3839
// the unzipped tree:
3940
cur: @mut TtFrame,
4041
/* for MBE-style macro transcription */
41-
interpolations: std::oldmap::HashMap<ident, @named_match>,
42+
interpolations: LinearMap<ident, @named_match>,
4243
repeat_idx: ~[uint],
4344
repeat_len: ~[uint],
4445
/* cached: */
@@ -51,7 +52,7 @@ pub struct TtReader {
5152
* should) be none. */
5253
pub fn new_tt_reader(sp_diag: @span_handler,
5354
itr: @ident_interner,
54-
interp: Option<std::oldmap::HashMap<ident,@named_match>>,
55+
interp: Option<LinearMap<ident,@named_match>>,
5556
+src: ~[ast::token_tree])
5657
-> @mut TtReader {
5758
let r = @mut TtReader {
@@ -65,7 +66,7 @@ pub fn new_tt_reader(sp_diag: @span_handler,
6566
up: option::None
6667
},
6768
interpolations: match interp { /* just a convienience */
68-
None => std::oldmap::HashMap(),
69+
None => LinearMap::new(),
6970
Some(x) => x
7071
},
7172
repeat_idx: ~[],
@@ -123,7 +124,10 @@ fn lookup_cur_matched_by_matched(r: &mut TtReader,
123124
}
124125

125126
fn lookup_cur_matched(r: &mut TtReader, name: ident) -> @named_match {
126-
lookup_cur_matched_by_matched(r, r.interpolations.get(&name))
127+
// FIXME (#3850): this looks a bit silly with an extra scope.
128+
let start;
129+
{ start = *r.interpolations.get(&name); }
130+
return lookup_cur_matched_by_matched(r, start);
127131
}
128132
enum lis {
129133
lis_unconstrained, lis_constraint(uint, ident), lis_contradiction(~str)

src/libsyntax/parse/common.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub impl Parser {
129129

130130
// A sanity check that the word we are asking for is a known keyword
131131
fn require_keyword(&self, word: &~str) {
132-
if !self.keywords.contains_key(word) {
132+
if !self.keywords.contains(word) {
133133
self.bug(fmt!("unknown keyword: %s", *word));
134134
}
135135
}
@@ -153,7 +153,7 @@ pub impl Parser {
153153
fn is_any_keyword(&self, tok: &token::Token) -> bool {
154154
match *tok {
155155
token::IDENT(sid, false) => {
156-
self.keywords.contains_key(self.id_to_str(sid))
156+
self.keywords.contains(self.id_to_str(sid))
157157
}
158158
_ => false
159159
}
@@ -183,7 +183,7 @@ pub impl Parser {
183183
}
184184

185185
fn is_strict_keyword(&self, word: &~str) -> bool {
186-
self.strict_keywords.contains_key(word)
186+
self.strict_keywords.contains(word)
187187
}
188188

189189
fn check_strict_keywords(&self) {
@@ -203,7 +203,7 @@ pub impl Parser {
203203
}
204204

205205
fn is_reserved_keyword(&self, word: &~str) -> bool {
206-
self.reserved_keywords.contains_key(word)
206+
self.reserved_keywords.contains(word)
207207
}
208208

209209
fn check_reserved_keywords(&self) {

src/libsyntax/parse/obsolete.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ pub impl Parser {
225225
desc: &str) {
226226
self.span_err(sp, fmt!("obsolete syntax: %s", kind_str));
227227

228-
if !self.obsolete_set.contains_key(&kind) {
228+
if !self.obsolete_set.contains(&kind) {
229229
self.sess.span_diagnostic.handler().note(fmt!("%s", desc));
230-
self.obsolete_set.insert(kind, ());
230+
self.obsolete_set.insert(kind);
231231
}
232232
}
233233

src/libsyntax/parse/parser.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ use opt_vec::OptVec;
9292

9393
use core::either::{Either, Left, Right};
9494
use core::either;
95+
use core::hashmap::linear::LinearSet;
9596
use core::vec;
96-
use std::oldmap::HashMap;
9797

9898
#[deriving(Eq)]
9999
enum restriction {
@@ -240,7 +240,7 @@ pub fn Parser(sess: @mut ParseSess,
240240
keywords: token::keyword_table(),
241241
strict_keywords: token::strict_keyword_table(),
242242
reserved_keywords: token::reserved_keyword_table(),
243-
obsolete_set: HashMap(),
243+
obsolete_set: @mut LinearSet::new(),
244244
mod_path_stack: @mut ~[],
245245
}
246246
}
@@ -259,12 +259,12 @@ pub struct Parser {
259259
quote_depth: @mut uint, // not (yet) related to the quasiquoter
260260
reader: @reader,
261261
interner: @token::ident_interner,
262-
keywords: HashMap<~str, ()>,
263-
strict_keywords: HashMap<~str, ()>,
264-
reserved_keywords: HashMap<~str, ()>,
262+
keywords: LinearSet<~str>,
263+
strict_keywords: LinearSet<~str>,
264+
reserved_keywords: LinearSet<~str>,
265265
/// The set of seen errors about obsolete syntax. Used to suppress
266266
/// extra detail when the same error is seen twice
267-
obsolete_set: HashMap<ObsoleteSyntax, ()>,
267+
obsolete_set: @mut LinearSet<ObsoleteSyntax>,
268268
/// Used to determine the path to externally loaded source files
269269
mod_path_stack: @mut ~[~str],
270270

src/libsyntax/parse/token.rs

+26-28
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ use util::interner;
1818

1919
use core::cast;
2020
use core::char;
21+
use core::hashmap::linear::LinearSet;
2122
use core::str;
2223
use core::task;
23-
use std::oldmap::HashMap;
2424

2525
#[auto_encode]
2626
#[auto_decode]
@@ -458,35 +458,33 @@ pub fn mk_fake_ident_interner() -> @ident_interner {
458458
* appear as identifiers at all. Reserved keywords are not used anywhere in
459459
* the language and may not appear as identifiers.
460460
*/
461-
pub fn keyword_table() -> HashMap<~str, ()> {
462-
let keywords = HashMap();
463-
for temporary_keyword_table().each_key |&word| {
464-
keywords.insert(word, ());
465-
}
466-
for strict_keyword_table().each_key |&word| {
467-
keywords.insert(word, ());
468-
}
469-
for reserved_keyword_table().each_key |&word| {
470-
keywords.insert(word, ());
471-
}
472-
keywords
461+
pub fn keyword_table() -> LinearSet<~str> {
462+
let mut keywords = LinearSet::new();
463+
let mut tmp = temporary_keyword_table();
464+
let mut strict = strict_keyword_table();
465+
let mut reserved = reserved_keyword_table();
466+
467+
do tmp.consume |word| { keywords.insert(word); }
468+
do strict.consume |word| { keywords.insert(word); }
469+
do reserved.consume |word| { keywords.insert(word); }
470+
return keywords;
473471
}
474472
475473
/// Keywords that may be used as identifiers
476-
pub fn temporary_keyword_table() -> HashMap<~str, ()> {
477-
let words = HashMap();
474+
pub fn temporary_keyword_table() -> LinearSet<~str> {
475+
let mut words = LinearSet::new();
478476
let keys = ~[
479477
~"self", ~"static",
480478
];
481-
for keys.each |word| {
482-
words.insert(copy *word, ());
479+
do vec::consume(keys) |_, s| {
480+
words.insert(s);
483481
}
484-
words
482+
return words;
485483
}
486484
487485
/// Full keywords. May not appear anywhere else.
488-
pub fn strict_keyword_table() -> HashMap<~str, ()> {
489-
let words = HashMap();
486+
pub fn strict_keyword_table() -> LinearSet<~str> {
487+
let mut words = LinearSet::new();
490488
let keys = ~[
491489
~"as",
492490
~"break",
@@ -505,21 +503,21 @@ pub fn strict_keyword_table() -> HashMap<~str, ()> {
505503
~"unsafe", ~"use",
506504
~"while"
507505
];
508-
for keys.each |word| {
509-
words.insert(copy *word, ());
506+
do vec::consume(keys) |_, w| {
507+
words.insert(w);
510508
}
511-
words
509+
return words;
512510
}
513511
514-
pub fn reserved_keyword_table() -> HashMap<~str, ()> {
515-
let words = HashMap();
512+
pub fn reserved_keyword_table() -> LinearSet<~str> {
513+
let mut words = LinearSet::new();
516514
let keys = ~[
517515
~"be"
518516
];
519-
for keys.each |word| {
520-
words.insert(copy *word, ());
517+
do vec::consume(keys) |_, s| {
518+
words.insert(s);
521519
}
522-
words
520+
return words;
523521
}
524522

525523

0 commit comments

Comments
 (0)