-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathewts-parser.l
452 lines (414 loc) · 12.3 KB
/
ewts-parser.l
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
%{
/*
Tewts parser for the luaewts library.
Copyright (C) 2010 Elie Roux <[email protected]>
This file is under the Creative Commons CC0 license.
See the full text at
http://creativecommons.org/publicdomain/zero/1.0/legalcode
and a FAQ at
http://wiki.creativecommons.org/CC0
*/
#include <stdio.h>
#include <stdlib.h>
#include "ewts-parser.h"
// we redeclare yylex to pass an argument to it
#define YY_DECL int ewts_lex (ewts_state *curstate)
// the state is a global variable... looking for passing arguments to yylex...
//ewts_state *curstate;
inline void ewts_reinitialize(ewts_state *s)
{
s->last_is_plus = 0;
s->current_state = S_INITIAL;
s-> last_stack_num = 0;
s->nb_vow = 0;
}
#define _reinitialize() \
ewts_reinitialize(curstate);
// this function prints a char in utfbuf in utf8 and sets bufindex accordingly
void
ewts_print_unichar (pwchar to_print, ewts_state *curstate)
{
char *buf = curstate->utfbuf + curstate->utfbufindex;
if (to_print < 3)
{
if (!to_print)
{
return;
}
if (to_print == 1)
{
ewts_print_unichar (0xF55, curstate);
ewts_print_unichar (0xF39, curstate);
return;
}
if (to_print == 2)
{
ewts_print_unichar (0xF56, curstate);
ewts_print_unichar (0xF39, curstate);
return;
}
}
if (to_print <= 0x7F)
{
sprintf (buf, "%c", (unsigned char) to_print);
curstate->utfbufindex = curstate->utfbufindex + 1;
return;
}
if (to_print >= 0x80 && to_print <= 0x7FF)
{
sprintf (buf, "%c%c", 0xC0 | (to_print >> 6), 0x80 | (to_print & 0x3F));
curstate->utfbufindex = curstate->utfbufindex + 2;
return;
}
if ((to_print >= 0x800 && to_print <= 0xD7FF) ||
(to_print >= 0xE000 && to_print <= 0xFFFF))
{
sprintf (buf, "%c%c%c", 0xE0 | (to_print >> 12),
0x80 | ((to_print >> 6) & 0x3F), 0x80 | (to_print & 0x3F));
curstate->utfbufindex = curstate->utfbufindex + 3;
return;
}
if (to_print >= 0x10000 && to_print <= 0x10FFFF)
{
sprintf (buf, "%c%c%c%c", 0xF0 | (to_print >> 18),
0x80 | ((to_print >> 12) & 0x3F),
0x80 | ((to_print >> 6) & 0x3F), 0x80 | (to_print & 0x3F));
curstate->utfbufindex = curstate->utfbufindex + 4;
}
}
#define print_unichar(char) ewts_print_unichar(char, curstate);
// a function that returns the result of the combination of consone and constwo
// result is NON_STANDARD_STACK, NON_COMBINABLE_STACK or the standard combinable stack number
// consone can be also a stack
unsigned char
ewts_combine(unsigned char consone, unsigned char constwo)
{
switch(consone)
{
case c_r:
return right_combine_r[constwo];
break;
case c_l:
return right_combine_l[constwo];
break;
case c_s:
return right_combine_s[constwo];
break;
default:
break;
}
switch(constwo)
{
case c_y:
return left_combine_y[consone];
break;
case c_r:
return left_combine_r[consone];
break;
case c_l:
return left_combine_l[consone];
break;
case c_w:
return left_combine_w[consone];
break;
default:
return NON_STANDARD_STACK;
break;
}
}
void
ewts_add_cons(unsigned char cons, ewts_state *curstate)
{
unsigned char newstack = 0;
if (curstate->nb_vow != 0)
{
_reinitialize();
}
switch(curstate->current_state)
{
case S_INITIAL:
if (curstate->last_is_plus == 1) // case of \u0F88+k, not sure about the side effects though...
{
print_unichar(cons_subjoined[cons]);
curstate->current_state = S_WAIT_CONS;
curstate->last_stack_num = NON_STANDARD_STACK;
}
else
{
print_unichar(cons_initial[cons]);
curstate->current_state = S_WAIT_CONS;
curstate->last_stack_num = cons;
}
break;
case S_WAIT_CONS:
newstack = ewts_combine(curstate->last_stack_num, cons);
if (suffix[curstate->last_stack_num] == 1 && curstate->syllable_state == S_END && curstate->last_is_plus == 0)
{
_reinitialize();
print_unichar(cons_initial[cons]);
curstate->current_state = S_WAIT_CONS;
curstate->last_stack_num = cons;
}
else
{
if (newstack == NON_STANDARD_STACK && curstate->last_is_plus == 0)
{
if ((prefix[curstate->last_stack_num] == 1 && curstate->syllable_state == S_INITIAL) || curstate->syllable_state == S_END)
{
_reinitialize();
print_unichar(cons_initial[cons]);
curstate->current_state = S_WAIT_CONS;
curstate->last_stack_num = cons;
}
else
{
print_unichar(cons_subjoined[cons]);
curstate->current_state = S_END;
curstate->last_stack_num = newstack;
curstate->syllable_state = S_END;
}
}
else
{
print_unichar(cons_subjoined[cons]);
curstate->last_stack_num = newstack;
curstate->syllable_state = S_END;
}
}
break;
default:
//case S_END:
if (curstate->last_is_plus == 1)
{
print_unichar(cons_subjoined[cons]);
curstate->last_stack_num = NON_STANDARD_STACK;
}
else
{
_reinitialize();
print_unichar(cons_initial[cons]);
curstate->current_state = curstate->current_state + 1;
curstate->last_stack_num = cons;
}
break;
}
curstate->last_is_plus = 0;
}
void
ewts_add_vowel(unsigned char vow, ewts_state *curstate)
{
if (curstate->last_is_plus == 1 || (curstate->nb_vow == 0 && curstate->current_state != S_INITIAL))
{
print_unichar(vowelchars[vow]);
curstate->nb_vow = curstate->nb_vow + 1;
}
else
{
_reinitialize();
print_unichar(cons_initial[c_none]);
print_unichar(vowelchars[vow]);
if (vow != v_a) {
curstate->nb_vow = 1;
}
}
curstate->last_is_plus = 0;
curstate->syllable_state = S_END;
}
void
ewts_add_ambiguous(unsigned char one_vow, unsigned char two_cons, unsigned char two_vow, ewts_state *curstate)
{
if ((curstate->syllable_state == S_INITIAL && curstate->current_state == S_INITIAL) || (curstate->last_is_plus && curstate->nb_vow == 0))
{
ewts_add_cons(two_cons, curstate);
ewts_add_vowel(two_vow, curstate);
}
else
{
ewts_add_vowel(one_vow, curstate);
}
}
void
ewts_add_other(pwchar num, ewts_state *curstate)
{
_reinitialize();
curstate->syllable_state = S_INITIAL;
print_unichar(num);
}
void
ewts_add_verb(char *str, ewts_state *curstate)
{
_reinitialize();
sprintf (curstate->utfbuf+curstate->utfbufindex, "%s", str);
curstate->utfbufindex = curstate->utfbufindex + (int) strlen(str);
}
#define add_cons(num) ewts_add_cons(num, curstate);
#define add_vowel(num) ewts_add_vowel(num, curstate);
#define add_ambiguous(num, numtwo, numthree) ewts_add_ambiguous(num, numtwo, numthree, curstate);
#define add_verb(str) ewts_add_verb(str, curstate);
#define add_num(num) ewts_add_other(digchars[num], curstate);
#define plus_sign() curstate->last_is_plus = 1;
#define dot_sign() ewts_reinitialize(curstate);
#define add_uni(str) ewts_add_other(strtol(str, NULL, 16), curstate);
#define add_uni_dec(str) ewts_add_other(strtol(str, NULL, 10), curstate);
#define add_other(num) ewts_add_other(num, curstate)
%}
%option stack
%option pointer
%option nounput
%option noyy_push_state
%option noyy_pop_state
%option noyy_top_state
%option full
%option noread
%option nomain
%option align
%option noyylineno
%option prefix="ewts_"
%option outfile="ewts-parser.c"
%option noyywrap
%x verb
%%
(\n|\r|\t) {
add_other(ewts_text[0]);
}
\[ { BEGIN(verb); }
<verb>[^\]]+ { add_verb(strdup(ewts_text)); }
<verb>\] { BEGIN(INITIAL); }
\\u[0-9a-fA-F]{1,4} { add_uni(ewts_text+2); }
\\U[0-9]{1,8} { add_uni_dec(ewts_text+2); }
\\[^uU] { add_other(ewts_text[1]); }
\+a { plus_sign(); add_cons(c_none); }
brk { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_k); }
brg { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_g); }
brng { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_ng); }
brj { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_j); }
brl { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_l); }
brny { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_ny); }
brt { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_t); }
brd { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_d); }
brn { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_n); }
brts { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_ts); }
brdz { add_cons(c_b);dot_sign();add_cons(c_r);add_cons(c_dz); }
blt { add_cons(c_b);dot_sign();add_cons(c_l);add_cons(c_t); }
bld { add_cons(c_b);dot_sign();add_cons(c_l);add_cons(c_d); }
k { add_cons(c_k); }
c { add_cons(c_c); }
kh { add_cons(c_kh); }
g { add_cons(c_g); }
ng { add_cons(c_ng); }
ch { add_cons(c_ch); }
j { add_cons(c_j); }
ny { add_cons(c_ny); }
t { add_cons(c_t); }
th { add_cons(c_th); }
d { add_cons(c_d); }
n { add_cons(c_n); }
p { add_cons(c_p); }
ph { add_cons(c_ph); }
b { add_cons(c_b); }
m { add_cons(c_m); }
ts { add_cons(c_ts); }
tsh { add_cons(c_tsh); }
dz { add_cons(c_dz); }
w { add_cons(c_w); }
zh { add_cons(c_zh); }
z { add_cons(c_z); }
' { add_cons(c_apo); }
y { add_cons(c_y); }
r { add_cons(c_r); }
l { add_cons(c_l); }
sh { add_cons(c_sh); }
s { add_cons(c_s); }
h { add_cons(c_h); }
R { add_cons(c_R); }
D { add_cons(c_D); }
(dh|d\+h) { add_cons(c_dh); }
(Dh|D\+h) { add_cons(c_Dh); }
T { add_cons(c_T); }
Th { add_cons(c_Th); }
N { add_cons(c_N); }
Sh { add_cons(c_Sh); }
(gh|g\+h) { add_cons(c_gh); }
(bh|b\+h) { add_cons(c_bh); }
(dzh|dz\+h) { add_cons(c_dzh); }
(kSh|k+Sh) { add_cons(c_kSh); }
f { add_cons(c_f); }
v { add_cons(c_v); }
(OM|oM) { add_cons(c_OM); }
W { add_cons(c_W); }
Y { add_cons(c_Y); }
a { add_vowel(v_a); }
i { add_vowel(v_i); }
u { add_vowel(v_u); }
e { add_vowel(v_e); }
o { add_vowel(v_o); }
r-i { add_ambiguous(v_ri, c_r, v_mini); }
l-i { add_ambiguous(v_li, c_l, v_mini); }
-i { add_vowel(v_mini); }
A { add_vowel(v_A); }
I { add_vowel(v_I); }
U { add_vowel(v_U); }
ai { add_vowel(v_ai); }
au { add_vowel(v_au); }
r-I { add_ambiguous(v_rI, c_r, v_minI); }
l-I { add_ambiguous(v_lI, c_l, v_minI); }
-I { add_vowel(v_minI); }
[0-9] { add_num(ewts_text[0]-48); }
\/ { add_other(0xf0d); }
\/\/ { add_other(0xf0e); }
; { add_other(0xf0f); }
\| { add_other(0xf11); }
! { add_other(0xf08); }
: { add_other(0xf14); }
_ { add_other(' '); }
\ { add_other(0xf0b); }
\* { add_other(0xf0c); }
@ { add_other(0xf04); }
# { add_other(0xf05); }
\$ { add_other(0xf06); }
\% { add_other(0xf07); }
\< { add_other(0xf3a); }
> { add_other(0xf3b); }
\( { add_other(0xf3c); }
\) { add_other(0xf3d); }
= { add_other(0xf34); }
\^ { add_other(0xf39); }
~X { add_other(0xf35); }
X { add_other(0xf37); }
M { add_other(0xf7e); }
H { add_other(0xf7f); }
~M` { add_other(0xf82); }
~M { add_other(0xf83); }
\? { add_other(0xf84); }
& { add_other(0xf85); }
\+ { plus_sign(); }
\. { dot_sign(); }
. { /* Ignore all other characters. */ }
%%
ewts_state *new_state()
{
ewts_state *s;
s = malloc (sizeof (ewts_state));
s->utfbuf = NULL;
s->utfbufindex = 0;
s->last_is_plus = 0;
s->current_state = S_INITIAL;
s->syllable_state = S_INITIAL;
s-> last_stack_num = 0;
s->nb_vow = 0;
return s;
}
char* ewts_scanner(const char *argstr)
{
YY_BUFFER_STATE ybuf;
size_t len = 0;
ewts_state *curstate = new_state();
len = strlen(argstr);
curstate->utfbuf = (char *) malloc((len*4+1)*sizeof(char));
ybuf=yy_scan_string(argstr);
ewts_lex(curstate);
yy_flush_buffer(ybuf);
yy_delete_buffer(ybuf);
curstate->utfbuf[curstate->utfbufindex] = 0;
return curstate->utfbuf;
}