File: parser.cpp

package info (click to toggle)
musescore-snapshot 3.2.s20190704%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 218,116 kB
  • sloc: cpp: 290,563; xml: 200,238; sh: 3,706; ansic: 1,447; python: 393; makefile: 222; perl: 82; pascal: 79
file content (917 lines) | stat: -rw-r--r-- 24,939 bytes parent folder | download | duplicates (4)
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
//=============================================================================
//  BWW to MusicXML converter
//  Part of MusE Score
//  Linux Music Score Editor
//
//  Copyright (C) 2010 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  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, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

/**
 \file
 A simple parser for bww files. The file header is handled one line at a time,
 until a line starting with "&" is found. The parser then builds measures from
 uinterrupted sequences of notes.
 */

// #include <iostream>

#include <QtCore/QStringList>
#include <QtCore/QtDebug>
#include <QtCore/QMap>

#include "lexer.h"
#include "parser.h"
#include "writer.h"

// Duration of a whole measure in ticks
static const int WHOLE_MEASURE_DURATION = 192;

/**
 Determine if symbol is part of a note sequence
 */

static bool isNote(Bww::Symbol sym)
{
  return (sym == Bww::NOTE
          || sym == Bww::TIE
          || sym == Bww::TRIPLET
          || sym == Bww::GRACE);
}

/**
 Determine if symbol is part of a non-note sequence
 */

static bool isNonNote(Bww::Symbol sym)
{
  return (sym == Bww::CLEF
          || sym == Bww::KEY
          || sym == Bww::TSIG
          || sym == Bww::PART
          || sym == Bww::BAR);
}

/**
 Dump beams
 */

static void dumpBeams(QList<Bww::MeasureDescription> const& measures)
{
  for (int j = 0; j < measures.size(); ++j)
  {
    QString beams;
    QString beamStates;
    QVector<QString> beamList(3);
    for (int i = 0; i < measures.at(j).notes.size(); ++i)
    {
      QString beam = measures.at(j).notes.at(i).beam;
      if (beam == "")
        beam = " ";

      beams += beam;
      switch (measures.at(j).notes.at(i).beamState)
      {
      case Bww::ST_NONE:     beamStates += " "; break;
      case Bww::ST_START:    beamStates += "["; break;
      case Bww::ST_CONTINUE: beamStates += "_"; break;
      case Bww::ST_STOP:     beamStates += "]"; break;
      default:               beamStates += " ";
      }
      for (int k = 0; k < 3; k++)
        switch (measures.at(j).notes.at(i).beamList.at(k))
        {
        case Bww::BM_NONE:          beamList[k] += " "; break;
        case Bww::BM_BEGIN:         beamList[k] += "b"; break;
        case Bww::BM_CONTINUE:      beamList[k] += "c"; break;
        case Bww::BM_END:           beamList[k] += "e"; break;
        case Bww::BM_FORWARD_HOOK:  beamList[k] += ">"; break;
        case Bww::BM_BACKWARD_HOOK: beamList[k] += "<"; break;
        default:                    beamList[k] += "?";
        }
    }
    qDebug() << "beams measure #" << j + 1 << beams;
    qDebug() << "beams measure #" << j + 1 << beamStates;
    for (int k = 0; k < 3; k++)
      qDebug() << "beams measure #" << j + 1 << beamList.at(k);
  }
}

/**
 Dump measure contents
 */

