22#include <tqstringlist.h>
25#define CATEGORY_WHITESPACE -1
26#define CATEGORY_ALPHA -2
27#define CATEGORY_DIGIT -3
28#define CATEGORY_SPECIAL -4
29#define CATEGORY_LETTERHEX -5
30#define CATEGORY_INTTOOLONG -6
32#define CATEGORY_ANY -127
39int iswhitespace( char c){
40 return (c== ' ')||(c== '\n')||(c== '\t')||(c== '\r');
44 return (c== '*')||(c== '_')||(c== '?')||(c== '~')||(c== '-')||(c== '^')||(c== '`')||(c== '!')||(c== '.')||(c== '@')||(c== '&')||(c== '$')||(c== '=');
47int isletterhex( char c){
48 return (c== 'A')||(c== 'B')||(c== 'C')||(c== 'D')||(c== 'E')||(c== 'F');
51const char*statetoa (State state){
54 case State_Comment : return "comment";
55 case State_Integer : return "integer";
56 case State_Float : return "float";
57 case State_String : return "string";
58 case State_Token : return "token";
59 case State_Reference : return "reference";
60 case State_Start : return "start";
61 case State_BlockStart : return "block start";
62 case State_BlockEnd : return "block end";
63 case State_ArrayStart : return "array start";
64 case State_ArrayEnd : return "array end";
65 case State_Byte : return "byte";
66 case State_ByteArray : return "byte array";
67 case State_StringEncodedChar : return "encoded char (string)";
68 case State_CommentEncodedChar : return "encoded char (comment)";
69 case State_ByteArray2 : return "byte array (mode 2)";
70 default : return "unknown";
81static Transition transitions[] = {
82 { State_Comment, '\n', State_Start, Action_Output},
83 { State_Comment, '\r', State_Start, Action_Output},
84 { State_Comment, '\\', State_CommentEncodedChar, Action_InitTemp},
85 { State_Comment, CATEGORY_ANY, State_Comment, Action_Copy},
86 { State_Integer, CATEGORY_DIGIT, State_Integer, Action_Copy},
87 { State_Integer, CATEGORY_WHITESPACE, State_Start, Action_Output},
88 { State_Integer, '.', State_Float, Action_Copy},
89 { State_Integer, ']', State_Start, Action_OutputUnget},
90 { State_Integer, '}', State_Start, Action_OutputUnget},
91 { State_Integer, '#', State_Byte, Action_Copy },
92 { State_Integer, '/', State_Start, Action_OutputUnget },
93 { State_Integer, '{', State_Start, Action_OutputUnget },
94 { State_Integer, '%', State_Start, Action_OutputUnget },
95 { State_Integer, CATEGORY_LETTERHEX, State_ByteArray2, Action_Copy },
96 { State_Integer, CATEGORY_INTTOOLONG, State_ByteArray2, Action_Copy },
97 { State_Integer, CATEGORY_ANY, State_Start, Action_Abort},
98 { State_Float, CATEGORY_DIGIT, State_Float, Action_Copy},
99 { State_Float, CATEGORY_WHITESPACE, State_Start, Action_Output},
100 { State_Float, ']', State_Start, Action_OutputUnget},
101 { State_Float, '}', State_Start, Action_OutputUnget},
102 { State_Float, CATEGORY_ANY, State_Start, Action_Abort},
103 { State_Token, CATEGORY_ALPHA, State_Token, Action_Copy},
104 { State_Token, CATEGORY_DIGIT, State_Token, Action_Copy},
105 { State_Token, CATEGORY_SPECIAL, State_Token, Action_Copy},
106 { State_Token, '}', State_Start, Action_OutputUnget},
107 { State_Token, ']', State_Start, Action_OutputUnget},
108 { State_Token, '{', State_BlockStart, Action_Output},
109 { State_Token, '}', State_BlockEnd, Action_Output},
110 { State_Token, '/', State_Start, Action_OutputUnget},
111 { State_Token, CATEGORY_WHITESPACE, State_Start, Action_Output},
112 { State_Token, CATEGORY_ANY, State_Start, Action_Abort},
113 { State_String, ')', State_Start, Action_Output},
114 { State_String, '\\', State_StringEncodedChar, Action_InitTemp},
115 { State_String, CATEGORY_ANY, State_String, Action_Copy},
119 { State_BlockStart, CATEGORY_ANY, State_Start, Action_OutputUnget },
120 { State_BlockEnd, CATEGORY_ANY, State_Start, Action_OutputUnget },
121 { State_ArrayStart, CATEGORY_ANY, State_Start, Action_OutputUnget },
122 { State_ArrayEnd, CATEGORY_ANY, State_Start, Action_OutputUnget },
123 { State_Reference, '#', State_Reference, Action_Copy },
124 { State_Reference, CATEGORY_ALPHA, State_Reference, Action_Copy },
125 { State_Reference, CATEGORY_DIGIT, State_Reference, Action_Copy },
126 { State_Reference, CATEGORY_SPECIAL, State_Reference, Action_Copy },
127 { State_Reference, CATEGORY_ANY, State_Start, Action_OutputUnget },
128 { State_Byte, '/', State_Start, Action_OutputUnget },
129 { State_Byte, CATEGORY_DIGIT, State_Byte, Action_Copy},
130 { State_Byte, CATEGORY_ALPHA, State_Byte, Action_Copy},
131 { State_Byte, CATEGORY_WHITESPACE, State_Start, Action_Output},
132 { State_ByteArray, '>', State_Start, Action_Output },
133 { State_ByteArray, CATEGORY_ALPHA, State_ByteArray, Action_Copy },
134 { State_ByteArray, CATEGORY_DIGIT, State_ByteArray, Action_Copy },
135 { State_ByteArray, CATEGORY_WHITESPACE, State_ByteArray, Action_Ignore },
136 { State_ByteArray, CATEGORY_ANY, State_Start, Action_Abort },
137 { State_StringEncodedChar, '\\', State_String, Action_Copy},
138 { State_StringEncodedChar, CATEGORY_DIGIT, State_StringEncodedChar, Action_CopyTemp},
139 { State_StringEncodedChar, CATEGORY_ANY, State_String, Action_DecodeUnget},
140 { State_CommentEncodedChar, '\\', State_Comment, Action_Copy},
141 { State_CommentEncodedChar, CATEGORY_DIGIT, State_CommentEncodedChar, Action_CopyTemp},
142 { State_CommentEncodedChar, CATEGORY_ANY, State_Comment, Action_DecodeUnget},
143 { State_ByteArray2, '\n', State_Start, Action_Output},
144 { State_ByteArray2, '\r', State_Start, Action_Output},
145 { State_ByteArray2, '}', State_Start, Action_ByteArraySpecial},
146 { State_ByteArray2, CATEGORY_WHITESPACE, State_Start, Action_Output},
147 { State_ByteArray2, CATEGORY_DIGIT, State_ByteArray2, Action_Copy},
148 { State_ByteArray2, CATEGORY_LETTERHEX, State_ByteArray2, Action_Copy},
149 { State_ByteArray2, CATEGORY_ALPHA, State_Token, Action_Copy},
150 { State_ByteArray2, CATEGORY_ANY, State_Start, Action_Abort},
151 { State_Start, '%', State_Comment, Action_Ignore},
152 { State_Start, CATEGORY_DIGIT, State_Integer, Action_Copy},
153 { State_Start, '-', State_Integer, Action_Copy},
154 { State_Start, '+', State_Integer, Action_Copy},
155 { State_Start, '.', State_Float, Action_Copy},
156 { State_Start, '/', State_Reference, Action_Ignore },
157 { State_Start, '(', State_String, Action_Ignore},
158 { State_Start, '{', State_BlockStart, Action_Copy},
159 { State_Start, '}', State_BlockEnd, Action_Copy},
160 { State_Start, '[', State_ArrayStart, Action_Copy},
161 { State_Start, ']', State_ArrayEnd, Action_Copy},
162 { State_Start, '<', State_ByteArray, Action_Ignore},
163 { State_Start, CATEGORY_ALPHA, State_Token, Action_Copy},
164 { State_Start, CATEGORY_WHITESPACE, State_Start, Action_Output},
165 { State_Start, CATEGORY_SPECIAL, State_Token, Action_Copy},
166 { State_Start, CATEGORY_LETTERHEX, State_ByteArray2, Action_Copy},
167 { State_Start, CATEGORY_ANY, State_Start, Action_Abort},
168 { State_Start, STOP, State_Start, Action_Abort}
176bool AILexer::parse (TQIODevice& fin){
180 m_curState = State_Start;
193 nextStep (c, &newState, &action);
200 case Action_CopyOutput :
207 case Action_OutputUnget :
215 tqWarning ( "state %s / %s char %c (%d)" , statetoa(m_curState), statetoa(newState), c, c );
219 case Action_InitTemp :
222 case Action_CopyTemp :
225 case Action_DecodeUnget :
226 m_buffer.append (decode());
230 case Action_ByteArraySpecial :
231 m_curState = State_Token;
236 tqWarning ( "unknown action: %d ", action);
239 m_curState = newState;
246void AILexer::doOutput ()
248 if (m_buffer.length() == 0) return;
252 gotComment (m_buffer.latin1());
255 gotIntValue (m_buffer.toInt());
258 gotDoubleValue (m_buffer.toFloat());
261 gotStringValue (m_buffer.latin1());
264 gotToken (m_buffer.latin1());
266 case State_Reference :
267 gotReference (m_buffer.latin1());
269 case State_BlockStart :
272 case State_BlockEnd :
277 case State_ArrayStart :
280 case State_ArrayEnd :
286 case State_ByteArray :
287 case State_ByteArray2 :
288 doHandleByteArray ();
291 tqWarning ( "unknown state: %d", m_curState );
297void AILexer::gotComment ( const char *value) {
298 tqDebug ( "gotComment: %s ", value );
301void AILexer::gotIntValue ( int value) {
302 tqDebug ( "gotInt: %d ", value );
305void AILexer::gotDoubleValue ( double value) {
306 tqDebug ( "gotDouble: %f ", value );
309void AILexer::gotStringValue ( const char *value) {
310 tqDebug ( "gotString: %s ", value );
313void AILexer::gotToken ( const char *value) {
314 tqDebug ( "gotToken: %s ", value );
317void AILexer::gotReference ( const char *value) {
318 tqDebug ( "gotReference: %s ", value );
321void AILexer::gotBlockStart (){
322 tqDebug ( "gotBlockStart" );
325void AILexer::gotBlockEnd (){
326 tqDebug ( "gotBlockEnd" );
329void AILexer::gotArrayStart (){
330 tqDebug ( "gotArrayStart" );
333void AILexer::gotArrayEnd (){
334 tqDebug ( "gotArrayEnd" );
337void AILexer::parsingStarted() {
338 tqDebug ( "parsing started" );
341void AILexer::parsingFinished() {
342 tqDebug ( "parsing finished" );
345void AILexer::parsingAborted() {
346 tqDebug ( "parsing aborted" );
349void AILexer::gotByte (uchar value) {
350 tqDebug ( "got byte %d" , value );
353void AILexer::gotByteArray ( const TQByteArray &data) {
354 tqDebug ( "got byte array" );
365void AILexer::nextStep ( char c, State *newState, Action *newAction) {
369 Transition trans = transitions[i];
371 if (trans.c == STOP) {
372 *newState = trans.newState;
373 *newAction = trans.action;
379 if (trans.oldState == m_curState) {
381 case CATEGORY_WHITESPACE : found = isspace(c); break;
382 case CATEGORY_ALPHA : found = isalpha(c); break;
383 case CATEGORY_DIGIT : found = isdigit(c); break;
384 case CATEGORY_SPECIAL : found = isSpecial(c); break;
385 case CATEGORY_LETTERHEX : found = isletterhex(c); break;
386 case CATEGORY_INTTOOLONG : found = m_buffer.length() > MAX_INTLEN; break;
387 case CATEGORY_ANY : found = true; break;
388 default : found = (trans.c == c);
392 *newState = trans.newState;
393 *newAction = trans.action;
404void AILexer::doHandleByteArray ()
407 if (m_buffer.length () < MIN_HEXCHARS)
409 gotToken (m_buffer.latin1());
416 TQByteArray data (m_buffer.length() >> 1);
418 while (strIdx < m_buffer.length())
420 const TQString &item = m_buffer.mid (strIdx, 2);
421 uchar val = item.toShort(NULL, 16);
422 data[arrayIdx] = val;
430uchar AILexer::getByte()
434 TQStringList list = TQStringList::split ( "#", m_buffer.toString());
435 int radix = list[0].toShort();
436 uchar value = list[1].toShort (NULL, radix);
441uchar AILexer::decode()
443 uchar value = m_temp.toString().toShort(NULL, 8);
453StringBuffer::StringBuffer () {
454 m_buffer = ( char*)calloc (initialSize, sizeof( char));
456 m_capacity = initialSize;
459StringBuffer::~StringBuffer (){
463void StringBuffer::append ( char c){
464 ensureCapacity(m_length + 1);
465 m_buffer[m_length] = c;
469void StringBuffer::clear(){
470 for (uint i=0; i<m_length; i++) m_buffer[i] = '\0';
474TQString StringBuffer::toString() const {
475 TQString ret(m_buffer);
479void StringBuffer::ensureCapacity ( int p_capacity) {
480 if (m_capacity >= p_capacity) return;
482 int newSize = m_capacity + addSize;
483 if (p_capacity > newSize) newSize = p_capacity;
485 char* oldBuffer = m_buffer;
486 char *newBuffer = ( char*)calloc (newSize, sizeof( char));
487 strcpy (newBuffer, m_buffer);
489 m_buffer = newBuffer;
490 m_capacity = newSize;
493uint StringBuffer::length() {
497double StringBuffer::toFloat() {
498 TQString data = toString();
499 return data.toFloat();
502int StringBuffer::toInt() {
503 TQString data = toString();
507const char *StringBuffer::latin1() {
511TQString StringBuffer::mid( uint index, uint len) const {
512 TQString data = toString();
513 return data.mid(index,len);
|