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