]> git.donarmstrong.com Git - lilypond.git/blob - src/lexer.l
f83cf144df65ff5f045910cba000c2de60e2aefe
[lilypond.git] / src / 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
29 NOTECOMMAND     \\{WORD}
30 OPTSIGN         !?
31 NOTENAMEI       A|B|C|D|E|F|G|As|Bes|Ces|Des|Es|Fes|Ges|Ais|Bis|Cis|Dis|Eis|Fis|Gis
32 NOTENAMEII      a|b|c|d|e|f|g|as|bes|ces|des|es|fes|ges|ais|bis|cis|dis|eis|fis|gis
33 NOTENAMEIII      Ases|Beses|Ceses|Deses|Eses|Feses|Geses|Aisis|Bisis|Cisis|Disis|Eisis|Fisis|Gisis
34 NOTENAMEIIII      ases|beses|ceses|deses|eses|feses|geses|aisis|bisis|cisis|disis|eisis|fisis|gisis
35 RESTNAME        r|s
36 NOTENAME        {NOTENAMEI}|{NOTENAMEII}|{NOTENAMEIII}|{NOTENAMEIIII}
37 PITCH           ['`]*{OPTSIGN}{NOTENAME}
38 DURNAME         1|2|4|8|16|32
39 DURATION        {DURNAME}\.*
40 FULLNOTE        {PITCH}{DURATION}?
41 WORD            [a-zA-Z][a-zA-Z0-9_]+
42 REAL            -?[0-9]+(\.[0-9]*)?
43
44 %%
45
46 \$              {
47         BEGIN(notes); 
48 }
49
50 <notes>{NOTECOMMAND}    {
51         String c = YYText() +1;
52
53         int l = lookup_keyword(c);
54         if (l != -1)
55                 return l;
56         Identifier * id = lookup_identifier(c);
57         if (id) {               
58                 yylval.id = id;
59                 return IDENTIFIER;
60         }
61         String *sp = new String( c);
62         mtor << "new id: " << *sp;
63         yylval.string=sp;
64         return NEWIDENTIFIER;
65 }
66
67 <notes>{RESTNAME}       {
68         const char *s = YYText();
69         yylval.string = new String (s); 
70         mtor << "rest:"<< yylval.string;
71         return RESTNAME;
72 }
73 <notes>{PITCH}  {
74         const char *s = YYText();
75         yylval.string = new String (s);
76         mtor << "pitch:"<< *yylval.string;
77         return PITCH;
78 }
79 <notes>{DURATION}       {
80         yylval.string = new String (YYText());
81         return DURATION;
82 }
83 <notes>\|               {
84 }
85 <notes>[:space:]+               {
86 }
87 <notes>[ \t\n]+         {
88 }
89 <notes>[%#].*$          { 
90
91 }
92 <notes>\$       {
93         BEGIN(INITIAL); 
94 }
95 <notes>[{}]     {
96         return YYText()[0];
97         
98 }
99 <notes>[\[)]    { /* parens () are NO mistake */
100         yylval.c = YYText()[0];
101         return OPEN_REQUEST_PARENS;
102 }
103 <notes>[\](]    { /* parens () are NO mistake */
104         yylval.c = YYText()[0];
105         return CLOSE_REQUEST_PARENS;
106 }
107
108 <notes>.        {
109         String s("lexer error: illegal character found: " + String(YYText()));
110         yyerror(s);
111 }
112
113 \"              {
114         BEGIN(quote);
115 }
116 <quote>[^\"]*   {
117         yylval.string = new String (YYText());
118 }
119 <quote>\"       {
120         BEGIN(INITIAL);
121         return STRING;
122 }
123
124 <<EOF>> {
125         if(!close_input())
126                 yyterminate();
127 }
128 {WORD}          {
129         String c = YYText();
130         int l = lookup_keyword(c);
131         if (l != -1)
132                 return l;
133         Identifier * id = lookup_identifier(c);
134         if (id) {               
135                 yylval.id = id;
136                 return IDENTIFIER;
137         }
138         String *sp = new String( c);
139         mtor << "new id: " << *sp;
140         yylval.string=sp;
141         return NEWIDENTIFIER;
142 }
143
144 {REAL}          {
145         Real r;
146         int cnv=sscanf (YYText(), "%lf", &r);
147         assert(cnv == 1);
148         mtor  << "token (REAL)" << r;
149         yylval.real = r;
150         return REAL;
151 }
152
153 [\{\}\[\]\(\)]  {
154
155         mtor << "parens\n";
156         return YYText()[0];
157 }
158 [:=]            {
159         char c = YYText()[0];
160         mtor << "misc char" <<c<<"\n";
161         return c;
162 }
163 [ \t\n]+        {
164         
165 }
166
167 %.*             {
168         //ignore
169 }
170 .               {
171         error("lexer error: illegal character '"+String(YYText()[0])+
172           "' encountered");
173         return YYText()[0];
174 }
175
176 %%
177
178 yyFlexLexer *lexer=0;
179
180 bool
181 busy_parsing()
182 {
183     return lexer;       
184 }
185
186 // set the  new input to s, remember old file.
187 void
188 new_input(String s)
189 {    
190     istream *newin ;
191     
192     if (s=="")
193         newin = &cin;
194     else
195         newin = new ifstream( s ); //
196     
197    if ( ! *newin)
198       error("cant open "  + s);
199    cout << "["<<s<<flush;
200    
201    include_stack.push(newin);
202
203    if (!lexer) {
204        lexer = new yyFlexLexer;
205        lexer->set_debug( !monitor.silence("Lexer"));
206    }            
207    
208    lexer->switch_streams(newin);
209 }
210
211
212 // pop the inputstack.
213 bool
214 close_input()
215 {
216
217   istream *closing= include_stack.pop();
218   if (closing != &cin)
219       delete closing;
220   
221   cout << "]" << flush;
222   
223   if (include_stack.empty()) {
224         return false ;
225   } else 
226       lexer->switch_streams(include_stack.top());  
227   return true;  
228 }
229
230 int
231 yylex() {
232         return lexer->yylex();
233 }
234
235 void
236 yyerror(const char *s)
237 {
238   *mlog << "error in line " << lexer->lineno() <<  ": " << s << '\n';
239   exit(1);
240 }
241
242 void
243 kill_lexer()
244 {
245         delete lexer;
246         lexer = 0;
247 }