lilypond-1.1.13
[lilypond.git] / src / lexer.l
blob3f86adcd54f6689db771f98a9ec561a5ff6fa547
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 "input-score.hh"
12 #include "parseconstruct.hh"
13 #include "main.hh"
14 #include "string-convert.hh"
18 %option c++
19 %option noyywrap
20 %option nodefault
21 %option yylineno
22 %option debug
23 %option yyclass="My_flex_lexer"
24 %option stack
26 %x notes
27 %x incl
28 %x quote
29 %x lyrics
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}
42 WORD            {A}{AN}*
43 ALPHAWORD       {A}+
44 INT             -?{N}+
45 REAL            {INT}?(\.{N}*)?
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
59 \$              {
60         yy_push_state(notes); 
63 \@              {
64         yy_push_state(lyrics); 
67 <notes>{RESTNAME}       {
68         const char *s = YYText();
69         yylval.string = new String (s); 
70         mtor << "rest:"<< yylval.string;
71         return RESTNAME;
73 <notes>{UNOTENAME}      {
74         int *p=yylval.ii;
75         return ret_notename(p, YYText(), -1);
78 <notes>{NOTENAME}       {
79         int *p=yylval.ii;
80         return ret_notename(p, YYText(), 0);
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;
99 <notes>{PITCHMOD}       {
100         const char *s = YYText();
101         mtor << "pitchmod:"<< YYText()<<eol;
102         yylval.string = new String (s);
103         return PITCHMOD;
105 <notes>{DOTS}           {
106         yylval.i = strlen(YYText());
107         return DOTS;
109 <notes>{INT}            {
110         yylval.i = String_convert::dec2_i( String( YYText() ) );
111         return INT;
113 <notes>{COMMENT}        {
115 <notes>[ \t\n]+         {
118 <notes>\$       {
119         yy_pop_state();
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;
127 <notes>.        {
128         return yylval.c = YYText()[0];
131 \"              {
132         yy_push_state(quote);
134 <quote>[^"]*    {
135         yylval.string = new String (YYText());
137 <quote>\"       {
138         mtor << "quoted string\n";
139         yy_pop_state();
140         return STRING;
143 <lyrics>{DOTS}          {
144         yylval.i = strlen(YYText());
145         return DOTS;
147 <lyrics>{INT}           {
148         yylval.i = String_convert::dec2_i( String( YYText() ) );
149         return INT;
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;
158 /* let's try passing tex's typesetting macros like \ss \alpha \c */
159         String* str_p = new String(YYText());//huh?
160         return STRING;  
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);
170         yylval.string=sp;
171         return STRING;
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;
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;
193 <lyrics>\|      {
194         return YYText()[0];
196 <lyrics>{COMMENT}               { 
199 <lyrics>[{}]    {
200         return YYText()[0];
202 <lyrics>[()\[\]|/.^>_-] {
203         return yylval.c = YYText()[0];
205 <lyrics>[ \t\n]+                {
207 <lyrics>@       {
208         yy_pop_state();
211 <<EOF>> {
212         mtor << "<<EOF>>";
214         if (! close_input())
215           yyterminate(); // can't move this, since it actually rets a YY_NULL
219 include           {
220         yy_push_state(incl);
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();
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;
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;
258 [{}]    {
260         mtor << "parens\n";
261         return YYText()[0];
263 [*:=]           {
264         char c = YYText()[0];
265         mtor << "misc char" <<c<<"\n";
266         return c;
268 [ \t\n]+        {
269         
272 {COMMENT}               {
273         //ignore
275 .               {
276         error( String( "illegal character: " ) + String( YYText()[0] ), here_ch_c_l() );
277         return YYText()[0];