static void dumpMeasures(QList<Bww::MeasureDescription> const& measures)
{
  qDebug() << "dumpMeasures #measures" << measures.size()
      ;
  for (int j = 0; j < measures.size(); ++j)
  {
    qDebug() << "measure #" << j + 1;
    qDebug() << "Measure contents:";
    qDebug() << "mbf:"
        << "repeatBegin" << measures.at(j).mbf.repeatBegin
        << "endingFirst" << measures.at(j).mbf.endingFirst
        << "endingSecond" << measures.at(j).mbf.endingSecond
        << "firstOfSystem" << measures.at(j).mbf.firstOfSystem
        << "irregular" << measures.at(j).mbf.irregular
        ;
    for (int i = 0; i < measures.at(j).notes.size(); ++i)
    {
      qDebug()
          << measures.at(j).notes.at(i).pitch
          << measures.at(j).notes.at(i).beam
          << measures.at(j).notes.at(i).type
          << measures.at(j).notes.at(i).dots
          << measures.at(j).notes.at(i).tieStart
          << measures.at(j).notes.at(i).tieStop
          << measures.at(j).notes.at(i).triplet
          << measures.at(j).notes.at(i).grace
          ;
    }
    qDebug() << "mef:"
        << "repeatEnd" << measures.at(j).mef.repeatEnd
        << "endingEnd" << measures.at(j).mef.endingEnd
        << "lastOfSystem" << measures.at(j).mef.lastOfSystem
        << "lastOfPart" << measures.at(j).mef.lastOfPart
        ;
    qDebug() << "duration:" << measures.at(j).duration;
  }
}

/**
 Calculate measure durations
 */

static void calculateMeasureDurations(QList<Bww::MeasureDescription> & measures)
{
  for (int j = 0; j < measures.size(); ++j)
  {
    int measureDuration = 0;
    for (int i = 0; i < measures.at(j).notes.size(); ++i)
    {
      int ticks = WHOLE_MEASURE_DURATION / measures.at(j).notes.at(i).type.toInt();
      if (measures.at(j).notes.at(i).dots) ticks = 3 * ticks / 2;
      if (measures.at(j).notes.at(i).triplet != Bww::ST_NONE) ticks = 2 * ticks / 3;
      if (measures.at(j).notes.at(i).grace) ticks = 0; // grace notes don't count
      measureDuration += ticks;
      qDebug()
          << measures.at(j).notes.at(i).pitch
          << measures.at(j).notes.at(i).beam
          << measures.at(j).notes.at(i).type
          << measures.at(j).notes.at(i).dots
          << measures.at(j).notes.at(i).tieStart
          << measures.at(j).notes.at(i).tieStop
          << measures.at(j).notes.at(i).triplet
          << measures.at(j).notes.at(i).grace
          << "->" << ticks
          ;
    }
    qDebug() << "measureDuration:" << measureDuration;
    measures[j].duration = measureDuration;
  }
}

/**
 Determine time signature
 */

static void determineTimesig(QList<Bww::MeasureDescription> const& measures, int & beats, int & beat)
{
  QMap<int, int> map;
  for (int j = 0; j < measures.size(); ++j)
  {
    int dur = measures[j].duration;
    if (map.contains(dur))
      map[dur]++;
    else
      map.insert(dur, 1);
  }
  // determine most common duration
  int commonDur = 0;
  int max = 0;
  QMap<int, int>::const_iterator i = map.constBegin();
  while (i != map.constEnd())
  {
    qDebug() << "measureDurations:" << i.key() << i.value() << endl;
    if (i.value() > max)
    {
      commonDur = i.key();
      max = i.value();
    }
    ++i;
  }
  qDebug() << "measureDuration commonDur:" << commonDur << "max:" << max;
  // determine time signature
  beat = 4;
  beats = 0;
  int divisor = WHOLE_MEASURE_DURATION / 4;
  for (; beat < 64; beat *= 2, divisor /= 2)
  {
    if ((commonDur % divisor) == 0)
    {
      beats = commonDur / divisor;
      qDebug()
          << "measureDuration found beat:" << beat
          << "beats:" << beats
          << "divisor:" << divisor
          ;
      return;
    }
  }
  // could not determine time signature, set default
  beat = 4;
  beats = 4;
}

/**
 Find irregular measures
 */

