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