diff options
author | John Hodge <tpg@mutabah.net> | 2016-10-28 17:18:13 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-10-28 17:18:13 +0800 |
commit | 29e4c5f324c9af774e3c89a5de8d4f759ba493a5 (patch) | |
tree | e54f1763f668d47d2977005366b89da84e63b4ea /src | |
parent | ae297391200c96a43c811afd37a3260af3b2fc51 (diff) | |
download | mrust-29e4c5f324c9af774e3c89a5de8d4f759ba493a5.tar.gz |
Expand include! - Support paths relative to a file
Diffstat (limited to 'src')
-rw-r--r-- | src/expand/include.cpp | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/src/expand/include.cpp b/src/expand/include.cpp index a165f96f..b7fe0f53 100644 --- a/src/expand/include.cpp +++ b/src/expand/include.cpp @@ -12,6 +12,35 @@ #include <parse/lex.hpp> #include <ast/expr.hpp> +namespace { + ::std::string get_path_relative_to(const ::std::string& base_path, ::std::string path) + { + if( base_path.size() == 0 ) { + return path; + } + else if( base_path[base_path.size()-1] == '/' ) { + return base_path + path; + } + else { + + auto slash = base_path.find_last_of('/'); + if( slash == ::std::string::npos ) + { + return path; + } + else + { + slash += 1; + ::std::string rv; + rv.reserve( slash + path.size() ); + rv.append( base_path.begin(), base_path.begin() + slash ); + rv.append( path.begin(), path.end() ); + return rv; + } + } + } +}; + class CIncludeExpander: public ExpandProcMacro { @@ -23,24 +52,19 @@ class CIncludeExpander: Token tok; auto lex = TTStream(tt); + // TODO: Parse+expand 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 << "'"); - } + ::std::string file_path = get_path_relative_to(mod.m_file_info.path, mv$(path)); return box$( Lexer(file_path) ); } }; +// TODO: include_str! and include_bytes! + STATIC_MACRO("include", CIncludeExpander); |