Skip to content

Commit 57ffa2a

Browse files
committed
Make the parser more careful about keywords
Keywords are now only recognized in contexts where they are valid. The lexer no longer recognizes them, all words are lexed as IDENT tokens, that get interpreted by the parser.
1 parent be9aa1c commit 57ffa2a

File tree

6 files changed

+612
-1152
lines changed

6 files changed

+612
-1152
lines changed

src/comp/front/lexer.rs

Lines changed: 1 addition & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ state type reader = state obj {
2222
fn add_str(str) -> token::str_num;
2323
fn get_str(token::str_num) -> str;
2424
fn get_chpos() -> uint;
25-
fn get_keywords() -> hashmap[str,token::token];
26-
fn get_reserved() -> hashmap[str,()];
2725
fn get_filemap() -> codemap::filemap;
2826
fn err(str m);
2927
};
@@ -39,8 +37,6 @@ fn new_reader(session sess, io::reader rdr,
3937
mutable uint mark_chpos,
4038
mutable uint chpos,
4139
mutable vec[str] strs,
42-
hashmap[str,token::token] keywords,
43-
hashmap[str,()] reserved,
4440
codemap::filemap fm) {
4541

4642
fn is_eof() -> bool {
@@ -82,10 +78,6 @@ fn new_reader(session sess, io::reader rdr,
8278
}
8379
}
8480

85-
fn get_keywords() -> hashmap[str,token::token] {
86-
ret keywords;
87-
}
88-
8981
fn add_str(str s) -> token::str_num {
9082
strs += vec(s);
9183
ret _vec::len[str](strs) - 1u;
@@ -95,10 +87,6 @@ fn new_reader(session sess, io::reader rdr,
9587
ret strs.(i);
9688
}
9789

98-
fn get_reserved() -> hashmap[str,()] {
99-
ret reserved;
100-
}
101-
10290
fn get_filemap() -> codemap::filemap {
10391
ret fm;
10492
}
@@ -111,133 +99,11 @@ fn new_reader(session sess, io::reader rdr,
11199
let vec[str] strs = vec();
112100
auto rd = reader(sess, file, _str::byte_len(file), 0u, -1 as char,
113101
filemap.start_pos, filemap.start_pos,
114-
strs, keyword_table(),
115-
reserved_word_table(),
116-
filemap);
102+
strs, filemap);
117103
rd.init();
118104
ret rd;
119105
}
120106

121-
fn keyword_table() -> std::map::hashmap[str, token::token] {
122-
auto keywords = new_str_hash[token::token]();
123-
124-
keywords.insert("mod", token::MOD);
125-
keywords.insert("use", token::USE);
126-
keywords.insert("meta", token::META);
127-
keywords.insert("auth", token::AUTH);
128-
129-
keywords.insert("syntax", token::SYNTAX);
130-
131-
keywords.insert("if", token::IF);
132-
keywords.insert("else", token::ELSE);
133-
keywords.insert("while", token::WHILE);
134-
keywords.insert("do", token::DO);
135-
keywords.insert("alt", token::ALT);
136-
keywords.insert("case", token::CASE);
137-
138-
keywords.insert("for", token::FOR);
139-
keywords.insert("each", token::EACH);
140-
keywords.insert("break", token::BREAK);
141-
keywords.insert("cont", token::CONT);
142-
keywords.insert("put", token::PUT);
143-
keywords.insert("ret", token::RET);
144-
keywords.insert("be", token::BE);
145-
146-
keywords.insert("fail", token::FAIL);
147-
keywords.insert("drop", token::DROP);
148-
149-
keywords.insert("type", token::TYPE);
150-
keywords.insert("check", token::CHECK);
151-
keywords.insert("assert", token::ASSERT);
152-
keywords.insert("claim", token::CLAIM);
153-
keywords.insert("prove", token::PROVE);
154-
155-
keywords.insert("state", token::STATE);
156-
keywords.insert("gc", token::GC);
157-
158-
keywords.insert("unsafe", token::UNSAFE);
159-
160-
keywords.insert("native", token::NATIVE);
161-
keywords.insert("mutable", token::MUTABLE);
162-
keywords.insert("auto", token::AUTO);
163-
164-
keywords.insert("fn", token::FN);
165-
keywords.insert("pred", token::PRED);
166-
keywords.insert("iter", token::ITER);
167-
168-
keywords.insert("import", token::IMPORT);
169-
keywords.insert("export", token::EXPORT);
170-
171-
keywords.insert("let", token::LET);
172-
keywords.insert("const", token::CONST);
173-
174-
keywords.insert("log", token::LOG);
175-
keywords.insert("log_err", token::LOG_ERR);
176-
keywords.insert("spawn", token::SPAWN);
177-
keywords.insert("thread", token::THREAD);
178-
keywords.insert("yield", token::YIELD);
179-
keywords.insert("join", token::JOIN);
180-
181-
keywords.insert("bool", token::BOOL);
182-
183-
keywords.insert("int", token::INT);
184-
keywords.insert("uint", token::UINT);
185-
keywords.insert("float", token::FLOAT);
186-
187-
keywords.insert("char", token::CHAR);
188-
keywords.insert("str", token::STR);
189-
190-
191-
keywords.insert("rec", token::REC);
192-
keywords.insert("tup", token::TUP);
193-
keywords.insert("tag", token::TAG);
194-
keywords.insert("vec", token::VEC);
195-
keywords.insert("any", token::ANY);
196-
197-
keywords.insert("obj", token::OBJ);
198-
keywords.insert("self", token::SELF);
199-
200-
keywords.insert("port", token::PORT);
201-
keywords.insert("chan", token::CHAN);
202-
203-
keywords.insert("task", token::TASK);
204-
205-
keywords.insert("true", token::LIT_BOOL(true));
206-
keywords.insert("false", token::LIT_BOOL(false));
207-
208-
keywords.insert("in", token::IN);
209-
210-
keywords.insert("as", token::AS);
211-
keywords.insert("with", token::WITH);
212-
213-
keywords.insert("bind", token::BIND);
214-
215-
keywords.insert("u8", token::MACH(common::ty_u8));
216-
keywords.insert("u16", token::MACH(common::ty_u16));
217-
keywords.insert("u32", token::MACH(common::ty_u32));
218-
keywords.insert("u64", token::MACH(common::ty_u64));
219-
keywords.insert("i8", token::MACH(common::ty_i8));
220-
keywords.insert("i16", token::MACH(common::ty_i16));
221-
keywords.insert("i32", token::MACH(common::ty_i32));
222-
keywords.insert("i64", token::MACH(common::ty_i64));
223-
keywords.insert("f32", token::MACH(common::ty_f32));
224-
keywords.insert("f64", token::MACH(common::ty_f64));
225-
226-
ret keywords;
227-
}
228-
229-
fn reserved_word_table() -> std::map::hashmap[str, ()] {
230-
auto reserved = new_str_hash[()]();
231-
reserved.insert("f16", ()); // IEEE 754-2008 'binary16' interchange fmt
232-
reserved.insert("f80", ()); // IEEE 754-1985 'extended'
233-
reserved.insert("f128", ()); // IEEE 754-2008 'binary128'
234-
reserved.insert("m32", ()); // IEEE 754-2008 'decimal32'
235-
reserved.insert("m64", ()); // IEEE 754-2008 'decimal64'
236-
reserved.insert("m128", ()); // IEEE 754-2008 'decimal128'
237-
reserved.insert("dec", ()); // One of m32, m64, m128
238-
ret reserved;
239-
}
240-
241107
fn in_range(char c, char lo, char hi) -> bool {
242108
ret lo <= c && c <= hi;
243109
}
@@ -604,17 +470,6 @@ fn next_token(reader rdr) -> token::token {
604470
ret token::UNDERSCORE;
605471
}
606472

607-
auto kwds = rdr.get_keywords();
608-
if (kwds.contains_key(accum_str)) {
609-
ret kwds.get(accum_str);
610-
}
611-
612-
auto rsvd = rdr.get_reserved();
613-
if (rsvd.contains_key(accum_str)) {
614-
rdr.err(#fmt("reserved keyword: %s", accum_str));
615-
fail;
616-
}
617-
618473
ret token::IDENT(rdr.add_str(accum_str));
619474
}
620475

0 commit comments

Comments
 (0)