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