]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
patch::: 1.3.24.jcn1
[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--2000 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   {"commandspanrequest", COMMANDSPANREQUEST},  
29   {"simultaneous", SIMULTANEOUS},
30   {"sequential", SEQUENTIAL},
31   {"accepts", ACCEPTS},
32   {"alternative", ALTERNATIVE},
33   {"bar", BAR},
34   {"breathe", BREATHE},
35   {"char", CHAR_T},
36   {"chordmodifiers", CHORDMODIFIERS},
37   {"chords", CHORDS},
38   {"clef", CLEF},
39   {"cm", CM_T},
40   {"consists", CONSISTS},
41   {"consistsend", CONSISTSEND},
42   {"context", CONTEXT},
43   {"duration", DURATION},
44   {"font", FONT},
45   {"grace", GRACE},
46   {"header", HEADER},
47   {"in", IN_T},
48   {"lyrics", LYRICS},
49   {"key", KEY},
50   {"keysignature", KEYSIGNATURE},
51   {"mark", MARK},
52   {"musicalpitch", MUSICAL_PITCH},
53   {"time", TIME_T},
54   {"times", TIMES},
55   {"midi", MIDI},
56   {"mm", MM_T},
57   {"name", NAME},
58   {"notenames", NOTENAMES},
59   {"notes", NOTES},
60   {"partial", PARTIAL},
61   {"paper", PAPER},
62   {"penalty", PENALTY},
63   {"property", PROPERTY},
64   {"pt", PT_T},
65   {"relative", RELATIVE},
66   {"remove", REMOVE},
67   {"repeat", REPEAT},
68   {"repetitions", REPETITIONS},
69   {"addlyrics", ADDLYRICS},
70   {"score", SCORE},
71   {"script", SCRIPT},
72   {"skip", SKIP},
73   {"textscript", TEXTSCRIPT},
74   {"tempo", TEMPO},
75   {"translator", TRANSLATOR},
76   {"transpose", TRANSPOSE},
77   {"type", TYPE},
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_symbol2scm (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 (flower_dstream && !flower_dstream->silent_b ("InitDeclarations") && flower_dstream)
113     print_declarations (true);
114   if (flower_dstream && !flower_dstream->silent_b ("InitLexer") && flower_dstream)
115     set_debug (1);
116
117
118   new_input (main_input_str_, source_global_l);
119   allow_includes_b_ = allow_includes_b_ &&  !(safe_global_b);
120   
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       DEBUG_OUT << "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       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
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_->get_pitch (s);
186 }
187
188 Musical_pitch
189 My_lily_lexer::lookup_chordmodifier (String s)
190 {
191   return chordmodifier_tab_p_->get_pitch (s);
192 }
193
194
195 void
196 My_lily_lexer::set_notename_table (Notename_table *p)
197 {
198   delete note_tab_p_;
199   note_tab_p_ = p;
200 }
201
202
203
204 void
205 My_lily_lexer::set_chordmodifier_table (Notename_table *p)
206 {
207   delete chordmodifier_tab_p_;
208   chordmodifier_tab_p_ = p;
209 }
210
211 char
212 My_lily_lexer::escaped_char(char c) const
213 {
214   switch(c)
215     {
216     case 'n':
217       return '\n';
218     case 't':
219       return '\t';
220
221     case '\'':
222     case '\"':
223     case '\\':
224       return c;
225     }
226   return 0;
227 }
228
229 Input
230 My_lily_lexer::here_input () const
231 {
232   Source_file * f_l= source_file_l ();
233   return Input (f_l, (char*)here_ch_C ());
234 }