summaryrefslogtreecommitdiff
path: root/Build/source/texk/gregorio/gregorio-src/src/gabc/gabc-notes-determination.l
blob: d142b546a01a107bd32aff17dadfd2930685a199 (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
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
%{
/*
 * Gregorio is a program that translates gabc files to GregorioTeX
 * This file implements the note parser.
 *
 * Copyright (C) 2006-2021 The Gregorio Project (see CONTRIBUTORS.md)
 *
 * This file is part of Gregorio.
 *
 * This program 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.
 *
 * This program 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 "config.h"
#include <stdio.h>
#include <ctype.h> /* for tolower */
#include "bool.h"
#include "struct.h"
#include "messages.h"
#include "support.h"

#include "gabc.h"

#define YY_NO_INPUT

#define YY_USER_ACTION gabc_update_location(&notes_lloc, \
        gabc_notes_determination_text, gabc_notes_determination_leng);

static gregorio_scanner_location notes_lloc;
static gregorio_note *current_note;
static char char_for_brace;
static unsigned int nbof_isolated_episema;
static char *notesmacros[10];
static char tempstr[256];
static unsigned short overbrace_var = 0, underbrace_var = 0;
static const char *overbrace_var_kind;
static int before_ledger_type;
static char *before_ledger_length = NULL;
static unsigned short ledger_var[2] = { 0, 0 };
static unsigned char staff_lines;
static signed char highest_pitch;
static signed char high_ledger_line_pitch;
static bool legacy_oriscus_orientation;
static unsigned short he_adjustment_index[2] = { 0, 0 };
static signed char bracket_low_pitch, bracket_high_pitch;
static unsigned short left_bracket_texverb = 0;

#define LEDGER(WHICH, SPECIFICITY, VALUE) \
    if (LEDGER_##SPECIFICITY > current_note->WHICH##_ledger_specificity) { \
        current_note->WHICH##_ledger_line = VALUE; \
        current_note->WHICH##_ledger_specificity = LEDGER_##SPECIFICITY; \
    }

typedef struct slur_info {
    unsigned short var;
    char shift;
    gregorio_note *start;
} slur_info;

static slur_info slur[2] = { { 0, '\0', NULL }, { 0, '\0', NULL } };

static __inline gregorio_sign_orientation letter_to_sign_orientation(
        const char letter) {
    switch (letter) {
    case 'u': /* "u"under */
        return SO_UNDER;
    case 'o': /* "o"ver */
        return SO_OVER;
    }
    /* not reachable unless there's a programming error */
    /* LCOV_EXCL_START */
    gregorio_fail2(letter_to_sign_orientation,
            "invalid sign orientation letter: %c", letter);
    return SO_OVER;
    /* LCOV_EXCL_STOP */
}

static __inline int letter_to_pitch_adjustment(const char letter) {
    switch (letter_to_sign_orientation(letter)) {
    case SO_OVER:
        return 1;
    case SO_UNDER:
        return -1;
    }
    /* not reachable unless there's a programming error */
    /* LCOV_EXCL_START */
    gregorio_fail2(letter_to_pitch_adjustment,
            "invalid sign orientation letter: %c", letter);
    return 0;
    /* LCOV_EXCL_STOP */
}

static __inline signed char pitch_letter_to_height(const char pitch) {
    char result = pitch - 'a' + LOWEST_PITCH;
    if (pitch == 'p') {
        --result;
    }
    if (result > highest_pitch) {
        gregorio_messagef("pitch_letter_to_height", VERBOSITY_ERROR, 0,
                _("invalid pitch for %u lines: %c"), (unsigned int)staff_lines,
                pitch);
    }
    if (left_bracket_texverb) {
        if (result < bracket_low_pitch) {
            bracket_low_pitch = result;
        }
        if (result > bracket_high_pitch) {
            bracket_high_pitch = result;
        }
    }
    return result;
}

static gregorio_shape punctum_inclinatum(const char orientation)
{
    switch (orientation) {
    case '0':
        return S_PUNCTUM_INCLINATUM_DESCENDENS;

    case '2':
        return S_PUNCTUM_INCLINATUM_STANS;

    case '1':
        return S_PUNCTUM_INCLINATUM_ASCENDENS;
    }

    return S_PUNCTUM_INCLINATUM_UNDETERMINED;
}

static __inline void lex_add_note(int i, gregorio_shape shape, char signs,
        char liquescentia)
{
    signed char height = pitch_letter_to_height(tolower(
            (unsigned char)gabc_notes_determination_text[i]));

    nbof_isolated_episema = 0;
    gregorio_add_note(&current_note, height, shape, signs, liquescentia, NULL,
            &notes_lloc);
    current_note->he_adjustment_index[SO_OVER] = he_adjustment_index[SO_OVER];
    current_note->he_adjustment_index[SO_UNDER] = he_adjustment_index[SO_UNDER];

    if (height >= high_ledger_line_pitch) {
        LEDGER(high, DRAWN, true);
    } else if (ledger_var[SO_OVER]) {
        LEDGER(high, EXPLICIT, true);
    }

    if (height <= LOW_LEDGER_LINE_PITCH) {
        LEDGER(low, DRAWN, true);
    } else if (ledger_var[SO_UNDER]) {
        LEDGER(low, EXPLICIT, true);
    }
}

static __inline void add_bar_as_note(gregorio_bar bar)
{
    nbof_isolated_episema = 0;
    gregorio_add_bar_as_note(&current_note, bar, &notes_lloc);
}

static __inline void error(void)
{
    gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
            _("undefined macro used: m%d"),
            gabc_notes_determination_text[3] - '0');
}

static void add_h_episema(void)
{
    grehepisema_size size = H_NORMAL;
    gregorio_vposition vposition = VPOS_AUTO;
    bool disable_bridge = false;

    char *ptr = gabc_notes_determination_text;
    char current;
    /* first character is the underscore */
    while ((current = *(++ptr))) {
        switch(current) {
        case '0':
            vposition = VPOS_BELOW;
            break;
        case '1':
            vposition = VPOS_ABOVE;
            break;
        case '2':
            disable_bridge = true;
            break;
        case '3':
            size = H_SMALL_LEFT;
            break;
        case '4':
            size = H_SMALL_CENTRE;
            break;
        case '5':
            size = H_SMALL_RIGHT;
            break;
        default:
            /* not reachable unless there's a programming error */
            /* LCOV_EXCL_START */
            gregorio_fail2(gabc_notes_determination,
                    "unrecognized horizontal episema modifier: %c", current);
            break;
            /* LCOV_EXCL_STOP */
        };
    }

    gregorio_add_h_episema(current_note, size, vposition, disable_bridge,
                &nbof_isolated_episema);
}

