lilypond-0.0.3
[lilypond.git] / lexer.l
blob0063227f4e2112184e059ae4340af45c1edb2deb
1 %{ // -*-Fundamental-*-
3 #include <fstream.h>
4 #include <stdio.h>
5 #include "glob.hh"
6 #include "string.hh"
8 #include "lexer.hh"
9 #include "keyword.hh"
10 #include "vray.hh"
11 #include "parser.hh"
12 #include "debug.hh"
14 sstack<istream *> include_stack;
15 static int last_print;
16 const int DOTPRINT=50; // every 50 lines dots
19 %option c++
20 %option noyywrap
21 %option nodefault
22 %option yylineno
23 %option debug
24 %x notes
25 %x incl
27 OPTSIGN         !?
28 NOTENAMEI       A|B|C|D|E|F|G|As|Bes|Ces|Des|Es|Fes|Ges|Ais|Bis|Cis|Dis|Eis|Fis|Gis
29 NOTENAMEII      a|b|c|d|e|f|g|as|bes|ces|des|es|fes|ges|ais|bis|cis|dis|eis|fis|gis
30 NOTENAMEIII      Ases|Beses|Ceses|Deses|Eses|Feses|Geses|Aisis|Bisis|Cisis|Disis|Eisis|Fisis|Gisis
31 NOTENAMEIIII      ases|beses|ceses|deses|eses|feses|geses|aisis|bisis|cisis|disis|eisis|fisis|gisis
32 RESTNAME        r|s
33 NOTENAME        {NOTENAMEI}|{NOTENAMEII}|{NOTENAMEIII}|{NOTENAMEIIII}
34 PITCH           ['`]*{OPTSIGN}{NOTENAME}
35 DURNAME         1|2|4|8|16|32
36 DURATION        {DURNAME}\.*
37 FULLNOTE        {PITCH}{DURATION}?
38 WORD            [a-zA-Z]+
39 REAL            [0-9]+(\.[0-9]*)?
43 \$              {
44         BEGIN(notes); return '$';
47 <notes>{RESTNAME}       {
48         const char *s = YYText();
49         yylval.string = new String (s);
50         if (debug_flags & DEBUGTOKEN)
51                 mtor << "rest:"<< yylval.string;
52         return RESTNAME;
54 <notes>{PITCH}  {
55         const char *s = YYText();
56         yylval.string = new String (s);
57         if (debug_flags & DEBUGTOKEN)
58                 mtor << "pitch:"<< yylval.string;
59         return PITCH;
61 <notes>{DURATION}       {
62         yylval.string = new String (YYText());
63         return DURATION;
65 <notes>[:space:]+               {
67 <notes>[ \t\n]+         {
69 <notes>%.*              {
72 <notes>\$       {
73         BEGIN(INITIAL); return '$';
75 <notes>.        {
76          cout << "SCANNER <notes> HOLE `" << YYText()<<'\''<<endl; 
77          return YYText()[0];
79 <<EOF>> {
80         if(!close_input())
81                 yyterminate();
83 {WORD}          {
84         int l = lookup_keyword(YYText());
85         if (l == -1){
86             yylval.id = lookup_identifier(YYText());
87             return IDENTIFIER;
88         } else
89             return l;
92 {REAL}          {
93         Real r;
94         sscanf (YYText(), "%lf", &r);
95         yylval.real = r;
96         return REAL;
99 [\{\}\[\]\(\)]  {
100         if (debug_flags & DEBUGTOKEN)
101             cout << "parens\n";
102         return YYText()[0];
104 [ \t\n]+        {
105         
107 %.*             {
108         //ignore
110 .               {
111         error("lexer error: illegal character '"+String(YYText()[0])+
112           "' encountered");
113         return YYText()[0];
118 yyFlexLexer *lexer=0;
120 // set the  new input to s, remember old file.
121 void
122 new_input(String s)
123 {    
124     istream *newin ;
125     
126     if (s=="")
127         newin = &cin;
128     else
129         newin = new ifstream( s ); //
130     
131    if ( ! *newin)
132       error("cant open "  + s);
133    cout << "["<<s<<flush;
134    
135    include_stack.push(newin);
137    if (!lexer) {
138        lexer = new yyFlexLexer;
139        lexer->set_debug( bool(debug_flags & DEBUGPARSER));
140    }            
141    
142    lexer->switch_streams(newin);
146 // pop the inputstack.
147 bool
148 close_input()
151   istream *closing= include_stack.pop();
152   if (closing != &cin)
153       delete closing;
154   
155   cout << "]" << flush;
156   
157   if (include_stack.empty())
158       return false ;  
159   else 
160       lexer->switch_streams(include_stack.top());  
161   return true;  
165 yylex() {
166         return lexer->yylex();
169 void
170 yyerror(char *s)
172   *mlog << "error in line " << lexer->lineno() <<  ": " << s << '\n';
173   exit(1);
177 #if 0
179 <notes>{NOTENAME}       {
180         yylval.string = new String (YYText());
181         return NOTENAME;
184 #endif