~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to filters/karbon/ai/ailexer.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
#define STOP 0
38
38
 
39
 
int iswhitespace(char c){
40
 
  return (c==' ')||(c=='\n')||(c=='\t')||(c=='\r');
41
 
}
42
 
 
43
 
int isSpecial(char c){
44
 
  return (c=='*')||(c=='_')||(c=='?')||(c=='~')||(c=='-')||(c=='^')||(c=='`')||(c=='!')||(c=='.')||(c=='@')||(c=='&')||(c=='$')||(c=='=');
45
 
}
46
 
 
47
 
int isletterhex(char c){
48
 
  return (c=='A')||(c=='B')||(c=='C')||(c=='D')||(c=='E')||(c=='F');
49
 
}
50
 
 
51
 
const char*statetoa (State state){
52
 
  switch (state)
53
 
  {
 
39
int iswhitespace(char c)
 
40
{
 
41
    return (c == ' ') || (c == '\n') || (c == '\t') || (c == '\r');
 
42
}
 
43
 
 
44
int isSpecial(char c)
 
45
{
 
46
    return (c == '*') || (c == '_') || (c == '?') || (c == '~') || (c == '-') || (c == '^') || (c == '`') || (c == '!') || (c == '.') || (c == '@') || (c == '&') || (c == '$') || (c == '=');
 
47
}
 
48
 
 
49
int isletterhex(char c)
 
50
{
 
51
    return (c == 'A') || (c == 'B') || (c == 'C') || (c == 'D') || (c == 'E') || (c == 'F');
 
52
}
 
53
 
 
54
const char*statetoa(State state)
 
55
{
 
56
    switch (state) {
54
57
    case State_Comment : return "comment";
55
58
    case State_Integer : return "integer";
56
59
    case State_Float : return "float";
68
71
    case State_CommentEncodedChar : return "encoded char (comment)";
69
72
    case State_ByteArray2 : return "byte array (mode 2)";
70
73
    default : return "unknown";
71
 
  }
 
74
    }
72
75
}
73
76
 
74
77
typedef struct {
75
 
  State oldState;
76
 
  char c;
77
 
  State newState;
78
 
  Action action;
 
78
    State oldState;
 
79
    char c;
 
80
    State newState;
 
81
    Action action;
79
82
} Transition;
80
83
 
81
84
static 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},
 
85
    { State_Comment, '\n', State_Start, Action_Output},
 
86
    { State_Comment, '\r', State_Start, Action_Output},
 
87
    { State_Comment, '\\', State_CommentEncodedChar, Action_InitTemp},
 
88
    { State_Comment, CATEGORY_ANY, State_Comment, Action_Copy},
 
89
    { State_Integer, CATEGORY_DIGIT, State_Integer, Action_Copy},
 
90
    { State_Integer, CATEGORY_WHITESPACE, State_Start, Action_Output},
 
91
    { State_Integer, '.', State_Float, Action_Copy},
 
92
    { State_Integer, ']', State_Start, Action_OutputUnget},
 
93
    { State_Integer, '}', State_Start, Action_OutputUnget},
 
94
    { State_Integer, '#', State_Byte, Action_Copy },
 
95
    { State_Integer, '/', State_Start, Action_OutputUnget },
 
96
    { State_Integer, '{', State_Start, Action_OutputUnget },
 
97
    { State_Integer, '%', State_Start, Action_OutputUnget },
 
98
    { State_Integer, CATEGORY_LETTERHEX, State_ByteArray2, Action_Copy },
 
99
    { State_Integer, CATEGORY_INTTOOLONG, State_ByteArray2, Action_Copy },
 
100
    { State_Integer, CATEGORY_ANY, State_Start, Action_Abort},
 
101
    { State_Float, CATEGORY_DIGIT, State_Float, Action_Copy},
 
102
    { State_Float, CATEGORY_WHITESPACE, State_Start, Action_Output},
 
103
    { State_Float, ']', State_Start, Action_OutputUnget},
 
104
    { State_Float, '}', State_Start, Action_OutputUnget},
 
