lilypond-0.0.33
[lilypond.git] / src / lexer.l
blob1e4e8488a558801ec27db128310198d0821c332c
1 %{ // -*-Fundamental-*-
3 #include <stdio.h>
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"
17 %option c++
18 %option noyywrap
19 %option nodefault
20 %option yylineno
21 %option debug
22 %option yyclass="My_flex_lexer"
23 %option stack
25 %x notes
26 %x incl
27 %x quote
28 %x lyrics
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}
41 WORD            {A}{AN}*
42 ALPHAWORD       {A}+
43 INT             -?{N}+
44 REAL            {INT}?(\.{N}*)?
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
58 \$              {
59         yy_push_state(notes); 
62 \@              {
63         yy_push_state(lyrics); 
66 <notes>{RESTNAME}       {
67         const char *s = YYText();
68         yylval.string = new String (s); 
69         mtor << "rest:"<< yylval.string;
70         return RESTNAME;
72 <notes>{UNOTENAME}      {
73         int *p=yylval.ii;
74         return ret_notename(p, YYText(), -1);
77 <notes>{NOTENAME}       {
78         int *p=yylval.ii;
79         return ret_notename(p, YYText(), 0);
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;
98 <notes>{PITCHMOD}       {
99         const char *s = YYText();
100         mtor << "pitchmod:"<< YYText()<<eol;
101         yylval.string = new String (s);
102         return PITCHMOD;
104 <notes>{DOTS}           {
105         yylval.i = strlen(YYText());
106         return DOTS;
108 <notes>{INT}            {
109         yylval.i = String(YYText()).value();
110         return INT;
112 <notes>{COMMENT}        {
114 <notes>[ \t\n]+         {
117 <notes>\$       {
118         yy_pop_state();
120 <notes>\"[^"]*\" {
121         String s (YYText()+1);
122         s = s.left(s.len()-1);
123         yylval.string = new String(s);
124         return STRING;
126 <notes>.        {
127         return yylval.c = YYText()[0];
130 \"              {
131         yy_push_state(quote);
133 <quote>[^"]*    {
134         yylval.string = new String (YYText());
136 <quote>\"       {
137         mtor << "quoted string\n";
138         yy_pop_state();
139         return STRING;
142 <lyrics>{DOTS}          {
143         yylval.i = strlen(YYText());
144         return DOTS;
146 <lyrics>{INT}           {
147         yylval.i = String(YYText()).value();
148         return INT;
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;
157 /* let's try passing tex's typesetting macros like \ss \alpha \c */
158         String* str_p = new String(YYText());//huh?
159         return STRING;  
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);
169         yylval.string=sp;
170         return STRING;
173 <lyrics>\"[^"]*\" {
174         String s (YYText()+1);
175         s = s.left(s.len()-1);
176         yylval.string = new String(s);
177         return STRING;
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;
192 <lyrics>\|      {
193         return YYText()[0];
195 <lyrics>{COMMENT}               { 
198 <lyrics>[{}]    {
199         return YYText()[0];
201 <lyrics>[()\[\]|/.^>_-] {
202         return yylval.c = YYText()[0];
204 <lyrics>[ \t\n]+                {
206 <lyrics>@       {
207         yy_pop_state();
210 <<EOF>> {
211         mtor << "<<EOF>>";
213         if (! close_input())
214           yyterminate(); // can't move this, since it actually rets a YY_NULL
218 include           {
219         yy_push_state(incl);
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();
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;
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;
257 [{}]    {
259         mtor << "parens\n";
260         return YYText()[0];
262 [*:=]           {
263         char c = YYText()[0];
264         mtor << "misc char" <<c<<"\n";
265         return c;
267 [ \t\n]+        {
268         
271 {COMMENT}               {
272         //ignore
274 .               {
275         error( String( "illegal character: " ) + String( YYText()[0] ), here_ch_c_l() );
276         return YYText()[0];