summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-09-27 22:20:34 +0800
committerJohn Hodge <tpg@mutabah.net>2016-09-27 22:20:34 +0800
commit1f57abaf71ab7b557aca2bd78784b1078ad14358 (patch)
treea5738b78f9b4fd51c0b44d11032b667dd75b5b62
parent4bf7b149e0e41327a6809dd9571fca4b2e9c7917 (diff)
downloadmrust-1f57abaf71ab7b557aca2bd78784b1078ad14358.tar.gz
Expand - hack in an include! macro
-rw-r--r--Makefile1
-rw-r--r--src/expand/include.cpp46
2 files changed, 47 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 08783e82..9c4ade8b 100644
--- a/Makefile
+++ b/Makefile
@@ -50,6 +50,7 @@ OBJ += expand/format_args.o expand/asm.o
OBJ += expand/concat.o expand/stringify.o expand/file_line.o
OBJ += expand/derive.o expand/lang_item.o
OBJ += expand/std_prelude.o expand/crate_tags.o
+OBJ += expand/include.o
OBJ += macro_rules/mod.o macro_rules/eval.o macro_rules/parse.o
OBJ += resolve/use.o resolve/index.o resolve/absolute.o
OBJ += hir/from_ast.o hir/from_ast_expr.o
diff --git a/src/expand/include.cpp b/src/expand/include.cpp
new file mode 100644
index 00000000..a165f96f
--- /dev/null
+++ b/src/expand/include.cpp
@@ -0,0 +1,46 @@
+/*
+ * MRustC - Rust Compiler
+ * - By John Hodge (Mutabah/thePowersGang)
+ *
+ * expand/include.cpp
+ * - include!/include_str!/include_bytes! support
+ */
+#include <synext_macro.hpp>
+#include <parse/common.hpp>
+#include <parse/parseerror.hpp> // for GET_CHECK_TOK
+#include <parse/tokentree.hpp> // TTStream
+#include <parse/lex.hpp>
+#include <ast/expr.hpp>
+
+class CIncludeExpander:
+ public ExpandProcMacro
+{
+ ::std::unique_ptr<TokenStream> expand(const Span& sp, const AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override
+ {
+ if( ident != "" )
+ ERROR(sp, E0000, "include! doesn't take an ident");
+
+ Token tok;
+ auto lex = TTStream(tt);
+
+ GET_CHECK_TOK(tok, lex, TOK_STRING);
+ auto path = mv$(tok.str());
+ GET_CHECK_TOK(tok, lex, TOK_EOF);
+
+ auto base_path = mod.m_file_info.path;
+
+ ::std::string file_path;
+ if( base_path[base_path.size()-1] == '/' ) {
+ file_path = base_path + path;
+ }
+ else {
+ TODO(sp, "Open '" << path << "' relative to '" << base_path << "'");
+ }
+
+ return box$( Lexer(file_path) );
+ }
+};
+
+STATIC_MACRO("include", CIncludeExpander);
+
+