105
    { State_Float, CATEGORY_ANY, State_Start, Action_Abort},
 
106
    { State_Token, CATEGORY_ALPHA, State_Token, Action_Copy},
 
107
    { State_Token, CATEGORY_DIGIT, State_Token, Action_Copy},
 
108
    { State_Token, CATEGORY_SPECIAL, State_Token, Action_Copy},
 
109
    { State_Token, '}', State_Start, Action_OutputUnget},
 
110
    { State_Token, ']', State_Start, Action_OutputUnget},
 
111
    { State_Token, '{', State_BlockStart, Action_Output},
 
112
    { State_Token, '}', State_BlockEnd, Action_Output},
 
113
    { State_Token, '/', State_Start, Action_OutputUnget},
 
114
    { State_Token, CATEGORY_WHITESPACE, State_Start, Action_Output},
 
115
    { State_Token, CATEGORY_ANY, State_Start, Action_Abort},
 
116
    { State_String, ')', State_Start, Action_Output},
 
117
    { State_String, '\\', State_StringEncodedChar, Action_InitTemp},
 
118
    { State_String, CATEGORY_ANY, State_String, Action_Copy},
116
119
//  { State_Array, CATEGORY_ALPHA, State_Array, Action_Copy},
117
120
//  { State_Array, CATEGORY_DIGIT, State_Array, Action_Copy},
118
121
//  { State_Array, ' ', State_Array, 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}
 
122
    { State_BlockStart, CATEGORY_ANY, State_Start, Action_OutputUnget },
 
123
    { State_BlockEnd, CATEGORY_ANY, State_Start, Action_OutputUnget },
 
124
    { State_ArrayStart, CATEGORY_ANY, State_Start, Action_OutputUnget },
 
125
    { State_ArrayEnd, CATEGORY_ANY, State_Start, Action_OutputUnget },
 
126
    { State_Reference, '#', State_Reference, Action_Copy },
 
127
    { State_Reference, CATEGORY_ALPHA, State_Reference, Action_Copy },
 
128
    { State_Reference, CATEGORY_DIGIT, State_Reference, Action_Copy },
 
129
    { State_Reference, CATEGORY_SPECIAL, State_Reference, Action_Copy },
 
130
    { State_Reference, CATEGORY_ANY, State_Start, Action_OutputUnget },
 
131
    { State_Byte, '/', State_Start, Action_OutputUnget },
 
132
    { State_Byte, CATEGORY_DIGIT, State_Byte, Action_Copy},
 
133
    { State_Byte, CATEGORY_ALPHA, State_Byte, Action_Copy},
 
134
    { State_Byte, CATEGORY_WHITESPACE, State_Start, Action_Output},
 
135
    { State_ByteArray, '>', State_Start, Action_Output },
 
136
    { State_ByteArray, CATEGORY_ALPHA, State_ByteArray, Action_Copy },
 
137
    { State_ByteArray, CATEGORY_DIGIT, State_ByteArray, Action_Copy },
 
138
    { State_ByteArray, CATEGORY_WHITESPACE, State_ByteArray, Action_Ignore },
 
139
    { State_ByteArray, CATEGORY_ANY, State_Start, Action_Abort },
 
140
    { State_StringEncodedChar, '\\', State_String, Action_Copy},
 
141
    { State_StringEncodedChar, CATEGORY_DIGIT, State_StringEncodedChar, Action_CopyTemp},
 
142
    { State_StringEncodedChar, CATEGORY_ANY, State_String, Action_DecodeUnget},
 
143
    { State_CommentEncodedChar, '\\', State_Comment, Action_Copy},
 
144
    { State_CommentEncodedChar, CATEGORY_DIGIT, State_CommentEncodedChar, Action_CopyTemp},
 
145
    { State_CommentEncodedChar, CATEGORY_ANY, State_Comment, Action_DecodeUnget},
 
146
    { State_ByteArray2, '\n', State_Start, Action_Output},
 
147
    { State_ByteArray2, '\r', State_Start, Action_Output},
 