static void add_sign(gregorio_sign sign)
{
    gregorio_vposition vposition = VPOS_AUTO;
    switch(gabc_notes_determination_text[1]) {
    case '0':
        vposition = VPOS_BELOW;
        break;
    case '1':
        vposition = VPOS_ABOVE;
        break;
    }
    gregorio_add_sign(current_note, sign, vposition);
}

static void save_before_ledger(const char *const before_ledger)
{
    if (strcmp(before_ledger, "0") == 0) {
        before_ledger_type = 0;
        before_ledger_length = "";
    } else if (strcmp(before_ledger, "1") == 0) {
        before_ledger_type = 1;
        before_ledger_length = "";
    } else {
        before_ledger_type = 2;
        before_ledger_length = gregorio_strdup(before_ledger);
    }
}

static void add_static_ledger(const gregorio_sign_orientation type,
        const char *length) {
    gregorio_snprintf(tempstr, sizeof tempstr,
            "\\GreDrawAdditionalLine{%d}{%s}{%d}{%s}{0}{}",
            type, length + 1, before_ledger_type, before_ledger_length);

    if (before_ledger_type == 2) {
        free(before_ledger_length);
        before_ledger_length = NULL;
    }

    gregorio_add_texverb_as_note(&current_note, gregorio_strdup(tempstr),
            GRE_TEXVERB_GLYPH, &notes_lloc);
}

static __inline const char *over_or_under(
        const gregorio_sign_orientation type) {
    switch (type) {
    case SO_OVER:
        return "over";
    case SO_UNDER:
        return "under";
    }
    /* not reachable unless there's a programming error */
    /* LCOV_EXCL_START */
    gregorio_fail2(over_or_under, "invalid ledger type %d", type);
    return "";
    /* LCOV_EXCL_STOP */
}

static void add_variable_ledger(const gregorio_sign_orientation type,
        const char *after_ledger)
{
    if (ledger_var[type]) {
        const char *const typename = over_or_under(type);
        gregorio_messagef("add_variable_ledger", VERBOSITY_ERROR, 0,
                _("variable %s-staff ledger line without termination of "
                "previous %s-staff ledger line"), typename, typename);
    } else {
        int after_ledger_type;
        const char *after_ledger_length;

        ++after_ledger;

        if (strcmp(after_ledger, "0") == 0) {
            after_ledger_type = 0;
            after_ledger_length = "";
        } else if (strcmp(after_ledger, "1") == 0) {
            after_ledger_type = 1;
            after_ledger_length = "";
        } else {
            after_ledger_type = 2;
            after_ledger_length = after_ledger;
        }

        ledger_var[type] = ++tex_position_id;
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreVarBraceSavePos{%hu}{0}{1}"
                "\\GreDrawAdditionalLine{%d}{\\GreVarBraceLength{%hu}}"
                "{%d}{%s}{%d}{%s}",
                ledger_var[type], type, ledger_var[type], before_ledger_type,
                before_ledger_length, after_ledger_type, after_ledger_length);

        if (before_ledger_type == 2) {
            free(before_ledger_length);
            before_ledger_length = NULL;
        }

        gregorio_add_texverb_as_note(&current_note, gregorio_strdup(tempstr),
                GRE_TEXVERB_GLYPH, &notes_lloc);
    }
}

static void end_variable_ledger(const gregorio_sign_orientation type)
{
    if (!ledger_var[type]) {
        const char *const typename = over_or_under(type);
        gregorio_messagef("end_variable_ledger", VERBOSITY_ERROR, 0,
                _("variable %s-staff ledger line termination without variable "
                "%s-staff ledger line start"), typename, typename);
    } else {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreVarBraceSavePos{%hu}{0}{2}", ledger_var[type]);
        ledger_var[type] = 0;
        gregorio_add_texverb_as_note(&current_note, gregorio_strdup(tempstr),
                GRE_TEXVERB_GLYPH, &notes_lloc);
    }
}

static __inline int parse_clef_line(char line)
{
    line -= '0';
    if (line < 0 || line > staff_lines) {
        gregorio_messagef("parse_clef_line", VERBOSITY_ERROR, 0,
                _("invalid clef line for %u lines: %d"),
                (unsigned int)staff_lines, (int)line);
        return 1;
    }
    return line;
}

static __inline gregorio_bar parse_dominican_bar(char bar)
{
    bar -= '0';
    if (bar < 1 || bar > (2 * (staff_lines - 1))) {
        gregorio_messagef("parse_dominican_line", VERBOSITY_ERROR, 0,
                _("invalid Dominican bar for %u lines: ;%d"),
                (unsigned int)staff_lines, (int)bar);
    }

    switch (bar) {
    case 1:
        return B_DIVISIO_MINOR_D1;
    case 2:
        return B_DIVISIO_MINOR_D2;
    case 3:
        return B_DIVISIO_MINOR_D3;
    case 4:
        return B_DIVISIO_MINOR_D4;
    case 5:
        return B_DIVISIO_MINOR_D5;
    case 6:
        return B_DIVISIO_MINOR_D6;
    case 7:
        return B_DIVISIO_MINOR_D7;
    case 8:
        return B_DIVISIO_MINOR_D8;
    }
    /* not reachable unless there's a programming error */
    /* LCOV_EXCL_START */
    gregorio_fail2(check_dominican_line, "invalid dominican bar: %d", (int)bar);
    return B_NO_BAR;
    /* LCOV_EXCL_STOP */
}

static __inline gregorio_clef letter_to_clef(char letter)
{
    switch (letter) {
    case 'c':
        return CLEF_C;
    case 'f':
        return CLEF_F;
    }
    /* not reachable unless there's a programming error */
    /* LCOV_EXCL_START */
    gregorio_fail2(letter_to_clef, "invalid clef: %c", letter);
    return CLEF_C;
    /* LCOV_EXCL_STOP */
}

