ARB
aisc_token.h
Go to the documentation of this file.
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 '@'
25  TOK_EOS = TOK_SEMI, // simulate a semicolon at EOSTR
26 
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:
88  head = NULp;
89  tail = NULp;
90  next = NULp;
91  parent = NULp;
92  }
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:
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
TOKEN
Definition: aisc_token.h:18
void append(Token *tok)
Definition: aisc_token.h:61
const TokenList * parent_list() const
Definition: aisc_token.h:73
void set_parent(TokenList *list)
Definition: aisc_token.h:62
void append(TokenList *cmd)
Definition: aisc_token.h:105
Token(const char *key_, const char *val_)
Definition: aisc_token.h:50
char * val
Definition: aisc_token.h:46
const TokenListBlock * parent_block() const
Definition: aisc_token.h:111
TokenListBlock * sub
Definition: aisc_token.h:45
void set_block_token(Token *tok)
Definition: aisc_token.h:138
void set_parent(TokenListBlock *block)
Definition: aisc_token.h:106
void append(Token *tok)
Definition: aisc_token.h:98
#define true
Definition: ureadseq.h:14
#define false
Definition: ureadseq.h:13
const TokenList * next_list() const
Definition: aisc_token.h:110
const TokenList * first_list() const
Definition: aisc_token.h:140
const char * get_key() const
Definition: aisc_token.h:64
const Token * next_token() const
Definition: aisc_token.h:72
const char * get_value() const
Definition: aisc_token.h:70
const Token * first_token() const
Definition: aisc_token.h:109
const Token * parent_block_token() const
Definition: aisc_token.h:165
~TokenList()
Definition: aisc_token.h:93
const Token * first_token() const
Definition: aisc_token.h:141
#define NULp
Definition: cxxforward.h:116
bool has_value() const
Definition: aisc_token.h:69
static ED4_block block
Definition: ED4_block.cxx:74
const Token * block_token() const
Definition: aisc_token.h:142
~Token()
Definition: aisc_token.h:159
#define aisc_assert(cond)
Definition: aisc_def.h:11
const TokenListBlock * get_content() const
Definition: aisc_token.h:67
bool empty() const
Definition: aisc_token.h:130
void append(TokenList *cmd)
Definition: aisc_token.h:131
bool empty() const
Definition: aisc_token.h:108
bool is_block() const
Definition: aisc_token.h:66