summaryrefslogtreecommitdiffabout
path: root/src/grecs-lex.l
blob: baf85de0f69de49c034ce44786b021b7508b65dd (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* grecs - Gray's Extensible Configuration System       -*- c -*- */
%top {
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
}
%{
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2011 Sergey Poznyakoff

   Grecs is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Grecs is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Grecs. If not, see <http://www.gnu.org/licenses/>. */

#include <grecs.h>
#include <grecs-gram.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
  
#include <wordsplit.h>

static char *multiline_delimiter;
static size_t multiline_delimiter_len;
static int multiline_unescape;         /* Unescape here-document contents */
static int (*char_to_strip) (char);    /* Strip matching characters of each
					  here-document line */

grecs_locus_t grecs_current_locus;   /* Input file location */
/* Line correction.  Equals to the number of #line directives inserted into
   the input by the preprocessor instance.  The external preprocessor, if
   any, counts these as input lines and therefore the line numbers in *its*
   #line directives are offset by the value of XLINES.
   
   Uff, running two preprocessors is confusing...
*/
static size_t xlines; 

static struct grecs_list *line_acc;
static struct grecs_list *string_list;
    
static void multiline_begin (char *);
static void multiline_add (char *);
static char *multiline_strip_tabs (char *text);
static void line_add_unescape_last (char *text, size_t len);
static int ident (void);
static int isemptystr (int off);

static void parse_line (char *text, grecs_locus_t *ploc, size_t *pxlines);
static void parse_line_cpp (char *text, grecs_locus_t *ploc, size_t *pxlines);

#undef YY_INPUT
#define YY_INPUT(buf,result,max_size)			    \
  do							    \
    {							    \
      if (grecs_preprocessor)				    \
	result = fread (buf, 1, max_size, yyin);	    \
      else						    \
	result = grecs_preproc_fill_buffer(buf, max_size);		    \
    }							    \
  while (0)
 
%}
    
    
%x COMMENT ML STR

WS [ \t\f][ \t\f]*
ID [a-zA-Z_][a-zA-Z_0-9-]+
P [1-9][0-9]*

%%
         /* C-style comments */
"/*"         BEGIN (COMMENT);
<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<COMMENT>\n             ++grecs_current_locus.line;
<COMMENT>"*"+"/"        BEGIN (INITIAL);
         /* Line directive */
^[ \t]*#[ \t]*{P}[ \t]+\".*\".*\n { parse_line_cpp (yytext,
						    &grecs_current_locus,
						    &xlines); }
^[ \t]*#[ \t]*line[ \t].*\n       { parse_line (yytext, &grecs_current_locus,
						&xlines); }
         /* End-of-line comments */
#.*\n     { grecs_current_locus.line++; }
#.*     /* end-of-file comment */;
"//".*\n  { grecs_current_locus.line++; }
"//".*    /* end-of-file comment */;
        /* Identifiers */
<INITIAL>{ID}           return ident ();
         /* Strings */
[a-zA-Z0-9_\.\*/:@-]+    { grecs_line_begin ();
                          grecs_line_add (yytext, yyleng);
                          yylval.string = grecs_line_finish ();
                          return STRING; }
         /* Quoted strings */
\"[^\\"\n]*\"         { grecs_line_begin ();
                        grecs_line_add (yytext + 1, yyleng - 2);
                        yylval.string = grecs_line_finish ();
                        return QSTRING; }
\"[^\\"\n]*\\. |
\"[^\\"\n]*\\\n        { BEGIN (STR);
                        grecs_line_begin ();
		        line_add_unescape_last (yytext + 1, yyleng - 1); }
<STR>[^\\"\n]*\\. |
<STR>\"[^\\"\n]*\\\n  { line_add_unescape_last (yytext, yyleng); }
<STR>[^\\"\n]*\"      { BEGIN(INITIAL);
                        if (yyleng > 1) 
                          grecs_line_add (yytext, yyleng - 1); 
                        yylval.string = grecs_line_finish ();
		        return QSTRING; }
         /* Multiline strings */
"<<"(-" "?)?\\?{ID}[ \t]*#.*\n |
"<<"(-" "?)?\\?{ID}[ \t]*"//".*\n |
"<<"(-" "?)?\\?{ID}[ \t]*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*#.*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*\n {
                        BEGIN (ML);
			multiline_begin (yytext+2);
			grecs_current_locus.line++; }
         /* Ignore m4 line statements */
