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