/* this assertion should only fail if the lex rules are incorrect */
static __inline void slur_assert(char *fn, bool test) {
    if (!test) {
        /* not reachable unless there's a programming error */
        /* LCOV_EXCL_START */
        gregorio_message(_("invalid slur text"), fn, VERBOSITY_FATAL, 0);
        exit(1);
        /* LCOV_EXCL_STOP */
    }
}

static char *parse_slur_shift(char *shift)
{
    char *c;

    c = strchr(gabc_notes_determination_text, ':');
    slur_assert("parse_slur_shift", c != NULL);
    slur_assert("parse_slur_shift", *(++c) != '\0');
    *shift = *c;
    return c;
}

static void parse_slur(void)
{
    const int direction = letter_to_pitch_adjustment(
            gabc_notes_determination_text[1]);
    char shift, *width, *height, *end;

    if (!current_note || current_note->type != GRE_NOTE) {
        gregorio_message(
                _("cannot add a slur to something that is not a note"),
                "parse_slur", VERBOSITY_ERROR, 0);
        return;
    }

    end = parse_slur_shift(&shift);
    width = strchr(end, ';');
    slur_assert("parse_slur", width != NULL);
    height = strchr(++width, ',');
    slur_assert("parse_slur", height != NULL);
    *height = '\0';
    end = strchr(++height, ']');
    slur_assert("parse_slur", end != NULL);
    *end = '\0';

    gregorio_snprintf(tempstr, sizeof tempstr,
            "\\GreSlur{%d}{%d}{%c}{%s}{%s}{}",
            current_note->u.note.pitch + direction, direction, shift, width,
            height);
    gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
}

static void start_var_slur(void)
{
    const gregorio_sign_orientation index = letter_to_sign_orientation(
            gabc_notes_determination_text[1]);

    if (!current_note || current_note->type != GRE_NOTE) {
        gregorio_message(
                _("cannot add a slur to something that is not a note"),
                "start_var_slur", VERBOSITY_ERROR, 0);
        return;
    }

    if (slur[index].var) {
        gregorio_messagef("start_var_slur", VERBOSITY_ERROR, 0,
                _("variable %s-note slur without termination of previous slur"),
                over_or_under(index));
        return;
    }

    slur[index].var = ++tex_position_id;
    parse_slur_shift(&(slur[index].shift));
    slur[index].start = current_note;
}

static void end_var_slur(void)
{
    const int direction = letter_to_pitch_adjustment(
            gabc_notes_determination_text[1]);
    const gregorio_sign_orientation index = letter_to_sign_orientation(
            gabc_notes_determination_text[1]);
    char shift;

    if (!current_note || current_note->type != GRE_NOTE) {
        gregorio_message(
                _("cannot add a slur to something that is not a note"),
                "end_var_slur", VERBOSITY_ERROR, 0);
        return;
    }

    if (!slur[index].var || !slur[index].shift || !slur[index].start) {
        gregorio_messagef("end_var_slur", VERBOSITY_ERROR, 0,
                _("variable %s-note slur end without start"),
                over_or_under(index));
        return;
    }

    parse_slur_shift(&shift);

    gregorio_snprintf(tempstr, sizeof tempstr,
            "\\GreVarBraceSavePos{%hu}{%c}{1}"
            "\\GreSlur{%d}{%d}{%c}{\\GreVarBraceLength{%hu}}{}{%d}",
            slur[index].var, slur[index].shift,
            slur[index].start->u.note.pitch + direction, direction,
            slur[index].shift, slur[index].var,
            current_note->u.note.pitch + direction);
    gregorio_add_texverb_to_note(slur[index].start, gregorio_strdup(tempstr));

    gregorio_snprintf(tempstr, sizeof tempstr,
            "\\GreVarBraceSavePos{%hu}{%c}{2}", slur[index].var, shift);
    gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));


    slur[index].var = 0;
    slur[index].shift = '\0';
    slur[index].start = NULL;
}

static void left_bracket(void)
{
    if (left_bracket_texverb) {
        gregorio_message(
                _("cannot add a left bracket before closing the previous one"),
                "left_bracket", VERBOSITY_ERROR, 0);
        return;
    }

    /* when setting the left bracket, temporarily store the point-and-click
     * information in the texverb */
    if (notes_lloc.generate_point_and_click) {
        gregorio_snprintf(tempstr, sizeof tempstr, "%u:%u:%u",
                notes_lloc.first_line, notes_lloc.first_offset,
                notes_lloc.first_column + 1);
    } else {
        tempstr[0] = '\0';
    }
    left_bracket_texverb = gregorio_add_texverb_as_note(&current_note,
            gregorio_strdup(tempstr), GRE_TEXVERB_GLYPH, &notes_lloc);
    bracket_low_pitch = MAX_PITCH;
    bracket_high_pitch = LOWEST_PITCH;
}

static void right_bracket(void)
{
    if (!left_bracket_texverb) {
        gregorio_message(
                _("cannot add a right bracket without a matching left bracket"),
                "right_bracket", VERBOSITY_ERROR, 0);
        return;
    }

    if (bracket_high_pitch < bracket_low_pitch) {
        gregorio_message(
                _("cannot add brackets without notes between them"),
                "right_bracket", VERBOSITY_ERROR, 0);
        return;
    }

    gregorio_snprintf(tempstr, sizeof tempstr, "\\GreBracket{0}{%d}{%d}{%s}",
            bracket_low_pitch, bracket_high_pitch,
            gregorio_texverb(left_bracket_texverb));
    gregorio_change_texverb(left_bracket_texverb, gregorio_strdup(tempstr));

    if (notes_lloc.generate_point_and_click) {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreBracket{1}{%d}{%d}{%u:%u:%u}", bracket_low_pitch,
                bracket_high_pitch, notes_lloc.first_line,
                notes_lloc.first_offset, notes_lloc.first_column + 1);
    } else {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreBracket{1}{%d}{%d}{}", bracket_low_pitch,
                bracket_high_pitch);
    }
    gregorio_add_texverb_as_note(&current_note, gregorio_strdup(tempstr),
            GRE_TEXVERB_GLYPH, &notes_lloc);

    left_bracket_texverb = 0;
}