148
    { State_ByteArray2, '}', State_Start, Action_ByteArraySpecial},
 
149
    { State_ByteArray2, CATEGORY_WHITESPACE, State_Start, Action_Output},
 
150
    { State_ByteArray2, CATEGORY_DIGIT, State_ByteArray2, Action_Copy},
 
151
    { State_ByteArray2, CATEGORY_LETTERHEX, State_ByteArray2, Action_Copy},
 
152
    { State_ByteArray2, CATEGORY_ALPHA, State_Token, Action_Copy},
 
153
    { State_ByteArray2, CATEGORY_ANY, State_Start, Action_Abort},
 
154
    { State_Start, '%', State_Comment, Action_Ignore},
 
155
    { State_Start, CATEGORY_DIGIT, State_Integer, Action_Copy},
 
156
    { State_Start, '-', State_Integer, Action_Copy},
 
157
    { State_Start, '+', State_Integer, Action_Copy},
 
158
    { State_Start, '.', State_Float, Action_Copy},
 
159
    { State_Start, '/', State_Reference, Action_Ignore },
 
160
    { State_Start, '(', State_String, Action_Ignore},
 
161
    { State_Start, '{', State_BlockStart, Action_Copy},
 
162
    { State_Start, '}', State_BlockEnd, Action_Copy},
 
163
    { State_Start, '[', State_ArrayStart, Action_Copy},
 
164
    { State_Start, ']', State_ArrayEnd, Action_Copy},
 
165
    { State_Start, '<', State_ByteArray, Action_Ignore},
 
166
    { State_Start, CATEGORY_ALPHA, State_Token, Action_Copy},
 
167
    { State_Start, CATEGORY_WHITESPACE, State_Start, Action_Output},
 
168
    { State_Start, CATEGORY_SPECIAL, State_Token, Action_Copy},
 
169
    { State_Start, CATEGORY_LETTERHEX, State_ByteArray2, Action_Copy},
 
170
    { State_Start, CATEGORY_ANY, State_Start, Action_Abort},
 
171
    { State_Start, STOP, State_Start, Action_Abort}
169
172
};
170
173
 
171
 
AILexer::AILexer(){
172
 
}
173
 
AILexer::~AILexer(){
174
 
}
175
 
 
176
 
