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