summaryrefslogtreecommitdiff
path: root/iup/src/iup_ledlex.c
blob: 5cf86402979bed89992513804ebae1671cf32524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/** \file
 * \brief lexical analysis manager for LED
 *
 * See Copyright Notice in "iup.h"
 */

#include <stdarg.h>  
#include <stdio.h>   
#include <stdlib.h>  
#include <ctype.h>
#include <string.h>  
#include <errno.h>   

#include "iup.h"

#include "iup_class.h"
#include "iup_ledlex.h"
#include "iup_str.h"
#include "iup_table.h"
#include "iup_register.h"


static struct          /* lexical variables */
{
  const char* filename;   /* file name */
  const char* f;
  FILE* file;             /* file handle */
  int token;              /* lookahead iLexToken */
  char name[40960];       /* lexical identifier value */
  float number;           /* lexical number value */
  int line;               /* line number */
  Iclass *ic;             /* control class when func is CONTROL_ */
} ilex = {NULL, NULL, NULL, 0, "", (float) 0.0, 0, NULL};

static int iLexGetChar (void);
static int iLexToken(int *erro);
static int iLexCapture (char* dlm);
static void iLexSkipComment (void);
static int iLexCaptureAttr (void);

int iupLexStart(const char* filename, int is_file)      /* initialize lexical analysis */
{
  ilex.filename = filename;
  if (is_file)
  {
    ilex.file = fopen (ilex.filename,"r");
    if (!ilex.file)
      return iupLexError (IUPLEX_FILENOTOPENED, filename);
  }
  else
  {
    ilex.f = ilex.filename;
    ilex.file = NULL;
  }
  ilex.line = 0;
  ilex.line = 1;
  return iupLexAdvance();
}

void iupLexClose(void)
{
  if (!ilex.file)
    return;
  fclose (ilex.file);
  ilex.file = NULL;
}

static void iLexUngetc(int c)
{
  if (ilex.file)
    ungetc(c, ilex.file);
  else
  {
    if (*ilex.f != 0)
      *(char*)ilex.f = (char)c;  /* write back to the string ???? */
  }
}

static int iLexGetc(void)
{
  if (ilex.file)
    return getc(ilex.file);
  else
  {
    if (*ilex.f != 0)
      ilex.f++;
    if (*ilex.f == 0)
      return EOF;
    return *ilex.f;
  }
}

int iupLexLookAhead(void)
{
  return ilex.token;
}

int iupLexAdvance(void)
{
  int erro = 0;
  ilex.token = iLexToken(&erro);
  return erro;
}

int iupLexFollowedBy(int t)
{
  return (ilex.token==t);
}

int iupLexMatch(int t)
{
  if (ilex.token==t)
    return iupLexAdvance();
  else
    return iupLexError (IUPLEX_NOTMATCH, ilex.token, t);
}


int iupLexSeenMatch(int t, int *erro)
{
  if (ilex.token==t)
  {
    *erro = iupLexAdvance();
    return 1;
  }
  else
    return 0;
}

unsigned char iupLexByte(void)
{
  unsigned int b;
  sscanf(ilex.name,"%u", &b);  /* read as integer to avoid reading number as characters */
  if (b>255) b = 255;
  return (unsigned char)b;
}

int iupLexInt(void)
{
  int i;
  sscanf(ilex.name,"%d", &i);
  return i;
}

float iupLexFloat(void)
{
  float f;
  sscanf(ilex.name,"%g", &f);
  return f;
}

char* iupLexGetName(void)
{
  if (ilex.name)
    return iupStrDup(ilex.name);
  else
    return NULL;
}

char* iupLexName(void)
{
  return ilex.name;
}

float iupLexGetNumber(void)
{
  return ilex.number;
}

Iclass *iupLexGetClass(void)
{
  return ilex.ic;
}

