forked from Ntsekees/ilmentufa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfanzatufa-morfo.js.peg
213 lines (182 loc) · 10.4 KB
/
fanzatufa-morfo.js.peg
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
{
var stressed_vowels = {
a: "à", e: "è", i: "ì", o: "ò", u: "ù",
ai: "ài", ei: "èi", oi: "òi", au: "àu",
}
var empty_word = {word: "", syllables: []};
function truthy(val) {
return val && (!Array.isArray(val) || val.length);
}
function join(array) {
if (!Array.isArray(array)) return array;
return array.filter(truthy).map(join).join("");
}
function concat(left, right) {
return left.concat(right);
}
function join_word(array, delim) {
if (!delim) delim = "";
return array.filter(truthy)
.reduce(concat, [])
.filter(truthy)
.map(el => el.word || el)
.join(delim);
}
function join_syllables(array) {
return array.filter(truthy)
.reduce(concat, [])
.filter(truthy)
.map(el => el.syllables || el)
.reduce(concat, []);
}
function join_obj(obj, array, delim) {
obj.word = join_word(array, delim);
obj.syllables = join_syllables(array);
return obj;
}
var _g_zoi_delim;
function _zoi_assign_delim(word) {
_g_zoi_delim = word.replace(/\./gm, "");
return word.replace(/\.?$/, ".");
}
function _is_zoi_delim(word) {
return word.replace(/\./gm, "") === _g_zoi_delim;
}
var _g_glide_dot;
function _set_glide_dot() {
_g_glide_dot = true;
}
function _pop_glide_dot() {
var result = _g_glide_dot ? "." : "";
_g_glide_dot = false;
return result;
}
}
// LOhAI SAhAI LEhAI
text = space? (si_clause / &SI cmavo)* w:clause* (&FAhO cmavo .*)? { return w }
FAhO = f a h o word_break
clause = !(&clause_non_start cmavo) w:word (!zei_tail !bu_tail si_clause)* t:(zei_tail / bu_tail)* si_clause* {
return truthy(t) ? join_obj({selmaho: t[t.length-1].selmaho}, [[w], t], " ") : w
}
clause_non_start = SI / ZEI / BU / FAhO
zei_tail = &ZEI z:cmavo w:single_word { return join_obj({selmaho: "GOhA"}, [[z], [w]], " ") }
bu_tail = &BU b:cmavo { return {selmaho: "BY", word: b.word, syllables: b.syllables} }
ZEI = z e i word_break
BU = b u word_break
si_clause = y_cmavo !bu_tail / clause &SI cmavo
SI 'cmavrsi' = s i word_break
word = cmevo / quote / !quote_start w:(cmavo / brivo) { return w }
single_word = cmevo / cmavo / brivo
quote = zo_clause / zohoi_clause / gohoi_clause / mehoi_clause / lohu_clause / zoi_clause / muhoi_clause
quote_start = GOhOI / LOhU / ZOI / MUhOI
zo_clause = &(z o word_break) z:cmavo w:single_word { return join_obj({selmaho: "KOhA"}, [[z], [w]], " ") }
zohoi_clause = &ZOhOI z:cmavo w:zoi_word_1 zoi_space* { return join_obj({selmaho: "KOhA"}, [[z], [w]], " ") }
ZOhOI = l a h o i word_break / r a h o i word_break / z o h o i word_break
gohoi_clause = &GOhOI z:cmavo w:single_word { return join_obj({selmaho: "GOhA"}, [[z], [w]], " ") }
GOhOI 'cmavrgohoi' = b o h e i word_break / g o h o i word_break / s a u h e word_break / t a h a i word_break / z e h o i word_break
mehoi_clause = &(m e h o i word_break) z:cmavo w:zoi_word_1 zoi_space* { return {selmaho: "GOhA", word: z.word + " " + w} }
lohu_clause = &LOhU lohu:cmavo w:(!LEhU w:single_word { return w })* &LEhU lehu:cmavo { return join_obj({selmaho: "KOhA"}, [[lohu], w, [lehu]], " ") }
LOhU 'cmavrlohu' = l o h u word_break
LEhU 'cmavrlehu' = l e h u word_break
zoi_clause = &ZOI zoi:cmavo tail:zoi_tail { return {selmaho: "KOhA", word: zoi.word + " " + tail} }
muhoi_clause = &MUhOI zoi:cmavo tail:zoi_tail { return {selmaho: "GOhA", word: zoi.word + " " + tail} }
ZOI 'cmavrzoi' = l a h o word_break / z o i word_break
MUhOI 'cmavrmuhoi' = m u h o i word_break
zoi_tail = open:zoi_open w:zoi_word* close:zoi_close { return open + " " + w.join("") + " " + close }
zoi_open = w:single_word { return _zoi_assign_delim(w.word) }
zoi_close = w:single_word &{ return _is_zoi_delim(w.word) } { return w.word.replace(/^\.?/, ".") }
zoi_word = !zoi_close w:zoi_word_1 s:zoi_space* { return w + s.join("") }
zoi_word_1 = w:(!zoi_space c:. { return c })+ { return w.join("") }
zoi_space = zoi_space_1 &zoi_close { return "" } / zoi_space_1
zoi_space_1 = [. \t\r\n]
zoi_candidate = &(w:single_word { _zoi_assign_delim(w, true) }) / &{ _g_zoi_delim_candidate = null }
cmevo 'cmevla' = !h head:(h / consonant !glide !pause !h / glide / nucleus)* tail:consonant pause dot:(!(nucleus / cmevo) {return "."})? { return join_obj({selmaho: "cmevla"}, [["." + join(head) + tail + (dot || "")]]) }
cmavo 'cmavo' = y_cmavo / !CVCy_lujvo start_dot:(!onset {return "."})? end_dot:(&y_dot {return "."})? head:cmavo_head tail:cmavo_tail* word_break { return join_obj({selmaho: "cmavo"}, [[start_dot], [head], tail, [end_dot]]) }
cmavo_head = &(!h !cluster onset? cmavo_nucleus syllable_break) s:syllable &cmavo_continuation { return s }
cmavo_tail = &(h cmavo_nucleus syllable_break) s:syllable &cmavo_continuation { return s }
cmavo_continuation = cmavo_tail / word_break
cmavo_nucleus = !stressed_vowel nucleus / nucleus !cluster
CVCy_lujvo = onset? nucleus medial_syllable (brivo / medial_syllable* brivo_tail_syllable)
y_cmavo = y+ word_break { return {selmaho: "cmavo", word: ".y", syllables: [".y"]} }
y_dot = onset? (nucleus h)* y word_break (cmevo / !y_dot y_then_vowel)
y_then_vowel = onset vowel / onset y coda? y_then_vowel
brivo 'brivla' = !h dot:(&(slinkuhi / !onset) {return "."})? head:brivo_head? tail:brivo_tail word_break { return join_obj({selmaho: "brivla"}, [[dot], [head], [tail]]) }
brivo_head = a:brivo_head_syllable b:unstressed_syllable* { return {word: a + join(b), syllables: [a].concat(b)} }
brivo_tail = a:stressed_syllable b:medial_syllable* c:brivo_tail_syllable { return {word: a + join(b) + c, syllables: [a.toUpperCase()].concat(b).concat([c])} }
brivo_head_syllable = &(onset? unstressed_vowel) s:syllable { return s }
unstressed_syllable = &(onset? !stressed_vowel) s:syllable { return s }
medial_syllable = &(onset !vowel) s:syllable { return s }
brivo_tail_syllable = &(onset nucleus syllable_break) s:syllable { return s }
slinkuhi = &(cluster vowel) syllable h y syllable / !rafsi_string consonant rafsi_string
rafsi_string = rafsi_prefix / rafsi_whole
rafsi_prefix = raf:y_rafsi { return [join(raf)] } / raf:y_less_rafsi rest:rafsi_prefix { return [join(raf)].concat(rest) }
rafsi_whole = raf:(final_rafsi / gismu) word_break { return raf } / raf:unstressed_y_less_rafsi rest:rafsi_whole { return [join(raf)].concat(rest) }
vowel_final_rafsi_tail = consonant pure_vowel !coda / diphthong hyphen? !coda / pure_vowel h pure_vowel hyphen? !coda
hyphen = r &consonant / n &r
y_less_rafsi = &syllable consonant (vowel_final_rafsi_tail / pure_vowel consonant)
y_rafsi = &syllable consonant (gismu_form_tail (pure_vowel h)? y / vowel_final_rafsi_tail h y / pure_vowel consonant y) &syllable
unstressed_y_less_rafsi = !rafsi_stress y_less_rafsi
final_rafsi = a:(consonant &stressed_vowel pure_vowel h pure_vowel &word_break) { return [join(a)] } / &rafsi_stress a:y_less_rafsi !rafsi_stress b:y_less_rafsi &word_break { return [join(a), join(b)] }
rafsi_stress = initial (stressed_vowel / vowel h stressed_vowel)
gismu = a:(&syllable (consonant consonant &stressed_vowel pure_vowel / consonant &stressed_vowel pure_vowel consonant) consonant pure_vowel) { return join(a) }
gismu_form_tail = (consonant pure_vowel / pure_vowel consonant) consonant
final_CVV_CCV = consonant &unstressed_vowel diphthong / &initial consonant consonant &unstressed_vowel pure_vowel
syllable = expr:(onset_consonantal nucleus_consonantal coda_allow_glide / onset? nucleus coda?) { return join(expr) }
onset = h / glide / initial
nucleus = vowel / y
coda = !syllable c:consonant !glide &syllable_break { return c }
coda_allow_glide = !syllable c:consonant &syllable_break { return c }
stressed_syllable = expr:(onset? stressed_vowel coda?) { return join(expr) }
onset_consonantal = expr:(affricate / liquid / other (liquid &coda / &coda) / sibilant (other liquid &coda / liquid &coda / other &coda)?) { return join(expr) }
nucleus_consonantal = &syllabic &coda { return "y" }
syllable_break = &onset &syllable / &space / !.
word_break = syllable_break !cmevo (space / &(!slinkuhi single_word) / !.) { return null }
space = (!letter .)+ { return null }
pause = space / !.
letter = [A-Za-z'àèìòùỳáéíóúýÀÈÌÒÙỲÁÉÍÓÚÝĭŭĬŬ]
cluster = consonant consonant+
initial = expr:(affricate !h / !nucleus sibilant? other? liquid? !h) { return join(expr) }
affricate = expr:(t c / t s / d j / d z) { return join(expr) }
sibilant = expr:(c / s !x / (j / z) !(!coda (n / liquid))) { return join(expr) }
liquid = l / r
other = expr:(p / t !(!coda l) / k / f / x / b / d !(!coda l) / g / v / m / n !(!coda liquid)) { return join(expr) }
consonant = c:(voiced / unvoiced / syllabic) !h { return c }
syllabic = l / m / n / r
voiced = b / d / g / j / v / z
unvoiced = c / f / k / p / s / t / x
b = comma [Bb] !b !unvoiced { return "b" }
c = comma [Cc] !c !voiced !sibilant { return "c" }
d = comma [Dd] !d !unvoiced { return "d" }
f = comma [Ff] !f !voiced { return "f" }
g = comma [Gg] !g !unvoiced { return "g" }
h = comma [Hh'] !h &nucleus { return "'" }
j = comma [Jj] !j !unvoiced !sibilant { return "j" }
k = comma [Kk] !k !voiced { return "k" }
l = comma [Ll] !l { return "l" }
m = comma [Mm] !m { return "m" }
n = comma [Nn] !n !affricate { return "n" }
p = comma [Pp] !p !voiced { return "p" }
r = comma [Rr] !r { return "r" }
s = comma [Ss] !s !voiced !sibilant { return "s" }
t = comma [Tt] !t !voiced { return "t" }
v = comma [Vv] !v !unvoiced { return "v" }
x = comma [Xx] !x !voiced { return "x" }
z = comma [Zz] !z !unvoiced !sibilant { return "z" }
glide = g:(iy / uy) { return _pop_glide_dot() + g }
iy = comma [iĭIĬ] &nucleus !glide { return "ĭ" }
uy = comma [uŭUŬ] &nucleus !glide { return "ŭ" }
nucleus = vowel / y
vowel = diphthong / pure_vowel
diphthong = d:(a i / e i / o i) diphthong_tail<iy> { return join(d) } / a u diphthong_tail<uy> { return "au" }
diphthong_tail<g> = !nucleus !g &(word_break g { _set_glide_dot() })?
pure_vowel = v:(a / e / i / o / u) !nucleus { return v }
stressed_vowel = ![aeiou] v:vowel { return v } / v:vowel &stress { return v }
unstressed_vowel = &[aeiou] v:vowel !stress { return v }
stress = coda? medial_syllable* brivo_tail_syllable pause
a = comma [AaàáÀÁ] { return "a" }
e = comma [EeèéÈÉ] { return "e" }
i = comma [IiìíÌÍ] { return "i" }
o = comma [OoòóÒÓ] { return "o" }
u = comma [UuùúÙÚ] { return "u" }
y = comma [YyỳýỲÝ] !(!y nucleus) { return "y" }
comma = ","*