chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / qdparse.c
1 /* -*-c-*-
2  *
3  * Quick-and-dirty parser
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <string.h>
32
33 #include <mLib/macros.h>
34
35 #include "qdparse.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @qd_skipspc@ --- *
40  *
41  * Arguments:   @qd_parse *qd@ = context
42  *
43  * Returns:     ---
44  *
45  * Use:         Skips spaces in the string.  No errors.
46  */
47
48 void qd_skipspc(qd_parse *qd)
49 {
50   while (ISSPACE(*qd->p))
51     qd->p++;
52 }
53
54 /* --- @qd_delim@ --- *
55  *
56  * Arguments:   @qd_parse *qd@ = context
57  *              @int ch@ = character to compare with
58  *
59  * Returns:     Nonzero if it was, zero if it wasn't.
60  *
61  * Use:         Checks the next (non-whitespace) character is what we
62  *              expect.  If it is, the character is eaten; otherwise it's no
63  *              big deal.
64  */
65
66 int qd_delim(qd_parse *qd, int ch)
67 {
68   qd_skipspc(qd);
69   if (*qd->p != ch)
70     return (0);
71   qd->p++;
72   return (1);
73 }
74
75 /* --- @qd_enum@ --- *
76  *
77  * Arguments:   @qd_parse *qd@ = context
78  *              @const char *e@ = list of enum strings, space separated
79  *
80  * Returns:     Index of the string matched, or @-1@.
81  *
82  * Use:         Matches a keyword.
83  */
84
85 int qd_enum(qd_parse *qd, const char *e)
86 {
87   size_t n;
88   int i = 0;
89
90   qd_skipspc(qd);
91   for (;;) {
92     e += strspn(e, ", ");
93     if (!*e) break;
94     n = strcspn(e, ", ");
95     if (STRNCMP(qd->p, ==, e, n) && !ISALNUM(qd->p[n])) {
96       qd->p += n;
97       return (i);
98     }
99     i++; e += n;
100   }
101   qd->e = "unrecognized keyword";
102   return (-1);
103 }
104
105 /* --- @qd_getmp@ --- *
106  *
107  * Arguments:   @qd_parse *qd@ = context
108  *
109  * Returns:     The integer extracted, or null.
110  *
111  * Use:         Parses a multiprecision integer from a string.
112  */
113
114 mp *qd_getmp(qd_parse *qd)
115 {
116   char *q;
117   mp *m;
118
119   qd_skipspc(qd);
120   m = mp_readstring(MP_NEW, qd->p, &q, 0);
121   if (m && !ISALNUM(*q))
122     qd->p = q;
123   else {
124     mp_drop(m);
125     qd->e = "bad number";
126   }
127   return (m);
128 }
129
130 /* --- @qd_eofp@ --- *
131  *
132  * Arguments:   @qd_parse *qd@ = context
133  *
134  * Returns:     Nonzero if at EOF, zero otherwise.
135  */
136
137 int qd_eofp(qd_parse *qd)
138 {
139   qd_skipspc(qd);
140   return (!*qd->p);
141 }
142
143 /*----- That's all, folks -------------------------------------------------*/