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