-
Notifications
You must be signed in to change notification settings - Fork 9
/
general-use.js
977 lines (940 loc) · 44.7 KB
/
general-use.js
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
"use strict";
var TengwarAnnatar = require("./tengwar-annatar");
var Notation = require("./notation");
var Parser = require("./parser");
var makeDocumentParser = require("./document-parser");
var punctuation = require("./punctuation");
var parseNumber = require("./numbers");
exports.name = "Mode for general use";
var defaults = {};
exports.makeOptions = makeOptions;
function makeOptions(options) {
options = options || defaults;
// legacy
if (options.blackSpeech) {
options.language = "blackSpeech";
}
if (options.language === "blackSpeech") {
options.language = "black-speech";
}
return {
font: options.font || TengwarAnnatar,
block: options.block,
plain: options.plain,
doubleNasalsWithTildeBelow: options.doubleNasalsWithTildeBelow,
// Any tengwa can be doubled by placing a tilde above, and any tengwa
// can be prefixed with the nasal from the same series by putting a
// tilde below. Doubled nasals have the special distinction that
// either of these rules might apply so the tilde can go either above
// or below.
// false: by default, place a tilde above doubled nasals.
// true: place the tilde below doubled nasals.
reverseCurls: options.reverseCurls || options.language === "black-speech",
// false: by default, o is forward, u is backward
// true: o is backward, u is forward
swapDotSlash: options.swapDotSlash,
// false: by default, e is a slash, i is a dot
// true: e is a dot, i is a slash
medialOre: options.medialOre || options.language === "black-speech",
// false: by default, ore only appears in final position
// true: ore also appears before consonants, as in the ring inscription
language: options.language,
// by default, no change
// "english": final e implicitly silent
// "black speech": sh is harmacalma, gh is unqueungwe, as in
// the ring inscription
// not "black-speech": sh is harma, gh is unque
noAchLaut: options.noAchLaut,
// false: "kh" is interpreted as ach-laut, as in "bach".
// true: "kh" is interpreted as merely "k", as in "khan".
sHook: options.sHook,
// false: "is" is silme with I tehta
// true: "is" is short carrier with S hook and I tehta
tsdz: options.tsdz,
// false: "ts" and "dz" are rendered as separate characters
// true: "ts" is IPA "c" and "dz" is IPA "dʒ"
duodecimal: options.duodecimal
// false: numbers are decimal by default
// true: numbers are duodecimal by default
};
}
exports.transcribe = transcribe;
function transcribe(text, options) {
options = makeOptions(options);
var font = options.font;
return font.transcribe(parse(text.toLowerCase(), options), options);
}
exports.encode = encode;
function encode(text, options) {
options = makeOptions(options);
return Notation.encode(parse(text.toLowerCase(), options), options);
}
var parse = exports.parse = makeDocumentParser(parseWord, makeOptions);
function parseWord(callback, options) {
var font = options.font;
var makeColumn = font.makeColumn;
return scanWord(function (word, rewind) {
if (options.language === "english") {
if (word === "of") {
return function (character) {
if (Parser.isBreak(character)) {
return scanWord(function (word, rewind) {
if (word === "the") {
return callback([
makeOfThe(makeColumn)
]);
} else if (word === "the`") {
return callback([
makeOf(makeColumn),
makeThePrime(makeColumn)
]);
} else if (word === "the``") {
return callback([
makeOf(makeColumn),
makeThePrime(makeColumn)
]);
} else {
return rewind(callback([
makeOf(makeColumn)
]));
}
});
} else {
return callback([makeOf(makeColumn)])(character);
}
}
} else if (word === "of`") {
return scanWord(function (word, rewind) {
if (word === "the") {
return callback([
makeOfPrime(makeColumn),
makeThe(makeColumn)
]);
} else if (word === "the`") {
return callback([
makeOfPrime(makeColumn),
makeThePrime(makeColumn)
]);
} else if (word === "the``") {
return callback([
makeOfPrime(makeColumn),
makeThePrimePrime(makeColumn)
]);
} else {
return rewind(callback([
makeOfPrime(makeColumn)
]));
}
});
} else if (word === "the") {
return callback([
makeThe(makeColumn)
]);
} else if (word === "the`") {
return callback([
makeThePrime(makeColumn)
]);
} else if (word === "the``") {
return callback([
makeThePrimePrime(makeColumn)
]);
} else if (word === "of`the") {
return callback([
makeOf(makeColumn),
])("t")("h")("e");
} else if (word === "of`the`") {
return callback([
makeOfPrime(makeColumn)
])("t")("h")("e")("`");
} else if (word === "and") {
return callback([
makeAnd(makeColumn)
]);
} else if (word === "and`") {
return callback([
makeAndPrime(makeColumn)
]);
} else if (word === "and``") {
return callback([
makeAndPrimePrime(makeColumn)
]);
} else if (word === "we") {
return callback([
makeColumn("vala", {from: "w"}),
makeColumn("short-carrier", {from: ""})
.addAbove("e", {from: "e"})
.varies()
]);
} else if (word === "we`") { // Unattested, my invention - kriskowal
return callback([
makeColumn("vala", {from: "w", diphthong: true})
.addBelow("y", {from: "ē"})
]);
}
}
if (book[word]) {
return callback(Notation.decodeWord(book[word], makeColumn), {
from: word
});
} else {
return callback(parseWordPiecewise(word, word.length, options), word);
}
}, options);
}
var book = {
"iant": "yanta;tinco:a,tilde-above",
"iaur": "yanta;vala:a;ore",
"baranduiniant": "umbar;romen:a;ando:a,tilde-above;anna:u;yanta;anto:a,tilde-above",
"ioreth": "yanta;romen:o;thule:e",
"noldo": "nwalme;lambe:o;ando;short-carrier:o",
"noldor": "nwalme;lambe:o;ando;ore:o"
};
// TODO Fix bug where "of", "the", and "and" decompose with following
// punctuation.
function scanWord(callback, options, word, rewind) {
word = word || "";
rewind = rewind || function (state) {
return state;
};
return function (character) {
if (Parser.isBreak(character)) {
return callback(word, rewind)(character);
} else {
return scanWord(callback, options, word + character, function (state) {
return rewind(state)(character);
});
}
};
}
var parseWordPiecewise = Parser.makeParser(function (callback, length, options) {
return parseWordTail(callback, length, options, []);
});
function parseWordTail(callback, length, options, columns, previous) {
return parseColumn(function (moreColumns) {
if (!moreColumns.length) {
return callback(columns);
} else {
return parseWordTail(
callback,
length,
options,
columns.concat(moreColumns),
moreColumns[moreColumns.length - 1] // previous
);
}
}, length, options, previous);
}
function makeOf(makeColumn) {
return makeColumn("ampaumbar", {from: "of"})
.varies();
}
function makeOfPrime(makeColumn) {
return makeOf(makeColumn)
.addAbove("o", {from: "o", silent: true})
.varies(); // TODO is this supposed to be u above?
}
function makeOfPrimePrime(makeColumn) {
return makeColumn("formen", {from: "f"})
.addAbove("o", {from: "o"});
}
function makeThe(makeColumn) {
return makeColumn("antoando", {from: "the"})
.varies();
}
function makeThePrime(makeColumn) {
return makeThe(makeColumn).addBelow("i-below", {from: ""})
.varies();
}
function makeThePrimePrime(makeColumn) {
return makeColumn("thule", {from: "th"}).addBelow("i-below", {from: "e", silent: true});
}
function makeOfThe(makeColumn) {
return makeColumn("ampaumbar", {from: "of the"})
.addTildeBelow({from: ""});
}
function makeAnd(makeColumn) {
return makeColumn("ando", {from: "and"})
.addTildeAbove({from: ""});
}
function makeAndPrime(makeColumn) {
return makeAnd(makeColumn)
.addBelow("i-below", {from: ""})
.varies();
}
function makeAndPrimePrime(makeColumn) {
return makeColumn("ando", {from: "d"})
.addTildeAbove("n", {from: "n"})
.addAbove("a", {from: "a"});
}
function parseColumn(callback, length, options, previous) {
var font = options.font;
var makeColumn = font.makeColumn;
return parseTehta(function (tehta, tehtaFrom) {
if (tehta === "y" && options.language === "english" && previous == null) {
return callback([makeColumn("anna", {from: "y (initial)"})]);
}
return parseTengwa(function (column, tehta, tehtaFrom) {
if (tehta) {
if (options.reverseCurls) {
tehta = reverseCurls[tehta] || tehta;
}
if (options.swapDotSlash) {
tehta = swapDotSlash[tehta] || tehta;
}
}
if (column) {
if (tehta) {
if (column.tengwa === "silme" && tehta && options.sHook) {
return callback([
makeColumn("short-carrier", {from: ""})
.addAbove(tehta, {from: tehtaFrom})
.addBelow("s", {from: "s"})
]);
} else if (tehta === "y") {
var columns = [column];
if (options.language === "english" && column.canAddAbove("y-english")) {
column.addAbove("y-english", {from: tehtaFrom});
} else if (column.canAddAbove("y-sindarin")) {
column.addAbove("y-sindarin", {from: tehtaFrom});
} else {
columns.push(makeColumn("anna", {from: "y"}));
}
return parseTengwaAnnotations(function (column) {
return callback(columns);
}, column, length, options);
} else if (canAddAboveTengwa(tehta) && column.canAddAbove(tehta)) {
column.addAbove(tehta, {from: tehtaFrom});
return parseTengwaAnnotations(function (column) {
return callback([column]);
}, column, length, options);
} else {
// some tengwar inherently lack space above them
// and cannot be reversed to make room.
// some long tehtar cannot be placed on top of
// a tengwa.
// put the previous tehta over the appropriate carrier
// then follow up with this tengwa.
return parseTengwaAnnotations(function (column) {
return callback([makeCarrier(tehta, tehtaFrom, options), column]);
}, column, length, options);
}
} else {
return parseTengwaAnnotations(function (column) {
return callback([column]);
}, column, length, options);
}
} else if (tehta === "y") {
var column;
if (options.language === "english") {
column = makeColumn("short-carrier").addAbove("y-english", {from: "y"});
} else {
column = makeColumn("short-carrier").addAbove("y-sindarin", {from: "y"});
}
return parseTengwaAnnotations(function (column) {
return callback([column]);
}, column, length, options);
} else if (tehta) {
return parseTengwaAnnotations(function (carrier) {
return callback([carrier]);
}, makeCarrier(tehta, tehtaFrom, options), length, options);
} else {
return function (character) {
if (Parser.isBreak(character)) {
return callback([])(character);
} else if (/\d/.test(character)) {
return parseNumber(callback, options)(character);
} else if (punctuation[character]) {
return callback([makeColumn(punctuation[character], {from: character})]);
} else {
return callback([
makeColumn("ure", {from: character})
.addError(
"Cannot transcribe " +
JSON.stringify(character) +
" in General Use Mode"
)
]);
}
};
}
}, options, tehta, tehtaFrom);
}, options);
}
function makeCarrier(tehta, tehtaFrom, options) {
var font = options.font;
var makeColumn = font.makeColumn;
if (tehta === "á") {
return makeColumn("anna", {from: "a"})
.addAbove("a", {from: "a"});
} else if (shorterVowels[tehta]) {
return makeColumn("long-carrier", {from: tehtaFrom})
.addAbove(shorterVowels[tehta], {from: ""});
} else {
return makeColumn("short-carrier", {from: tehtaFrom})
.addAbove(tehta, {from: ""});
}
}
function parseTehta(callback, options) {
return function (character) {
if (character === "") {
return callback();
}
var from = character;
if (character === "ë" && options.language !== "english") {
character = "e";
}
var caretIndex = caretVowels.indexOf(character);
if (caretIndex !== -1) {
character = acuteVowels[caretIndex];
}
var shortIndex = shortVowels.indexOf(character);
if (shortIndex !== -1) {
return function (nextCharacter) {
// Doubling vowels as in the English word GREEN is generally
// rendered orthographically, with two separate E tehtar.
// However, in other languages, it is convenient to allow users
// who do not have ready access to diacrtics on their keyboard
// the ability to get a long vowel by doubling.
if (options.language !== "english" && nextCharacter === character) { // doubled
return callback(acuteVowels[shortIndex], character + nextCharacter);
} else {
return callback(character, from)(nextCharacter);
}
};
} else if (nonLengthenableVowels.indexOf(character) !== -1) {
return callback(character, from);
} else {
return callback()(from);
}
};
}
var caretVowels = "âêîôû";
var acuteVowels = "áéíóú";
var shortVowels = "aeiou";
var nonLengthenableVowels = "áéíóúy";
var tehtarThatCanBeAddedAbove = "aeiouóú";
var vowels = "aeëiouáéíóú";
var shorterVowels = {"á": "a", "é": "e", "í": "i", "ó": "o", "ú": "u"};
var reverseCurls = {"o": "u", "u": "o", "ó": "ú", "ú": "ó"};
var swapDotSlash = {"i": "e", "e": "i"};
function canAddAboveTengwa(tehta) {
return tehtarThatCanBeAddedAbove.indexOf(tehta) !== -1;
}
function parseTengwa(callback, options, tehta, tehtaFrom) {
var font = options.font;
var makeColumn = font.makeColumn;
return function (character) {
if (character === "n") {
return function (character) {
if (character === "n") { // nn
if (options.doubleNasalsWithTildeBelow) {
return callback(
makeColumn("numen", {from: "n"})
.addTildeBelow({from: "n"}),
tehta,
tehtaFrom
);
} else {
return callback(
makeColumn("numen", {from: "n"})
.addTildeAbove({from: "n"}),
tehta,
tehtaFrom
);
}
} else if (character === "t") { // nt
return function (character) {
if (character === "h") { // nth
return callback(
makeColumn("thule", {from: "th"})
.addTildeAbove({from: "n"}),
tehta,
tehtaFrom
);
} else { // nt.
return callback(
makeColumn("tinco", {from: "t"})
.addTildeAbove({from: "n"}),
tehta,
tehtaFrom
)(character);
}
};
} else if (character === "d") { // nd
return callback(makeColumn("ando", {from: "d"}).addTildeAbove({from: "n"}), tehta, tehtaFrom);
} else if (character === "c" || character === "k") { // nc -> ñc
return callback(makeColumn("quesse", {from: character}).addTildeAbove({from: "ñ"}), tehta, tehtaFrom);
} else if (character === "g") { // ng -> ñg
return callback(makeColumn("ungwe", {from: "g"}).addTildeAbove({from: "ñ"}), tehta, tehtaFrom);
} else if (character === "j") { // nj
return callback(makeColumn("anca", {from: "j"}).addTildeAbove({from: "n"}), tehta, tehtaFrom);
} else if (character === "f") { // nf -> nv
return callback(makeColumn("numen", {from: "n"}), tehta, tehtaFrom)("v");
} else if (character === "w") { // nw -> ñw
return function (character) {
if (character === "a") { // nwa
return function (character) { // nwal
if (character === "l") {
return callback(makeColumn("nwalme", {from: "n"}).addAbove("w", {from: "w"}), tehta, tehtaFrom)("a")(character);
} else { // nwa.
return callback(makeColumn("numen", {from: "n"}).addAbove("w", {from: "w"}), tehta, tehtaFrom)("a")(character);
}
};
} else if (character === "nw`") { // nw/ prime -> ñw
return callback(makeColumn("nwalme", {from: "ñ"}).addAbove("w", {from: "w"}), tehta, tehtaFrom);
} else { // nw.
return callback(makeColumn("numen", {from: "n"}).addAbove("w", {from: "w"}), tehta, tehtaFrom)(character);
}
};
} else { // n.
return callback(makeColumn("numen", {from: "n"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "m") { // m
return function (character) {
if (character === "m") { // mm
if (options.doubleNasalsWithTildeBelow) {
return callback(makeColumn("malta", {from: "m"}).addTildeBelow({from: "m"}), tehta, tehtaFrom);
} else {
return callback(makeColumn("malta", {from: "m"}).addTildeAbove({from: "m"}), tehta, tehtaFrom);
}
} else if (character === "p") { // mp
return callback(makeColumn("parma", {from: "p"}).addTildeAbove({from: "m"}), tehta, tehtaFrom);
} else if (character === "b") { // mb
return callback(makeColumn("umbar", {from: "b"}).addTildeAbove({from: "m"}), tehta, tehtaFrom);
} else if (character === "f") { // mf
return callback(makeColumn("formen", {from: "f"}).addTildeAbove({from: "m"}), tehta, tehtaFrom);
} else if (character === "v") { // mv
return callback(makeColumn("ampa", {from: "v"}).addTildeAbove({from: "m"}), tehta, tehtaFrom);
} else { // m.
return callback(makeColumn("malta", {from: "m"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "ñ") { // ñ
return function (character) {
// ññ does not exist to the best of my knowledge
// ñw is handled naturally by following w
if (character === "c" || character === "k") { // ñc
return callback(makeColumn("quesse", {from: character}).addTildeAbove({from: "ñ"}), tehta, tehtaFrom);
} else if (character === "g") { // ñg
return callback(makeColumn("ungwe", {from: "g"}).addTildeAbove({from: "ñ"}), tehta, tehtaFrom);
} else { // ñ.
return callback(makeColumn("nwalme", {from: "ñ"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "t") { // t
return function (character) {
if (character === "t") { // tt
return callback(makeColumn("tinco", {from: "t"}).addTildeBelow({from: "t"}), tehta, tehtaFrom);
} else if (character === "h") { // th
return callback(makeColumn("thule", {from: "th"}), tehta, tehtaFrom);
} else if (character === "c") { // tc
return function (character) {
if (character === "h") { // tch -> tinco calma
return callback(makeColumn("tinco", {from: "t"}), tehta, tehtaFrom)("c")("h")("`");
} else {
return callback(makeColumn("tinco", {from: "t"}), tehta, tehtaFrom)("c")(character);
}
};
} else if (character === "s" && options.tsdz) { // ts
return callback(makeColumn("calma", {from: "ts"}), tehta, tehtaFrom);
} else { // t.
return callback(makeColumn("tinco", {from: "t"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "p") { // p
return function (character) {
if (character === "p") { // pp
return callback(makeColumn("parma", {from: "p"}).addTildeBelow({from: "p"}), tehta, tehtaFrom);
} else if (character === "h") { // ph
return Parser.countPrimes(function (primes) {
var column;
if (primes === 0) {
column = makeColumn("formenparma", {from: "ph"}).varies();
} else if (primes >= 1) {
column = makeColumn("formen", {from: "ph"});
}
if (primes > 1) {
column.addError("PH cluster only has two alternate representations.");
}
return callback(column, tehta, tehtaFrom);
});
} else { // p.
return callback(makeColumn("parma", {from: "p"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "c") {
return function (character2) {
if (character2 == "h" && options.language !== "english") {
return callback(makeColumn("hwesta", {from: character + character2}), tehta, tehtaFrom);
} else if (character2 === "k") {
return callback(makeColumn("quesse", {from: character2}).addTildeBelow({from: character}), tehta, tehtaFrom);
} else if (character2 === "h" || character2 === "c") { // ch and cc
return callback(makeColumn("calma", {from: character + character2}), tehta, tehtaFrom);
} else { // c.
return callback(makeColumn("quesse", {from: character}), tehta, tehtaFrom)(character2);
}
};
} else if (character === "k") {
return function (character2) {
if (character2 === "h") { // kh is ach laut
if (!options.noAchLaut) {
return callback(makeColumn("hwesta", {from: character + character2}), tehta, tehtaFrom);
} else { // kh is just k
return callback(makeColumn("quesse", {from: character}), tehta, tehtaFrom);
}
} else { // c. or k.
return callback(makeColumn("quesse", {from: character}), tehta, tehtaFrom)(character2);
}
};
} else if (character === "q") {
return callback(makeColumn("quesse", {from: character}), tehta, tehtaFrom);
} else if (character === "x") {
return callback(makeColumn("quesse", {from: "x (k-)"}).addBelow("s", {from: "x (-s)"}), tehta, tehtaFrom);
} else if (character === "d") {
return function (character) {
if (character === "d") { // dd
return callback(makeColumn("ando", {from: "d"}).addTildeBelow({from: "d"}), tehta, tehtaFrom);
} else if (character === "j") { // dj
return callback(makeColumn("anga", {from: "dj"}), tehta, tehtaFrom);
} else if (character === "z" && options.tsdz) { // dz
// TODO annotate dz to indicate that options.tsdz affects this cluster
return callback(makeColumn("anga", {from: "dz"}), tehta, tehtaFrom);
} else if (character === "h") { // dh
return callback(makeColumn("anto", {from: "dh"}), tehta, tehtaFrom);
} else { // d.
return callback(makeColumn("ando", {from: "d"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "b") { // b
return function (character) {
if (character === "b") { // bb
return callback(makeColumn("umbar", {from: "b"}).addTildeBelow({from: "b"}), tehta, tehtaFrom);
} else { // b.
return callback(makeColumn("umbar", {from: "b"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "g") { // g
return function (character) {
if (character === "g") { // gg
return callback(makeColumn("ungwe", {from: "g"}).addTildeBelow({from: "g"}), tehta, tehtaFrom);
} else if (character === "h") { // gh
if (options.language === "black-speech") {
return callback(makeColumn("unqueungwe", {from: "gh"}), tehta, tehtaFrom);
} else {
return callback(makeColumn("unque", {from: "gh"}), tehta, tehtaFrom);
}
} else { // g.
return callback(makeColumn("ungwe", {from: "g"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "f") { // f
return function (character) {
if (character === "f") { // ff
return callback(makeColumn("formen", {from: "f"}).addTildeBelow({from: "f"}), tehta, tehtaFrom);
} else { // f.
return callback(makeColumn("formen", {from: "f"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "v") { // v
return callback(makeColumn("ampa", {from: "v"}), tehta, tehtaFrom);
} else if (character === "j") { // j
if (options.language === 'english') {
return Parser.countPrimes(function (primes) {
if (primes === 0) {
return callback(makeColumn("anga", {from: "j"}).varies(), tehta, tehtaFrom); //HH Changed anca to anga
} else {
var column = callback(makeColumn("anca", {from: "j"}), tehta, tehtaFrom);
if (primes > 1) {
column.addError("J only has two English variants: 1. anga, as pronounced in JACK and 2. anca , as pronounced in measure.");
}
return column;
}
});
} else {
return callback(makeColumn("anca", {from: "j"}), tehta, tehtaFrom);
}
} else if (character === "s") { // s
return function (character) {
if (character === "s") { // ss
return Parser.countPrimes(function (primes) {
var tengwa = primes > 0 ? "silme-nuquerna" : "silme";
var tengwaFrom = primes > 0 ? "s′" : "s";
var column = makeColumn(tengwa, {from: tengwaFrom}).addTildeBelow({from: "s"});
if (primes === 0) {
column.varies();
}
if (primes > 1) {
column.addError("Silme does not have this many alternate forms.");
}
return callback(column, tehta, tehtaFrom);
});
} else if (character === "h") { // sh
if (options.language === "black-speech") {
return callback(makeColumn("harmacalma", {from: "sh"}), tehta, tehtaFrom);
} else {
return callback(makeColumn("harma", {from: "sh"}), tehta, tehtaFrom);
}
} else { // s.
return Parser.countPrimes(function (primes) {
var tengwa = primes > 0 ? "silme-nuquerna" : "silme";
var tengwaFrom = primes > 0 ? "s′" : "s";
var column = makeColumn(tengwa, {from: tengwaFrom});
if (primes === 0) {
column.varies();
}
if (primes > 1) {
column.addError("Silme does not have this many alternate forms.");
}
return callback(column, tehta, tehtaFrom);
})(character);
}
};
} else if (character === "z") { // z
return function (character) {
if (character === "z") { // zz
return Parser.countPrimes(function (primes) {
var tengwa = primes > 0 ? "esse-nuquerna" : "esse";
var column = makeColumn(tengwa, {from: "z"}).addTildeBelow({from: "z"});
if (primes === 0) {
column.varies();
}
if (primes > 1) {
column.addError("Esse does not have this many alternate forms.");
}
return callback(column, tehta, tehtaFrom);
});
} else { // z.
return Parser.countPrimes(function (primes) {
var tengwa = primes > 0 ? "esse-nuquerna" : "esse";
var column = makeColumn(tengwa, {from: "z"});
if (primes === 0) {
column.varies();
}
if (primes > 1) {
column.addError("Silme does not have this many alternate forms.");
}
return callback(column, tehta, tehtaFrom);
})(character);
}
};
} else if (character === "h") { // h
return function (character) {
if (character === "w") { // hw
return callback(makeColumn("hwesta-sindarinwa", {from: "hw"}), tehta, tehtaFrom);
} else { // h.
return callback(makeColumn("hyarmen", {from: "h"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "r") { // r
return function (character) {
if (character === "r") { // rr
return callback(makeColumn("romen", {from: "r"}).addTildeBelow({from: "r"}), tehta, tehtaFrom);
} else if (character === "h") { // rh
return callback(makeColumn("arda", {from: "rh"}), tehta, tehtaFrom);
} else if (
Parser.isFinal(character) || (
options.medialOre &&
vowels.indexOf(character) === -1
)
) { // r final (optionally r before consonant)
return callback(makeColumn("ore", {from: "r", final: true}), tehta, tehtaFrom)(character);
} else { // r.
return callback(makeColumn("romen", {from: "r"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "l") {
return function (character) {
if (character === "l") { // ll
return callback(makeColumn("lambe", {from: "l"}).addTildeBelow({from: "l"}), tehta, tehtaFrom);
} else if (character === "h") { // lh
return callback(makeColumn("alda", {from: "lh"}), tehta, tehtaFrom);
} else { // l.
return callback(makeColumn("lambe", {from: "l"}), tehta, tehtaFrom)(character);
}
};
} else if (character === "i") { // i
return callback(makeColumn("anna", {from: "i", diphthong: true}), tehta, tehtaFrom);
} else if (character === "u") { // u
return callback(makeColumn("vala", {from: "u", diphthong: true}), tehta, tehtaFrom);
} else if (character === "w") { // w
return function (character) {
if (character === "h") { // wh
return callback(makeColumn("hwesta-sindarinwa", {from: "wh"}), tehta, tehtaFrom);
} else { // w.
return callback(makeColumn("vala", {from: "w", dipththong: true}), tehta, tehtaFrom)(character);
}
};
} else if (character === "e" && (!tehta || tehta === "a")) { // ae or e after consonants
return callback(makeColumn("yanta", {from: "e", diphthong: true}), tehta, tehtaFrom);
} else if (character === "e" && (!tehta || tehta === "i")) { // ie or e after consonants
return callback(makeColumn("yanta", {from: "e", diphthong: true}), tehta, tehtaFrom);
} else if (character === "e" && (!tehta || tehta === "o")) { // oe or e after consonants
return callback(makeColumn("yanta", {from: "e", diphthong: true}), tehta, tehtaFrom);
} else if (character === "ë") { // if "ë" makes it this far, it's a diaresis for english
return callback(makeColumn("short-carrier", {from: ""}).addAbove("e", {from: "e"}));
} else if (character === "y") {
return Parser.countPrimes(function (primes) {
if (primes === 0) {
return callback(makeColumn("anna", {from: ""}), tehta, tehtaFrom);
// 21-9-19 HH edit return callback(makeColumn("wilya", {from: ""}).addBelow("y", {from: "y"}), tehta, tehtaFrom);
} else if (primes === 1) {
return callback(makeColumn("long-carrier", {from: "y"}).addAbove("i", {from: ""}), tehta, tehtaFrom);
} else {
return callback(makeColumn("ure", {from: "y"}).addError("Consonantal Y only has one variation"));
}
});
} else if (shorterVowels[character]) {
return callback(
makeCarrier(character, character, options)
.addAbove(shorterVowels[character], {from: ""}),
tehta,
tehtaFrom
);
} else if (character === "`" && options.language === "english" && tehta === "e") {
return function (character2) {
if (character2 === "") {
// final e` in english should be equivalent to diaresis.
// tehta deliberately consumed in this case, not passed forward.
return callback(
makeColumn("short-carrier", {from: ""})
.addAbove("e", {from: "e"})
);
} else {
// tehta deliberately consumed in this case, not passed forward.
return callback(
makeColumn("short-carrier", {from: ""})
.addBelow("i-below", {from: "e", silent: true})
)(character)(character2);
}
};
} else if (character === "" && options.language === "english" && tehta === "e") {
// tehta deliberately consumed in this case, not passed forward.
return callback(
makeColumn("short-carrier", {from: ""})
.addBelow("i-below", {from: "e", silent: true})
)(character);
} else {
return callback(null, tehta, tehtaFrom)(character);
}
};
}
exports.parseTengwaAnnotations = parseTengwaAnnotations;
function parseTengwaAnnotations(callback, column, length, options) {
return parseFollowingAbove(function (column) {
return parseFollowingBelow(function (column) {
return parseFollowing(callback, column);
}, column, length, options);
}, column);
}
// add a following-w above the current character if the next character is W and
// there is room for it.
function parseFollowingAbove(callback, column) {
if (column.canAddAbove("w", "w")) {
return function (character) {
if (character === "w") {
return callback(column.addAbove("w", {from: "e"}));
} else {
return callback(column)(character);
}
};
} else {
return callback(column);
}
}
function parseFollowingBelow(callback, column, length, options) {
return function (character) {
if (character === "ë" && options.language !== "english") {
character = "e";
}
if (options.language === "english" && character === "y" && column.canAddAbove("y-english")) {
return callback(column.addAbove("y-english", {from: "y"}));
} else if (character === "y" && column.canAddAbove("y-sindarin")) {
return callback(column.addAbove("y-sindarin", {from: "y"}));
} else if (character === "e" && column.canAddBelow("i-below")) {
return Parser.countPrimes(function (primes) {
return function (character) {
if (Parser.isFinal(character) && options.language === "english" && length > 2) {
if (primes === 0) {
return callback(
column.addBelow("i-below", {from: "e", silent: true})
.varies()
)(character);
} else {
if (primes > 1) {
column.addError("Following E has only one variation.");
}
return callback(column)("e")(character);
}
} else {
if (primes === 0) {
return callback(column.varies())("e")(character);
} else {
if (primes > 1) {
column.addError("Following E has only one variation.");
}
return callback(column.addBelow("i-below", {from: "e", eilent: true}))(character);
}
}
};
});
} else {
return callback(column)(character);
}
};
}
function parseFollowing(callback, column) {
return function (character) {
if (character === "s") {
if (column.canAddBelow("s")) {
return Parser.countPrimes(function (primes, rewind) {
if (primes === 0) {
return callback(column.addBelow("s", {from: "s"}).varies());
} else if (primes) {
if (primes > 1) {
column.addError("Only one alternate form for following S.");
}
return rewind(callback(column)("s"));
}
});
} else {
return Parser.countPrimes(function (primes, rewind) {
return function (character) {
if (Parser.isFinal(character)) { // end of word
if (column.canAddFollowing("s-final") && primes-- === 0) {
column.addFollowing("s-final", {from: "s"});
} else if (column.canAddFollowing("s-inverse") && primes -- === 0) {
column.addFollowing("s-inverse", {from: "s"});
if (column.canAddFollowing("s-final")) {
column.varies();
}
} else if (column.canAddFollowing("s-extended") && primes-- === 0) {
column.addFollowing("s-extended", {from: "s"});
if (column.canAddFollowing("s-inverse")) {
column.varies();
}
} else if (column.canAddFollowing("s-flourish") && primes-- === 0) {
column.addFollowing("s-flourish", {from: "s"});
if (column.canAddFollowing("s-extended")) {
column.varies();
}
} else {
// rewind primes for subsequent alterations
var state = callback(column)("s");
while (primes-- > 0) {
state = state("`");
}
return state(character);
}
return callback(column)(character);
} else {
return rewind(callback(column)("s"))(character);
}
};
});
}
} else {
return callback(column)(character);
}
};
}