1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#ifndef MACROS_HPP_INCLUDED
#define MACROS_HPP_INCLUDED
#include "parse/lex.hpp"
#include "parse/tokentree.hpp"
#include <map>
#include <memory>
#include <cstring>
class MacroExpander;
class MacroRuleEnt
{
friend class MacroExpander;
Token tok;
::std::string name;
public:
MacroRuleEnt(Token tok):
tok(tok),
name("")
{
}
MacroRuleEnt(::std::string name):
name(name)
{
}
};
struct MacroPatEnt
{
Token tok;
::std::string name;
enum Type {
PAT_TOKEN,
PAT_TT,
PAT_IDENT,
PAT_PATH,
PAT_EXPR,
PAT_STMT,
PAT_BLOCK,
} type;
MacroPatEnt(::std::string name, Type type):
tok(),
name(name),
type(type)
{
}
};
/// A rule within a macro_rules! blcok
class MacroRule
{
public:
::std::vector<MacroPatEnt> m_pattern;
::std::vector<MacroRuleEnt> m_contents;
};
/// A sigle 'macro_rules!' block
typedef ::std::vector<MacroRule> MacroRules;
struct cmp_str {
bool operator()(const char* a, const char* b) const {
return ::std::strcmp(a, b) < 0;
}
};
class MacroExpander:
public TokenStream
{
typedef ::std::map<const char*, TokenTree, cmp_str> t_mappings;
const t_mappings m_mappings;
const ::std::vector<MacroRuleEnt>& m_contents;
size_t m_ofs;
::std::auto_ptr<TTStream> m_ttstream;
public:
MacroExpander(const MacroExpander& x):
m_mappings(x.m_mappings),
m_contents(x.m_contents),
m_ofs(0)
{
}
MacroExpander(const ::std::vector<MacroRuleEnt>& contents, t_mappings mappings):
m_mappings(mappings),
m_contents(contents),
m_ofs(0)
{
}
virtual Token realGetToken();
};
extern MacroExpander Macro_Invoke(const char* name, TokenTree input);
#endif // MACROS_HPP_INCLUDED
|