static int iLexToken(int *erro)
{
  for (;;)
  {
    int c = iLexGetChar();
    switch (c)
    {
    case 26:
    case EOF:
      return IUPLEX_TK_END;

    case ']':
      return IUPLEX_TK_ENDATTR;

    case '#':          /* Skip comment */
    case '%':          /* Skip comment */
      iLexSkipComment();
      continue;

    case ' ':          /* ignore whitespace */
    case '\t':
    case '\n':
    case '\r':
    case '\f':
    case '\v':
      continue;

    case '=':          /* attribuicao */
      return IUPLEX_TK_SET;

    case ',':
      return IUPLEX_TK_COMMA;

    case '(':          /* begin parameters */
      return IUPLEX_TK_BEGP;

    case ')':          /* end parameters */
      return IUPLEX_TK_ENDP;

    case '[':          /* attributes */
      if (iLexCaptureAttr() == IUPLEX_TK_END)
      {
        *erro=iupLexError (IUPLEX_NOTENDATTR);
        return 0;
      }
      return IUPLEX_TK_ATTR;

    case '\"':          /* string */
      iLexCapture ("\"");
      return IUPLEX_TK_STR;

    case '\'':          /* string */
      iLexCapture ("\'");
      return IUPLEX_TK_STR;

    default:
      if (c > 32)          /* identifier */
      {
        char class_name[50];
        iLexUngetc(c);
        iLexUngetc(iLexCapture ("=[](), \t\n\r\f\v"));
        iupStrLower(class_name, iupLexName());
        ilex.ic = iupRegisterFindClass(class_name);
        if (ilex.ic)
          return IUPLEX_TK_FUNC;
        else
          return IUPLEX_TK_NAME;
      }
    }
    return c;
  }
}

static int iLexCapture (char* dlm)
{
  int i=0;
  int c;
  do
  {
    c = iLexGetChar ();
    if (i < sizeof(ilex.name))
      ilex.name[i++] = (char) c;
  } while ((c > 0) && !strchr (dlm,c));
  ilex.name[i-1]='\0';                            /* discard delimiter */
  return c;                                      /* return delimiter */
}

static int iLexCaptureAttr (void)
{
  int i=0;
  int c;
  int aspas=0;
  do
  {
    c = iLexGetChar ();
    if (i < sizeof(ilex.name))
      ilex.name[i++] = (char) c;
    if (c == '"')
      ++aspas;
  } while ((c > 0) && ((aspas & 1) || c != ']'));
  ilex.name[i-1]='\0';                            /* discard delimiter */
  return c;                                      /* return delimiter */
}

static void iLexSkipComment (void)
{
  int c;
  do
  {
    c = iLexGetChar();
  } while ((c > 0) && (c != '\n'));
}

static int iLexGetChar (void)
{
  int c = iLexGetc(); if (c == '\n') ++ilex.line;
  if (c == '\\')
  {
    c = iLexGetc();
    if (c == 'n')
      return '\n';
    else if (c == '\\')
      return '\\';
  }
  return c;
}

static char* iupTokenStr(int t)
{
  switch (t)
  {
  case IUPLEX_TK_END  : return "end of file";
  case IUPLEX_TK_BEGP : return "(";
  case IUPLEX_TK_ENDP : return ")";
  case IUPLEX_TK_ATTR : return "[";
  case IUPLEX_TK_STR  : return "string";
  case IUPLEX_TK_NAME : return "identifier";
  case IUPLEX_TK_NUMB : return "number";
  case IUPLEX_TK_SET  : return "=";
  case IUPLEX_TK_COMMA: return ",";
  case IUPLEX_TK_FUNC : return "function";
  case IUPLEX_TK_ENDATTR : return "]";
  }
  return "";
}

static char ilex_erromsg[10240];

char *iupLexGetError(void)
{
  return ilex_erromsg;
}

int iupLexError (int n, ...)
{
  char msg[10240];
  va_list va;
  va_start(va,n);
  switch (n)
  {
  case IUPLEX_FILENOTOPENED:
    {
      char *fn=va_arg(va,char *);
      sprintf (msg, "cannot open file %s", fn);
    }
    break;
  case IUPLEX_NOTMATCH:
    {
      int tr=va_arg(va,int);        /* iLexToken read */
      int te=va_arg(va,int);        /* iLexToken expected */
      char *str=iupTokenStr(tr);
      char *ste=iupTokenStr(te);
      sprintf (msg, "expected %s but found %s", ste, str);
    }
    break;
  case IUPLEX_NOTENDATTR:
    {
      sprintf (msg, "missing ']'");
    }
    break;
  case IUPLEX_PARSEERROR:
    {
      char* s=va_arg(va,char*);        /* iLexToken expected */
      sprintf(msg,"%.*s",(int)(sizeof(msg)-1),s);
    }
    break;
  }
  va_end(va);
  sprintf(ilex_erromsg, "led(%s): bad input at line %d - %s\n", ilex.filename, ilex.line, msg);
  return n;
}