static void findIrregularMeasures(QList<Bww::MeasureDescription> & measures, int beats, int beat)
{
  qDebug() << "findIrregularMeasures" << measures.size()
      << "beats" << beats
      << "beat" << beat
      ;

  int normalDuration = WHOLE_MEASURE_DURATION * beats / beat;

  // need at least one measure
  if (measures.size() == 0) return;

  // if the first measure is shorter that normal, it is irregular
  if (measures.at(0).duration < normalDuration) measures[0].mbf.irregular = true;

  for (int j = 1; j < measures.size(); ++j)
  {
    // the second measure of a pair where the sum of their duration adds up
    // to the normal duration is also irregular
    const int d1 = measures.at(j - 1).duration;
    const int d2 = measures.at(j).duration;
    if (d1 > 0 && d2 > 0 && (d1 + d2) == normalDuration)
      measures[j].mbf.irregular = true;
  }
}

/**
 Set mef.lastOfPart flag on last measure
 */

static void setLastOfPart(QList<Bww::MeasureDescription> & measures)
{
  qDebug() << "dumpMeasures #measures" << measures.size()
      ;

  // need at least one measure
  if (measures.size() == 0) return;

  // set lastOfPart flag on last measure
  int j = measures.size() - 1;
  measures[j].mef.lastOfPart = true;
}

static QString findNextNextNoteBeam(QList<Bww::MeasureDescription> const& measures, int measureNr, int noteNr)
{
  for (int i = noteNr + 1; i < measures.at(measureNr).notes.size(); ++i)
  {
    if (measures.at(measureNr).notes.at(i).grace)
      // ignore grace notes
      continue;
    return measures.at(measureNr).notes.at(i).beam;
  }
  return " "; // no next non-grace note found
}

static int type2beams(QString type)
{
  if (type == "8") return 1;
  else if (type == "16") return 2;
  else if (type == "32") return 3;
  else return 0;
}

static void calculateHigherBeamStates(Bww::MeasureDescription & m)
{
  qDebug() << "calculateHigherBeamStates";
  for (int i = 0; i < m.notes.size(); ++i)
  {
    qDebug()
        << m.notes.at(i).pitch
        << m.notes.at(i).beam
        << m.notes.at(i).type
        << m.notes.at(i).dots
        << m.notes.at(i).tieStart
        << m.notes.at(i).tieStop
        << m.notes.at(i).triplet
        << m.notes.at(i).grace
        ;

    if (m.notes.at(i).grace)
    {
      // just copy beamList[0] into beamList[1] and beamList[2]
      m.notes[i].beamList[1] = m.notes.at(i).beamList[0];
      m.notes[i].beamList[2] = m.notes.at(i).beamList[0];
    }
    else
    {
      int blp = -1; // beam level previous chord
      int blc = -1; // beam level current chord
      int bln = -1; // beam level next chord

      // find beam level current note
      blc = type2beams(m.notes.at(i).type);
      if (blc == 0)
        continue; // note does not have a beam

      // find beam level previous note
      if (m.notes.at(i).beamList[0] == Bww::BM_CONTINUE
          || m.notes.at(i).beamList[0] == Bww::BM_END)
      {
        for (int j = i - 1; blp == -1 && j >= 0; --j)
        {
          if (m.notes.at(j).grace)
            continue; // ignore grace notes
          blp = type2beams(m.notes.at(j).type);
        }
      }

      // find beam level next note
      if (m.notes.at(i).beamList[0] == Bww::BM_BEGIN
          || m.notes.at(i).beamList[0] == Bww::BM_CONTINUE)
      {
        for (int j = i + 1; bln == -1 && j < m.notes.size(); ++j)
        {
          if (m.notes.at(j).grace)
            continue; // ignore grace notes
          bln = type2beams(m.notes.at(j).type);
        }
      }

      qDebug()
          << "blp" << blp
          << "blc" << blc
          << "bln" << bln;
      for (int j = 2; j <= blc; ++j)
      {
        Bww::BeamType bt = Bww::BM_NONE;
        if (blp < j && bln >= j) bt = Bww::BM_BEGIN;
        else if (blp < j && bln < j) {
          if (bln > 0) bt = Bww::BM_FORWARD_HOOK;
          else if (blp > 0) bt = Bww::BM_BACKWARD_HOOK;
        }
        else if (blp >= j && bln < j) bt = Bww::BM_END;
        else if (blp >= j && bln >= j) bt = Bww::BM_CONTINUE;
        m.notes[i].beamList[j - 1] = bt;
        qDebug() << "beamList" << j - 1 << "=" << bt;
      }
    } // else
  } // for (int i = 0; i < m.notes.size(); ++i)
}

