]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.1.54
[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   {"grace", GRACE},
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   {"repetitions", REPETITIONS},
63   {"rhythm", RHYTHM},
64   {"scm", SCM_T},
65   {"scmfile", SCMFILE},
66   {"score", SCORE},
67   {"script", SCRIPT},
68   {"shape", SHAPE},
69   {"skip", SKIP},
70   {"textscript", TEXTSCRIPT},
71   {"tempo", TEMPO},
72   {"translator", TRANSLATOR},
73   {"transpose", TRANSPOSE},
74   {"type", TYPE},
75   {"version", VERSION},
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_symbol (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 (!monitor->silent_b ("InitDeclarations") && check_debug)
111     print_declarations (true);
112   if (!monitor->silent_b ("InitLexer") && check_debug)
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       DOUT << "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 }