Subversion Repositories spk

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
/*
2
  defines bod_text_parser - class that can parse bod syntax at general level
3
*/
4
 
5
#ifndef BOD_TEXT_PARSER_INCLUDED
6
#define BOD_TEXT_PARSER_INCLUDED
7
 
8
#include "../common/ext_list.h"
9
 
10
class bod_text_parser
11
{
12
	public:
114 cycrow 13
		enum ParseFlags
14
		{
15
			none=0,
16
			parseIgnoreSlash=1,
17
			parseEqual=2,
18
			parseStdBrackets=4,
19
			parseQuotedStrings=8,
20
			parseBODComments=16,
21
			parseCComments=32,
22
			parseOperators=64
23
		};
24
 
25
		class Error
26
		{
27
			private:
28
				static const char *m_messages[];
29
			public:
30
				enum Code {
31
					errNone,
32
					errNewLineInConstant
33
				};
34
				Code code;
35
				int col, line;
36
 
37
				Error() { col=line=-1; code=errNone; } 
38
 
39
				const char* message() const { return m_messages[code]; }
40
		};
41
 
42
		/*enum Error
43
		{
44
			errNone,
45
			errNewLineInConstant
46
		};*/
47
 
1 cycrow 48
		struct token
49
		{
114 cycrow 50
			/* 
51
				before messing with the order of the enums, 
52
				make sure it will not break X3_TOKEN* declarations in x3objects.h 
53
			*/
1 cycrow 54
			enum Type{
114 cycrow 55
				t_openCrBracket,
56
				t_closeCrBracket,
1 cycrow 57
				t_semicolon,
58
				t_colon,
114 cycrow 59
				t_equal,
1 cycrow 60
				t_openInstrBlock,
61
				t_closeInstrBlock,
114 cycrow 62
				t_hdrinfo,
63
				t_openStdBracket,
64
				t_closeStdBracket,
65
				t_slash,
66
				t_dot,
67
				t_plus,
1 cycrow 68
				t_text,
114 cycrow 69
				t_quotedString
1 cycrow 70
			};
71
 
72
			static const char* specialChars[];
73
			static char tabWidth;
74
 
75
			const char *text;
76
 
77
			int line;
78
			short col;
79
 
80
			Type type;
81
 
82
			token() { text=0; tabWidth=4; }
83
			token(const token& t) { text=t.text; line=t.line; col=t.col; type=t.type; }
84
			virtual ~token() { }
85
 
86
			const char* getText() const;
87
 
88
		};
89
 
90
	private:
91
		size_t m_lastPos;
92
		size_t m_newPos;
93
 
94
		const char *m_pszBuffer;
95
		size_t m_buffLen;
96
		int m_lineIdx;
97
 
114 cycrow 98
		Error m_error;
99
		ParseFlags m_parseFlags;
100
 
101
		bool testFlag(ParseFlags f) const { return (parseFlags() & f) > 0; }
102
 
103
		bool parseLine(char *line, int idx);
1 cycrow 104
 
114 cycrow 105
		void error(int line, int col, Error::Code code) 
106
		{ 
107
			m_error.code=code; 
108
			m_error.col=col; m_error.line=line;
109
			// set the last pos to end so eof() will return true
110
			if(code!=Error::errNone)
111
				m_lastPos=m_buffLen; 
112
		}
113
 
1 cycrow 114
	public:
115
		char * nextLine(); // public because of use in x3objects which is hack anyway
116
 
117
	public:
118
		typedef ext::list<token*> TokenList;
119
		typedef TokenList::iterator iterator;
120
		TokenList tokens;
121
 
122
		bod_text_parser() 
123
		{ 
124
			tabWidth(4);
125
			m_lastPos=0; m_newPos=0;
126
			m_pszBuffer=0;
127
			m_buffLen=0;
128
			m_lineIdx=0;
114 cycrow 129
			parseFlags(none);
1 cycrow 130
		}
131
 
114 cycrow 132
		~bod_text_parser()
133
		{
134
			for(iterator &it=tokens.begin(); it!=tokens.end(); ++it){
135
				delete *it;
136
			}
137
		}
1 cycrow 138
 
114 cycrow 139
		Error error() const { return m_error; }
140
 
141
		bool eof() const { return m_lastPos >= m_buffLen; }
142
 
1 cycrow 143
		void tabWidth(int width) { token::tabWidth=width; }
144
		int tabWidth() const { return token::tabWidth; }
145
 
114 cycrow 146
		void parseFlags(ParseFlags newf) { m_parseFlags=newf; }
147
		ParseFlags parseFlags() const { return m_parseFlags; }
1 cycrow 148
 
149
		void preParseBuffer(char *pszBuffer, size_t size);
150
		size_t parseBuffer(size_t limit=-1);
151
};
152
 
153
#endif // !defined(BOD_TEXT_PARSER_INCLUDED)