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 |
|
---|
79 | class Token : public std::string
|
---|
80 | {
|
---|
81 | //ã¡ã³ãã®é è½ã¯ããªã
|
---|
82 | public:
|
---|
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 |
|
---|
117 | class Parser
|
---|
118 | {
|
---|
119 | public:
|
---|
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 |
|
---|
130 | protected:
|
---|
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 |
|
---|
164 | public:
|
---|
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 |
|
---|
199 | class ParseUnit
|
---|
200 | {
|
---|
201 | protected:
|
---|
202 | static Token & parseParameter(Parser &);
|
---|
203 | static int parseParameters(Parser &, Directory *, int, int=0);
|
---|
204 | static int parseParameters(Parser &, Directory *, const char *);
|
---|
205 |
|
---|
206 | public:
|
---|
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 |
|
---|
220 | class StaticAPI : public ParseUnit
|
---|
221 | {
|
---|
222 | protected:
|
---|
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 |
|
---|
228 | public:
|
---|
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 |
|
---|
242 | class Directive : public ParseUnit
|
---|
243 | {
|
---|
244 | public:
|
---|
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 |
|
---|
255 | class ParserComponent : public Component
|
---|
256 | {
|
---|
257 | protected:
|
---|
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 |
|
---|
270 | public:
|
---|
271 | ParserComponent(void) throw();
|
---|
272 | virtual ~ParserComponent(void) throw();
|
---|
273 | };
|
---|
274 |
|
---|
275 | //---
|
---|
276 |
|
---|
277 | namespace 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 |
|
---|
288 | inline Token::Token(enum tagTokenType type, const char * term)
|
---|
289 | {
|
---|
290 | type = type;
|
---|
291 | value = 0;
|
---|
292 | assign(term);
|
---|
293 | }
|
---|
294 |
|
---|
295 | inline 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 |
|
---|
304 | inline 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 |
|
---|
317 | inline 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 |
|
---|
323 | inline 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 |
|
---|
337 | inline void Parser::putBack(Token & token)
|
---|
338 | { TokenStack.push_front(token); }
|
---|
339 |
|
---|
340 | inline 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 |
|
---|
351 | inline void StaticAPI::dropLastObject(void)
|
---|
352 | {
|
---|
353 | if(last != NULL)
|
---|
354 | {
|
---|
355 | last->erase();
|
---|
356 | last = NULL;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | inline 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 |
|
---|
368 | inline 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 |
|
---|