]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.l
release: 0.0.42.pre3
[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 "identifier.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 RESTNAME        r|s|p
53 NOTECOMMAND     \\{WORD}
54 DOTS            \.+
55 LYRICS          {TEX}+
56 COMMENT         %.*\n
57
58 %%
59
60
61
62
63 include           {
64         yy_push_state(incl);
65 }
66 <incl>[ \t]*      { /* eat the whitespace */ }
67 <incl>\"[^"]*\"+   { /* got the include file name */
68    String s (YYText()+1);
69    s = s.left_str(s.length_i()-1);
70    defined_ch_c_l = here_ch_c_l() - String( YYText() ).length_i() - 1;
71    new_input(s);
72    yy_pop_state();
73 }
74
75 <notes>{RESTNAME}       {
76         const char *s = YYText();
77         yylval.string = new String (s); 
78         mtor << "rest:"<< yylval.string;
79         return RESTNAME;
80 }
81 <notes,INITIAL>{ALPHAWORD}      {
82         String str = YYText();
83         mtor << "word: `" << str<< "'\n";
84         Identifier * id = lookup_identifier(str);
85         if (id) {               
86                 yylval.id = id;
87                 return id->token_code_i_;
88         }
89
90         yylval.string=new String( str );
91         return STRING;
92 }
93 <notes,INITIAL>{NOTECOMMAND}    {
94         String c = YYText() +1;
95         mtor << "\\word: `" << YYText()+1<<"'\n";
96         int l = lookup_keyword(c);
97         if (l != -1)
98                 return l;
99         
100         String *sp = new String( c);
101         yylval.string=sp;
102         return STRING;
103 }
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         return CONCAT;
120 }
121 <notes>\" {
122         start_quote();
123 }
124 <notes>.        {
125         return yylval.c = YYText()[0];
126 }
127
128
129 \"              {
130         start_quote();
131 }
132 <quote>[^"]+    {
133         *yylval.string += YYText();
134 }
135 <quote>\"       {
136         mtor << "quoted string: `" << *yylval.string << "'\n";
137         yy_pop_state();
138         return STRING;
139 }
140
141 <lyrics>\" {
142         start_quote();
143 }
144 <lyrics>{DOTS}          {
145         yylval.i = strlen(YYText());
146         return DOTS;
147 }
148 <lyrics>{INT}           {
149         yylval.i = String_convert::dec2_i( String( YYText() ) );
150         return INT;
151 }
152 <lyrics>{NOTECOMMAND}   {
153         String c = YYText() +1;
154         mtor << "\\word: `" << YYText()+1<<"'\n";
155         int l = lookup_keyword(c);
156         if (l != -1)
157                 return l;
158
159 /* let's try passing tex's typesetting macros like \ss \alpha \c */
160         String* str_p = new String(YYText());
161         yylval.string=str_p;
162         mtor << "\\word: `" << *str_p << "'\n";
163         return STRING;  
164
165 /* and skip identifiers...
166         Identifier * id = lookup_identifier(c);
167         if (id) {               
168                 yylval.id = id;
169                 return IDENTIFIER;
170         }
171         String *sp = new String( c);
172
173         yylval.string=sp;
174
175         return STRING;
176 */
177 }
178 <lyrics>{LYRICS} {
179         /* ugr. This sux. */
180         String s (YYText()); 
181         int i = 0;
182         while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
183                 *(s.ch_l() + i) = ' ';
184         if ((i=s.index_i("\\,")) != -1)   // change "\," to TeX's "\c "
185                 {
186                 *(s.ch_l() + i + 1) = 'c';
187                 s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
188                 }
189         yylval.string = new String(s);
190         mtor << "lyric : `" << s << "'\n";
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
208 <<EOF>> {
209         mtor << "<<EOF>>";
210
211         if (! close_input())
212           yyterminate(); // can't move this, since it actually rets a YY_NULL
213 }
214 {REAL}          {
215         Real r;
216         int cnv=sscanf (YYText(), "%lf", &r);
217         assert(cnv == 1);
218         mtor  << "REAL" << r<<'\n';
219         yylval.real = r;
220         return REAL;
221 }
222
223 [{}]    {
224
225         mtor << "parens\n";
226         return YYText()[0];
227 }
228 [*:=]           {
229         char c = YYText()[0];
230         mtor << "misc char" <<c<<"\n";
231         return c;
232 }
233 [ \t\n]+        {
234         
235 }
236
237 {COMMENT}               {
238         //ignore
239 }
240 .               {
241         error( String( "illegal character: " ) + String( YYText()[0] ), here_ch_c_l() );
242         return YYText()[0];
243 }
244
245 %%
246
247 void
248 My_flex_lexer::push_note_state()
249 {
250         yy_push_state(notes);
251 }
252
253 void
254 My_flex_lexer::push_lyric_state()
255 {
256         yy_push_state(lyrics);
257 }
258 void
259 My_flex_lexer::pop_state()
260 {
261         yy_pop_state();
262 }