source: anotherchoice/tags/jsp-1.4.4-full-UTF8/cfg/base/parser.h@ 26

Last change on this file since 26 was 26, checked in by ykominami, 12 years ago

initial

File size: 11.6 KB
RevLine 
[26]1/*
2 * TOPPERS/JSP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 *
9 * 上記著作権者
10は,以下の (1)〜(4) の条件か,Free Software Foundation
11 * によってå…
12¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
13 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
14 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
15å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
16 * 利用と呼ぶ)することを無償で許諾する.
17 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
18 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
19 * スコード中に含まれていること.
20 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
21 * 用できる形で再é…
22å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
23å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
24 * 者
25マニュアルなど)に,上記の著作権表示,この利用条件および下記
26 * の無保証規定を掲載すること.
27 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
28 * 用できない形で再é…
29å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
30 * と.
31 * (a) 再é…
32å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
33マニュアルなど)に,上記の著
34 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
35 * (b) 再é…
36å¸ƒã®å½¢æ…
37‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
38 * 報告すること.
39 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
40 * 害からも,上記著作権者
41およびTOPPERSプロジェクトをå…
42è²¬ã™ã‚‹ã“と.
43 *
44 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
45お
46 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
47 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
48 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
49 *
50 * @(#) $Id: parser.h,v 1.14 2003/12/20 06:32:48 takayuki Exp $
51 */
52
53
54// $Header: /home/CVS/configurator/base/parser.h,v 1.14 2003/12/20 06:32:48 takayuki Exp $
55
56#ifndef PARSER_H
57#define PARSER_H
58
59#include "base/defs.h"
60
61#include "base/message.h"
62
63#include "base/directorymap.h"
64#include "base/component.h"
65
66#include <stdarg.h>
67#include <iostream>
68#include <list>
69#include <string>
70#include <fstream>
71#include <sstream>
72
73#define PARSERESULT "/parse_result"
74
75#ifndef ERROR
76#undef ERROR /* MinGW環境だとwingdi.hでERRORが定義される */
77#endif
78
79class Token : public std::string
80{
81 //メンバの隠蔽はしない
82public:
83 enum tagTokenType
84 {
85 IDENTIFIER = 0x01,
86 INTEGER = 0x02,
87 STRINGLITERAL = 0x03,
88 STRING = 0x04,
89 OPERATOR = 0x05,
90 PUNCTUATOR = 0x06,
91 RESERVEDWORD = 0x07,
92 SPECIAL = 0x80,
93 SPACE = 0x81,
94 UNKNOWN = 0xfd,
95 ERROR = 0xfe,
96 EOS = 0xff //End of Stream
97 };
98
99 enum tagTokenType type;
100 long value;
101 unsigned int line;
102
103 Token(void) { type = UNKNOWN; value = 0; };
104 Token(const Token & src) { (*this) = src; };
105 Token(enum tagTokenType, const char *);
106
107 operator const enum tagTokenType (void) const { return type; };
108
109 Token & operator =(const Token & src);
110 bool operator == (const Token & src) const;
111
112 Token & trim(void);
113 Token & chopLiteral(void);
114 void confirm(const char *) const;
115};
116
117class Parser
118{
119public:
120 enum tagFunctionarities
121 { UNKNOWN = 0, DIRECTIVE = 1, LOGGING = 2 };
122
123 struct tagFile
124 {
125 std::string identifier;
126 std::istream * stream;
127 unsigned int line;
128 };
129
130protected:
131 static const char * Punctuator;
132 static const char * Operator;
133 static Token lastErrorToken;
134
135 std::string * LogBuffer;
136 unsigned int PutBackCount;
137 bool isHeadofLine;
138
139 Directory * Container;
140
141 tagFile * current;
142 std::list<Token> TokenStack;
143 std::list<tagFile *> fileStack;
144 int functionalities;
145
146 std::string preprocessname; //プリプロセッサを通すときに使った名前
147 std::string originalname; //本来の名前
148
149 bool parseDirectives(Token &, int, bool);
150
151 bool getIdentifier(Token &, int);
152 bool getWhitespace(Token &, int, bool);
153 bool getInteger(Token &, int);
154 bool getStringLiteral(Token &, int);
155 bool getOperator(Token &, int);
156
157 int getChar(void);
158 void putBack(int);
159
160 void initialize(void) { current = 0; functionalities = DIRECTIVE|LOGGING; PutBackCount = 0; LogBuffer = 0; isHeadofLine = true; };
161
162 bool isenabled(enum tagFunctionarities func) { return (functionalities & (int)func) != 0; };
163
164public:
165 Parser(Directory & cnt) : Container(&cnt) { initialize(); };
166 Parser(Directory * cnt) : Container(cnt) { initialize(); };
167 ~Parser(void);
168
169 void pushStream(const std::string & filename, std::string = "");
170 void pushStdStream(std::string = "Standard Input");
171
172 void setStreamIdentifier(const std::string & id);
173 void setCurrentLine(unsigned int pos) { current->line = pos; };
174 unsigned int getCurrentLine(void);
175 const char * getStreamIdentifier(void);
176 std::string getStreamLocation(void);
177
178 enum Token::tagTokenType getToken(Token &,bool = false);
179 void getToken(Token &,enum Token::tagTokenType, const char * = NULL);
180 void getToken(const char *) throw(Exception);
181 void getToken(const char *, const char * , const char * = 0, const char * = 0) throw(Exception);
182
183 void putBack(Token &);
184
185 static const Token & getLastErrorToken(void) { return lastErrorToken; };
186
187 void enable(enum tagFunctionarities func) { functionalities |= (int)func; };
188 void disable(enum tagFunctionarities func) { functionalities &= ~(int)func; };
189
190 std::string * setLogBuffer(std::string * buffer);
191 std::streampos getLogBufferPos(int offset = 0);
192
193 void doPreProcess(const char * cmd);
194};
195
196
197//---
198
199class ParseUnit
200{
201protected:
202 static Token & parseParameter(Parser &);
203 static int parseParameters(Parser &, Directory *, int, int=0);
204 static int parseParameters(Parser &, Directory *, const char *);
205
206public:
207 ParseUnit(void *, const char *);
208 virtual ~ParseUnit(void) {};
209
210 const char * getIdentifier(void) const;
211 virtual void body(const std::string &, Directory &, Parser &, const std::string &) =0;
212
213 static void printList(void * container);
214};
215
216#define __DECLARE_PARSEUNIT(x,y,z) class x##_##y : public x { public: x##_##y(void) : x(z) {}; protected: virtual void body(const std::string &, Directory &, Parser &, const std::string &); } instance_of_##x##_##y; void x##_##y::body(const std::string & identifier, Directory & container, Parser & p, const std::string & domain)
217
218//---
219
220class StaticAPI : public ParseUnit
221{
222protected:
223 static Directory * last;
224 Directory * allocate(Parser & p, Directory &, const Token &, const char *, bool = true);
225 Directory * allocate(Directory &, const Token &, const char *, bool = true);
226 Directory * find (Directory &, const Token &, const char *);
227
228public:
229 StaticAPI(const char * name) : ParseUnit(&(container()), name) {};
230
231 static void printList(void) { ParseUnit::printList(&(container())); };
232 static void clearLastObject(void) { last = NULL; };
233 static void dropLastObject(void);
234
235 static std::map<std::string, class ParseUnit *> & container(void);
236};
237
238#define DECLARE_API(x,y) __DECLARE_PARSEUNIT(StaticAPI,x,y)
239
240//---
241
242class Directive : public ParseUnit
243{
244public:
245 static std::map<std::string, class ParseUnit *> & container(void);
246
247 Directive(const char * name) : ParseUnit(&(container()), name) {};
248 static void printList(void) { ParseUnit::printList(&(container())); }
249};
250
251#define DECLARE_DIRECTIVE(x,y) __DECLARE_PARSEUNIT(Directive,x,y)
252
253//---
254
255class ParserComponent : public Component
256{
257protected:
258 int failCount;
259 bool ignoreUnknownAPI;
260
261 static void throughConfigurationFile(std::string & log, Directory & container);
262
263 virtual void parseOption(Directory &);
264 virtual void body(Directory &);
265
266 bool parseStaticAPI(Parser & p, Directory & container, Token token, const std::string = "");
267
268 virtual bool parse(Parser & p, Directory & container) = 0;
269
270public:
271 ParserComponent(void) throw();
272 virtual ~ParserComponent(void) throw();
273};
274
275//---
276
277namespace Common {
278
279 enum tagAssignmentOrder { UNKNOWN, ALPHABETIC, FCFS, REVERSE=0x80, REVERSE_ALPHABETIC, REVERSE_FCFS };
280
281 enum tagAssignmentOrder parseOrder(Directory * order_option_node);
282 enum tagAssignmentOrder parseOrder(OptionParameter::OptionItem order_option_node);
283 int assignID(Directory & container, const char * category, const char * top, enum tagAssignmentOrder = FCFS);
284}
285
286//---
287
288inline Token::Token(enum tagTokenType type, const char * term)
289{
290 type = type;
291 value = 0;
292 assign(term);
293}
294
295inline Token & Token::operator =(const Token & src)
296{
297 type = src.type;
298 value = src.value;
299 line = src.line;
300 assign(src);
301 return *this;
302}
303
304inline bool Token::operator ==(const Token & src) const
305{
306 if(type != src.type)
307 return false;
308 if(type == Token::INTEGER && value != src.value)
309 return false;
310 else
311 if(compare(src) != 0)
312 return false;
313
314 return true;
315}
316
317inline void Token::confirm(const char * str) const
318{
319 if(compare(str) != 0)
320 ExceptionMessage("Illegal token (%) appears during parse process.","字句解析の途中で不正なトークン(%)が出現しました") << str << throwException;
321}
322
323inline void Parser::getToken(Token & token, enum Token::tagTokenType type, const char * term)
324{
325 getToken(token, type == Token::SPACE);
326
327 if(type == Token::STRING && token == Token::STRINGLITERAL)
328 token.chopLiteral();
329
330 if((type != token) || (term != NULL && token.compare(term) != 0))
331 {
332 lastErrorToken = token;
333 ExceptionMessage("Parse error on reading [%]","字句解析のエラー [%]") << token << throwException;
334 }
335}
336
337inline void Parser::putBack(Token & token)
338{ TokenStack.push_front(token); }
339
340inline Directory * StaticAPI::find(Directory & container, const Token & token, const char * id)
341{
342 Directory * node;
343
344 node = container.findChild(id,token.c_str(),NULL);
345 if(node == 0)
346 ExceptionMessage("The object %(%) does not exist.","オブジェクト%(%)は未定義です") << token << throwException;
347
348 return node;
349}
350
351inline void StaticAPI::dropLastObject(void)
352{
353 if(last != NULL)
354 {
355 last->erase();
356 last = NULL;
357 }
358}
359
360inline Directory * StaticAPI::allocate(Parser & p, Directory & container, const Token & token, const char * category, bool regist)
361{
362 Directory * node = allocate(container, token, category, regist);
363 if(node != 0)
364 (*node)["position"] = p.getStreamLocation();
365 return node;
366}
367
368inline void Parser::setStreamIdentifier(const std::string & id)
369{
370 if(preprocessname.compare(id) != 0)
371 current->identifier = id;
372 else
373 current->identifier = originalname;
374}
375
376#endif
377
Note: See TracBrowser for help on using the repository browser.