Skip to content

Commit af46f3e

Browse files
committed
rustc: Introduce ext module. Move some things from parser to ext.
Introduce an ext_ctxt record to provide a span_err method for use while expanding syntax extensions. Hopefully it will be useful for other things.
1 parent dbd066a commit af46f3e

File tree

5 files changed

+68
-24
lines changed

5 files changed

+68
-24
lines changed

src/comp/front/ext.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import std::option;
2+
import std::map::hashmap;
3+
4+
import driver::session::session;
5+
import util::common::span;
6+
import util::common::new_str_hash;
7+
8+
type syntax_expander = fn(&ext_ctxt, &parser::parser, span,
9+
&vec[@ast::expr],
10+
option::t[str]) -> @ast::expr;
11+
12+
// Temporary: to introduce a tag in order to make a recursive type work
13+
tag syntax_extension {
14+
x(syntax_expander);
15+
}
16+
17+
// A temporary hard-coded map of methods for expanding syntax extension
18+
// AST nodes into full ASTs
19+
fn syntax_expander_table() -> hashmap[str, syntax_extension] {
20+
auto syntax_expanders = new_str_hash[syntax_extension]();
21+
syntax_expanders.insert("fmt", x(extfmt::expand_syntax_ext));
22+
syntax_expanders.insert("env", x(extenv::expand_syntax_ext));
23+
ret syntax_expanders;
24+
}
25+
26+
type span_err_fn = fn (span sp, str msg) -> !;
27+
28+
// Provides a limited set of services necessary for syntax extensions
29+
// to do their thing
30+
type ext_ctxt = rec(span_err_fn span_err);
31+
32+
fn mk_ctxt(session sess) -> ext_ctxt {
33+
fn ext_span_err_(session sess, span sp, str err) -> ! {
34+
sess.span_err(sp, err);
35+
}
36+
auto ext_span_err = bind ext_span_err_(sess, _, _);
37+
ret rec(span_err = ext_span_err);
38+
}
39+
40+
//
41+
// Local Variables:
42+
// mode: rust
43+
// fill-column: 78;
44+
// indent-tabs-mode: nil
45+
// c-basic-offset: 4
46+
// buffer-file-coding-system: utf-8-unix
47+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
48+
// End:
49+
//

src/comp/front/extenv.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import std::vec;
1111
import std::option;
1212
import std::generic_os;
1313

14+
import ext::*;
15+
1416
export expand_syntax_ext;
1517

1618
// FIXME: Need to thread parser through here to handle errors correctly
17-
fn expand_syntax_ext(&parser::parser p,
19+
fn expand_syntax_ext(&ext_ctxt cx,
20+
&parser::parser p,
1821
common::span sp,
1922
&vec[@ast::expr] args,
2023
option::t[str] body) -> @ast::expr {

src/comp/front/extfmt.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import std::option::some;
1515

1616
import std::extfmt::ct::*;
1717

18+
import ext::*;
19+
1820
export expand_syntax_ext;
1921

20-
fn expand_syntax_ext(&parser p, common::span sp,
22+
fn expand_syntax_ext(&ext_ctxt cx,
23+
&parser p, common::span sp,
2124
&vec[@ast::expr] args,
2225
option::t[str] body) -> @ast::expr {
2326

src/comp/front/parser.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ tag file_type {
2727

2828
type ty_or_bang = util::common::ty_or_bang[@ast::ty];
2929

30-
// Temporary: to introduce a tag in order to make a recursive type work
31-
tag syntax_extension {
32-
x(syntax_expander);
33-
}
34-
3530
state type parser =
3631
state obj {
3732
fn peek() -> token::token;
@@ -53,15 +48,12 @@ state type parser =
5348
fn get_reader() -> lexer::reader;
5449
fn get_filemap() -> codemap::filemap;
5550
fn get_bad_expr_words() -> hashmap[str, ()];
56-
fn get_syntax_expanders() -> hashmap[str, syntax_extension];
51+
fn get_syntax_expanders() -> hashmap[str, ext::syntax_extension];
5752
fn get_chpos() -> uint;
5853
fn get_ann() -> ast::ann;
5954
fn next_ann_num() -> uint;
6055
};
6156

62-
type syntax_expander = fn(&parser, common::span, &vec[@ast::expr],
63-
option::t[str]) -> @ast::expr;
64-
6557
fn new_parser(session::session sess,
6658
eval::env env,
6759
ast::def_id initial_def,
@@ -80,7 +72,8 @@ fn new_parser(session::session sess,
8072
vec[op_spec] precs,
8173
mutable uint next_ann_var,
8274
hashmap[str, ()] bad_words,
83-
hashmap[str, syntax_extension] syntax_expanders)
75+
hashmap[str, ext::syntax_extension]
76+
syntax_expanders)
8477
{
8578
fn peek() -> token::token {
8679
ret tok;
@@ -153,7 +146,7 @@ fn new_parser(session::session sess,
153146
ret bad_words;
154147
}
155148

156-
fn get_syntax_expanders() -> hashmap[str, syntax_extension] {
149+
fn get_syntax_expanders() -> hashmap[str, ext::syntax_extension] {
157150
ret syntax_expanders;
158151
}
159152

@@ -183,7 +176,7 @@ fn new_parser(session::session sess,
183176
ret stdio_parser(sess, env, ftype, lexer::next_token(rdr),
184177
npos, npos, npos, initial_def._1, UNRESTRICTED,
185178
initial_def._0, rdr, prec_table(), next_ann,
186-
bad_expr_word_table(), syntax_expander_table());
179+
bad_expr_word_table(), ext::syntax_expander_table());
187180
}
188181

189182
// These are the words that shouldn't be allowed as value identifiers,
@@ -227,13 +220,6 @@ fn bad_expr_word_table() -> hashmap[str, ()] {
227220
ret words;
228221
}
229222

230-
fn syntax_expander_table() -> hashmap[str, syntax_extension] {
231-
auto syntax_expanders = new_str_hash[syntax_extension]();
232-
syntax_expanders.insert("fmt", x(extfmt::expand_syntax_ext));
233-
syntax_expanders.insert("env", x(extenv::expand_syntax_ext));
234-
ret syntax_expanders;
235-
}
236-
237223
fn unexpected(&parser p, token::token t) -> ! {
238224
let str s = "unexpected token: ";
239225
s += token::to_str(p.get_reader(), t);
@@ -1060,11 +1046,13 @@ fn expand_syntax_ext(&parser p, common::span sp,
10601046
auto extname = path.node.idents.(0);
10611047

10621048
alt (p.get_syntax_expanders().find(extname)) {
1063-
case (none[syntax_extension]) {
1049+
case (none) {
10641050
p.err("unknown syntax expander: '" + extname + "'");
10651051
}
1066-
case (some[syntax_extension](x(?ext))) {
1067-
ret ast::expr_ext(path, args, body, ext(p, sp, args, body),
1052+
case (some(ext::x(?ext))) {
1053+
auto ext_cx = ext::mk_ctxt(p.get_session());
1054+
ret ast::expr_ext(path, args, body,
1055+
ext(ext_cx, p, sp, args, body),
10681056
p.get_ann());
10691057
}
10701058
}

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod pretty {
3838
mod front {
3939
mod ast;
4040
mod creader;
41+
mod ext;
4142
mod extfmt;
4243
mod extenv;
4344
mod codemap;

0 commit comments

Comments
 (0)