]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/lexer.ll
release: 1.1.65
[lilypond.git] / lily / lexer.ll
index 551cf48aec682c7a4f755e35fae5912a76c05bf6..472f2d0c49f2d4ac58d474e12a61022f90681387 100644 (file)
 #include "interval.hh"
 #include "parser.hh"
 #include "debug.hh"
-#include "parseconstruct.hh"
 #include "main.hh"
 #include "musical-request.hh"
 #include "identifier.hh"
+#include "mudela-version.hh"
+#include "version.hh"
+
 void strip_trailing_white (String&);
 void strip_leading_white (String&);
 
+
+bool
+valid_version_b (String s);
+
+
+
 #define start_quote()  \
        yy_push_state (quote);\
        yylval.string = new String
@@ -62,6 +70,8 @@ LYRICS                ({AA}|{TEX})[^0-9 \t\n\f]*
 %option never-interactive 
 %option warn
 
+%x version
+%x chords
 %x incl
 %x lyrics
 %x notes
@@ -91,10 +101,8 @@ RESTNAME    [rs]
 NOTECOMMAND    \\{A}+
 LYRICS         ({AA}|{TEX})[^0-9 \t\n\f]*
 ESCAPED                [nt\\'"]
-PLET           \\\[
-TELP           \\\]
-EXTENDER       [_][_]
-
+EXTENDER       __
+HYPHEN         --
 %%
 
 
@@ -102,7 +110,7 @@ EXTENDER    [_][_]
        // windows-suck-suck-suck
 }
 
-<notes,incl,INITIAL,lyrics>{
+<INITIAL,chords,incl,lyrics,notes>{
   "%{" {
        yy_push_state (longcomment);
   }
@@ -119,6 +127,21 @@ EXTENDER   [_][_]
   }
 }
 
+<INITIAL,chords,lyrics,notes>\\version{WHITE}* {
+       yy_push_state (version);
+}
+<version>\"[^"]*\";?   { /* got the include file name */
+       String s (YYText ()+1);
+       s = s.left_str (s.index_last_i ('"'));
+       DOUT << "#version `" << s << "\'\n";
+       if (!valid_version_b (s))
+               return INVALID;
+       yy_pop_state ();
+}
+<version>.     {
+       LexerError ("No quoted string found after \\version");
+       yy_pop_state ();
+}
 <longcomment>{
        [^\%]*          {
        }
@@ -136,11 +159,17 @@ EXTENDER  [_][_]
 }
 
 
-<notes,INITIAL,lyrics>\\maininput           {
-       start_main_input ();
+<INITIAL,chords,lyrics,notes>\\maininput           {
+       if (!main_input_b_)
+       {
+               start_main_input ();
+               main_input_b_ = true;
+       }
+       else
+               error ("\\maininput disallowed outside init files.");
 }
 
-<notes,INITIAL,lyrics>\\include           {
+<INITIAL,chords,lyrics,notes>\\include           {
        yy_push_state (incl);
 }
 <incl>\"[^"]*\";?   { /* got the include file name */
@@ -175,38 +204,36 @@ EXTENDER  [_][_]
        cerr << _ ("missing end quote") << endl;
        exit (1);
 }
