]> git.donarmstrong.com Git - lilypond.git/blob - lexer.l
release: 0.0.5
[lilypond.git] / lexer.l
1 %{ // -*-Fundamental-*-
2
3 #include <fstream.h>
4 #include <stdio.h>
5 #include "glob.hh"
6 #include "string.hh"
7
8 #include "lexer.hh"
9 #include "keyword.hh"
10 #include "vray.hh"
11 #include "parser.hh"
12 #include "debug.hh"
13
14 sstack<istream *> include_stack;
15 static int last_print;
16 const int DOTPRINT=50; // every 50 lines dots
17 %}
18
19 %option c++
20 %option noyywrap
21 %option nodefault
22 %option yylineno
23 %option debug
24 %x notes
25 %x incl
26 %x quote
27
28 OPTSIGN         !?
29 NOTENAMEI       A|B|C|D|E|F|G|As|Bes|Ces|Des|Es|Fes|Ges|Ais|Bis|Cis|Dis|Eis|Fis|Gis
30 NOTENAMEII      a|b|c|d|e|f|g|as|bes|ces|des|es|fes|ges|ais|bis|cis|dis|eis|fis|gis
31 NOTENAMEIII      Ases|Beses|Ceses|Deses|Eses|Feses|Geses|Aisis|Bisis|Cisis|Disis|Eisis|Fisis|Gisis
32 NOTENAMEIIII      ases|beses|ceses|deses|eses|feses|geses|aisis|bisis|cisis|disis|eisis|fisis|gisis
33 RESTNAME        r|s
34 NOTENAME        {NOTENAMEI}|{NOTENAMEII}|{NOTENAMEIII}|{NOTENAMEIIII}
35 PITCH           ['`]*{OPTSIGN}{NOTENAME}
36 DURNAME         1|2|4|8|16|32
37 DURATION        {DURNAME}\.*
38 FULLNOTE        {PITCH}{DURATION}?
39 WORD            [a-zA-Z]+
40 REAL            [0-9]+(\.[0-9]*)?
41
42 %%
43
44 \$              {
45         BEGIN(notes); return '$';
46 }
47
48 <notes>{RESTNAME}       {
49         const char *s = YYText();
50         yylval.string = new String (s); 
51         mtor << "rest:"<< yylval.string;
52         return RESTNAME;
53 }
54 <notes>{PITCH}  {
55         const char *s = YYText();
56         yylval.string = new String (s);
57         mtor << "pitch:"<< *yylval.string;
58         return PITCH;
59 }
60 <notes>{DURATION}       {
61         yylval.string = new String (YYText());
62         return DURATION;
63 }
64 <notes>[:space:]+               {
65 }
66 <notes>[ \t\n]+         {
67 }
68 <notes>%.*              {
69
70 }
71 <notes>\$       {
72         BEGIN(INITIAL); return '$';
73 }
74 <notes>.        {
75         error("lexer error: illegal character found: " + String(YYText()));
76 }
77
78 \"              {
79         BEGIN(quote);
80 }
81 <quote>[^\"]*   {
82         yylval.string = new String (YYText());
83 }
84 <quote>\"       {
85         BEGIN(INITIAL);
86         return STRING;
87 }
88
89 <<EOF>> {
90         if(!close_input())
91                 yyterminate();
92 }
93 {WORD}          {
94         int l = lookup_keyword(YYText());
95         if (l == -1){
96             yylval.id = lookup_identifier(YYText());
97             return IDENTIFIER;
98         } else
99             return l;
100 }
101
102 {REAL}          {
103         Real r;
104         int cnv=sscanf (YYText(), "%lf", &r);
105         assert(cnv == 1);
106         mtor  << "token (REAL)" << r;
107         yylval.real = r;
108         return REAL;
109 }
110
111 [\{\}\[\]\(\)]  {
112
113         mtor << "parens\n";
114         return YYText()[0];
115 }
116 [:]             {
117         char c = YYText()[0];
118         mtor << "misc char" <<c<<"\n";
119         return c;
120 }
121 [ \t\n]+        {
122         
123 }
124
125 %.*             {
126         //ignore
127 }
128 .               {
129         error("lexer error: illegal character '"+String(YYText()[0])+
130           "' encountered");
131         return YYText()[0];
132 }
133
134 %%
135
136 yyFlexLexer *lexer=0;
137
138 // set the  new input to s, remember old file.
139 void
140 new_input(String s)
141 {    
142     istream *newin ;
143     
144     if (s=="")
145         newin = &cin;
146     else
147         newin = new ifstream( s ); //
148     
149    if ( ! *newin)
150       error("cant open "  + s);
151    cout << "["<<s<<flush;
152    
153    include_stack.push(newin);
154
155    if (!lexer) {
156        lexer = new yyFlexLexer;
157        lexer->set_debug( !monitor.silence("Lexer"));
158    }            
159    
160    lexer->switch_streams(newin);
161 }
162
163
164 // pop the inputstack.
165 bool
166 close_input()
167 {
168
169   istream *closing= include_stack.pop();
170   if (closing != &cin)
171       delete closing;
172   
173   cout << "]" << flush;
174   
175   if (include_stack.empty())
176       return false ;  
177   else 
178       lexer->switch_streams(include_stack.top());  
179   return true;  
180 }
181
182 int
183 yylex() {
184         return lexer->yylex();
185 }
186
187 void
188 yyerror(char *s)
189 {
190   *mlog << "error in line " << lexer->lineno() <<  ": " << s << '\n';
191   exit(1);
192 }
193
194
195 #if 0
196
197 <notes>{NOTENAME}       {
198         yylval.string = new String (YYText());
199         return NOTENAME;
200 }
201
202 #endif