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