/**
 Determine all beam states.
 First for normal notes convert "r" and "l" in notes.beam into BM_BEGIN,
 BM_CONTINUE and BM_END in notes.beamList[0]. For grace notes "b", "c" and
 "e" can be directly converted into the corresponding beam states in
 notes.beamList[0].
 Then calculate the higher level beams.
 */

static void determineBeamStates(QList<Bww::MeasureDescription> & measures)
{
  enum State { NONE, LEFT, RIGHT };
  for (int j = 0; j < measures.size(); ++j)
  {
    State state = NONE;
    for (int i = 0; i < measures.at(j).notes.size(); ++i)
    {
      QString beam = measures.at(j).notes.at(i).beam;
      // handle normal notes
      if (beam == "")
      {
        measures[j].notes[i].beamState = Bww::ST_NONE;
        measures[j].notes[i].beamList[0] = Bww::BM_NONE;
        state = NONE;
      }
      else if (beam == "r")
      {
        if (state == NONE)
        {
          measures[j].notes[i].beamState = Bww::ST_START;
          measures[j].notes[i].beamList[0] = Bww::BM_BEGIN;
          state = LEFT; // now in left part of beam
        }
        else if (state == LEFT)
        {
          measures[j].notes[i].beamState = Bww::ST_CONTINUE;
          measures[j].notes[i].beamList[0] = Bww::BM_CONTINUE;
        }
        else if (state == RIGHT)
        {
          // shouldn't happen TODO report (internal?) error
        }
      }
      else if (beam == "l")
      {
        if (state == NONE)
        {
          // shouldn't happen TODO report error
        }
        else if (state == LEFT || state == RIGHT)
        {
          // if the beam does not end here (next note has beam "l")
          // then beamState is CONTINUE else STOP
          if (findNextNextNoteBeam(measures, j, i) == "l")
          {
            measures[j].notes[i].beamState = Bww::ST_CONTINUE;
            measures[j].notes[i].beamList[0] = Bww::BM_CONTINUE;
            state = RIGHT; // now in right part of beam
          }
          else
          {
            measures[j].notes[i].beamState = Bww::ST_STOP;
            measures[j].notes[i].beamList[0] = Bww::BM_END;
            state = NONE; // now in right part of beam
          }
        }
      }
      // handle grace notes
      else if (beam == "b") measures[j].notes[i].beamList[0] = Bww::BM_BEGIN;
      else if (beam == "c") measures[j].notes[i].beamList[0] = Bww::BM_CONTINUE;
      else if (beam == "e") measures[j].notes[i].beamList[0] = Bww::BM_END;
    }
    calculateHigherBeamStates(measures[j]);
  }
}

namespace Bww {

  /**
   Parser constructor, using Lexer \a l and Writer \a w.
   */

  Parser::Parser(Lexer& l, Writer& w)
    : lex(l),
    wrt(w),
    tempo(0),
    //inMeasure(false),
    //measureNr(0),
    tieStart(false),
    inTie(false),
    tripletStart(false),
    inTriplet(false),
    tsigFound(false)
  {
    qDebug() << "Parser::Parser()";
  }

  /**
   Parse the input stream and write result.
   */

