]> git.cameronkatri.com Git - mandoc.git/blob - tokens.c
*** empty log message ***
[mandoc.git] / tokens.c
1 /* $Id: tokens.c,v 1.4 2008/12/09 17:09:12 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "private.h"
24
25
26 static int rofftok_dashes(const char *, int *);
27 static int rofftok_special(const char *, int *);
28 static int rofftok_predef(const char *, int *);
29 static int rofftok_defined(const char *, int *);
30
31
32 static int
33 rofftok_defined(const char *buf, int *i)
34 {
35 const char *p;
36
37 if (0 == buf[*i])
38 return(-1);
39 if (0 == buf[*i + 1])
40 return(-1);
41
42 (*i)++;
43 p = &buf[(*i)++];
44
45 if (0 == memcmp(p, ">=", 2))
46 return(ROFFTok_Ge);
47 else if (0 == memcmp(p, "<=", 2))
48 return(ROFFTok_Le);
49 else if (0 == memcmp(p, "Rq", 2))
50 return(ROFFTok_Rquote);
51 else if (0 == memcmp(p, "Lq", 2))
52 return(ROFFTok_Lquote);
53 else if (0 == memcmp(p, "ua", 2))
54 return(ROFFTok_Uparrow);
55 else if (0 == memcmp(p, "aa", 2))
56 return(ROFFTok_Acute);
57 else if (0 == memcmp(p, "ga", 2))
58 return(ROFFTok_Grave);
59 else if (0 == memcmp(p, "Pi", 2))
60 return(ROFFTok_Pi);
61 else if (0 == memcmp(p, "Ne", 2))
62 return(ROFFTok_Ne);
63 else if (0 == memcmp(p, "Le", 2))
64 return(ROFFTok_Le);
65 else if (0 == memcmp(p, "Ge", 2))
66 return(ROFFTok_Ge);
67 else if (0 == memcmp(p, "Lt", 2))
68 return(ROFFTok_Lt);
69 else if (0 == memcmp(p, "Gt", 2))
70 return(ROFFTok_Gt);
71 else if (0 == memcmp(p, "Pm", 2))
72 return(ROFFTok_Plusmin);
73 else if (0 == memcmp(p, "If", 2))
74 return(ROFFTok_Infty);
75 else if (0 == memcmp(p, "Na", 2))
76 return(ROFFTok_Nan);
77 else if (0 == memcmp(p, "Ba", 2))
78 return(ROFFTok_Bar);
79
80 return(-1);
81 }
82
83
84 static int
85 rofftok_predef(const char *buf, int *i)
86 {
87 if (0 == buf[*i])
88 return(-1);
89 if ('(' == buf[*i])
90 return(rofftok_defined(buf, i));
91
92 switch (buf[*i]) {
93 case ('q'):
94 return(ROFFTok_Quote);
95 default:
96 break;
97 }
98
99 return(-1);
100 }
101
102
103 static int
104 rofftok_dashes(const char *buf, int *i)
105 {
106
107 if (0 == buf[*i])
108 return(-1);
109 else if (buf[(*i)++] != 'e')
110 return(-1);
111 if (0 == buf[*i])
112 return(-1);
113
114 switch (buf[*i]) {
115 case ('m'):
116 return(ROFFTok_Em);
117 case ('n'):
118 return(ROFFTok_En);
119 default:
120 break;
121 }
122 return(-1);
123 }
124
125
126 static int
127 rofftok_special(const char *buf, int *i)
128 {
129
130 if (0 == buf[*i])
131 return(ROFFTok_Slash);
132
133 switch (buf[*i]) {
134 case ('a'):
135 return(ROFFTok_Sp_A);
136 case ('b'):
137 return(ROFFTok_Sp_B);
138 case ('f'):
139 return(ROFFTok_Sp_F);
140 case ('n'):
141 return(ROFFTok_Sp_N);
142 case ('r'):
143 return(ROFFTok_Sp_R);
144 case ('t'):
145 return(ROFFTok_Sp_T);
146 case ('v'):
147 return(ROFFTok_Sp_V);
148 case ('0'):
149 return(ROFFTok_Sp_0);
150 default:
151 break;
152 }
153 return(-1);
154 }
155
156
157 int
158 rofftok_scan(const char *buf, int *i)
159 {
160
161 assert(*buf);
162 assert(buf[*i] == '\\');
163
164 (*i)++;
165
166 for ( ; buf[*i]; (*i)++) {
167 switch (buf[*i]) {
168 case ('e'):
169 (*i)++;
170 return(rofftok_special(buf, i));
171 case ('('):
172 (*i)++;
173 return(rofftok_dashes(buf, i));
174 case (' '):
175 return(ROFFTok_Space);
176 case ('&'):
177 return(ROFFTok_Null);
178 case ('-'):
179 return(ROFFTok_Hyphen);
180 case ('*'):
181 (*i)++;
182 return(rofftok_predef(buf, i));
183 case ('\\'):
184 return(ROFFTok_Slash);
185 default:
186 break;
187 }
188 }
189
190 return(-1);
191 }
192
193