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