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