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