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