]> git.cameronkatri.com Git - mandoc.git/blob - tokens.c
*** empty log message ***
[mandoc.git] / tokens.c
1 /* $Id: tokens.c,v 1.3 2008/12/04 19:31:57 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 "libmdocml.h"
24 #include "private.h"
25
26
27 static int rofftok_dashes(const char *, int *);
28 static int rofftok_special(const char *, int *);
29 static int rofftok_predef(const char *, int *);
30 static int rofftok_defined(const char *, int *);
31
32
33 static int
34 rofftok_defined(const char *buf, int *i)
35 {
36 const char *p;
37
38 if (0 == buf[*i])
39 return(-1);
40 if (0 == buf[*i + 1])
41 return(-1);
42
43 (*i)++;
44 p = &buf[(*i)++];
45
46 if (0 == memcmp(p, ">=", 2))
47 return(ROFFTok_Ge);
48 else if (0 == memcmp(p, "<=", 2))
49 return(ROFFTok_Le);
50 else if (0 == memcmp(p, "Rq", 2))
51 return(ROFFTok_Rquote);
52 else if (0 == memcmp(p, "Lq", 2))
53 return(ROFFTok_Lquote);
54 else if (0 == memcmp(p, "ua", 2))
55 return(ROFFTok_Uparrow);
56 else if (0 == memcmp(p, "aa", 2))
57 return(ROFFTok_Acute);
58 else if (0 == memcmp(p, "ga", 2))
59 return(ROFFTok_Grave);
60 else if (0 == memcmp(p, "Pi", 2))
61 return(ROFFTok_Pi);
62 else if (0 == memcmp(p, "Ne", 2))
63 return(ROFFTok_Ne);
64 else if (0 == memcmp(p, "Le", 2))
65 return(ROFFTok_Le);
66 else if (0 == memcmp(p, "Ge", 2))
67 return(ROFFTok_Ge);
68 else if (0 == memcmp(p, "Lt", 2))
69 return(ROFFTok_Lt);
70 else if (0 == memcmp(p, "Gt", 2))
71 return(ROFFTok_Gt);
72 else if (0 == memcmp(p, "Pm", 2))
73 return(ROFFTok_Plusmin);
74 else if (0 == memcmp(p, "If", 2))
75 return(ROFFTok_Infty);
76 else if (0 == memcmp(p, "Na", 2))
77 return(ROFFTok_Nan);
78 else if (0 == memcmp(p, "Ba", 2))
79 return(ROFFTok_Bar);
80
81 return(-1);
82 }
83
84
85 static int
86 rofftok_predef(const char *buf, int *i)
87 {
88 if (0 == buf[*i])
89 return(-1);
90 if ('(' == buf[*i])
91 return(rofftok_defined(buf, i));
92
93 switch (buf[*i]) {
94 case ('q'):
95 return(ROFFTok_Quote);
96 default:
97 break;
98 }
99
100 return(-1);
101 }
102
103
104 static int
105 rofftok_dashes(const char *buf, int *i)
106 {
107
108 if (0 == buf[*i])
109 return(-1);
110 else if (buf[(*i)++] != 'e')
111 return(-1);
112 if (0 == buf[*i])
113 return(-1);
114
115 switch (buf[*i]) {
116 case ('m'):
117 return(ROFFTok_Em);
118 case ('n'):
119 return(ROFFTok_En);
120 default:
121 break;
122 }
123 return(-1);
124 }
125
126
127 static int
128 rofftok_special(const char *buf, int *i)
129 {
130
131 if (0 == buf[*i])
132 return(ROFFTok_Slash);
133
134 switch (buf[*i]) {
135 case ('a'):
136 return(ROFFTok_Sp_A);
137 case ('b'):
138 return(ROFFTok_Sp_B);
139 case ('f'):
140 return(ROFFTok_Sp_F);
141 case ('n'):
142 return(ROFFTok_Sp_N);
143 case ('r'):
144 return(ROFFTok_Sp_R);
145 case ('t'):
146 return(ROFFTok_Sp_T);
147 case ('v'):
148 return(ROFFTok_Sp_V);
149 case ('0'):
150 return(ROFFTok_Sp_0);
151 default:
152 break;
153 }
154 return(-1);
155 }
156
157
158 int
159 rofftok_scan(const char *buf, int *i)
160 {
161
162 assert(*buf);
163 assert(buf[*i] == '\\');
164
165 (*i)++;
166
167 for ( ; buf[*i]; (*i)++) {
168 switch (buf[*i]) {
169 case ('e'):
170 (*i)++;
171 return(rofftok_special(buf, i));
172 case ('('):
173 (*i)++;
174 return(rofftok_dashes(buf, i));
175 case (' '):
176 return(ROFFTok_Space);
177 case ('&'):
178 return(ROFFTok_Null);
179 case ('-'):
180 return(ROFFTok_Hyphen);
181 case ('*'):
182 (*i)++;
183 return(rofftok_predef(buf, i));
184 case ('\\'):
185 return(ROFFTok_Slash);
186 default:
187 break;
188 }
189 }
190
191 return(-1);
192 }
193
194