bool AILexer::parse (QIODevice& fin){
177
 
  char c;
178
 
 
179
 
  m_buffer.clear();
180
 
  m_curState = State_Start;
181
 
 
182
 
  parsingStarted();
183
 
 
184
 
  while (!fin.atEnd())
185
 
  {
186
 
    c = fin.getch ();
 
174
AILexer::AILexer()
 
175
{
 
176
}
 
177
AILexer::~AILexer()
 
178
{
 
179
}
 
180
 
 
181
bool AILexer::parse(QIODevice& fin)
 
182
{
 
183
    char c;
 
184
 
 
185
    m_buffer.clear();
 
186
    m_curState = State_Start;
 
187
 
 
188
    parsingStarted();
 
189
 
 
190
    while (!fin.atEnd()) {
 
191
        c = fin.getch();
187
192
 
188
193
//    qDebug ("got %c", c);
189
194
 
190
 
    State newState;
191
 
    Action action;
192
 
 
193
 
    nextStep (c, &newState, &action);
194
 
 
195
 
    switch (action)
196
 
    {
197
 
      case Action_Copy :
198
 
        m_buffer.append (c);
199
 
        break;
200
 
      case Action_CopyOutput :
201
 
        m_buffer.append (c);
202
 
        doOutput();
203
 
        break;
204
 
      case Action_Output :
205
 
        doOutput();
206
 
        break;
207
 
      case Action_OutputUnget :
208
 
        doOutput();
209
 
        fin.ungetch(c);
210
 
        break;
211
 
      case Action_Ignore :
212
 
        /* ignore */
213
 
        break;
214
 
      case Action_Abort :
215
 
        qWarning ( "state %s / %s char %c (%d)" , statetoa(m_curState), statetoa(newState), c, c );
216
 
        parsingAborted();
217
 
        return false;
218
 
        break;
219
 
      case Action_InitTemp :
220
 
        m_temp.clear();
221
 
        break;
222
 
      case Action_CopyTemp :
223
 
        m_temp.append (c);
224
 
        break;
225
 
      case Action_DecodeUnget :
226
 
        m_buffer.append (decode());
227
 
        fin.ungetch(c);
228
 
        break;
229
 
      // in Postscript Quelltext: Kombination F}
230
 
      case Action_ByteArraySpecial :
231
 
        m_curState = State_Token;
232
 
        doOutput();
233
 
        fin.ungetch(c);
234
 
        break;
235
 
      default :
236
 
        qWarning ( "unknown action: %d ", action);
 
195
        State newState;
 
196
        Action action;
 
197
 
 
198
        nextStep(c, &newState, &action);
 
199
 
 
200
        switch (action) {
 
201
        case Action_Copy :
 
202
            m_buffer.append(c);
 
203
            break;
 
204
        case Action_CopyOutput :
 
205
            m_buffer.append(c);
 
206
            doOutput();
 
207
            break;
 
208
        case Action_Output :
 
209
            doOutput();
 
210
            break;
 
211
        case Action_OutputUnget :
 
212
            doOutput();
 
213
            fin.ungetch(c);
 
214
            break;
 
215
        case Action_Ignore :
 
216
            /* ignore */
 
217
            break;
 
218
        case Action_Abort :
 
219
            qWarning("state %s / %s char %c (%d)" , statetoa(m_curState), statetoa(newState), c, c);
 
220
            parsingAborted();
 
221
            return false;
 
222
            break;
 
223
        case Action_InitTemp :
 
224
            m_temp.clear();
 
225
            break;
 
226
        case Action_CopyTemp :
 
227
            m_temp.append(c);
 
228
            break;
 
229
        case Action_DecodeUnget :
 
230
            m_buffer.append(decode());
 
231
            fin.ungetch(c);
 
232
            break;
 
233
            // in Postscript Quelltext: Kombination F}
 
234
        case Action_ByteArraySpecial :
 
235
            m_curState = State_Token;
 
236
            doOutput();
 
237
            fin.ungetch(c);
 
238
            break;
 
239
        default :
 
240
            qWarning("unknown action: %d ", action);
 
241
        }
 
242
 
 
243
        m_curState = newState;
237
244
    }
238
245
 
239
 
    m_curState = newState;
240
 
  }
241
 
 
242
 
  parsingFinished();
243
 
  return true;
 
246
    parsingFinished();
 
247
    return true;
244
248
}
245
249
 
246
 
void AILexer::doOutput ()
 
250
void AILexer::doOutput()
247
251
{
248
 
  if (m_buffer.length() == 0) return;
249
 
  switch (m_curState)
250
 
  {
 
252
    if (m_buffer.length() == 0) return;
 
253
    switch (m_curState) {
251
254
    case State_Comment :
252
 
      gotComment (m_buffer.latin1());
253
 
      break;
 
255
        gotComment(m_buffer.toLatin1());
 
256
        break;
254
257
    case State_Integer :
255
 
      gotIntValue (m_buffer.toInt());
256
 
      break;
 
258
        gotIntValue(m_buffer.toInt());
 
259
        break;
257
260
    case State_Float :
258
 
      gotDoubleValue (m_buffer.toFloat());
259
 
      break;
 
261
        gotDoubleValue(m_buffer.toFloat());
 
262
        break;
260
263
    case State_String :
261
 
      gotStringValue (m_buffer.latin1());
262
 
      break;
 
264
        gotStringValue(m_buffer.toLatin1());
 
265
        break;
263
266
    case State_Token :
264
 
      gotToken (m_buffer.latin1());
265
 
      break;
 
267
        gotToken(m_buffer.toLatin1());
 
268
        break;
266
269
    case State_Reference :
267
 
      gotReference (m_buffer.latin1());
268
 
      break;
 
270
        gotReference(m_buffer.toLatin1());
 
271
        break;
269
272
    case State_BlockStart :
270
 
      gotBlockStart ();
271
 
      break;
 
273
        gotBlockStart();
 
274
        break;
272
275
    case State_BlockEnd :
273
 
      gotBlockEnd ();
274
 
      break;
 
276
        gotBlockEnd();
 
277
        break;
275
278
    case State_Start :
276
 
      break;
 
279
        break;
277
280
    case State_ArrayStart :
278
 
      gotArrayStart ();
279
 
      break;
 
281
        gotArrayStart();
 
282
        break;
280
283
    case State_ArrayEnd :
281
 
      gotArrayEnd ();
282
 
      break;
 
284
        gotArrayEnd();
 
285
        break;
283
286
    case State_Byte :
284
 
      gotByte (getByte());
285
 
      break;
 
287
        gotByte(getByte());
 
288
        break;
286
289
    case State_ByteArray :
287
290
    case State_ByteArray2 :
288
 
      doHandleByteArray ();
289
 
      break;
 
291
        doHandleByteArray();
 
292
        break;
290
293
    default:
291
 
      qWarning ( "unknown state: %d", m_curState );
292
 
  }
293
 
 
294
 
  m_buffer.clear();
295
 
}
296
 
 
297
 
void AILexer::gotComment (const char *value) {
298
 
  qDebug ( "gotComment: %s ", value );
299
 
}
300
 
 
301
 
void AILexer::gotIntValue (int value) {
302
 
  qDebug ( "gotInt: %d ", value );
303
 
}
304
 
 
305
 
void AILexer::gotDoubleValue (double value) {
306
 
  qDebug ( "gotDouble: %f ", value );
307
 
}
308
 
 
309
 
void AILexer::gotStringValue (const char *value) {
310
 
  qDebug ( "gotString: %s ", value );
311
 
}
312
 
 
313
 
void AILexer::gotToken (const char *value) {
314
 
  qDebug ( "gotToken: %s ", value );
315
 
}
316
 
 
317
 
void AILexer::gotReference (const char *value) {
318
 
  qDebug ( "gotReference: %s ", value );
319
 
}
320
 
 
321
 
void AILexer::gotBlockStart (){
322
 
  qDebug ( "gotBlockStart" );
323
 
}
324
 
 
325
 
void AILexer::gotBlockEnd (){
326
 
  qDebug ( "gotBlockEnd" );
327
 
}
328
 
 
329
 
void AILexer::gotArrayStart (){
330
 
  qDebug ( "gotArrayStart" );
331
 
}
332
 
 
333
 
void AILexer::gotArrayEnd (){
334
 
  qDebug ( "gotArrayEnd" );
335
 
}
336
 
 
337
 
void AILexer::parsingStarted() {
338
 
  qDebug ( "parsing started" );
339
 
}
340
 
 
341
 
void AILexer::parsingFinished() {
342
 
  qDebug ( "parsing finished" );
343
 
}
344
 
 
345
 
void AILexer::parsingAborted() {
346
 
  qDebug ( "parsing aborted" );
347
 
}
348
 
 
349
 
void AILexer::gotByte (uchar value) {
350
 
  qDebug ( "got byte %d" , value );
351
 
}
352
 
 
353
 
void AILexer::gotByteArray (const QByteArray &data) {
354
 
  qDebug ( "got byte array" );
355
 
/*  for ( uint i = 0; i < data.size(); i++ )
356
 
  {
357
 
    uchar value = data[i];
358
 
    qDebug( "%d: %x", i, value );
359
 
  }
360
 
  qDebug ( "/byte array" ); */
361
 
 
362
 
}
363
 
 
364
 
 
365
 
void AILexer::nextStep (char c, State *newState, Action *newAction) {
366
 
  int i=0;
367
 
 
368
 
  while (true) {
369
 
    Transition trans = transitions[i];
370
 
 
371
 
    if (trans.c == STOP) {
372
 
      *newState = trans.newState;
373
 
      *newAction = trans.action;
374
 
      return;
 
294
        qWarning("unknown state: %d", m_curState);
375
295
    }
376
296
 
377
 
    bool found = false;
378
 
 
379
 
    if (trans.oldState == m_curState) {
380
 
      switch (trans.c) {
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);
 
297
    m_buffer.clear();
 
298
}
 
299
 
 
300
void AILexer::gotComment(const char *value)
 
301
{
 
302
    qDebug("gotComment: %s ", value);
 
303
}
 
304
 
 
305
void AILexer::gotIntValue(int value)
 
306
{
 
307
    qDebug("gotInt: %d ", value);
 
308
}
 
309
 
 
310
void AILexer::gotDoubleValue(double value)
 
311
{
 
312
    qDebug("gotDouble: %f ", value);
 
313
}
 
314
 
 
315
void AILexer::gotStringValue(const char *value)
 
316
{
 
317
    qDebug("gotString: %s ", value);
 
318
}
 
319
 
 
320
void AILexer::gotToken(const char *value)
 
321
{
 
322
    qDebug("gotToken: %s ", value);
 
323
}
 
324
 
 
325
void AILexer::gotReference(const char *value)
 
326
{
 
327
    qDebug("gotReference: %s ", value);
 
328
}
 
329
 
 
330
void AILexer::gotBlockStart()
 
331
{
 
332
    qDebug("gotBlockStart");
 
333
}
 
334
 
 
335
void AILexer::gotBlockEnd()
 
336
{
 
337
    qDebug("gotBlockEnd");
 
338
}
 
339
 
 
340
void AILexer::gotArrayStart()
 
341
{
 
342
    qDebug("gotArrayStart");
 
343
}
 
344
 
 
345
void AILexer::gotArrayEnd()
 
346
{
 
347
    qDebug("gotArrayEnd");
 
348
}
 
349
 
 
350
void AILexer::parsingStarted()
 
351
{
 
352
    qDebug("parsing started");
 
353
}
 
354
 
 
355
void AILexer::parsingFinished()
 
356
{
 
357
    qDebug("parsing finished");
 
358
}
 
359
 
 
360
void AILexer::parsingAborted()
 
361
{
 
362
    qDebug("parsing aborted");
 
363
}
 
364
 
 
365
void AILexer::gotByte(uchar value)
 
366
{
 
367
    qDebug("got byte %d" , value);
 
368
}
 
369
 
 
370
void AILexer::gotByteArray(const QByteArray &data)
 
371
{
 
372
    qDebug("got byte array");
 
373
    /*  for ( uint i = 0; i < data.size(); i++ )
 
374
      {
 
375
        uchar value = data[i];
 
376
        qDebug( "%d: %x", i, value );
389
377
      }
390
 
 
391
 
      if (found) {
392
 
        *newState = trans.newState;
393
 
        *newAction = trans.action;
394
 
 
 
378
      qDebug ( "/byte array" ); */
 
379
 
 
380
}
 
381
 
 
382
 
 
383
void AILexer::nextStep(char c, State *newState, Action *newAction)
 
384
{
 
385
    int i = 0;
 
386
 
 
387
    while (true) {
 
388
        Transition trans = transitions[i];
 
389
 
 
390
        if (trans.c == STOP) {
 
391
            *newState = trans.newState;
 
392
            *newAction = trans.action;
 
393
            return;
 
394
        }
 
395
 
 
396
        bool found = false;
 
397
 
 
398
        if (trans.oldState == m_curState) {
 
399
            switch (trans.c) {
 
400
            case CATEGORY_WHITESPACE : found = isspace(c); break;
 
401
            case CATEGORY_ALPHA : found = isalpha(c); break;
 
402
            case CATEGORY_DIGIT : found = isdigit(c); break;
 
403
            case CATEGORY_SPECIAL : found = isSpecial(c); break;
 
404
            case CATEGORY_LETTERHEX : found = isletterhex(c); break;
 
405
            case CATEGORY_INTTOOLONG : found = m_buffer.length() > MAX_INTLEN; break;
 
406
            case CATEGORY_ANY : found = true; break;
 
407
            default : found = (trans.c == c);
 
408
            }
 
409
 
 
410
            if (found) {
 
411
                *newState = trans.newState;
 
412
                *newAction = trans.action;
 
413
 
 
414
                return;
 
415
            }
 
416
        }
 
417
 
 
418
 
 
419
        i++;
 
420
    }
 
421
}
 
422
 
 
423
void AILexer::doHandleByteArray()
 
424
{
 
425
    // Special case - too short
 
426
    if (m_buffer.length() < MIN_HEXCHARS) {
 
427
        gotToken(m_buffer.toLatin1());
395
428
        return;
396
 
      }
397
 
    }
398
 
 
399
 
 
400
 
    i++;
401
 
  }
402
 
}
403
 
 
404
 
void AILexer::doHandleByteArray ()
405
 
{
406
 
  // Special case - too short
407
 
  if (m_buffer.length () < MIN_HEXCHARS)
408
 
  {
409
 
    gotToken (m_buffer.latin1());
410
 
    return;
411
 
  }
412
 
 
413
 
  uint strIdx = 0;
414
 
  uint arrayIdx = 0;
415
 
 
416
 
  QByteArray data (m_buffer.length() >> 1);
417
 
 
418
 
  while (strIdx < m_buffer.length())
419
 
  {
420
 
    const QString &item = m_buffer.mid (strIdx, 2);
421
 
    uchar val = item.toShort(NULL, 16);
422
 
    data[arrayIdx] = val;
423
 
    strIdx += 2;
424
 
    arrayIdx++;
425
 
  }
426
 
 
427
 
  gotByteArray (data);
 
429
    }
 
430
 
 
431
    uint strIdx = 0;
 
432
    uint arrayIdx = 0;
 
433
 
 
434
    QByteArray data(m_buffer.length() >> 1);
 
435
 
 
436
    while (strIdx < m_buffer.length()) {
 
437
        const QString &item = m_buffer.mid(strIdx, 2);
 
438
        uchar val = item.toShort(NULL, 16);
 
439
        data[arrayIdx] = val;
 
440
        strIdx += 2;
 
441
        arrayIdx++;
 
442
    }
 
443
 
 
444
    gotByteArray(data);
428
445
}
429
446
 
430
447
uchar AILexer::getByte()
431
448
{
432
 
//  qDebug ("convert string to byte (%s)", m_buffer.latin1());
433
 
 
434
 
  QStringList list = QStringList::split ("#", m_buffer.toString());
435
 
  int radix = list[0].toShort();
436
 
  uchar value = list[1].toShort (NULL, radix);
437
 
 
438
 
  return value;
 
449
//  qDebug ("convert string to byte (%s)", m_buffer.toLatin1());
 
450
 
 
451
    QStringList list = QStringList::split("#", m_buffer.toString());
 
452
    int radix = list[0].toShort();
 
453
    uchar value = list[1].toShort(NULL, radix);
 
454
 
 
455
    return value;
439
456
}
440
457
 
441
458
uchar AILexer::decode()
442
459
{
443
 
  uchar value = m_temp.toString().toShort(NULL, 8);
 
460
    uchar value = m_temp.toString().toShort(NULL, 8);
444
461
//  qDebug ("got encoded char %c",value);
445
 
  return value;
 
462
    return value;
446
463
}
447
464
 
448
465
/* StringBuffer implementation */
450
467
int initialSize = 20;
451
468
int addSize = 10;
452
469
 
453
 
StringBuffer::StringBuffer () {
454
 
  m_buffer = (char*)calloc (initialSize, sizeof(char));
455
 
  m_length = 0;
456
 
  m_capacity = initialSize;
457
 
}
458
 
 
459
 
StringBuffer::~StringBuffer (){
460
 
  free(m_buffer);
461
 
}
462
 
 
463
 
void StringBuffer::append (char c){
464
 
  ensureCapacity(m_length + 1);
465
 
  m_buffer[m_length] = c;
466
 
  m_length++;
467
 
}
468
 
 
469
 
void StringBuffer::clear(){
470
 
  for (uint i=0; i<m_length; i++) m_buffer[i] = '\0';
471
 
  m_length = 0;
472
 
}
473
 
 
474
 
QString StringBuffer::toString() const {
475
 
  QString ret(m_buffer);
476
 
  return ret;
477
 
}
478
 
 
479
 
void StringBuffer::ensureCapacity (int p_capacity) {
480
 
  if (m_capacity >= p_capacity) return;
481
 
 
482
 
  int newSize = m_capacity + addSize;
483
 
  if (p_capacity > newSize) newSize = p_capacity;
484
 
 
485
 
  char* oldBuffer = m_buffer;
486
 
  char *newBuffer = (char*)calloc (newSize, sizeof(char));
487
 
  strcpy (newBuffer, m_buffer);
488
 
  free(oldBuffer);
489
 
  m_buffer = newBuffer;
490
 
  m_capacity = newSize;
491
 
}
492
 
 
493
 
uint StringBuffer::length() {
494
 
  return m_length;
495
 
}
496
 
 
497
 
double StringBuffer::toFloat() {
498
 
  QString data = toString();
499
 
  return data.toFloat();
500
 
}
501
 
 
502
 
int StringBuffer::toInt() {
503
 
  QString data = toString();
504
 
  return data.toInt();
505
 
}
506
 
 
507
 
const char *StringBuffer::latin1() {
508
 
  return m_buffer;
509
 
}
510
 
 
511
 
QString StringBuffer::mid( uint index, uint len) const {
512
 
  QString data = toString();
513
 
  return data.mid(index,len);
 
470
StringBuffer::StringBuffer()
 
471
{
 
472
    m_buffer = (char*)calloc(initialSize, sizeof(char));
 
473
    m_length = 0;
 
474
    m_capacity = initialSize;
 
475
}
 
476
 
 
477
StringBuffer::~StringBuffer()
 
478
{
 
479
    free(m_buffer);
 
480
}
 
481
 
 
482
void StringBuffer::append(char c)
 
483
{
 
484
    ensureCapacity(m_length + 1);
 
485
    m_buffer[m_length] = c;
 
486
    m_length++;
 
487
}
 
488
 
 
489
void StringBuffer::clear()
 
490
{
 
491
    for (uint i = 0; i < m_length; i++) m_buffer[i] = '\0';
 
492
    m_length = 0;
 
493
}
 
494
 
 
495
QString StringBuffer::toString() const
 
496
{
 
497
    QString ret(m_buffer);
 
498
    return ret;
 
499
}
 
500
 
 
501
void StringBuffer::ensureCapacity(int p_capacity)
 
502
{
 
503
    if (m_capacity >= p_capacity) return;
 
504
 
 
505
    int newSize = m_capacity + addSize;
 
506
    if (p_capacity > newSize) newSize = p_capacity;
 
507
 
 
508
    char* oldBuffer = m_buffer;
 
509
    char *newBuffer = (char*)calloc(newSize, sizeof(char));
 
510
    strcpy(newBuffer, m_buffer);
 
511
    free(oldBuffer);
 
512
    m_buffer = newBuffer;
 
513
    m_capacity = newSize;
 
514
}
 
515
 
 
516
uint StringBuffer::length()
 
517
{
 
518
    return m_length;
 
519
}
 
520
 
 
521
double StringBuffer::toFloat()
 
522
{
 
523
    QString data = toString();
 
524
    return data.toFloat();
 
525
}
 
526
 
 
527
int StringBuffer::toInt()
 
528
{
 
529
    QString data = toString();
 
530
    return data.toInt();
 
531
}
 
532
 
 
533
const char *StringBuffer::toLatin1()
 
534
{
 
535
    return m_buffer;
 
536
}
 
537
 
 
538
QString StringBuffer::mid(uint index, uint len) const
 
539
{
 
540
    QString data = toString();
 
541
    return data.mid(index, len);
514
542
}