summaryrefslogtreecommitdiffstats
path: root/src/preproc/refer/token.cpp
blob: e643cbd4ad453113a0f36fedc6389aff8e0cdb1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// -*- C++ -*-
/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
     Written by James Clark (jjc@jclark.com)

This file is part of groff.

groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or
(at your option) any later version.

groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include "refer.h"
#include "token.h"

#define TOKEN_TABLE_SIZE 1009
// I believe in Icelandic thorn sorts after z.
#define THORN_SORT_KEY "{"

struct token_table_entry {
  const char *tok;
  token_info ti;
  token_table_entry();
};

token_table_entry token_table[TOKEN_TABLE_SIZE];
int ntokens = 0;

static void skip_name(const char **ptr, const char *end)
{
  if (*ptr < end) {
    switch (*(*ptr)++) {
    case '(':
      if (*ptr < end) {
	*ptr += 1;
	if (*ptr < end)
	  *ptr += 1;
      }
      break;
    case '[':
      while (*ptr < end)
	if (*(*ptr)++ == ']')
	  break;
      break;
    }
  }
}

int get_token(const char **ptr, const char *end)
{
  if (*ptr >= end)
    return 0;
  char c = *(*ptr)++;
  if (c == '\\' && *ptr < end) {
    switch (**ptr) {
    default:
      *ptr += 1;
      break;
    case '(':
    case '[':
      skip_name(ptr, end);
      break;
    case '*':
    case 'f':
      *ptr += 1;
      skip_name(ptr, end);
      break;
    }
  }
  return 1;
}

token_info::token_info()
: type(TOKEN_OTHER), sort_key(0), other_case(0)
{
}

void token_info::set(token_type t, const char *sk, const char *oc)
{
  assert(oc == 0 || t == TOKEN_UPPER || t == TOKEN_LOWER);
  type = t;
  sort_key = sk;
  other_case = oc;
}

void token_info::sortify(const char *start, const char *end, string &result)
     const
{
  if (sort_key)
    result += sort_key;
  else if (type == TOKEN_UPPER || type == TOKEN_LOWER) {
    for (; start < end; start++)
      if (csalpha(*start))
	result += cmlower(*start);
  }
}

int token_info::sortify_non_empty(const char *start, const char *end) const
{
  if (sort_key)
    return *sort_key != '\0';
  if (type != TOKEN_UPPER && type != TOKEN_LOWER)
    return 0;
  for (; start < end; start++)
    if (csalpha(*start))
      return 1;
  return 0;
}


void token_info::lower_case(const char *start, const char *end,
			    string &result) const
{
  if (type != TOKEN_UPPER) {
    while (start < end)
      result += *start++;
  }
  else if (other_case)
    result += other_case;
  else {
    while (start < end)
      result += cmlower(*start++);
  }
}

void token_info::upper_case(const char *start, const char *end,
			    string &result) const
{
  if (type != TOKEN_LOWER) {
    while (start < end)
      result += *start++;
  }
  else if (other_case)
    result += other_case;
  else {
    while (start < end)
      result += cmupper(*start++);
  }
}

token_table_entry::token_table_entry()
: tok(0)
{
}

static void store_token(const char *tok, token_type typ,
			const char *sk = 0, const char *oc = 0)
{
  unsigned n = hash_string(tok, strlen(tok)) % TOKEN_TABLE_SIZE;
  for (;;) {
    if (token_table[n].tok == 0) {
      if (++ntokens == TOKEN_TABLE_SIZE)
	assert(0);
      token_table[n].tok = tok;
      break;
    }
    if (strcmp(tok, token_table[n].tok) == 0)
      break;
    if (n == 0)
      n = TOKEN_TABLE_SIZE - 1;
    else
      --n;
  }
  token_table[n].ti.set(typ, sk, oc);
}


token_info default_token_info;

const token_info *lookup_token(const char *start, const char *end)
{
  unsigned n = hash_string(start, end - start) % TOKEN_TABLE_SIZE;
  for (;;) {
    if (token_table[n].tok == 0)
      break;
    if (strlen(token_table[n].tok) == size_t(end - start)
	&& memcmp(token_table[n].tok, start, end - start) == 0)
      return &(token_table[n].ti);
    if (n == 0)
      n = TOKEN_TABLE_SIZE - 1;
    else
      --n;
  }
  return &default_token_info;
}

static void init_ascii()
{
  const char *p;
  for (p = "abcdefghijklmnopqrstuvwxyz"; *p; p++) {
    char buf[2];
    buf[0] = *p;
    buf[1] = '\0';
    store_token(strsave(buf), TOKEN_LOWER);
    buf[0] = cmupper(buf[0]);
    store_token(strsave(buf), TOKEN_UPPER);
  }
  for (p = "0123456789"; *p; p++) {
    char buf[2];
    buf[0] = *p;
    buf[1] = '\0';
    const char *s = strsave(buf);
    store_token(s, TOKEN_OTHER, s);
  }
  for (p = ".,:;?!"; *p; p++) {
    char buf[2];
    buf[0] = *p;
    buf[1] = '\0';
    store_token(strsave(buf), TOKEN_PUNCT);
  }
  store_token("-", TOKEN_HYPHEN);
}

static void store_letter(const char *lower, const char *upper,
		  const char *sort_key = 0)
{
  store_token(lower, TOKEN_LOWER, sort_key, upper);
  store_token(upper, TOKEN_UPPER, sort_key, lower);
}

static void init_letter(unsigned char uc_code, unsigned char lc_code,
		 const char *sort_key)
{
  char lbuf[2];
  lbuf[0] = lc_code;
  lbuf[1] = 0;
  char ubuf[2];
  ubuf[0] = uc_code;
  ubuf[1] = 0;
  store_letter(strsave(lbuf), strsave(ubuf), sort_key);
}