<ML>^"#line ".*\n {  grecs_current_locus.line++; }
<ML>.*\n { char *p = multiline_strip_tabs (yytext);
	   
           if (!strncmp (p, multiline_delimiter, multiline_delimiter_len)
	       && isemptystr (p + multiline_delimiter_len - yytext))
	     {
	       free (multiline_delimiter);
	       multiline_delimiter = NULL;
	       BEGIN (INITIAL);
	       yylval.string = grecs_line_finish ();
	       return MSTRING;
	     }
           grecs_current_locus.line++;
	   multiline_add (p); } 
{WS}     ;
         /* Other tokens */
\n       { grecs_current_locus.line++; } 
[,;{}()] return yytext[0];
.        { if (isascii (yytext[0]) && isprint (yytext[0]))
	     grecs_error (&grecs_current_locus, 0, _("stray character %c"), yytext[0]);
           else
             grecs_error (&grecs_current_locus, 0, _("stray character \\%03o"),
		   	   (unsigned char) yytext[0]); }
%%

pid_t grecs_preproc_pid;

int
yywrap ()
{
  if (yyin)
    grecs_preproc_extrn_shutdown (grecs_preproc_pid);
  else
    grecs_preproc_done ();
  grecs_current_locus.file = NULL;
  return 1;
}

static void
line_acc_free_entry (void *ptr)
{
  free (ptr);
}

int
grecs_lex_begin (const char *name)
{
  if (yy_flex_debug > 0)
    yy_flex_debug = 0;

  line_acc = grecs_list_create ();
  line_acc->free_entry = line_acc_free_entry;
  string_list = grecs_list_create ();
  
  if (grecs_preprocessor)
    {
      int fd;
	
      fd = open (name, O_RDONLY);
      if (fd == -1)
	{
	  grecs_error (NULL, errno, _("Cannot open `%s'"), name);
	  return 1;
	}
      close (fd);

      yyin = grecs_preproc_extrn_start (name, &grecs_preproc_pid);
      if (!yyin)
	{
	  grecs_error (NULL, errno,
		       _("Unable to start external preprocessor `%s'"),
		       grecs_preprocessor);
	  return 1;
	}
    }
  else
    return grecs_preproc_init (name);

  return 0;
}

void
grecs_lex_end ()
{
}

static int
isemptystr (int off)
{
  for (; yytext[off] && isspace (yytext[off]); off++)
    ;
  if (yytext[off] == ';')
    {
      int i;
      for (i = off + 1; yytext[i]; i++) 
	if (!isspace (yytext[i]))
	  return 0;
      yyless (off);
      return 1;
    }
  return yytext[off] == 0;
}

char *
multiline_strip_tabs (char *text)
{
  if (char_to_strip)
    for (; *text && char_to_strip (*text); text++)
      ;
  return text;
}

static int
unquote_char (int c)
{
  static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";

  char *p;

  for (p = quote_transtab; *p; p += 2)
    {
      if (*p == c)
	return p[1];
    }
  return -1;
}

struct line_acc_entry
{
  size_t size;
};
#define line_acc_ptr(entry) (char*)(entry + 1)
  
static void
line_acc_add_string (const char *str, size_t len)
{
  struct line_acc_entry *ent = grecs_malloc (sizeof (*ent) + len + 1);
  char *p = line_acc_ptr (ent);
  memcpy (p, str, len);
  p[len] = 0;
  ent->size = len;
  grecs_list_append (line_acc, ent);
}

static void
line_acc_add_char (int c)
{
  char t = c;
  line_acc_add_string (&t, 1);
}

static void
list_acc_unescape_char (int c)
{
  if (c != '\n')
    {
      int t = unquote_char (c);
      if (t != -1)
	line_acc_add_char (t);
      else
	{
	  grecs_warning(&grecs_current_locus, 0,
			_("unknown escape sequence '\\%c'"),
			c);
	  line_acc_add_char (c);
	}
    }
}

void
grecs_line_add (const char *text, size_t len)
{
  line_acc_add_string (text, len);
}

/* Same, but unescapes the last character from yytext */
static void
line_add_unescape_last (char *text, size_t len)
{
  line_acc_add_string (text, len - 2);
  list_acc_unescape_char (text[len - 1]);
}

static void
multiline_add (char *s)
{
  if (multiline_unescape)
    {
      for (; *s; s++)
	{
	  if (*s == '\\')
	    {
	      list_acc_unescape_char (s[1]);
	      ++s;
	    }
	  else
	    line_acc_add_char (*s);
	}
    }
  else
    grecs_line_add (s, strlen (s));
}

void
grecs_line_begin ()
{
    /* FIXME: nothing so far. Maybe prepare stk by calling obstack_finish? */
}

