summaryrefslogtreecommitdiff
path: root/src
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 /src
parent4bf7b149e0e41327a6809dd9571fca4b2e9c7917 (diff)
downloadmrust-1f57abaf71ab7b557aca2bd78784b1078ad14358.tar.gz
Expand - hack in an include! macro
Diffstat (limited to 'src')
-rw-r--r--src/expand/include.cpp46
1 files changed, 46 insertions, 0 deletions
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);
+
+