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