1 | // Coded by Ralf Westram (coder@reallysoft.de) in March 2011 // |
---|
2 | // Institute of Microbiology (Technical University Munich) // |
---|
3 | // http://www.arb-home.de/ // |
---|
4 | |
---|
5 | #ifndef AISC_TOKEN_H |
---|
6 | #define AISC_TOKEN_H |
---|
7 | |
---|
8 | #ifndef AISC_DEF_H |
---|
9 | #include "aisc_def.h" |
---|
10 | #endif |
---|
11 | #ifndef ARBTOOLS_H |
---|
12 | #include <arbtools.h> |
---|
13 | #endif |
---|
14 | |
---|
15 | // ------------------------------------------------------------ |
---|
16 | // structures holding data read from *.aisc files |
---|
17 | |
---|
18 | enum TOKEN { |
---|
19 | TOK_WORD = 100, // normal words |
---|
20 | TOK_AT_WORD, // words starting with '@' |
---|
21 | TOK_BRACE_CLOSE, |
---|
22 | TOK_BRACE_OPEN, |
---|
23 | TOK_COMMA, |
---|
24 | TOK_SEMI, |
---|
25 | TOK_EOS = TOK_SEMI, // simulate a semicolon at EOSTR |
---|
26 | |
---|
27 | TOK_INVALID, |
---|
28 | }; |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | class TokenList; |
---|
33 | class TokenListBlock; |
---|
34 | |
---|
35 | // -------------- |
---|
36 | // Token |
---|
37 | |
---|
38 | class Token : virtual Noncopyable { |
---|
39 | Token *next; // (owned) |
---|
40 | TokenList *parent; |
---|
41 | |
---|
42 | bool isBlock; |
---|
43 | char *key; |
---|
44 | union { |
---|
45 | TokenListBlock *sub; // (owned), NULp = empty block |
---|
46 | char *val; // NULp means "" |
---|
47 | } content; |
---|
48 | |
---|
49 | public: |
---|
50 | Token(const char *key_, const char *val_) : |
---|
51 | next(NULp), |
---|
52 | parent(NULp), |
---|
53 | isBlock(false), |
---|
54 | key(strdup(key_)) |
---|
55 | { |
---|
56 | content.val = val_ ? strdup(val_) : NULp; |
---|
57 | } |
---|
58 | Token(const char *key_, TokenListBlock *block_); // takes ownage of block_ |
---|
59 | ~Token(); |
---|
60 | |
---|
61 | void append(Token *tok) { next = tok; } |
---|
62 | void set_parent(TokenList *list) { aisc_assert(!parent); parent = list; } |
---|
63 | |
---|
64 | const char *get_key() const { return key; } |
---|
65 | |
---|
66 | bool is_block() const { return isBlock; } |
---|
67 | const TokenListBlock *get_content() const { aisc_assert(isBlock); return content.sub; } |
---|
68 | |
---|
69 | bool has_value() const { return !is_block() && content.val; } |
---|
70 | const char *get_value() const { aisc_assert(has_value()); return content.val; } |
---|
71 | |
---|
72 | const Token *next_token() const { return next; } |
---|
73 | const TokenList *parent_list() const { return parent; } |
---|
74 | const Token *parent_block_token() const; |
---|
75 | }; |
---|
76 | |
---|
77 | // ------------------ |
---|
78 | // TokenList |
---|
79 | |
---|
80 | class TokenList : virtual Noncopyable { |
---|
81 | Token *head; // list of tokens (owned) |
---|
82 | Token *tail; |
---|
83 | TokenList *next; // owned |
---|
84 | TokenListBlock *parent; |
---|
85 | |
---|
86 | public: |
---|
87 | TokenList() { |
---|
88 | head = NULp; |
---|
89 | tail = NULp; |
---|
90 | next = NULp; |
---|
91 | parent = NULp; |
---|
92 | } |
---|
93 | ~TokenList() { |
---|
94 | delete head; |
---|
95 | delete next; |
---|
96 | } |
---|
97 | |
---|
98 | void append(Token *tok) { |
---|
99 | if (!head) head = tok; |
---|
100 | else tail->append(tok); |
---|
101 | |
---|
102 | tail = tok; |
---|
103 | tok->set_parent(this); |
---|
104 | } |
---|
105 | void append(TokenList *cmd) { next = cmd; } |
---|
106 | void set_parent(TokenListBlock *block) { parent = block; } |
---|
107 | |
---|
108 | bool empty() const { return !head; } |
---|
109 | const Token *first_token() const { return head; } |
---|
110 | const TokenList *next_list() const { return next; } |
---|
111 | const TokenListBlock *parent_block() const { return parent; } |
---|
112 | }; |
---|
113 | |
---|
114 | // ----------------------- |
---|
115 | // TokenListBlock |
---|
116 | |
---|
117 | class TokenListBlock : virtual Noncopyable { |
---|
118 | TokenList *head; // list of TokenLists (owned) |
---|
119 | TokenList *tail; |
---|
120 | const Token *parent; |
---|
121 | |
---|
122 | public: |
---|
123 | TokenListBlock() { |
---|
124 | head = NULp; |
---|
125 | tail = NULp; |
---|
126 | parent = NULp; |
---|
127 | } |
---|
128 | ~TokenListBlock() { delete head; } |
---|
129 | |
---|
130 | bool empty() const { return !head; } |
---|
131 | void append(TokenList *cmd) { |
---|
132 | if (!head) head = cmd; |
---|
133 | else tail->append(cmd); |
---|
134 | |
---|
135 | tail = cmd; |
---|
136 | cmd->set_parent(this); |
---|
137 | } |
---|
138 | void set_block_token(Token *tok) { aisc_assert(!parent); parent = tok; } |
---|
139 | |
---|
140 | const TokenList *first_list() const { return head; } |
---|
141 | const Token *first_token() const { return head->first_token(); } |
---|
142 | const Token *block_token() const { return parent; } |
---|
143 | }; |
---|
144 | |
---|
145 | // ------------------------------------------------------------ |
---|
146 | |
---|
147 | inline Token::Token(const char *key_, TokenListBlock *block_) : |
---|
148 | next(NULp), |
---|
149 | parent(NULp), |
---|
150 | isBlock(true), |
---|
151 | key(strdup(key_)) |
---|
152 | { |
---|
153 | aisc_assert(block_); |
---|
154 | content.sub = block_; |
---|
155 | if (content.sub) { |
---|
156 | content.sub->set_block_token(this); |
---|
157 | } |
---|
158 | } |
---|
159 | inline Token::~Token() { |
---|
160 | free(key); |
---|
161 | if (isBlock) delete content.sub; |
---|
162 | else free(content.val); |
---|
163 | delete next; |
---|
164 | } |
---|
165 | inline const Token *Token::parent_block_token() const { |
---|
166 | return parent_list()->parent_block()->block_token(); |
---|
167 | } |
---|
168 | |
---|
169 | #else |
---|
170 | #error aisc_token.h included twice |
---|
171 | #endif // AISC_TOKEN_H |
---|