  void Parser::parse()
  {
    // read the header, handling only strings and tune tempo
    while (lex.symType() == COMMENT
           || lex.symType() == STRING
           || lex.symType() == TEMPO)
    {
      if (lex.symType() == STRING)
        parseString();
      else if (lex.symType() == TEMPO)
        parseTempo();
      else if (lex.symType() == COMMENT)
        lex.getSym();
    }
    qDebug() << "Parser::parse()"
        << "title:" << title
        << "type:" << type
        << "composer:" << composer
        << "footer:" << footer
        ;
    wrt.header(title, type, composer, footer, tempo);

    // read the actual music
    if (lex.symType() != CLEF)
      errorHandler("clef ('&') expected");
    while (lex.symType() != NONE)
    {
      if (isNonNote(lex.symType()))
        parseSeqNonNotes();
      else if (isNote(lex.symType()))
        parseSeqNotes();
      else if (lex.symType() == UNKNOWN)
      {
        errorHandler("unknown symbol '" + lex.symValue() + "'");
        lex.getSym();
      }
      else
      {
        ; // others not implemented yet: silently ignored
        lex.getSym();
      }
    }

    qDebug() << "Parser::parse() finished, #measures" << measures.size()
        ;

    calculateMeasureDurations(measures);
    if (!tsigFound)
    {
      determineTimesig(measures, beats, beat);
      wrt.tsig(beats, beat);
    }
    findIrregularMeasures(measures, beats, beat);
    determineBeamStates(measures);
    setLastOfPart(measures);
    dumpMeasures(measures);
    dumpBeams(measures);

    for (int j = 0; j < measures.size(); ++j)
    {
      wrt.beginMeasure(measures.at(j).mbf);
      for (int i = 0; i < measures.at(j).notes.size(); ++i)
      {
        wrt.note(measures.at(j).notes.at(i).pitch,
                 measures.at(j).notes.at(i).beamList,
                 measures.at(j).notes.at(i).type,
                 measures.at(j).notes.at(i).dots,
                 measures.at(j).notes.at(i).tieStart,
                 measures.at(j).notes.at(i).tieStop,
                 measures.at(j).notes.at(i).triplet,
                 measures.at(j).notes.at(i).grace
                 );
      }
      wrt.endMeasure(measures.at(j).mef);
    }

    // trailer
    wrt.trailer();
  }

  /**
   Display error \a s.
   */

  void Parser::errorHandler(QString /*s*/)
  {
#if 0 // WS
    std::cerr << "Parse error line "
        << lex.symLineNumber() + 1
        << ": "
        << qPrintable(s)
        << std::endl;
#endif
  }

  /**
   Parse a bww bar symbol.
   */

  void Parser::parseBar(Bww::MeasureEndFlags& mef)
  {
    qDebug() << "Parser::parseBar() value:" << qPrintable(lex.symValue());
    if (lex.symValue() == "!!t") mef.doubleBarLine = true;
    lex.getSym();
  }

  /**
   Parse a bww note.
   */

