summaryrefslogtreecommitdiffstats
path: root/gcc/bi-lexer.c
blob: 0766b5ff95360c623cf82c9318b9332089d6ff9d (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
/* Lexer for scanner of bytecode definition file.
   Copyright (C) 1993 Free Software Foundation, Inc.

This file is part of GNU CC.

GNU CC 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 2, or (at your option)
any later version.

GNU CC 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 GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <stdio.h>
#include "hconfig.h"
#include "bi-parser.h"

/* Current read buffer and point */
static char *buffer = NULL;
static char *inpoint = NULL;


/* Safely allocate NBYTES bytes of memory. Reuturns pointer to block of
   memory. */

static char *
xmalloc (nbytes)
     int nbytes;
{
  char *tmp = (char *) malloc (nbytes);

  if (!tmp)
    {
      fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n", nbytes);
      exit (FATAL_EXIT_CODE);
    }

  return tmp;
}


/* Safely reallocate BLOCK so its size becomes NBYTES.
   The block returned may be different from the one supplied. */

static char *
xrealloc (block, nbytes)
     char *block;
     int nbytes;
{
  char *tmp = (block
	       ? (char *) realloc (block, nbytes)
	       : (char *) malloc (nbytes));

  if (!tmp)
    {
      fprintf (stderr, "can't reallocate %d bytes (out of virtual memory)\n", nbytes);
      exit (FATAL_EXIT_CODE);
    }

  return tmp;
}


/* Scan for string token on standard input.  A string is, for our
   purposes here, a sequence of characters that starts with the regexp
   ``[^ #\t\n(),]'' and is then followed by the regexp ``[^#(),]*''. Any
   character is accepted if preceded by a backslash, "\\".  It is assumed
   that the first character has already been checked by the main loop. */

static char *
scan_string ()
{
  char *buffer = NULL;
  char *point = NULL;
  int buffer_size = 0;
  int c;

  while ((c = getc (stdin)) != EOF
	 && c != '#' && c != '(' && c != ')' && c != ',')
    {
      /* Extend buffer, if necessary (minus two so there's room for the NUL
	 trailer as well as another character if this one is a backslash).  */
      if (!buffer_size || (point - buffer >= buffer_size-2))
	{
	  int previous_point_index = point - buffer;

	  buffer_size = (!buffer_size ? 32 : buffer_size * 2);
	  if (!buffer)
	    buffer = xmalloc (buffer_size);
	  else
	    buffer = xrealloc (buffer, buffer_size);
	  
	  point = buffer + previous_point_index;
	}
      *point++ = c & 0xff;

      if (c == '\\')
	{
	  c = getc (stdin);

	  /* Catch special case: backslash at end of file */
	  if (c == EOF)
	    break;

	  *point++ = c;
	}
    }
  *point = 0;

  if (c != EOF)
    ungetc (c, stdin);

  return buffer;
}


int
yylex ()
{
  int c;
  char *token;


  /* First char determines what token we're looking at */
  for (;;)
    {
      c = getc (stdin);

      switch (c)
	{
	case EOF:
	  return 0;
	  
	case ' ':
	case '\t':
	case '\n':
	  /* Ignore whitespace */
	  continue;
	  
	case '#':
	  /* Comments advance to next line */
	  while ((c = getc (stdin)) != '\n' && c != EOF);
	  continue;
	  
	default:
	  if (c != '(' && c != ')' && c != '\\' && c != ',')
	    {
	      ungetc (c, stdin);
	      yylval.string = scan_string ();

	      /* Check if string is "define_operator"; if so, return
		 a DEFOP token instead.  */
	      if (!strcmp (yylval.string, "define_operator"))
		{
		  free (yylval.string);
		  yylval.string = 0;
		  return DEFOP;
		}
	      return STRING;
	    }
	  return c & 0xff;
	}
    }
}
OpenPOWER on IntegriCloud