]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
01b33c406eaaeddd4fdcf602832abba91c85c806
[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   {"clear", CLEAR},
31   {"clef", CLEF},
32   {"cm", CM_T},
33   {"consists", CONSISTS},
34   {"contains", CONTAINS},
35   {"duration", DURATION},
36   {"font", FONT},
37   {"grouping", GROUPING},
38   {"header", HEADER},
39   {"in", IN_T},
40   {"lyric", LYRIC},
41   {"key", KEY},
42   {"keysignature", KEYSIGNATURE},
43   {"mark", MARK},
44   {"musicalpitch", MUSICAL_PITCH},
45   {"time", TIME_T},
46   {"midi", MIDI},
47   {"mm", MM_T},
48   {"notenames", NOTENAMES},
49   {"notes" , NOTES},
50   {"octave", OCTAVE},
51   {"output", OUTPUT},
52   {"partial", PARTIAL},
53   {"paper", PAPER},
54   {"penalty", PENALTY},
55   {"property", PROPERTY},
56   {"pt", PT_T},
57   {"relative", RELATIVE},
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]->elt_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 = lookup_identifier (name_str);
114   if  (old)
115     {
116 #if 0
117       if (unique_b)
118         old->warning(_f ("redeclaration of `\\%s\'", name_str));
119 #endif
120       delete old;
121     }
122   if (lookup_keyword (name_str) >= 0)
123     {
124       warning (  _f ("Identifier name is a keyword (`%s')", name_str));
125     }
126   
127   (*scope_l_arr_.top ())[name_str] = i;
128 }
129
130 My_lily_lexer::~My_lily_lexer()
131 {
132   delete keytable_p_;
133   delete toplevel_scope_p_ ;
134   delete note_tab_p_;
135 }
136
137 void
138 My_lily_lexer::print_declarations (bool init_b) const
139 {
140   for (int i=scope_l_arr_.size (); i--; )
141     {
142       DOUT << "Scope no. " << i << '\n';
143       scope_l_arr_[i]->print ();
144     }
145 }
146
147 void
148 My_lily_lexer::LexerError (char const *s)
149 {
150   if (include_stack_.empty())
151     {
152       *mlog << _f ("error at EOF: %s", s) << endl;
153     }
154   else
155     {
156       errorlevel_i_ |= 1;
157       Input spot (source_file_l(),here_ch_C());
158       spot.error (s);
159     }
160 }
161
162 Musical_pitch
163 My_lily_lexer::lookup_pitch (String s)
164 {
165   return (*note_tab_p_)[s];
166 }
167
168 bool
169 My_lily_lexer::notename_b (String s) const
170 {
171   return note_tab_p_->elt_b (s);
172 }
173
174 void
175 My_lily_lexer::add_notename (String s, Musical_pitch p)
176 {
177   (*note_tab_p_)[s] = p;
178 }
179
180 void
181 My_lily_lexer::clear_notenames()
182 {
183   delete note_tab_p_;
184   note_tab_p_ = new Notename_table;
185 }
186
187 char
188 My_lily_lexer::escaped_char(char c) const
189 {
190   switch(c)
191     {
192     case 'n':
193       return '\n';
194     case 't':
195       return '\t';
196
197     case '\'':
198     case '\"':
199     case '\\':
200       return c;
201     }
202   return 0;
203 }