From 1f57abaf71ab7b557aca2bd78784b1078ad14358 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 27 Sep 2016 22:20:34 +0800 Subject: Expand - hack in an include! macro --- Makefile | 1 + src/expand/include.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/expand/include.cpp 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 +#include +#include // for GET_CHECK_TOK +#include // TTStream +#include +#include + +class CIncludeExpander: + public ExpandProcMacro +{ + ::std::unique_ptr 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); + + -- cgit v1.2.3