static void parse_hepisema_adjustment(void)
{
    /* See https://github.com/gregorio-project/gregorio/issues/872
     *
     * [xh:yzw]
     *
     * - x is o(ver) or u(under)
     * - y is optional and is
     *   - l for low in the space (chooses ol or ul depending on x)
     *   - m for middle of the space
     *   - h for high in the space (chooses oh or uh depending on x)
     *   - ol for low in the space as if the episema were over the note
     *   - oh for high in the space as if the episema were over the note
     *   - ul for low in the space as if the episema were under the note
     *   - uh for high in the space as if the episema were under the note
     * - z is an optional nudge and must start with + or -
     * - w is optional and may be { to start a range or } to end a range.
     *
     * at least one of y, z, or w must be provided
     * if y and z are omitted, the : may be omitted
     * y and z are not permitted when w is }
     */

    const gregorio_sign_orientation index = letter_to_sign_orientation(
            gabc_notes_determination_text[1]);
    gregorio_sign_orientation det_index = index;
    char *ch = gabc_notes_determination_text + 3;
    gregorio_hepisema_vbasepos vbasepos = HVB_AUTO;
    char *nudge = NULL;
    char save;
    short hepisema_adjustment_id;

    if (he_adjustment_index[index]) {
        gregorio_messagef("parse_hepisema_adustment", VERBOSITY_ERROR, 0,
                _("horizontal %s-episema adjustment start before ending the "
                "previous adjustment"), over_or_under(index));
        return;
    }

    if (*ch == ':') {
        ++ch;
        if (*ch == 'm') {
            vbasepos = HVB_MIDDLE;
        } else {
            switch (*ch) {
            case 'o':
                ++ch;
                det_index = SO_OVER;
                break;
            case 'u':
                ++ch;
                det_index = SO_UNDER;
                break;
            }

            switch (*ch) {
            case 'l':
                switch (det_index) {
                case SO_OVER:
                    vbasepos = HVB_O_LOW;
                    break;
                case SO_UNDER:
                    vbasepos = HVB_U_LOW;
                    break;
                }
                break;
            case 'h':
                switch (det_index) {
                case SO_OVER:
                    vbasepos = HVB_O_HIGH;
                    break;
                case SO_UNDER:
                    vbasepos = HVB_U_HIGH;
                    break;
                }
                break;
            }
        }
        if (vbasepos) {
            ++ch;
        }
        if (*ch == '+' || *ch == '-') {
            nudge = ch;
            do {
                ++ch;
            } while (*ch && *ch != '{' && *ch != ']');
            save = *ch;
            *ch = '\0';
            nudge = gregorio_strdup(nudge);
            *ch = save;
        }
    }

    hepisema_adjustment_id = gregorio_add_hepisema_adjustment(vbasepos, nudge);

    if (*ch == '{') {
        he_adjustment_index[index] = hepisema_adjustment_id;
    } else {
        if (!current_note || current_note->type != GRE_NOTE) {
            gregorio_message(_("cannot add a horizontal episema adjustment to "
                    "something that is not a note"),
                    "parse_hepisema_adjustment", VERBOSITY_ERROR, 0);
            return;
        }
        current_note->he_adjustment_index[index] = hepisema_adjustment_id;
    }
}

static void end_hepisema_adjustment(void)
{
    /* [xh:}]
     * - x indicates l(ow) or h(igh) episema
     * - : is optional
     */

    const gregorio_sign_orientation index = letter_to_sign_orientation(
            gabc_notes_determination_text[1]);

    if (!he_adjustment_index[index]) {
        gregorio_messagef("end_hepisema_adustment", VERBOSITY_ERROR, 0,
                _("horizontal %s-episema adjustment end with no matching "
                "start"), over_or_under(index));
        return;
    }

    he_adjustment_index[index] = 0;
}

void gabc_det_notes_finish(void)
{
    gregorio_sign_orientation orientation;
    if (overbrace_var) {
        gregorio_message(_("unclosed variable over-staff brace"),
                "gabc_det_notes_finish", VERBOSITY_ERROR, 0);
        overbrace_var = 0;
    }
    if (underbrace_var) {
        gregorio_message(_("unclosed variable under-staff brace"),
                "gabc_det_notes_finish", VERBOSITY_ERROR, 0);
        underbrace_var = 0;
    }
    for (orientation = SO_OVER; orientation <= SO_UNDER; ++orientation) {
        const char *name = over_or_under(orientation);
        if (ledger_var[orientation]) {
            gregorio_messagef("gabc_det_notes_finish", VERBOSITY_ERROR, 0,
                    _("unclosed variable %s-staff ledger line"), name);
            ledger_var[orientation] = 0;
        }
        if (slur[orientation].var) {
            gregorio_messagef("gabc_det_notes_finish", VERBOSITY_ERROR, 0,
                    _("unclosed variable %s-note slur"), name);
            slur[orientation].var = 0;
            slur[orientation].shift = '\0';
            slur[orientation].start = NULL;
        }
        if (he_adjustment_index[orientation]) {
            gregorio_messagef("gabc_det_notes_finish", VERBOSITY_ERROR, 0,
                    _("unclosed horizontal %s-episema adjustment"),
                    over_or_under(orientation));
            ledger_var[orientation] = 0;
        }
    }
    if (left_bracket_texverb) {
        gregorio_message(_("unclosed left bracket"),
                "gabc_det_notes_finish", VERBOSITY_ERROR, 0);
        left_bracket_texverb = 0;
    }
}

%}

%option stack
%option 8bit
%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="gabc_notes_determination_"
%option noyywrap

%x texverbnote
%x texverbglyph
%x texverbelement
%x choralsign
%x choralnabc
%x alt
%x comments
%x overbrace
%x underbrace
%x overcurlybrace
%x overcurlyaccentusbrace
%x space
%x nbspace
%x overledger overledger2
%x underledger underledger2
%x endledger

%%
<INITIAL>\% {
        BEGIN(comments);
    }
<comments>(\n|\r)+ {
        BEGIN(INITIAL);
    }
<comments>[^\n\r]* {
        /* ignored */
    }