static int
is_tab (char c)
{
  return c == '\t';
}
 
static int
is_ws (char c)
{
  return c == '\t' || c == ' ';
}

void
multiline_begin (char *p)
{
  if (*p == '-')
    {
      if (*++p == ' ')
	{
	  char_to_strip = is_ws;
	  p++;
	}
      else
	char_to_strip = is_tab;
    }
  else
    char_to_strip = NULL;
  if (*p == '\\')
    {
      p++;
      multiline_unescape = 0;
    }
  else if (*p == '"')
    {
      char *q;
 
      p++;
      multiline_unescape = 0;
      q = strchr (p, '"');
      multiline_delimiter_len = q - p;
    }
  else
    {
      multiline_delimiter_len = strcspn (p, " \t");
      multiline_unescape = 1;
    }

  /* Remove trailing newline */
  multiline_delimiter_len--;
  multiline_delimiter = grecs_malloc (multiline_delimiter_len + 1);
  memcpy (multiline_delimiter, p, multiline_delimiter_len);
  multiline_delimiter[multiline_delimiter_len] = 0;
  grecs_line_begin ();
}

char *
grecs_line_finish ()
{
  struct grecs_list_entry *ep;
  size_t size = 0;
  char *str, *p;
  
  for (ep = line_acc->head; ep; ep = ep->next)
    {
      struct line_acc_entry *ent = ep->data;
      size += ent->size;
    }
  
  str = grecs_malloc (size + 1);
  grecs_list_append (string_list, str);
  for (ep = line_acc->head, p = str; ep; ep = ep->next)
    {
      struct line_acc_entry *ent = ep->data;
      char *str = line_acc_ptr (ent);
      memcpy (p, str, ent->size);
      p += ent->size;
    }
  *p = 0;
  grecs_list_clear (line_acc);
  return str;
}

static int
ident ()
{
  char *p;
  char *str;
  size_t len;
  
  for (p = yytext; *p && isspace (*p); p++)
    ;

  len = strlen (p);
  str = grecs_malloc (len + 1);
  strcpy (str, p);
  grecs_list_append (string_list, str);
  yylval.string = str;
  return IDENT;
}

void
grecs_lex_trace (int n)
{
  yy_flex_debug = -n;
}

grecs_value_t *
grecs_value_dup (grecs_value_t *input)
{
  grecs_value_t *ptr = grecs_malloc (sizeof (*ptr));
  grecs_list_append (string_list, ptr);
  *ptr = *input;
  return ptr;
}


static int
assign_locus (grecs_locus_t *ploc, char *name, char *line, size_t *pxlines)
{
  char *p;

  if (name)
    {
      if (pxlines && (!ploc->file || strcmp(name, ploc->file)))
	*pxlines = 0;
      ploc->file = grecs_install_text (name);
    }
  ploc->line = strtoul (line, &p, 10) - (pxlines ? *pxlines : 0);
  return *p != 0;
}

static void
parse_line (char *text, grecs_locus_t *ploc, size_t *pxlines)
{
  int rc = 1;
  struct wordsplit ws;
  
  if (wordsplit (text, &ws, WRDSF_DEFFLAGS))
    grecs_error (ploc, 0, _("cannot parse #line line"));
  else
    {
      if (ws.ws_wordc == 2)
	rc = assign_locus (ploc, NULL, ws.ws_wordv[1], pxlines);
      else if (ws.ws_wordc == 3) 
	rc = assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], pxlines);
      else if (ws.ws_wordc == 4)
	{
	  rc = assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], 0);
	  if (rc == 0)
	    {
	      char *p;
	      unsigned long x = strtoul (ws.ws_wordv[3], &p, 10);
	      rc = *p != 0;
	      if (rc == 0)
		*pxlines = x;
	    }
	}
      else 
	grecs_error (ploc, 0, _("invalid #line statement"));
	
      if (rc) 
	grecs_error (ploc, 0, _("malformed #line statement"));
      wordsplit_free (&ws);
    }
}

static void
parse_line_cpp (char *text, grecs_locus_t *ploc, size_t *pxlines)
{
  struct wordsplit ws;

  if (wordsplit (text, &ws, WRDSF_DEFFLAGS))
    {
      grecs_error (ploc, 0, _("cannot parse #line line"));
      return;
    }
  else if (ws.ws_wordc < 3)
    grecs_error (ploc, 0, _("invalid #line statement"));
  else
    {
      if (assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], pxlines))
	grecs_error (ploc, 0, _("malformed #line statement"));
    }
  wordsplit_free (&ws);
}

Return to:

Send suggestions and report system problems to the System administrator.