Debian Codegraph @

s_parse.c

raw symbols SCIP
1 /*** s_parse.c ****************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2020 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **
25 ******************************************************************************/
26
27 #include <bibtool/general.h>
28 #include <bibtool/symbols.h>
29 #include <bibtool/error.h>
30 #include <bibtool/parse.h>
31 #include <bibtool/s_parse.h>
32 #include <bibtool/type.h>
33
34 /*****************************************************************************/
35 /* Internal Programs */
36 /*===========================================================================*/
37
38 /*****************************************************************************/
39 /* External Programs */
40 /*===========================================================================*/
41
42 /*---------------------------------------------------------------------------*/
43
44 #define Error(E,S,A,B) \
45 if (E) error(ERR_ERROR|ERR_POINT, \
46 (String)A, \
47 (String)B, \
48 (String)0, \
49 sp_line == NULL ? S : sp_line, \
50 (String)S, \
51 0, \
52 (String)0)
53
54 static String unexpected = (String)"Unexpected "; /* */
55 static String expected = (String)" expected."; /* */
56
57 static String sp_line = StringNULL; /* */
58
59 /*-----------------------------------------------------------------------------
60 ** Function: sp_open()
61 ** Purpose: Open a string for parsing. The argument string is used
62 ** for the parsing process. Thus this string should not
63 ** be modified during this time. Especially it should
64 ** not be freed if it is a pointer to dynamically
65 ** allocated memory.
66 ** Arguments:
67 ** s String to open for parsing.
68 ** Returns: nothing
69 **___________________________________________________ */
70 void sp_open(s) /* */
71 String s; /* */
72 { sp_line = s; /* */
73 } /*------------------------*/
74
75 /*-----------------------------------------------------------------------------
76 ** Function: sp_close()
77 ** Type: void
78 ** Purpose: Release the string parser and rest it to its initial state.
79 ** Arguments: none
80 ** Returns: nothing
81 **___________________________________________________ */
82 void sp_close() /* */
83 { /* */
84 sp_line = StringNULL; /* */
85 } /*------------------------*/
86
87 /*-----------------------------------------------------------------------------
88 ** Function: sp_eos()
89 ** Type: Sting
90 ** Purpose: The string is analyzed and any remaining characters which are
91 ** not whitespace are reported as error. A pointer to the
92 ** terminating 0 byte
93 ** Arguments:
94 ** sp the string pointer
95 ** Returns:
96 **___________________________________________________ */
97 String sp_eos(sp) /* */
98 String *sp; /* */
99 { register String s = *sp; /* */
100 /* */
101 while( is_space(*s) ) s++; /* */
102 *sp = s; /* */
103 /* */
104 if ( *s ) /* */
105 { Error(-1, s, unexpected, /* */
106 "characters at end of string."); /* */
107 } /* */
108 /* */
109 sp_line = NULL; /* */
110 return (*s ? s : NULL); /* */
111 } /*------------------------*/
112
113 /*-----------------------------------------------------------------------------
114 ** Function: sp_skip()
115 ** Type: void
116 ** Purpose: Advance a String pointer to the next character which is not a
117 ** white-space, equals or hash.
118 ** Arguments:
119 ** sp the string pointer
120 ** Returns:
121 **___________________________________________________ */
122 void sp_skip(sp) /* */
123 register String *sp; /* */
124 { register String s = *sp; /* */
125 while ( is_space(*s) /* */
126 || *s == '=' /* */
127 || *s == '#' ) s++; /* */
128 *sp = s; /* */
129 } /*------------------------*/
130
131 /*-----------------------------------------------------------------------------
132 ** Function: s_parse()
133 ** Purpose: Parse a string for a certain entity. Leading
134 ** whitespace is ignored. |type| determines which kind of
135 ** entity should be expected. It can take the following
136 ** values which are defined in |s_parse.h|:
137 ** \begin{description}
138 ** \item[StringParseValue] The string is analyzed and the
139 ** proper type is determined automatically. This can be
140 ** considered as the normal way of operation.
141 ** \item[StringParseSymbol] The string is analyzed and
142 ** only a symbol is accepted, i.e.\ a sequence of
143 ** allowed characters.
144 ** \item[StringParseNumber] The string is analyzed and
145 ** only a number is accepted.
146 ** \item[StringParseBraces] The string is analyzed and
147 ** only an expression in braces is accepted. The braced
148 ** contained must come in matching pairs. The whole
149 ** expression -- including the braces -- is returned.
150 ** \item[StringParseUnquotedBraces] The string is analyzed and
151 ** only an expression in braces is accepted. The braced
152 ** contained must come in matching pairs. The
153 ** expression without the outer braces is returned.
154 ** \item[StringParseString] The string is analyzed and
155 ** only a string enclosed in double quotes is
156 ** accepted. The string must contain braces in matching
157 ** pairs. Double quotes which are inside of braces are
158 ** not considered as end of the string. The whole
159 ** string -- including the double quotes is returned.
160 ** \item[StringParseUnquotedString] The string is analyzed and
161 ** only a string enclosed in double quotes is
162 ** accepted. The string must contain braces in matching
163 ** pairs. Double quotes which are inside of braces are
164 ** not considered as end of the string. The string
165 ** without the outer double quotes is returned.
166 ** \end{description}
167 ** If an error occurs or the requested entity is not
168 ** found then |NULL| is returned. As a side effect |sp|
169 ** is advanced to point to the next unprocessed
170 ** character.
171 **
172 ** The string analyzed should be opened at the beginning
173 ** with |sp_open()| in order to get an appropriate error
174 ** message.
175 **
176 ** This function is usually not called directly but the
177 ** convenience macros defined in |s_parse.h| should be
178 ** used instead.
179 ** Arguments:
180 ** type is the type of construct to parse. It is defined in s_parse.h
181 ** sp is a pointer to the string which is parsed. The value is
182 ** changed to hold the remaining characters at the end.
183 ** errp this boolean indicated whether or not a verbose error
184 ** message should be created in case of an error.
185 ** Returns: A symbol containing the requested entity or |NULL|.
186 **___________________________________________________ */
187 Symbol s_parse(type, sp, errp) /* */
188 int type; /* */
189 String *sp; /* */
190 bool errp; /* */
191 { register String s = *sp; /* */
192 Uchar c; /* */
193 String cp; /* */
194 /* */
195 DebugPrint2((type == StringParseNext ? "ParseNext ":
196 type == StringParseNumber ? "ParseNumber ":
197 type == StringParseSymbol ? "ParseSymbol ":
198 type == StringParseString ? "ParseString ":
199 type == StringParseUnquotedString ? "ParseUnquotedString ":
200 type == StringParseBraces ? "ParseBraces ":
201 type == StringParseUnquotedBraces ? "ParseUnquotedBraces ":
202 type == StringParseValue ? "ParseValue ":
203 "???"),s); /* */
204 while(is_space(*s)) s++; /* */
205 *sp = s; /* */
206 /* */
207 switch (type) /* */
208 { case StringParseSymbol: /* */
209 if ( is_allowed(*s) && !is_digit(*s) ) /* */
210 { do { s++; } while ( is_allowed(*s) ); /* */
211 } /* */
212 else /* */
213 { Error(errp, s, "Symbol", expected); /* */
214 return NULL; /* */
215 } /* */
216 break; /* */
217 /* */
218 case StringParseNumber: /* */
219 if (is_digit(*s)) /* */
220 { do { s++; } while ( is_digit(*s) ); /* */
221 } /* */
222 else /* */
223 { Error(errp, s, "Number", expected); /* */
224 return NULL; /* */
225 } /* */
226 break; /* */
227 /* */
228 case StringParseBraces: /* */
229 case StringParseUnquotedBraces: /* */
230 if (*s != '{') /* */
231 { Error(errp, s, "Brace", expected); /* */
232 return NULL; /* */
233 } /* */
234 s++; /* */
235 if (type == StringParseUnquotedBraces) /* */
236 { *sp = s; } /* */
237 { int level = 1; /* */
238 /* */
239 while ( *s && level > 0 ) /* */
240 { /* */
241 switch ( *s ) /* */
242 { case '{': level++; break; /* */
243 case '}': --level; break; /* */
244 case '"': /* */
245 s++; /* */
246 while (*s && *s != '"') /* */
247 { if (*s != '\\' || *++s ) s++; /* */
248 } /* */
249 break; /* */
250 } /* */
251 if ( *s ) s++; /* */
252 } /* */
253 if (level > 0) /* */
254 { Error(errp, s, unexpected, "end of braces.");/* */
255 } /* */
256 else if (type == StringParseUnquotedBraces)/* */
257 { s--; } /* */
258 } /* */
259 break; /* */
260 /* */
261 case StringParseString: /* */
262 case StringParseUnquotedString: /* */
263 if ( *s != '"' ) /* */
264 { Error(errp, s, "String", expected); /* */
265 return NULL; /* */
266 } /* */
267 s++; /* */
268 if ( type == StringParseUnquotedString ) /* */
269 { *sp = s; } /* */
270 /* */
271 while ( *s && *s != '"' ) /* */
272 { if ( *s == '\\' && *(s+1) != '\0' ) s += 2;/* */
273 else ++s; /* */
274 } /* */
275 /* */
276 if ( *s == '"' ) /* */
277 { if (type != StringParseUnquotedString) s++;/* */
278 } /* */
279 else /* */
280 { Error(errp, s, unexpected, "end of string.");/* */
281 } /* */
282 break; /* */
283 /* */
284 case StringParseValue: /* */
285 switch ( *s ) /* */
286 { case '"': /* */
287 return s_parse(StringParseUnquotedString, sp, errp);/* */
288 case '{': /* */
289 return s_parse(StringParseUnquotedBraces, sp, errp);/* */
290 case '0': case '1': case '2': case '3': /* */
291 case '4': case '5': case '6': case '7': /* */
292 case '8': case '9': /* */
293 return s_parse(StringParseNumber, sp, errp);/* */
294 default: /* */
295 return s_parse(StringParseSymbol, sp, errp);/* */
296 } /* */
297 break; /* */
298 /* */
299 default: /* */
300 return NULL; /* */
301 } /* */
302 /* */
303 c = *s; /* */
304 *s = (Uchar)'\0'; /* */
305 cp = newString(*sp); /* */
306 *s = c; /* */
307 if ( type == StringParseUnquotedString /* */
308 || type == StringParseUnquotedBraces ) s++; /* */
309 *sp = s; /* */
310 if (type == StringParseSymbol) /* */
311 { (void)lower(cp); } /* */
312 /* */
313 { Symbol sym = symbol(cp); /* */
314 free(cp); /* */
315 return sym; /* */
316 } /* */
317 } /*------------------------*/
318
319 /*-----------------------------------------------------------------------------
320 ** Function: sp_expect()
321 ** Type: int
322 ** Purpose: Read a sequence of expected characters after whitespace.
323 ** The source pointer is advanced until the expected
324 ** string has been read or to the first character which
325 ** is not expected.
326 ** Arguments:
327 ** sp the pointer to the source string
328 ** expect the expected string
329 ** verbose the indicator whether an error message should be produced
330 ** Returns: |true| iff the expected string is found
331 **___________________________________________________ */
332 bool sp_expect(sp, expect, verbose) /* */
333 register String *sp; /* */
334 register String expect; /* */
335 bool verbose; /* */
336 { /* */
337 while (is_space(**sp)) (*sp)++; /* */
338 /* */
339 for ( ; *expect; expect++) /* */
340 { if (*expect != **sp) /* */
341 { if (verbose) /* */
342 { Error(1, *sp, expect, expected); } /* */
343 return false; /* */
344 } /* */
345 (*sp)++; /* */
346 } /* */
347 return true; /* */
348 } /*------------------------*/
349
350 #define SkipSpaces(PTR) while( is_space(*PTR) ) (PTR)++
351
352 /*-----------------------------------------------------------------------------
353 ** Function: sp_symbols()
354 ** Type: String*
355 ** Purpose:
356 **
357 ** Arguments:
358 ** sp the pointer to the value to be parsed
359 ** Returns: an array of Symbols
360 **___________________________________________________ */
361 Symbol* sp_symbols(sp) /* */
362 String *sp; /* */
363 { Symbol s; /* */
364 Symbol *a = NULL; /* */
365 int n = 0; /* */
366 int i = 0; /* */
367 /* */
368 if (sp_expect(sp, (String)"{", false)) /* */
369 { n = 4; /* */
370 a = (Symbol*)malloc(n * sizeof(Symbol)); /* */
371 if (a == (Symbol*)NULL) /* */
372 { OUT_OF_MEMORY("symbols"); } /* */
373 /* */
374 SkipSpaces(*sp); /* */
375 while (**sp && **sp != '}') /* */
376 { SkipSpaces(*sp); /* */
377 if ((s = SParseSymbol(sp)) == NO_SYMBOL) { /* */
378 return (Symbol*)NULL; /* */
379 } /* */
380 if (i >= n - 1) /* */
381 { n += 4; /* */
382 a = (Symbol*)realloc(a, n * sizeof(Symbol));/* */
383 if (a == NULL) { OUT_OF_MEMORY("symbols"); }/* */
384 } /* */
385 a[i++] = s; /* */
386 SkipSpaces(*sp); /* */
387 } /* */
388 if (**sp == '\0') /* */
389 { Error(true, *sp, unexpected, /* */
390 "end of symbols."); /* */
391 return (Symbol*)NULL; /* */
392 } /* */
393 (*sp)++; /* */
394 a[i] = NO_SYMBOL; /* */
395 /* */
396 } else if ( !is_digit(**sp) && /* */
397 (s=s_parse(StringParseSymbol, /* */
398 sp, 0)) != NO_SYMBOL) { /* */
399 a = (Symbol*)malloc(2 * sizeof(Symbol)); /* */
400 if (a == NULL) { OUT_OF_MEMORY("symbols"); } /* */
401 a[0] = s; /* */
402 a[1] = NO_SYMBOL; /* */
403 } else { /* */
404 Error(true, *sp, "List of symbols", expected); /* */
405 } /* */
406 return a; /* */
407 } /*------------------------*/