]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.0.10
[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--1998 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 "assoc-iter.hh"
15 #include "parser.hh"
16 #include "keyword.hh"
17 #include "assoc.hh"
18 #include "my-lily-lexer.hh"
19 #include "debug.hh"
20 #include "source-file.hh"
21 #include "parseconstruct.hh"
22 #include "main.hh"
23 #include "scope.hh"
24
25 static Keyword_ent the_key_tab[]={
26   {"absdynamic", ABSDYNAMIC},
27   {"accepts", ACCEPTS},
28   {"bar", BAR},
29   {"cadenza", CADENZA},
30   {"clef", CLEF},
31   {"cm", CM_T},
32   {"consists", CONSISTS},
33   {"duration", DURATION},
34   {"font", FONT},
35   {"grouping", GROUPING},
36   {"header", HEADER},
37   {"in", IN_T},
38   {"lyric", LYRIC},
39   {"key", KEY},
40   {"keysignature", KEYSIGNATURE},
41   {"mark", MARK},
42   {"musicalpitch", MUSICAL_PITCH},
43   {"time", TIME_T},
44   {"midi", MIDI},
45   {"mm", MM_T},
46   {"notenames", NOTENAMES},
47   {"notes" , NOTES},
48   {"octave", OCTAVE},
49   {"output", OUTPUT},
50   {"partial", PARTIAL},
51   {"paper", PAPER},
52   {"penalty", PENALTY},
53   {"property", PROPERTY},
54   {"pt", PT_T},
55   {"relative", RELATIVE},
56   {"score", SCORE},
57   {"script", SCRIPT},
58   {"shape", SHAPE},
59   {"skip", SKIP},
60   {"table", TABLE},
61   {"spandynamic", SPANDYNAMIC},
62   {"symboltables", SYMBOLTABLES},
63   {"tempo", TEMPO},
64   {"translator", TRANSLATOR},
65   {"type", TYPE},
66   {"transpose", TRANSPOSE},
67   {"version", VERSION},
68   {0,0}
69 };
70
71 My_lily_lexer::My_lily_lexer()
72 {
73   keytable_p_ = new Keyword_table (the_key_tab);
74   toplevel_scope_p_ = new Scope;
75   scope_l_arr_.push (toplevel_scope_p_);
76   errorlevel_i_ = 0;
77   note_tab_p_ = new Notename_table;
78 }
79
80 int
81 My_lily_lexer::lookup_keyword (String s)
82 {
83   return keytable_p_->lookup (s.ch_C ());
84 }
85
86 Identifier*
87 My_lily_lexer::lookup_identifier (String s)
88 {
89   for (int i = scope_l_arr_.size (); i--; )
90     if (scope_l_arr_[i]->elt_b (s))
91       return (*scope_l_arr_[i])[s];
92   return 0;
93 }
94
95 void
96 My_lily_lexer::start_main_input ()
97 {  
98   if (!monitor->silent_b ("InitDeclarations") && check_debug)
99     print_declarations (true);
100   if (!monitor->silent_b ("InitLexer") && check_debug)
101     set_debug (1);
102
103   new_input (main_input_str_, source_global_l);
104   
105   print_declarations(true);
106 }
107
108 void
109 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool unique_b)
110 {
111   Identifier *old = lookup_identifier (name_str);
112   if  (old)
113     {
114 #if 0
115       if (unique_b)
116         old->warning(_f ("redeclaration of `\\%s\'", name_str));
117 #endif
118       delete old;
119     }
120   if (lookup_keyword (name_str) >= 0)
121     {
122       warning (  _f ("Identifier name is a keyword (`%s')", name_str));
123     }
124   
125   (*scope_l_arr_.top ())[name_str] = i;
126 }
127
128 My_lily_lexer::~My_lily_lexer()
129 {
130   delete keytable_p_;
131   delete toplevel_scope_p_ ;
132   delete note_tab_p_;
133 }
134
135 void
136 My_lily_lexer::print_declarations (bool init_b) const
137 {
138   for (int i=scope_l_arr_.size (); i--; )
139     {
140       DOUT << "Scope no. " << i << '\n';
141       scope_l_arr_[i]->print ();
142     }
143 }
144
145 void
146 My_lily_lexer::LexerError (char const *s)
147 {
148   if (include_stack_.empty())
149     {
150       *mlog << _f ("error at EOF: %s", s) << endl;
151     }
152   else
153     {
154       errorlevel_i_ |= 1;
155       Input spot (source_file_l(),here_ch_C());
156       spot.error (s);
157     }
158 }
159
160 Musical_pitch
161 My_lily_lexer::lookup_pitch (String s)
162 {
163   return (*note_tab_p_)[s];
164 }
165
166 bool
167 My_lily_lexer::notename_b (String s) const
168 {
169   return note_tab_p_->elt_b (s);
170 }
171
172 void
173 My_lily_lexer::add_notename (String s, Musical_pitch p)
174 {
175   (*note_tab_p_)[s] = p;
176 }
177
178 void
179 My_lily_lexer::set_notename_table(Notename_table *p)
180 {
181   delete note_tab_p_;
182   note_tab_p_ = p;
183 }
184
185 char
186 My_lily_lexer::escaped_char(char c) const
187 {
188   switch(c)
189     {
190     case 'n':
191       return '\n';
192     case 't':
193       return '\t';
194
195     case '\'':
196     case '\"':
197     case '\\':
198       return c;
199     }
200   return 0;
201 }