]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
286f09277a86ffa814fa16f3fd21328bea456117
[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   {"accidentals", ACCIDENTALS},
29   {"bar", BAR},
30   {"cadenza", CADENZA},
31   {"clear", CLEAR},
32   {"clef", CLEF},
33   {"cm", CM_T},
34   {"consists", CONSISTS},
35   {"contains", CONTAINS},
36   {"duration", DURATION},
37   {"font", FONT},
38   {"grouping", GROUPING},
39   {"in", IN_T},
40   {"lyric", LYRIC},
41   {"key", KEY},
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   (*scope_l_arr_.top ())[name_str] = i;
125 }
126
127 My_lily_lexer::~My_lily_lexer()
128 {
129   delete keytable_p_;
130   delete toplevel_scope_p_ ;
131   delete note_tab_p_;
132 }
133
134 void
135 My_lily_lexer::print_declarations (bool init_b) const
136 {
137   for (int i=scope_l_arr_.size (); i--; )
138     {
139       DOUT << "Scope no. " << i << '\n';
140       scope_l_arr_[i]->print ();
141     }
142 }
143
144 void
145 My_lily_lexer::LexerError (char const *s)
146 {
147   if (include_stack_.empty())
148     {
149       *mlog << _f ("error at EOF: %s", s) << endl;
150     }
151   else
152     {
153       errorlevel_i_ |= 1;
154       Input spot (source_file_l(),here_ch_C());
155       spot.error (s);
156     }
157 }
158
159 Musical_pitch
160 My_lily_lexer::lookup_pitch (String s)
161 {
162   return (*note_tab_p_)[s];
163 }
164
165 bool
166 My_lily_lexer::notename_b (String s) const
167 {
168   return note_tab_p_->elt_b (s);
169 }
170
171 void
172 My_lily_lexer::add_notename (String s, Musical_pitch p)
173 {
174   (*note_tab_p_)[s] = p;
175 }
176
177 void
178 My_lily_lexer::clear_notenames()
179 {
180   delete note_tab_p_;
181   note_tab_p_ = new Notename_table;
182 }
183
184 char
185 My_lily_lexer::escaped_char(char c) const
186 {
187   switch(c)
188     {
189     case 'n':
190       return '\n';
191     case 't':
192       return '\t';
193
194     case '\'':
195     case '\"':
196     case '\\':
197       return c;
198     }
199   return 0;
200 }