aboutsummaryrefslogtreecommitdiff
path: root/gcc/algol68/a68-parser-pragmat.cc
blob: 2407eaa1b721f46ac364190b9c37809e20618060 (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
/* Handling of compiler pragmats.
   Copyright (C) 2025 Jose E. Marchesi.

   Written by Jose E. Marchesi.

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

   GCC 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 GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#define INCLUDE_MEMORY
#include "config.h"
#include "system.h"
#include "coretypes.h"

#include "a68.h"

/* Utility macros useful for parsing pragmat contents.  */

#define SKIP_WHITESPACES(P)			\
  while (ISSPACE (*(P))) (P)++

#define PARSE_WORD(P,W)					\
  do							\
    {							\
      (W) = (char *) alloca (strlen ((P)));		\
      size_t i = 0;					\
      while (ISALPHA (*(P)))				\
	(W)[i++] = *((P)++);				\
      (W)[i] = '\0';					\
    } while (0)

/* Parse a string denotation and return its value in an allocated buffer in
   *STR.  It is up to the caller to dispose of the allocated memory.

   This function returns a pointer to the character following the string
   denotation in P, or P in case of parse error.

   In case of parse error *STR is also set to NULL.  */

const char *
parse_string_denotation (const char *p, char **str)
{
  const char *orig = p;
  size_t i = 0;
  char *s = (char *) xmalloc (2 * strlen (p));

  if (*(p++) != '\"')
    goto error;
  while (*p != '\0' && *p != '\"')
    {
      if (*p == '\'' && *(p + 1) == '\"')
	{
	  s[i++] = '\"';
	  p += 2;
	}
      else
	{
	  s[i++] = *p;
	  p++;
	}
    }
  if (*p != '\"')
    goto error;

  *str = s;
  return p + 1;
 error:
  free (s);
  *str = NULL;
  return orig;
}

/* PR access MODULE in "filename" PR  */

const char *
handle_access_in_pragmat (NODE_T *p, const char *pragmat, size_t pos)
{
  const char *beginning = pragmat;

  SKIP_WHITESPACES (pragmat);
  /* Parse module name.  */
  char *module;
  PARSE_WORD (pragmat, module);
  if (*module == '\0')
    {
      a68_error (p, "invalid pragmat: expected module name after %<access%>");
      return NULL;
    }
  SKIP_WHITESPACES (pragmat);
  /* Parse "in " */
  if (strncmp (pragmat, "in ", 3) != 0)
    {
      a68_error (p, "invalid pragmat: expected %<in%> after module name");
      return NULL;
    }
  pragmat += 3;
  SKIP_WHITESPACES (pragmat);
  /* Parse a string denotation.  */
  char *filename;
  pragmat = parse_string_denotation (pragmat, &filename);
  if (filename == NULL)
    {
      size_t off = pos + pragmat - beginning;
      char *found;
      PARSE_WORD (pragmat, found);
      a68_error_in_pragmat (p, off,
			    "in %<access%> pragmat, expected string, found Z",
			    found);
      return NULL;
    }
  /* Add entry in the module files map.  */
  const char **pmodule = A68_MODULE_FILES->get (module);
  if (pmodule != NULL)
    {
      a68_error_in_pragmat (p, pos + pragmat - beginning,
			    "module Z cannot appear in multiple %<access%> pragmats",
			    module);
      return NULL;
    }

  SKIP_WHITESPACES (pragmat);
  /* Skip closing PR or PRAGMAT.  */
  if (NPRAGMAT_TYPE (p) == STYLE_I_PRAGMAT_SYMBOL)
    pragmat += 2;
  else
    pragmat += 7;

  A68_MODULE_FILES->put (ggc_strdup (module), ggc_strdup (filename));
  free (filename);
  return pragmat;
}

/* Parse and action on a pragmat.  */

void
handle_pragmat (NODE_T *p)
{
  const char *pragmat = NPRAGMAT (p);
  if (pragmat != NULL
      && (NPRAGMAT_TYPE (p) == STYLE_I_PRAGMAT_SYMBOL
	  || NPRAGMAT_TYPE (p) == BOLD_PRAGMAT_SYMBOL))
    {
      /* Process pragmat commands.  */
      do
	{
	  SKIP_WHITESPACES (pragmat);
	  /* Skip PR or PRAGMAT.  */
	  if (NPRAGMAT_TYPE (p) == STYLE_I_PRAGMAT_SYMBOL)
	    pragmat += 2;
	  else
	    pragmat += 7;
	  SKIP_WHITESPACES (pragmat);
	  /* Get first word in pragmat and dispatch.  */
	  SKIP_WHITESPACES (pragmat);
	  char *word = (char *) alloca (strlen (pragmat));
	  size_t i = 0;
	  while (ISALPHA (*pragmat) || *pragmat == '\n')
	    word[i++] = *(pragmat++);
	  word[i] = '\0';

	  if (strcmp (word, "access") == 0)
	    {
	      pragmat
		= handle_access_in_pragmat (p, pragmat, pragmat - NPRAGMAT (p));
	      if (pragmat == NULL)
		break;
	    }
	  else if (strcmp (word, "include") == 0)
	    /* Include pragmats are handled in the scanner.  */
	    return;
	  else
	    {
	      a68_error_in_pragmat (p, pragmat - NPRAGMAT (p),
				    "unrecognized pragmat Z", word);
	      break;
	    }
	}
      while (*pragmat != '\0');
    }
}

/* Entry point: handle all pragmats in the given parse tree.  */

void
a68_handle_pragmats (NODE_T *p)
{
  for (; p != NO_NODE; FORWARD (p))
    {
      a68_handle_pragmats (SUB (p));
      handle_pragmat (p);
    }
}