]> git.donarmstrong.com Git - lilypond.git/blob - src/lexer.l
release: 0.0.33
[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 "parseconstruct.hh"
13 #include "main.hh"
14
15 %}
16
17 %option c++
18 %option noyywrap
19 %option nodefault
20 %option yylineno
21 %option debug
22 %option yyclass="My_flex_lexer"
23 %option stack
24
25 %x notes
26 %x incl
27 %x quote
28 %x lyrics
29
30
31
32 A               [a-zA-Z]
33 AA              {A}|_
34 N               [0-9]
35 AN              {AA}|{N}
36 PUNCT           [?!,.:;]
37 ACCENT          [\\'"^]
38 NATIONAL        [\241-\377]
39 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
40
41 WORD            {A}{AN}*
42 ALPHAWORD       {A}+
43 INT             -?{N}+
44 REAL            {INT}?(\.{N}*)?
45
46 OPTSIGN         !?
47 PITCHMOD        ['`]*{OPTSIGN}
48 RESTNAME        r|s|p
49 NOTECOMMAND     \\{WORD}
50 NOTENAME        [a-z]+
51 UNOTENAME       [A-Z][a-z]*
52 DOTS            \.+
53 LYRICS          {TEX}+
54 COMMENT         [%#].*\n
55
56 %%
57
58 \$              {
59         yy_push_state(notes); 
60 }
61
62 \@              {
63         yy_push_state(lyrics); 
64 }
65
66 <notes>{RESTNAME}       {
67         const char *s = YYText();
68         yylval.string = new String (s); 
69         mtor << "rest:"<< yylval.string;
70         return RESTNAME;
71 }
72 <notes>{UNOTENAME}      {
73         int *p=yylval.ii;
74         return ret_notename(p, YYText(), -1);
75 }
76
77 <notes>{NOTENAME}       {
78         int *p=yylval.ii;
79         return ret_notename(p, YYText(), 0);
80 }
81
82 <notes>{NOTECOMMAND}    {
83         String c = YYText() +1;
84         mtor << "\\word: " << YYText()+1<<eol;
85         int l = lookup_keyword(c);
86         if (l != -1)
87                 return l;
88         Identifier * id = lookup_identifier(c);
89         if (id) {               
90                 yylval.id = id;
91                 return IDENTIFIER;
92         }
93         String *sp = new String( c);
94         yylval.string=sp;
95         return STRING;
96 }
97
98 <notes>{PITCHMOD}       {
99         const char *s = YYText();
100         mtor << "pitchmod:"<< YYText()<<eol;
101         yylval.string = new String (s);
102         return PITCHMOD;
103 }
104 <notes>{DOTS}           {
105         yylval.i = strlen(YYText());
106         return DOTS;
107 }
108 <notes>{INT}            {
109         yylval.i = String(YYText()).value();
110         return INT;
111 }
112 <notes>{COMMENT}        {
113 }
114 <notes>[ \t\n]+         {
115
116 }
117 <notes>\$       {
118         yy_pop_state();
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         yy_push_state(quote);
132 }
133 <quote>[^"]*    {
134         yylval.string = new String (YYText());
135 }
136 <quote>\"       {
137         mtor << "quoted string\n";
138         yy_pop_state();
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());//huh?
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 STRING;
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) // change word binding "_" to " "
183                 *(s.ch_l() + i - 1) = ' ';
184         if ((i=s.pos("\\,")) !=0)   // change "\," to TeX's "\c "
185                 {
186                 *(s.ch_l() + 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         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(s.len()-1);
225    defined_ch_c_l = here_ch_c_l() - String( YYText() ).len() - 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