<INITIAL>\[cs: {
        BEGIN(choralsign);
    }
<INITIAL>\[cn: {
        BEGIN(choralnabc);
    }
<INITIAL>\[ob:[01]; {
        char_for_brace = gabc_notes_determination_text[4]-'0';
        BEGIN(overbrace);
    }
<INITIAL>\[ub:[01]; {
        char_for_brace = gabc_notes_determination_text[4]-'0';
        BEGIN(underbrace);
    }
<INITIAL>\[ocb:[01]; {
        char_for_brace = gabc_notes_determination_text[5]-'0';
        BEGIN(overcurlybrace);
    }
<INITIAL>\[ocba:[01]; {
        char_for_brace = gabc_notes_determination_text[6]-'0';
        BEGIN(overcurlyaccentusbrace);
    }
<INITIAL>\[ob:[01]\{\] {
        if (overbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace without termination of "
                                "previous overbrace"));
        } else {
            char_for_brace = gabc_notes_determination_text[4]-'0';
            overbrace_var = ++tex_position_id;
            overbrace_var_kind = "ob";
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{1}"
                    "\\GreOverBrace{\\GreVarBraceLength{%hu}}{0pt}{0pt}{%d}",
                    overbrace_var, char_for_brace, overbrace_var, char_for_brace);
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ub:[01]\{\] {
        if (underbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable underbrace without termination of "
                                "previous underbrace"));
        } else {
            char_for_brace = gabc_notes_determination_text[4]-'0';
            underbrace_var = ++tex_position_id;
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{1}"
                    "\\GreUnderBrace{\\GreVarBraceLength{%hu}}{0pt}{0pt}{%d}",
                    underbrace_var, char_for_brace, underbrace_var, char_for_brace);
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ocb:[01]\{\] {
        if (overbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace without termination of "
                                "previous overbrace"));
        } else {
            char_for_brace = gabc_notes_determination_text[5]-'0';
            overbrace_var = ++tex_position_id;
            overbrace_var_kind = "ocb";
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{1}"
                    "\\GreOverCurlyBrace{\\GreVarBraceLength{%hu}}{0pt}{0pt}{%d}{0}",
                    overbrace_var, char_for_brace, overbrace_var, char_for_brace);
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ocba:[01]\{\] {
        if (overbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace without termination of "
                                "previous overbrace"));
        } else {
            char_for_brace = gabc_notes_determination_text[6]-'0';
            overbrace_var = ++tex_position_id;
            overbrace_var_kind = "ocba";
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{1}"
                    "\\GreOverCurlyBrace{\\GreVarBraceLength{%hu}}{0pt}{0pt}{%d}{1}",
                    overbrace_var, char_for_brace, overbrace_var, char_for_brace);
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ob:[01]\}\] {
        if (!overbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace termination without "
                                "variable overbrace start"));
        } else if (strcmp (overbrace_var_kind, "ob")) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace started with %s "
                                "and terminated with ob"),
                              overbrace_var_kind);
        } else {
            char_for_brace = gabc_notes_determination_text[4]-'0';
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{2}", overbrace_var,
                    char_for_brace);
            overbrace_var = 0;
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ub:[01]\}\] {
        if (!underbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable underbrace termination without "
                                "variable underbrace start"));
        } else {
            char_for_brace = gabc_notes_determination_text[4]-'0';
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{2}", underbrace_var,
                    char_for_brace);
            underbrace_var = 0;
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ocb:[01]\}\] {
        if (!overbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace termination without "
                                "variable overbrace start"));
        } else if (strcmp (overbrace_var_kind, "ocb")) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace started with %s "
                                "and terminated with ocb"),
                              overbrace_var_kind);
        } else {
            char_for_brace = gabc_notes_determination_text[5]-'0';
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{2}", overbrace_var,
                    char_for_brace);
            overbrace_var = 0;
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[ocba:[01]\}\] {
        if (!overbrace_var) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace termination without "
                                "variable overbrace start"));
        } else if (strcmp (overbrace_var_kind, "ocba")) {
            gregorio_messagef("gabc_notes_determination", VERBOSITY_ERROR, 0,
                              _("variable overbrace started with %s "
                                "and terminated with ocba"),
                              overbrace_var_kind);
        } else {
            char_for_brace = gabc_notes_determination_text[6]-'0';
            gregorio_snprintf(tempstr, sizeof tempstr,
                    "\\GreVarBraceSavePos{%hu}{%d}{2}", overbrace_var,
                    char_for_brace);
            overbrace_var = 0;
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(tempstr));
        }
    }
<INITIAL>\[nm[1-9]\] {
        if (notesmacros[gabc_notes_determination_text[3]-'0']) {
            gregorio_add_texverb_to_note(current_note, gregorio_strdup(
                    notesmacros[gabc_notes_determination_text[3]-'0']));
        } else error();
    }
<INITIAL>\[gm[1-9]\] {
        if (notesmacros[gabc_notes_determination_text[3]-'0']) {
            gregorio_add_texverb_as_note(&current_note, gregorio_strdup(
                    notesmacros[gabc_notes_determination_text[3]-'0']),
                    GRE_TEXVERB_GLYPH, &notes_lloc);
        } else error();
    }
<INITIAL>\[em[1-9]\] {
        if (notesmacros[gabc_notes_determination_text[3]-'0']) {
            gregorio_add_texverb_as_note(&current_note, gregorio_strdup(
                    notesmacros[gabc_notes_determination_text[3]-'0']),
                    GRE_TEXVERB_ELEMENT, &notes_lloc);
        } else error();
    }
<INITIAL>\[altm[1-9]\] {
        if (notesmacros[gabc_notes_determination_text[5]-'0']) {
            gregorio_add_texverb_as_note(&current_note, gregorio_strdup(
                    notesmacros[gabc_notes_determination_text[5]-'0']),
                    GRE_TEXVERB_ELEMENT, &notes_lloc);
        } else error();
    }
<INITIAL>\[nv: {
        BEGIN(texverbnote);
    }
<INITIAL>\[gv: {
        BEGIN(texverbglyph);
    }
<INITIAL>\[ev: {
        BEGIN(texverbelement);
    }
<INITIAL>\[alt: {
        BEGIN(alt);
    }
<INITIAL><nlba> {
        gregorio_add_nlba_as_note(&current_note, NLBA_BEGINNING, &notes_lloc);
    }
<INITIAL><\/nlba> {
        gregorio_add_nlba_as_note(&current_note, NLBA_END, &notes_lloc);
    }
<INITIAL>@\[ {
        gregorio_start_autofuse(&current_note, &notes_lloc);
    }
<INITIAL>\] {
        gregorio_end_autofuse(&current_note, &notes_lloc);
    }
<overbrace>(\$.|[^\]])+ {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreOverBrace{%s}{0pt}{0pt}{%d}",
                gabc_notes_determination_text, char_for_brace);
        gregorio_add_texverb_to_note(current_note, gabc_unescape(tempstr));
    }
<underbrace>(\$.|[^\]])+ {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreUnderBrace{%s}{0pt}{0pt}{%d}",
                gabc_notes_determination_text, char_for_brace);
        gregorio_add_texverb_to_note(current_note, gabc_unescape(tempstr));
    }
