diff options
author | John Hodge <tpg@mutabah.net> | 2016-03-10 14:39:19 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-03-10 14:39:19 +0800 |
commit | 191a03b6409811c49d8656e16ba2d3a58040b69c (patch) | |
tree | dc532da6266611f6be10ba7895d4697cd7eb1a67 | |
parent | c734ccaecf6b01be64fb4c837d0593d17a808b02 (diff) | |
download | mrust-191a03b6409811c49d8656e16ba2d3a58040b69c.tar.gz |
Expand - Stub format_args! impl
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/expand/format_args.cpp | 53 |
2 files changed, 54 insertions, 1 deletions
@@ -29,7 +29,7 @@ OBJ += ast/ast.o ast/crate.o ast/path.o ast/expr.o ast/pattern.o OBJ += ast/provided_module.o OBJ += parse/parseerror.o parse/lex.o OBJ += parse/root.o parse/paths.o parse/types.o parse/expr.o parse/pattern.o parse/macro_rules.o -OBJ += expand/mod.o expand/macro_rules.o expand/cfg.o +OBJ += expand/mod.o expand/macro_rules.o expand/cfg.o expand/format_args.o OBJ += dump_as_rust.o OBJ += convert/ast_iterate.o #OBJ += convert/decorators.o diff --git a/src/expand/format_args.cpp b/src/expand/format_args.cpp new file mode 100644 index 00000000..0c809601 --- /dev/null +++ b/src/expand/format_args.cpp @@ -0,0 +1,53 @@ +/* + */ +#include <synext.hpp> +#include "../parse/common.hpp" +#include "../parse/parseerror.hpp" +#include "../parse/tokentree.hpp" +#include "../parse/lex.hpp" + +class CFormatArgsExpander: + public ExpandProcMacro +{ + bool expand_early() const override { return true; } + + ::std::unique_ptr<TokenStream> expand(Span sp, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override + { + Token tok; + + auto lex = TTStream(tt); + if( ident != "" ) + ERROR(sp, E0000, "format_args! doesn't take an ident"); + + GET_CHECK_TOK(tok, lex, TOK_STRING); + auto format_string = mv$(tok.str()); + + // TODO: Interpolated expression "tokens" + ::std::map< ::std::string, TokenTree> named_args; + ::std::vector<TokenTree> free_args; + + while( GET_TOK(tok, lex) == TOK_COMMA ) + { + if( lex.lookahead(0) == TOK_IDENT && lex.lookahead(1) == TOK_EQUAL ) + { + GET_CHECK_TOK(tok, lex, TOK_IDENT); + auto name = mv$(tok.str()); + GET_CHECK_TOK(tok, lex, TOK_EQUAL); + auto expr_tt = Parse_TT_Expr(lex); + + named_args.insert( ::std::make_pair(mv$(name), mv$(expr_tt)) ); + } + else + { + auto expr_tt = Parse_TT_Expr(lex); + free_args.push_back( mv$(expr_tt) ); + } + } + + // TODO: Expand format_args! + return box$( TTStreamO(TokenTree(::std::vector<TokenTree>{TokenTree(TOK_PAREN_OPEN), TokenTree(TOK_PAREN_CLOSE)})) ); + } +}; + +STATIC_MACRO("format_args", CFormatArgsExpander); + |