summary refs log tree commit diff
path: root/src/comp/syntax/ext/base.rs
diff options
context:
space:
mode:
authorMarijn Haverbeke <marijnh@gmail.com>2011-07-05 11:48:19 +0200
committerMarijn Haverbeke <marijnh@gmail.com>2011-07-05 15:57:21 +0200
commit6fd6fdea93fca19f168526943c177f942212cbc6 (patch)
tree967778dac7798a33628a4e7992f2d26492e9d71f /src/comp/syntax/ext/base.rs
parentc59ebf0f018b748011fc9b23ce0bab3dcfcfe733 (diff)
downloadrust-6fd6fdea93fca19f168526943c177f942212cbc6.tar.gz
rust-6fd6fdea93fca19f168526943c177f942212cbc6.zip
Move everything syntax-related to syntax/, break deps on rest of compiler
src/comp/syntax is currently just a sub-module of rustc, but it will,
in the near future, be its own crate. This includes:

 - The AST data structure
 - The parser
 - The pretty-printer
 - Visit, walk, and fold
 - The syntax extension system
 - Some utility stuff that should be in the stdlib*

*) Stdlib extensions currently require a snapshot before they can be
   used, and the win build is very broken right now. This is temporary
   and will be cleaned up when one of those problems goes away.

A lot of code was moved by this patch, mostly towards a more organized
layout. Some package paths did get longer, and I guess the new layout
will take some getting used to. Sorry about that!

Please try not to re-introduce any dependencies in syntax/ on any of
the other src/comp/ subdirs.
Diffstat (limited to 'src/comp/syntax/ext/base.rs')
-rw-r--r--src/comp/syntax/ext/base.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/comp/syntax/ext/base.rs b/src/comp/syntax/ext/base.rs
new file mode 100644
index 00000000000..719e96f5fe6
--- /dev/null
+++ b/src/comp/syntax/ext/base.rs
@@ -0,0 +1,98 @@
+import std::vec;
+import std::option;
+import std::map::hashmap;
+import parse::parser::parse_sess;
+import codemap::span;
+import syntax::_std::new_str_hash;
+import codemap;
+
+type syntax_expander = 
+    fn(&ext_ctxt, span, &vec[@ast::expr], option::t[str]) -> @ast::expr;
+type macro_definer = fn(&ext_ctxt, span, &vec[@ast::expr],
+                        option::t[str]) -> tup(str, syntax_extension);
+
+tag syntax_extension {
+    normal(syntax_expander);
+    macro_defining(macro_definer);
+}
+
+// A temporary hard-coded map of methods for expanding syntax extension
+// AST nodes into full ASTs
+fn syntax_expander_table() -> hashmap[str, syntax_extension] {
+    auto syntax_expanders = new_str_hash[syntax_extension]();
+    syntax_expanders.insert("fmt", normal(ext::fmt::expand_syntax_ext));
+    syntax_expanders.insert("env", normal(ext::env::expand_syntax_ext));
+    syntax_expanders.insert("macro",    
+                            macro_defining(ext::simplext::add_new_extension));
+    ret syntax_expanders;
+}
+
+type span_msg_fn = fn(span, str) -> !  ;
+
+type next_id_fn = fn() -> ast::node_id ;
+
+
+// Provides a limited set of services necessary for syntax extensions
+// to do their thing
+type ext_ctxt =
+    rec(span_msg_fn span_fatal,
+        span_msg_fn span_unimpl,
+        next_id_fn next_id);
+
+fn mk_ctxt(&parse_sess sess) -> ext_ctxt {
+    fn ext_span_fatal_(&codemap::codemap cm, span sp, str msg) -> ! {
+        codemap::emit_error(option::some(sp), msg, cm);
+        fail;
+    }
+    auto ext_span_fatal = bind ext_span_fatal_(sess.cm, _, _);
+    fn ext_span_unimpl_(&codemap::codemap cm, span sp, str msg) -> ! {
+        codemap::emit_error(option::some(sp), "unimplemented " + msg, cm);
+        fail;
+    }
+    auto ext_span_unimpl = bind ext_span_unimpl_(sess.cm, _, _);
+    auto ext_next_id = bind parse::parser::next_node_id(sess);
+    ret rec(span_fatal=ext_span_fatal,
+            span_unimpl=ext_span_unimpl,
+            next_id=ext_next_id);
+}
+
+fn expr_to_str(&ext_ctxt cx, @ast::expr expr, str error) -> str {
+    alt (expr.node) {
+        case (ast::expr_lit(?l)) {
+            alt (l.node) {
+                case (ast::lit_str(?s, _)) { ret s; }
+                case (_) { cx.span_fatal(l.span, error); }
+            }
+        }
+        case (_) { cx.span_fatal(expr.span, error); }
+    }
+}
+
+fn expr_to_ident(&ext_ctxt cx, @ast::expr expr, str error) -> ast::ident {
+    alt(expr.node) {
+        case (ast::expr_path(?p)) {
+            if (vec::len(p.node.types) > 0u 
+                || vec::len(p.node.idents) != 1u) {
+                cx.span_fatal(expr.span, error);
+            } else {
+                ret p.node.idents.(0);
+            }
+        }
+        case (_) {
+            cx.span_fatal(expr.span, error);
+        }
+    }
+}
+
+
+
+//
+// Local Variables:
+// mode: rust
+// fill-column: 78;
+// indent-tabs-mode: nil
+// c-basic-offset: 4
+// buffer-file-coding-system: utf-8-unix
+// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
+// End:
+//