<overcurlybrace>(\$.|[^\]])+ {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreOverCurlyBrace{%s}{0pt}{0pt}{%d}{0}",
                gabc_notes_determination_text, char_for_brace);
        gregorio_add_texverb_to_note(current_note, gabc_unescape(tempstr));
    }
<overcurlyaccentusbrace>(\$.|[^\]])+ {
        gregorio_snprintf(tempstr, sizeof tempstr,
                "\\GreOverCurlyBrace{%s}{0pt}{0pt}{%d}{1}",
                gabc_notes_determination_text, char_for_brace);
        gregorio_add_texverb_to_note(current_note, gabc_unescape(tempstr));
    }
<choralsign>(\$.|[^\]])+ {
        gregorio_add_cs_to_note(&current_note,
                gabc_unescape(gabc_notes_determination_text), false);
    }
<choralnabc>(\$.|[^\]])+ {
        gregorio_add_cs_to_note(&current_note,
                gabc_unescape(gabc_notes_determination_text), true);
    }
<texverbnote>(\$.|[^\]])+ {
        gregorio_add_texverb_to_note(current_note,
                gabc_unescape(gabc_notes_determination_text));
    }
<texverbglyph>(\$.|[^\]])+ {
        gregorio_add_texverb_as_note(&current_note,
                gabc_unescape(gabc_notes_determination_text),
                GRE_TEXVERB_GLYPH, &notes_lloc);
    }
<texverbelement>(\$.|[^\]])+ {
        gregorio_add_texverb_as_note(&current_note,
                gabc_unescape(gabc_notes_determination_text),
                GRE_TEXVERB_ELEMENT, &notes_lloc);
    }
<alt>(\$.|[^\]])+ {
        gregorio_add_texverb_as_note(&current_note,
                gabc_unescape(gabc_notes_determination_text), GRE_ALT,
                &notes_lloc);
    }
<INITIAL>\/\[ {
        BEGIN(space);
    }
<space>-?(\.[0-9]+|[0-9]+(\.[0-9]*)?)/\] {
        gregorio_add_space_as_note(&current_note, SP_AD_HOC_SPACE,
                gregorio_strdup(gabc_notes_determination_text), &notes_lloc);
    }
<INITIAL>!\/\[ {
        BEGIN(nbspace);
    }
<nbspace>-?(\.[0-9]+|[0-9]+(\.[0-9]*)?)/\] {
        gregorio_add_space_as_note(&current_note, SP_AD_HOC_SPACE_NB,
                gregorio_strdup(gabc_notes_determination_text), &notes_lloc);
    }
<INITIAL>\[oll:1\] {
        LEDGER(high, EXPLICITLY_DRAWN, true);
    }
<INITIAL>\[oll:0\] {
        LEDGER(high, EXPLICITLY_DRAWN, false);
    }
<INITIAL>\[oll:\}\] {
        end_variable_ledger(SO_OVER);
    }
<INITIAL>\[oll: {
        BEGIN(overledger);
    }
<overledger>[^;{]+ {
        save_before_ledger(gabc_notes_determination_text);
        BEGIN(overledger2);
    }
<overledger2>;[^\]]+ {
        add_static_ledger(SO_OVER, gabc_notes_determination_text);
        BEGIN(endledger);
    }
<overledger2>\{[^\]]+ {
        add_variable_ledger(SO_OVER, gabc_notes_determination_text);
        BEGIN(endledger);
    }
<INITIAL>\[ull:1\] {
        LEDGER(low, EXPLICITLY_DRAWN, true);
    }
<INITIAL>\[ull:0\] {
        LEDGER(low, EXPLICITLY_DRAWN, false);
    }
<INITIAL>\[ull:\}\] {
        end_variable_ledger(SO_UNDER);
    }
<INITIAL>\[ull: {
        BEGIN(underledger);
    }
<underledger>[^;{]+ {
        save_before_ledger(gabc_notes_determination_text);
        BEGIN(underledger2);
    }
<underledger2>;[^\]]+ {
        add_static_ledger(SO_UNDER, gabc_notes_determination_text);
        BEGIN(endledger);
    }
<underledger2>\{[^\]]+ {
        add_variable_ledger(SO_UNDER, gabc_notes_determination_text);
        BEGIN(endledger);
    }
<texverbnote,texverbglyph,texverbelement,choralsign,choralnabc,alt,overcurlyaccentusbrace,overcurlybrace,overbrace,underbrace,space,nbspace,endledger>\] {
        BEGIN(INITIAL);
    }
<INITIAL>\[[ou]slur:[012];[^,]+,[^\]]+\] {
        parse_slur();
    }
<INITIAL>\[[ou]slur:[012]\{\] {
        start_var_slur();
    }
<INITIAL>\[[ou]slur:[012]\}\] {
        end_var_slur();
    }