static void init_latin1()
{
  init_letter(0xc0, 0xe0, "a");
  init_letter(0xc1, 0xe1, "a");
  init_letter(0xc2, 0xe2, "a");
  init_letter(0xc3, 0xe3, "a");
  init_letter(0xc4, 0xe4, "a");
  init_letter(0xc5, 0xe5, "a");
  init_letter(0xc6, 0xe6, "ae");
  init_letter(0xc7, 0xe7, "c");
  init_letter(0xc8, 0xe8, "e");
  init_letter(0xc9, 0xe9, "e");
  init_letter(0xca, 0xea, "e");
  init_letter(0xcb, 0xeb, "e");
  init_letter(0xcc, 0xec, "i");
  init_letter(0xcd, 0xed, "i");
  init_letter(0xce, 0xee, "i");
  init_letter(0xcf, 0xef, "i");

  init_letter(0xd0, 0xf0, "d");
  init_letter(0xd1, 0xf1, "n");
  init_letter(0xd2, 0xf2, "o");
  init_letter(0xd3, 0xf3, "o");
  init_letter(0xd4, 0xf4, "o");
  init_letter(0xd5, 0xf5, "o");
  init_letter(0xd6, 0xf6, "o");
  init_letter(0xd8, 0xf8, "o");
  init_letter(0xd9, 0xf9, "u");
  init_letter(0xda, 0xfa, "u");
  init_letter(0xdb, 0xfb, "u");
  init_letter(0xdc, 0xfc, "u");
  init_letter(0xdd, 0xfd, "y");
  init_letter(0xde, 0xfe, THORN_SORT_KEY);

  store_token("\337", TOKEN_LOWER, "ss", "SS");
  store_token("\377", TOKEN_LOWER, "y", "Y");
}

static void init_two_char_letter(char l1, char l2, char u1, char u2,
				 const char *sk = 0)
{
  char buf[6];
  buf[0] = '\\';
  buf[1] = '(';
  buf[2] = l1;
  buf[3] = l2;
  buf[4] = '\0';
  const char *p = strsave(buf);
  buf[2] = u1;
  buf[3] = u2;
  store_letter(p, strsave(buf), sk);
  buf[1] = '[';
  buf[4] = ']';
  buf[5] = '\0';
  p = strsave(buf);
  buf[2] = l1;
  buf[3] = l2;
  store_letter(strsave(buf), p, sk);
  
}

static void init_special_chars()
{
  const char *p;
  for (p = "':^`~"; *p; p++)
    for (const char *q = "aeiouy"; *q; q++) {
      // Use a variable to work around bug in gcc 2.0
      char c = cmupper(*q);
      init_two_char_letter(*p, *q, *p, c);
    }
  for (p = "/l/o~n,coeaeij"; *p; p += 2) {
    // Use variables to work around bug in gcc 2.0
    char c0 = cmupper(p[0]);
    char c1 = cmupper(p[1]);
    init_two_char_letter(p[0], p[1], c0, c1);
  }
  init_two_char_letter('v', 's', 'v', 'S', "s");
  init_two_char_letter('v', 'z', 'v', 'Z', "z");
  init_two_char_letter('o', 'a', 'o', 'A', "a");
  init_two_char_letter('T', 'p', 'T', 'P', THORN_SORT_KEY);
  init_two_char_letter('-', 'd', '-', 'D');
  
  store_token("\\(ss", TOKEN_LOWER, 0, "SS");
  store_token("\\[ss]", TOKEN_LOWER, 0, "SS");

  store_token("\\(Sd", TOKEN_LOWER, "d", "\\(-D");
  store_token("\\[Sd]", TOKEN_LOWER, "d", "\\[-D]");
  store_token("\\(hy", TOKEN_HYPHEN);
  store_token("\\[hy]", TOKEN_HYPHEN);
  store_token("\\(en", TOKEN_RANGE_SEP);
  store_token("\\[en]", TOKEN_RANGE_SEP);
}

static void init_strings()
{
  char buf[6];
  buf[0] = '\\';
  buf[1] = '*';
  for (const char *p = "'`^^,:~v_o./;"; *p; p++) {
    buf[2] = *p;
    buf[3] = '\0';
    store_token(strsave(buf), TOKEN_ACCENT);
    buf[2] = '[';
    buf[3] = *p;
    buf[4] = ']';
    buf[5] = '\0';
    store_token(strsave(buf), TOKEN_ACCENT);
  }

  // -ms special letters
  store_letter("\\*(th", "\\*(Th", THORN_SORT_KEY);
  store_letter("\\*[th]", "\\*[Th]", THORN_SORT_KEY);
  store_letter("\\*(d-", "\\*(D-");
  store_letter("\\*[d-]", "\\*[D-]");
  store_letter("\\*(ae", "\\*(Ae", "ae");
  store_letter("\\*[ae]", "\\*[Ae]", "ae");
  store_letter("\\*(oe", "\\*(Oe", "oe");
  store_letter("\\*[oe]", "\\*[Oe]", "oe");

  store_token("\\*3", TOKEN_LOWER, "y", "Y");
  store_token("\\*8", TOKEN_LOWER, "ss", "SS");
  store_token("\\*q", TOKEN_LOWER, "o", "O");
}

struct token_initer {
  token_initer();
};

static token_initer the_token_initer;

token_initer::token_initer()
{
  init_ascii();
  init_latin1();
  init_special_chars();
  init_strings();
  default_token_info.set(TOKEN_OTHER);
}