  void Parser::parseNote()
  {
    qDebug() << "Parser::parseNote() value:" << qPrintable(lex.symValue());

    QRegExp rNotes("(LG|LA|[B-F]|HG|HA)([lr]?)_(1|2|4|8|16|32)");

    QStringList caps;
    if (rNotes.exactMatch(lex.symValue()))
    {
      caps = rNotes.capturedTexts();
      qDebug() << " match" << caps.size();
      if (caps.size() == 4)
      {
        qDebug()
            << "caps[1]" << caps.at(1)
            << "caps[2]" << caps.at(2)
            << "caps[3]" << caps.at(3)
            ;
      }
    }
    lex.getSym();

    int dots = 0;
    bool tieStop = false;
    if (tieStart) inTie = true;
    bool tripletStop = false;
    if (tripletStart) inTriplet = true;
    if (lex.symType() == DOT)
    {
      qDebug() << " dot" << qPrintable(lex.symValue());
      ++dots;
      lex.getSym();
    }
    else if (lex.symType() == TIE)
    {
      qDebug() << " tie" << qPrintable(lex.symValue());
      if (lex.symValue() == "^ts")
      {
        if (inTie) errorHandler("tie start ('^ts') unexpected");
      }
      else
      {
        if (!inTie) errorHandler("tie end ('^te') unexpected");
        else
        {
          tieStop = true;
          inTie = false;
          lex.getSym();
        }
      }
    }
    else if (lex.symType() == TRIPLET)
    {
      qDebug() << " triplet" << qPrintable(lex.symValue());
      if (lex.symValue() == "^3s")
      {
        if (inTriplet) errorHandler("triplet start ('^3s') unexpected");
      }
      else
      {
        if (!inTriplet) errorHandler("triplet end ('^3e') unexpected");
        else
        {
          tripletStop = true;
        }
      }
      lex.getSym();
    }

    StartStop triplet = ST_NONE;
    if (inTriplet)
    {
      if (tripletStart) triplet = ST_START;
      else if (tripletStop) triplet = ST_STOP;
      else triplet = ST_CONTINUE;
    }
    qDebug() << " tie start" << tieStart << " tie stop" << tieStop;
    qDebug() << " triplet start" << tripletStart << " triplet stop" << tripletStop;
    NoteDescription noteDesc(caps[1], caps[2], caps[3], dots, tieStart, tieStop, triplet);

    if (measures.isEmpty())
    {
      errorHandler("cannot append note: no measure");
    }
    else
    {
      measures.last().notes.append(noteDesc);
    }
    tieStart = false;
    tripletStart = false;
    if (tripletStop)
    {
      inTriplet = false;
      tripletStop = false;
    }
  }

  /**
   Determine beam for grace note \a index in a group of \a size grace notes.
   */

  static QString graceBeam(const int size, const int index)
  {
    if (size <= 1) return " ";                // no beam
    if (index < 0) return " ";                // no beam (but should not happen)
    if (index == 0) return "b";               // begin
    else if (index == (size - 1)) return "e"; // end
    else if (index >= size) return " ";       // no beam (but should not happen)
    else return "c";                          // continue
  }

  /**
   Parse a bww embellishment.
   */

  void Parser::parseGraces()
  {
    qDebug() << "Parser::parseGraces() value:" << qPrintable(lex.symValue());

    const QString c_type = "32";
    const int dots = 0;
    QStringList graces = lex.symValue().split(" ");
    for (int i = 0; i < graces.size(); ++i)
    {
      const QString beam = graceBeam(graces.size(), i);
      NoteDescription noteDesc(graces.at(i), beam, c_type, dots, false, false, ST_NONE, true);
      if (measures.isEmpty())
      {
        errorHandler("cannot append note: no measure");
      }
      else
      {
        measures.last().notes.append(noteDesc);
      }
    }
    lex.getSym();
  }

  /**
   Parse a bww part symbol.
   */

  void Parser::parsePart(Bww::MeasureBeginFlags& mbf, Bww::MeasureEndFlags& mef)
  {
    qDebug() << "Parser::parsePart() value:" << qPrintable(lex.symValue());
    if (lex.symValue() == "I!''")
    {
      mbf.repeatBegin = true;
    }
    else if (lex.symValue() == "'1")
    {
      mbf.endingFirst = true;
    }
    else if (lex.symValue() == "'2")
    {
      mbf.endingSecond = true;
    }
    else if (lex.symValue() == "''!I")
    {
      mef.repeatEnd = true;
    }
    else if (lex.symValue() == "_'")
    {
      mef.endingEnd = true;
    }
    else
    {
      ; // other silently ignored
    }
    lex.getSym();
  }

  /**
   Parse a sequence of non-notes.
   */