<INITIAL>\[[ou]h(?x:
  :(m|[ou]?[lh])\{?
| :(m|[ou]?[lh])?[+-][^\]\{]+\{?
| :?\{
)\] {
        parse_hepisema_adjustment();
    }
<INITIAL>\[[ou]h:?\}\] {
        end_hepisema_adjustment();
    }
<INITIAL>\[\[ {
        left_bracket();
    }
<INITIAL>\]\] {
        right_bracket();
    }
\{  {
        gregorio_add_texverb_as_note(&current_note,
                gregorio_strdup("\\hbox to 0pt{"), GRE_TEXVERB_ELEMENT,
                &notes_lloc);
    }
\}  {
        gregorio_add_texverb_as_note(&current_note,
                gregorio_strdup("\\hss%\n}%\n\\GreNoBreak\\relax "),
                GRE_TEXVERB_ELEMENT, &notes_lloc);
    }
[a-np]\+ {
        gregorio_add_manual_custos_as_note(&current_note,
                pitch_letter_to_height(gabc_notes_determination_text[0]),
                &notes_lloc);
    }
[\t\r\n]+ /* ignore ends of line and tabs */;
<INITIAL>\[nocustos\] {
        gregorio_add_suppress_custos_as_note(&current_note, &notes_lloc);
    }
z0  {
        gregorio_add_custos_as_note(&current_note, &notes_lloc);
    }
z   {
        gregorio_add_end_of_line_as_note(&current_note, false, false, false,
                &notes_lloc);
    }
z\+ {
        gregorio_add_end_of_line_as_note(&current_note, false, true, true,
                &notes_lloc);
    }
z-  {
        gregorio_add_end_of_line_as_note(&current_note, false, true, false,
                &notes_lloc);
    }
Z   {
        gregorio_add_end_of_line_as_note(&current_note, true, false, false,
                &notes_lloc);
    }
Z\+ {
        gregorio_add_end_of_line_as_note(&current_note, true, true, true,
                &notes_lloc);
    }
Z-  {
        gregorio_add_end_of_line_as_note(&current_note, true, true, false,
                &notes_lloc);
    }
[cf][1-5] {
        gregorio_add_clef_as_note(&current_note,
                letter_to_clef(gabc_notes_determination_text[0]),
                parse_clef_line(gabc_notes_determination_text[1]), false,
                &notes_lloc);
    }
[cf]b[1-5] {
        gregorio_add_clef_as_note(&current_note,
                letter_to_clef(gabc_notes_determination_text[0]),
                parse_clef_line(gabc_notes_determination_text[2]), true,
                &notes_lloc);
    }
@[cf][1-5] {
        gregorio_add_secondary_clef_to_note(current_note,
                letter_to_clef(gabc_notes_determination_text[1]),
                parse_clef_line(gabc_notes_determination_text[2]), false);
    }
@[cf]b[1-5] {
        gregorio_add_secondary_clef_to_note(current_note,
                letter_to_clef(gabc_notes_determination_text[1]),
                parse_clef_line(gabc_notes_determination_text[3]), true);
    }
`   {
        add_bar_as_note(B_VIRGULA);
    }
`0  {
        add_bar_as_note(B_VIRGULA_HIGH);
    }
`\? {
        add_bar_as_note(B_VIRGULA_PAREN);
    }
`0\?|`\?0 {
        add_bar_as_note(B_VIRGULA_PAREN_HIGH);
    }
\^  {
        add_bar_as_note(B_DIVISIO_MINIMIS);
    }
\^0 {
        add_bar_as_note(B_DIVISIO_MINIMIS_HIGH);
    }
,   {
        add_bar_as_note(B_DIVISIO_MINIMA);
    }
,0  {
        add_bar_as_note(B_DIVISIO_MINIMA_HIGH);
    }
,\? {
        add_bar_as_note(B_DIVISIO_MINIMA_PAREN);
    }
,0\?|,\?0 {
        add_bar_as_note(B_DIVISIO_MINIMA_PAREN_HIGH);
    }
[,;][1-8] {
        add_bar_as_note(parse_dominican_bar(gabc_notes_determination_text[1]));
    }
;   {
        add_bar_as_note(B_DIVISIO_MINOR);
    }
:   {
        add_bar_as_note(B_DIVISIO_MAIOR);
    }
::  {
        add_bar_as_note(B_DIVISIO_FINALIS);
    }
:\? {
        add_bar_as_note(B_DIVISIO_MAIOR_DOTTED);
    }
r   {
        gregorio_add_cavum(current_note);
    }
R   {
        gregorio_change_shape(current_note, S_LINEA_PUNCTUM,
                legacy_oriscus_orientation);
    }
r0  {
        gregorio_change_shape(current_note, S_LINEA_PUNCTUM,
                legacy_oriscus_orientation);
        gregorio_add_cavum(current_note);
    }
r1  {
        gregorio_add_special_sign(current_note, _ACCENTUS);
    }
r2  {
        gregorio_add_special_sign(current_note, _ACCENTUS_REVERSUS);
    }
r3  {
        gregorio_add_special_sign(current_note, _CIRCULUS);
    }
r4  {
        gregorio_add_special_sign(current_note, _SEMI_CIRCULUS);
    }
r5  {
        gregorio_add_special_sign(current_note, _SEMI_CIRCULUS_REVERSUS);
    }
r6  {
        gregorio_add_special_sign(current_note, _MUSICA_FICTA_FLAT);
    }
r7  {
        gregorio_add_special_sign(current_note, _MUSICA_FICTA_NATURAL);
    }
r8  {
        gregorio_add_special_sign(current_note, _MUSICA_FICTA_SHARP);
    }
x   {
        gregorio_change_shape(current_note, S_FLAT,
                legacy_oriscus_orientation);
    }
x\? {
        gregorio_change_shape(current_note, S_FLAT_PAREN,
                legacy_oriscus_orientation);
    }
#   {
        gregorio_change_shape(current_note, S_SHARP,
                legacy_oriscus_orientation);
    }
#\? {
        gregorio_change_shape(current_note, S_SHARP_PAREN,
                legacy_oriscus_orientation);
    }
y   {
        gregorio_change_shape(current_note, S_NATURAL,
                legacy_oriscus_orientation);
    }
y\? {
        gregorio_change_shape(current_note, S_NATURAL_PAREN,
                legacy_oriscus_orientation);
    }
!?\/0 {
        gregorio_add_space_as_note(&current_note, SP_HALF_SPACE, NULL,
                &notes_lloc);
    }
!?\/! {
        gregorio_add_space_as_note(&current_note, SP_INTERGLYPH_SPACE, NULL,
                &notes_lloc);
    }
\/  {
        gregorio_add_space_as_note(&current_note, SP_NEUMATIC_CUT, NULL,
                &notes_lloc);
    }
\//\/\[ {
        gregorio_add_space_as_note(&current_note, SP_NEUMATIC_CUT, NULL,
                &notes_lloc);
    }
\/\/ {
        gregorio_add_space_as_note(&current_note, SP_LARGER_SPACE, NULL,
                &notes_lloc);
    }
\   {
        gregorio_add_space_as_note(&current_note, SP_GLYPH_SPACE, NULL,
                &notes_lloc);
    }
!\/ {
        gregorio_add_space_as_note(&current_note, SP_NEUMATIC_CUT_NB, NULL,
                &notes_lloc);
    }
!\//\/\[ {
        gregorio_add_space_as_note(&current_note, SP_NEUMATIC_CUT_NB, NULL,
                &notes_lloc);
    }
!\/\/ {
        gregorio_add_space_as_note(&current_note, SP_LARGER_SPACE_NB, NULL,
                &notes_lloc);
    }
!\  {
        gregorio_add_space_as_note(&current_note, SP_GLYPH_SPACE_NB, NULL,
                &notes_lloc);
    }
!/[^\/ ] {
        gregorio_add_space_as_note(&current_note, SP_ZERO_WIDTH, NULL,
                &notes_lloc);
    }
=   {
        gregorio_change_shape(current_note, S_LINEA,
                legacy_oriscus_orientation);
    }
[a-npA-NP]vv {
        lex_add_note(0, S_BIVIRGA, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
[a-npA-NP]vvv {
        lex_add_note(0, S_TRIVIRGA, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
[a-npA-NP]VV {
        lex_add_note(0, S_BIVIRGA, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
[a-npA-NP]VVV {
        lex_add_note(0, S_TRIVIRGA, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
[a-npA-NP]ss {
        lex_add_note(0, S_DISTROPHA, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
[a-npA-NP]ss(\<|\>) {
        lex_add_note(0, S_DISTROPHA, _NO_SIGN, L_AUCTUS_ASCENDENS);
    }
[a-npA-NP]sss {
        lex_add_note(0, S_TRISTROPHA, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
[a-npA-NP]sss(\<|\>) {
        lex_add_note(0, S_TRISTROPHA, _NO_SIGN, L_AUCTUS_ASCENDENS);
    }
[a-np] {
        lex_add_note(0, S_PUNCTUM, _NO_SIGN, L_NO_LIQUESCENTIA);
    }
-[a-np] {
        lex_add_note(1, S_PUNCTUM, _NO_SIGN, L_INITIO_DEBILIS);
    }
@[a-np] {
        lex_add_note(1, S_PUNCTUM, _NO_SIGN, L_FUSED);
    }
[A-NP][012]? {
        lex_add_note(0, punctum_inclinatum(gabc_notes_determination_text[1]),
                _NO_SIGN, L_NO_LIQUESCENTIA);
    }
-[A-NP][012]? {
        lex_add_note(1, punctum_inclinatum(gabc_notes_determination_text[2]),
                _NO_SIGN, L_INITIO_DEBILIS);
    }
@[A-NP][012]? {
        lex_add_note(1, punctum_inclinatum(gabc_notes_determination_text[2]),
                _NO_SIGN, L_FUSED);
    }
\'[01]? {
        add_sign(_V_EPISEMA);
    }
_[0-5]* {
        add_h_episema();
    }
\.[01]? {
        add_sign(_PUNCTUM_MORA);
    }
~   {
        gregorio_add_tail_liquescentia(current_note, L_DEMINUTUS,
                legacy_oriscus_orientation);
    }
>   {
        gregorio_add_tail_liquescentia(current_note, L_AUCTUS_DESCENDENS,
                legacy_oriscus_orientation);
    }
\<  {
        gregorio_add_tail_liquescentia(current_note, L_AUCTUS_ASCENDENS,
                legacy_oriscus_orientation);
    }
q   {
        gregorio_change_shape(current_note, S_QUADRATUM,
                legacy_oriscus_orientation);
    }
o   {
        gregorio_change_shape(current_note, S_ORISCUS_UNDETERMINED,
                legacy_oriscus_orientation);
    }
o0  {
        gregorio_change_shape(current_note, S_ORISCUS_DESCENDENS,
                legacy_oriscus_orientation);
    }
o1  {
        gregorio_change_shape(current_note, S_ORISCUS_ASCENDENS,
                legacy_oriscus_orientation);
    }
O   {
        gregorio_change_shape(current_note, S_ORISCUS_SCAPUS_UNDETERMINED,
                legacy_oriscus_orientation);
    }
O0  {
        gregorio_change_shape(current_note, S_ORISCUS_SCAPUS_DESCENDENS,
                legacy_oriscus_orientation);
    }
O1  {
        gregorio_change_shape(current_note, S_ORISCUS_SCAPUS_ASCENDENS,
                legacy_oriscus_orientation);
    }
w   {
        gregorio_change_shape(current_note, S_QUILISMA,
                legacy_oriscus_orientation);
    }
W   {
        gregorio_change_shape(current_note, S_QUILISMA_QUADRATUM,
                legacy_oriscus_orientation);
    }
v   {
        gregorio_change_shape(current_note, S_VIRGA,
                legacy_oriscus_orientation);
    }
V   {
        gregorio_change_shape(current_note, S_VIRGA_REVERSA,
                legacy_oriscus_orientation);
    }
s   {
        gregorio_change_shape(current_note, S_STROPHA,
                legacy_oriscus_orientation);
    }
\[hl:1\] {
        LEDGER(high, EXPLICIT, true);
    }
\[hl:0\] {
        LEDGER(high, EXPLICIT, false);
    }
\[ll:1\] {
        LEDGER(low, EXPLICIT, true);
    }
\[ll:0\] {
        LEDGER(low, EXPLICIT, false);
    }
.|\n {
        gregorio_messagef("det_notes_from_string", VERBOSITY_ERROR, 0,
                _("unrecognized character: \"%c\""),
                gabc_notes_determination_text[0]);
    }

%%

gregorio_note *gabc_det_notes_from_string(char *str, char *newmacros[10],
        gregorio_scanner_location *loc, const gregorio_score *const score)
{
    int i;
    YY_BUFFER_STATE buf;

    notes_lloc.first_line = loc->first_line;
    notes_lloc.first_column = loc->first_column;
    notes_lloc.first_offset = loc->first_offset;
    /* yes... I do mean to set values from loc->first_* */
    notes_lloc.last_line = loc->first_line;
    notes_lloc.last_column = loc->first_column;
    notes_lloc.last_offset = loc->first_offset;
    notes_lloc.generate_point_and_click = loc->generate_point_and_click;

    staff_lines = score->staff_lines;
    highest_pitch = score->highest_pitch;
    high_ledger_line_pitch = score->high_ledger_line_pitch;
    legacy_oriscus_orientation = score->legacy_oriscus_orientation;

    /* a small optimization could uccur here: we could do it only once at the
     * beginning of the score, not at each syllable */
    for (i = 0; i < 10; i++) {
        notesmacros[i] = newmacros[i];
    }
    nbof_isolated_episema = 0;
    current_note = NULL;
    buf = yy_scan_string(str);
    yylex();
    yy_flush_buffer(buf);
    yy_delete_buffer(buf);
    gregorio_go_to_first_note(&current_note);
    return current_note;
}