-<notes>{RESTNAME}      {
+<chords,notes>{RESTNAME}       {
        const char *s = YYText ();
        yylval.string = new String (s); 
        DOUT << "rest:"<< yylval.string;
        return RESTNAME;
 }
-<notes>R               {
+<chords,notes>R                {
        return MEASURES;
 }
-<INITIAL,lyrics,notes>\\\${BLACK}*{WHITE}      {
+<INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE}       {
        String s=YYText () + 2;
        s=s.left_str (s.length_i () - 1);
        return scan_escaped_word (s); 
 }
-<INITIAL,lyrics,notes>\${BLACK}*{WHITE}                {
+<INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE}         {
        String s=YYText () + 1;
        s=s.left_str (s.length_i () - 1);
        return scan_bare_word (s);
 }
-<INITIAL,lyrics,notes>\\\${BLACK}*             { // backup rule
+<INITIAL,chords,lyrics,notes>\\\${BLACK}*              { // backup rule
        cerr << _ ("white expected") << endl;
        exit (1);
 }
-<INITIAL,lyrics,notes>\${BLACK}*               { // backup rule
+<INITIAL,chords,lyrics,notes>\${BLACK}*                { // backup rule
        cerr << _ ("white expected") << endl;
        exit (1);
 }
 <notes>{
-
        {ALPHAWORD}     {
                return scan_bare_word (YYText ());
-
        }
 
        {NOTECOMMAND}   {
@@ -217,7 +244,6 @@ EXTENDER    [_][_]
                yylval.i = String_convert::dec2_i (String (YYText ()));
                return DIGIT;
        }
-
        {UNSIGNED}              {
                yylval.i = String_convert::dec2_i (String (YYText ()));
                return UNSIGNED;
@@ -249,7 +275,6 @@ EXTENDER    [_][_]
 }
 
 <lyrics>{
-
        \" {
                start_quote ();
        }
@@ -265,6 +290,8 @@ EXTENDER    [_][_]
                String s (YYText ()); 
                if (s == "__")
                        return yylval.i = EXTENDER;
+               if (s == "--")
+                       return yylval.i = HYPHEN;
                int i = 0;
                        while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
                        *(s.ch_l () + i) = ' ';
@@ -273,6 +300,11 @@ EXTENDER   [_][_]
                        *(s.ch_l () + i + 1) = 'c';
                        s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
                        }
+
+               char c = s[s.length_i () - 1];
+               if (c == '{' &&  c == '}') // brace open is for not confusing dumb tools.
+                       here_input ().warning (
+                               "Brace found at end of lyric. Did you forget a space?");
                yylval.string = new String (s);
                DOUT << "lyric : `" << s << "'\n";
                return STRING;
@@ -281,6 +313,24 @@ EXTENDER   [_][_]
                return yylval.c = YYText ()[0];
        }
 }
+<chords>{
+       {ALPHAWORD}     {
+               return scan_bare_word (YYText ());
+       }
+       {NOTECOMMAND}   {
+               return scan_escaped_word (YYText () + 1);
+       }
+       {UNSIGNED}              {
+               yylval.i = String_convert::dec2_i (String (YYText ()));
+               return UNSIGNED;
+       }
+       \" {
+               start_quote ();
+       }
+       . {
+               return yylval.c = YYText ()[0];
+       }
+}
 
 <<EOF>> {
        DOUT << "<<eof>>";
@@ -322,14 +372,6 @@ EXTENDER   [_][_]
        return c;
 }
 
-<lyrics,notes>{PLET}   {
-       return yylval.i = PLET;
-}
-
-<lyrics,notes>{TELP}   {
-       return yylval.i = TELP;
-}
-
 <INITIAL,notes>.       {
        return yylval.c = YYText ()[0];
 }
@@ -350,7 +392,7 @@ EXTENDER    [_][_]
 }
 
 <*>.           {
-       String msg = _f ("illegal character: `%c\'", YYText ()[0]);
+       String msg = _f ("invalid character: `%c\'", YYText ()[0]);
        LexerError (msg.ch_C ());
        return YYText ()[0];
 }
@@ -363,11 +405,18 @@ My_lily_lexer::push_note_state ()
        yy_push_state (notes);
 }
 
+void
+My_lily_lexer::push_chord_state ()
+{
+       yy_push_state (chords);
+}
+
 void
 My_lily_lexer::push_lyric_state ()
 {
        yy_push_state (lyrics);
 }
+
 void
 My_lily_lexer::pop_state ()
 {
@@ -376,7 +425,7 @@ My_lily_lexer::pop_state ()
 
 int
 My_lily_lexer::scan_escaped_word (String str)
-{      
+{
        DOUT << "\\word: `" << str<<"'\n";
        int l = lookup_keyword (str);
        if (l != -1) {
@@ -389,14 +438,13 @@ My_lily_lexer::scan_escaped_word (String str)
                yylval.id = id;
                return id->token_code_i_;
        }
-       if (YYSTATE != notes) {
-               if (notename_b (str))
-                       {
-                       yylval.pitch = new Musical_pitch (lookup_pitch (str));
+       if ((YYSTATE != notes) && (YYSTATE != chords)) {
+               if (notename_b (str)) {
+                       yylval.pitch = new Musical_pitch (lookup_notename (str));
                        yylval.pitch->set_spot (Input (source_file_l (), 
                          here_ch_C ()));
                        return NOTENAME_PITCH;
-                       }
+               }
        }
        if (check_debug)
                print_declarations (true);
@@ -412,13 +460,19 @@ int
 My_lily_lexer::scan_bare_word (String str)
 {
        DOUT << "word: `" << str<< "'\n";       
-       if (YYSTATE == notes){
+       if ((YYSTATE == notes) || (YYSTATE == chords)) {
                if (notename_b (str)) {
                    DOUT << "(notename)\n";
-                   yylval.pitch = new Musical_pitch (lookup_pitch (str));
+                   yylval.pitch = new Musical_pitch (lookup_notename (str));
+                   yylval.pitch->set_spot (Input (source_file_l (), 
+                     here_ch_C ()));
+                    return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
+               } else if (chordmodifier_b (str)) {
+                   DOUT << "(chordmodifier)\n";
+                   yylval.pitch = new Musical_pitch (lookup_chordmodifier (str));
                    yylval.pitch->set_spot (Input (source_file_l (), 
                      here_ch_C ()));
-                   return NOTENAME_PITCH;
+                   return CHORDMODIFIER_PITCH;
                }
        }
 
@@ -432,6 +486,12 @@ My_lily_lexer::note_state_b () const
        return YY_START == notes;
 }
 
+bool
+My_lily_lexer::chord_state_b () const
+{
+       return YY_START == chords;
+}
+
 bool
 My_lily_lexer::lyric_state_b () const
 {
@@ -464,3 +524,20 @@ strip_trailing_white (String&s)
        s = s.left_str (i+1);
 }
 
+
+
+
+bool
+valid_version_b (String s)
+{
+  Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
+  Mudela_version ver (s);
+  if (!((ver >= oldest_version) && (ver <= current)))
+       {       
+               error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
+               if (!version_ignore_global_b)
+                       return false;
+    }
+  return true;
+}
+