  void Parser::parseSeqNonNotes()
  {
    qDebug() << "Parser::parseSeqNonNotes() value:" << qPrintable(lex.symValue());
    MeasureBeginFlags mbfl;
    MeasureEndFlags mefl;
    while (isNonNote(lex.symType()))
    {
      if (lex.symType() == CLEF)
      {
        mbfl.firstOfSystem = true;
        mefl.lastOfSystem = true;
        lex.getSym();
      }
      else if (lex.symType() == KEY)
        lex.getSym(); // ignore
      else if (lex.symType() == TSIG)
        parseTSig();
      else if (lex.symType() == PART)
        parsePart(mbfl, mefl);
      else if (lex.symType() == BAR)
        parseBar(mefl);
    }
    // First end the previous measure
    if (!measures.isEmpty())
    {
      measures.last().mef = mefl;
    }
    // Then start a new measure, if necessary
    if (isNote(lex.symType()))
    {
      MeasureDescription md;
      md.mbf = mbfl;
      measures.append(md);
    }
  }

  /**
   Parse a sequence of notes.
   Includes handling ties and triplets, but without extensive error checking.
   May break on invalid input.
   */

  void Parser::parseSeqNotes()
  {
    qDebug() << "Parser::parseSeqNotes() value:" << qPrintable(lex.symValue());
    while (lex.symType() == GRACE || lex.symType() == NOTE || lex.symType() == TIE || lex.symType() == TRIPLET)
    {
      if (lex.symType() == GRACE) parseGraces();
      else if (lex.symType() == NOTE) parseNote();
      else if (lex.symType() == TIE)
      {
        if (lex.symValue() == "^ts")
        {
          if (inTie) errorHandler("tie start ('^ts') unexpected");
          else tieStart = true;
        }
        else
        {
          errorHandler("tie end ('^te') unexpected");
        }
        lex.getSym();
      }
      else if (lex.symType() == TRIPLET)
      {
        if (lex.symValue() == "^3s")
        {
          if (inTriplet) errorHandler("triplet start ('^3s') unexpected");
          else tripletStart = true;
        }
        else
        {
          errorHandler("triplet end ('^3e') unexpected");
        }
        lex.getSym();
      }
    }
  }

  /**
   Parse a bww string. Extract text and type.
   Example: "Air",(Y,C,0,0,Times New Roman,14,400,0,0,18,0,0,0)
   */

  void Parser::parseString()
  {
    qDebug() << "Parser::parseString() value:" << qPrintable(lex.symValue());

    QRegExp rString("\\\"(.*)\\\",\\(([A-Z]),.*\\)");

    if (rString.exactMatch(lex.symValue()))
    {
      QStringList caps = rString.capturedTexts();
      if (caps.size() == 3)
      {
        if (caps.at(2) == "T") title = caps.at(1);
        if (caps.at(2) == "Y") type = caps.at(1);
        if (caps.at(2) == "M") composer = caps.at(1);
        if (caps.at(2) == "F") footer = caps.at(1);
      }
    }
    lex.getSym();
  }

  /**
   Parse a bww tempo symbol.
   */

  void Parser::parseTempo()
  {
    qDebug() << "Parser::parseTempo() value:" << qPrintable(lex.symValue());

    QRegExp rTempo("^TuneTempo,(\\d+)");

    if (rTempo.exactMatch(lex.symValue()))
    {
      QStringList caps = rTempo.capturedTexts();
      if (caps.size() == 2)
      {
        tempo = caps.at(1).toInt();
      }
    }
    lex.getSym();
  }
  /**
   Parse a bww tsig symbol.
   */

  void Parser::parseTSig()
  {
    qDebug() << "Parser::parseTSig() value:" << qPrintable(lex.symValue());

    QRegExp rTSig("(\\d+)_(1|2|4|8|16|32)");

    if (rTSig.exactMatch(lex.symValue()))
    {
      QStringList caps = rTSig.capturedTexts();
      if (caps.size() == 3)
      {
        beats = caps.at(1).toInt();
        beat  = caps.at(2).toInt();
        tsigFound = true;
        wrt.tsig(beats, beat);
      }
    }
    lex.getSym();
  }

} // namespace Bww