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