]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.1.50
[lilypond.git] / lily / my-lily-lexer.cc
1 /*
2   my-lily-lexer.cc -- implement My_lily_lexer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <strstream.h>
10 #include <ctype.h>
11 #include "notename-table.hh"
12 #include "interval.hh"
13 #include "identifier.hh"
14 #include "parser.hh"
15 #include "keyword.hh"
16 #include "my-lily-lexer.hh"
17 #include "debug.hh"
18 #include "source-file.hh"
19 #include "parseconstruct.hh"
20 #include "main.hh"
21 #include "scope.hh"
22
23 static Keyword_ent the_key_tab[]={
24   {"spanrequest", SPANREQUEST},
25   {"accepts", ACCEPTS},
26   {"alternative", ALTERNATIVE},
27   {"bar", BAR},
28   {"breathe", BREATHE},
29   {"cadenza", CADENZA},
30   {"chordmodifiers", CHORDMODIFIERS},
31   {"chords", CHORDS},
32   {"clef", CLEF},
33   {"cm", CM_T},
34   {"consists", CONSISTS},
35   {"consistsend", CONSISTSEND},
36   {"context", CONTEXT},
37   {"duration", DURATION},
38   {"font", FONT},
39   {"grouping", GROUPING},
40   {"header", HEADER},
41   {"in", IN_T},
42   {"lyrics", LYRICS},
43   {"key", KEY},
44   {"keysignature", KEYSIGNATURE},
45   {"mark", MARK},
46   {"musicalpitch", MUSICAL_PITCH},
47   {"time", TIME_T},
48   {"times", TIMES},
49   {"midi", MIDI},
50   {"mm", MM_T},
51   {"name", NAME},
52   {"notenames", NOTENAMES},
53   {"notes" , NOTES},
54   {"partial", PARTIAL},
55   {"paper", PAPER},
56   {"penalty", PENALTY},
57   {"property", PROPERTY},
58   {"pt", PT_T},
59   {"relative", RELATIVE},
60   {"remove", REMOVE},
61   {"repeat", REPEAT},
62   {"scm", SCM_T},
63   {"scmfile", SCMFILE},
64   {"score", SCORE},
65   {"script", SCRIPT},
66   {"shape", SHAPE},
67   {"skip", SKIP},
68   {"textscript", TEXTSCRIPT},
69   {"tempo", TEMPO},
70   {"translator", TRANSLATOR},
71   {"transpose", TRANSPOSE},
72   {"type", TYPE},
73   {"version", VERSION},
74   {0,0}
75 };
76
77 My_lily_lexer::My_lily_lexer()
78 {
79   keytable_p_ = new Keyword_table (the_key_tab);
80   toplevel_scope_p_ = new Scope;
81   scope_l_arr_.push (toplevel_scope_p_);
82   errorlevel_i_ = 0;
83   note_tab_p_ = new Notename_table;
84   chordmodifier_tab_p_ = new Notename_table;
85   main_input_b_ = false;
86 }
87
88 int
89 My_lily_lexer::lookup_keyword (String s)
90 {
91   return keytable_p_->lookup (s.ch_C ());
92 }
93
94 Identifier*
95 My_lily_lexer::lookup_identifier (String s)
96 {
97   SCM sym = ly_symbol (s.ch_C());
98   
99   for (int i = scope_l_arr_.size (); i--; )
100     if (scope_l_arr_[i]->elem_b (sym))
101       return scope_l_arr_[i]->elem(sym);
102   return 0;
103 }
104
105 void
106 My_lily_lexer::start_main_input ()
107 {  
108   if (!monitor->silent_b ("InitDeclarations") && check_debug)
109     print_declarations (true);
110   if (!monitor->silent_b ("InitLexer") && check_debug)
111     set_debug (1);
112
113
114   new_input (main_input_str_, source_global_l);
115   if (safe_global_b)
116     allow_includes_b_ = false;
117   
118   print_declarations(true);
119 }
120
121 void
122 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool )
123 {
124   Identifier *old =0;
125   if (scope_l_arr_.top ()->elem_b (name_str))
126     old = scope_l_arr_.top ()->elem(name_str);
127  
128    
129   if  (old)
130     {
131 #if 0
132       if (unique_b)
133         old->warning(_f ("redeclaration of `\\%s\'", name_str));
134 #endif
135       delete old;
136     }
137   if (lookup_keyword (name_str) >= 0)
138     {
139       warning (  _f ("Identifier name is a keyword (`%s')", name_str));
140     }
141   
142   scope_l_arr_.top ()->elem (name_str) = i;
143 }
144
145 My_lily_lexer::~My_lily_lexer()
146 {
147   delete chordmodifier_tab_p_;
148   delete keytable_p_;
149   delete toplevel_scope_p_ ;
150   delete note_tab_p_;
151 }
152
153 void
154 My_lily_lexer::print_declarations (bool ) const
155 {
156   for (int i=scope_l_arr_.size (); i--; )
157     {
158       DOUT << "Scope no. " << i << '\n';
159       scope_l_arr_[i]->print ();
160     }
161 }
162
163 void
164 My_lily_lexer::LexerError (char const *s)
165 {
166   if (include_stack_.empty())
167     {
168       *mlog << _f ("error at EOF: %s", s) << endl;
169     }
170   else
171     {
172       errorlevel_i_ |= 1;
173       Input spot (source_file_l(),here_ch_C());
174       spot.error (s);
175     }
176 }
177
178 Musical_pitch
179 My_lily_lexer::lookup_notename (String s)
180 {
181   return (*note_tab_p_)[s];
182 }
183
184 Musical_pitch
185 My_lily_lexer::lookup_chordmodifier (String s)
186 {
187   return (*chordmodifier_tab_p_)[s];
188 }
189
190 bool
191 My_lily_lexer::notename_b (String s) const
192 {
193   return note_tab_p_->elem_b (s);
194 }
195
196 void
197 My_lily_lexer::set_notename_table (Notename_table *p)
198 {
199   delete note_tab_p_;
200   note_tab_p_ = p;
201 }
202
203 bool
204 My_lily_lexer::chordmodifier_b (String s) const
205 {
206   return chordmodifier_tab_p_->elem_b (s);
207 }
208
209 void
210 My_lily_lexer::set_chordmodifier_table (Notename_table *p)
211 {
212   delete chordmodifier_tab_p_;
213   chordmodifier_tab_p_ = p;
214 }
215
216 char
217 My_lily_lexer::escaped_char(char c) const
218 {
219   switch(c)
220     {
221     case 'n':
222       return '\n';
223     case 't':
224       return '\t';
225
226     case '\'':
227     case '\"':
228     case '\\':
229       return c;
230     }
231   return 0;
232 }