chiark / gitweb /
math/: Implement Grantham's Frobenius (primality) test.
[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 "qdparse.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 /* --- @qd_skipspc@ --- *
38  *
39  * Arguments:   @qd_parse *qd@ = context
40  *
41  * Returns:     ---
42  *
43  * Use:         Skips spaces in the string.  No errors.
44  */
45
46 void qd_skipspc(qd_parse *qd)
47 {
48   while (isspace((unsigned char)*qd->p))
49     qd->p++;
50 }
51
52 /* --- @qd_delim@ --- *
53  *
54  * Arguments:   @qd_parse *qd@ = context
55  *              @int ch@ = character to compare with
56  *
57  * Returns:     Nonzero if it was, zero if it wasn't.
58  *
59  * Use:         Checks the next (non-whitespace) character is what we
60  *              expect.  If it is, the character is eaten; otherwise it's no
61  *              big deal.
62  */
63
64 int qd_delim(qd_parse *qd, int ch)
65 {
66   qd_skipspc(qd);
67   if (*qd->p != ch)
68     return (0);
69   qd->p++;
70   return (1);
71 }
72
73 /* --- @qd_enum@ --- *
74  *
75  * Arguments:   @qd_parse *qd@ = context
76  *              @const char *e@ = list of enum strings, space separated
77  *
78  * Returns:     Index of the string matched, or @-1@.
79  *
80  * Use:         Matches a keyword.
81  */
82
83 int qd_enum(qd_parse *qd, const char *e)
84 {
85   size_t n;
86   int i = 0;
87
88   qd_skipspc(qd);
89   for (;;) {
90     e += strspn(e, ", ");
91     if (!*e) break;
92     n = strcspn(e, ", ");
93     if (strncmp(qd->p, e, n) == 0 && !isalnum((unsigned char)qd->p[n])) {
94       qd->p += n;
95       return (i);
96     }
97     i++; e += n;
98   }
99   qd->e = "unrecognized keyword";
100   return (-1);
101 }
102
103 /* --- @qd_getmp@ --- *
104  *
105  * Arguments:   @qd_parse *qd@ = context
106  *
107  * Returns:     The integer extracted, or null.
108  *
109  * Use:         Parses a multiprecision integer from a string.
110  */
111
112 mp *qd_getmp(qd_parse *qd)
113 {
114   char *q;
115   mp *m;
116
117   qd_skipspc(qd);
118   m = mp_readstring(MP_NEW, qd->p, &q, 0);
119   if (m && !isalnum((unsigned char)*q))
120     qd->p = q;
121   else {
122     mp_drop(m);
123     qd->e = "bad number";
124   }
125   return (m);
126 }
127
128 /* --- @qd_eofp@ --- *
129  *
130  * Arguments:   @qd_parse *qd@ = context
131  *
132  * Returns:     Nonzero if at EOF, zero otherwise.
133  */
134
135 int qd_eofp(qd_parse *qd)
136 {
137   qd_skipspc(qd);
138   return (!*qd->p);
139 }
140
141 /*----- That's all, folks -------------------------------------------------*/