]> git.donarmstrong.com Git - lilypond.git/blob - src/lexer.l
release: 0.0.27
[lilypond.git] / src / lexer.l
1 %{ // -*-Fundamental-*-
2
3 #include <stdio.h>
4
5 #include "string.hh"
6 #include "notename.hh"
7 #include "lexer.hh"
8 #include "varray.hh"
9 #include "parser.hh"
10 #include "debug.hh"
11 #include "inputscore.hh"
12 #include "main.hh"
13
14 %}
15
16 %option c++
17 %option noyywrap
18 %option nodefault
19 %option yylineno
20 %option debug
21 %option yyclass="My_flex_lexer"
22 %option stack
23
24 %x notes
25 %x incl
26 %x quote
27 %x lyrics
28
29
30
31 A               [a-zA-Z]
32 AA              {A}|_
33 N               [0-9]
34 AN              {AA}|{N}
35 PUNCT           [?!,.:;]
36 ACCENT          [\\'"^]
37 NATIONAL        [\241-\377]
38 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
39
40 WORD            {A}{AN}*
41 ALPHAWORD       {A}+
42 INT             -?{N}+
43 REAL            {INT}?(\.{N}*)?
44
45 OPTSIGN         !?
46 PITCHMOD        ['`]*{OPTSIGN}
47 RESTNAME        r|s|p
48 NOTECOMMAND     \\{WORD}
49 NOTENAME        [a-z]+
50 UNOTENAME       [A-Z][a-z]*
51 DOTS            \.+
52 LYRICS          {TEX}+
53 COMMENT         [%#].*\n
54
55 %%
56
57 \$              {
58         yy_push_state(notes); 
59 }
60
61 \@              {
62         yy_push_state(lyrics); 
63 }
64
65 <notes>{RESTNAME}       {
66         const char *s = YYText();
67         yylval.string = new String (s); 
68         mtor << "rest:"<< yylval.string;
69         return RESTNAME;
70 }
71 <notes>{UNOTENAME}      {
72         int *p=yylval.ii;
73         return ret_notename(p, YYText(), -1);
74 }
75
76 <notes>{NOTENAME}       {
77         int *p=yylval.ii;
78         return ret_notename(p, YYText(), 0);
79 }
80
81 <notes>{NOTECOMMAND}    {
82         String c = YYText() +1;
83         mtor << "\\word: " << YYText()+1<<eol;
84         int l = lookup_keyword(c);
85         if (l != -1)
86                 return l;
87         Identifier * id = lookup_identifier(c);
88         if (id) {               
89                 yylval.id = id;
90                 return IDENTIFIER;
91         }
92         String *sp = new String( c);
93         yylval.string=sp;
94         return STRING;
95 }
96
97 <notes>{PITCHMOD}       {
98         const char *s = YYText();
99         mtor << "pitchmod:"<< YYText()<<eol;
100         yylval.string = new String (s);
101         return PITCHMOD;
102 }
103 <notes>{DOTS}           {
104         yylval.i = strlen(YYText());
105         return DOTS;
106 }
107 <notes>{INT}            {
108         yylval.i = String(YYText()).value();
109         return INT;
110 }
111 <notes>{COMMENT}        {
112 }
113 <notes>[ \t\n]+         {
114
115 }
116 <notes>\$       {
117         yy_pop_state();
118 }
119 <notes>\"[^"]*\" {
120         String s (YYText()+1);
121         s = s.left(s.len()-1);
122         yylval.string = new String(s);
123         return STRING;
124 }
125 <notes>.        {
126         return yylval.c = YYText()[0];
127 }
128
129 \"              {
130         yy_push_state(quote);
131 }
132 <quote>[^"]*    {
133         yylval.string = new String (YYText());
134 }
135 <quote>\"       {
136         mtor << "quoted string\n";
137         yy_pop_state();
138         return STRING;
139 }
140
141 <lyrics>{DOTS}          {
142         yylval.i = strlen(YYText());
143         return DOTS;
144 }
145 <lyrics>{INT}           {
146         yylval.i = String(YYText()).value();
147         return INT;
148 }
149 <lyrics>{NOTECOMMAND}   {
150         String c = YYText() +1;
151         mtor << "\\word: " << YYText()+1<<eol;
152         int l = lookup_keyword(c);
153         if (l != -1)
154                 return l;
155
156 /* let's try passing tex's typesetting macros like \ss \alpha \c */
157         String* str_p = new String(YYText());//huh?
158         return STRING;  
159
160 /* and skip identifiers...
161         Identifier * id = lookup_identifier(c);
162         if (id) {               
163                 yylval.id = id;
164                 return IDENTIFIER;
165         }
166         String *sp = new String( c);
167
168         yylval.string=sp;
169         return STRING;
170 */
171 }
172 <lyrics>\"[^"]*\" {
173         String s (YYText()+1);
174         s = s.left(s.len()-1);
175         yylval.string = new String(s);
176         return STRING;
177 }
178 <lyrics>{LYRICS} {
179         String s (YYText()); 
180         int i = 0;
181         while ((i=s.pos("_")) != 0)// ugh. Whats this.
182                 *((char*)s.cptr() + i - 1) = ' ';
183         if ((i=s.pos("\\,")) !=0)
184                 {
185                 *((char*)s.cptr() + i) = 'c';
186                 s = s.left(i+1) + " " + s.right(s.len()-i-1);
187                 }
188         yylval.string = new String(s);
189         return STRING;
190 }
191 <lyrics>\|      {
192         return YYText()[0];
193 }
194 <lyrics>{COMMENT}               { 
195
196 }
197 <lyrics>[{}]    {
198         return YYText()[0];
199 }
200 <lyrics>[()\[\]|/.^>_-] {
201         return yylval.c = YYText()[0];
202 }
203 <lyrics>[ \t\n]+                {
204 }
205 <lyrics>@       {
206         yy_pop_state();
207 }
208
209 <<EOF>> {
210         mtor << "<<EOF>>";
211
212         if (! close_input())
213           yyterminate(); // can't move this, since it actually rets a YY_NULL
214 }
215
216
217 include           {
218         yy_push_state(incl);
219 }
220 <incl>[ \t]*      { /* eat the whitespace */ }
221 <incl>\"[^"]*\"+   { /* got the include file name */
222    String s (YYText()+1);
223         s = s.left(s.len()-1);
224    new_input(s);
225    yy_pop_state();
226 }
227
228
229 {WORD}          {
230         mtor << "word: " << YYText()<<eol;
231         String c = YYText();
232         int l = lookup_keyword(c);
233         if (l != -1)
234                 return l;
235         Identifier * id = lookup_identifier(c);
236         if (id) {               
237                 yylval.id = id;
238                 return IDENTIFIER;
239         }
240         String *sp = new String( c);
241         mtor << "new id: " << *sp << eol;
242         yylval.string=sp;
243         return STRING;
244 }
245
246 {REAL}          {
247         Real r;
248         int cnv=sscanf (YYText(), "%lf", &r);
249         assert(cnv == 1);
250         mtor  << "REAL" << r<<'\n';
251         yylval.real = r;
252         return REAL;
253 }
254
255 [{}]    {
256
257         mtor << "parens\n";
258         return YYText()[0];
259 }
260 [*:=]           {
261         char c = YYText()[0];
262         mtor << "misc char" <<c<<"\n";
263         return c;
264 }
265 [ \t\n]+        {
266         
267 }
268
269 {COMMENT}               {
270         //ignore
271 }
272 .               {
273 //      error( "lexer error: illegal character '"+String(YYText()[0])+ "' encountered");
274         error( String( "illegal character: " ) + String( YYText()[0] ) + ": ",
275                 here_ch_c_l() );
276         return YYText()[0];
277 }
278
279 %%
280