]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 0.1.61
[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@stack.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
24 static Keyword_ent the_key_tab[]={
25   {"accepts", ACCEPTS},
26   {"bar", BAR},
27   {"break", BREAK},
28   {"cadenza", CADENZA},
29   {"clear", CLEAR},
30   {"clef", CLEF},
31   {"cm", CM_T},
32   {"consists", CONSISTS},
33   {"contains", CONTAINS},
34   {"duration", DURATION},
35   {"absdynamic", ABSDYNAMIC},
36   {"in", IN_T},
37   {"translator", TRANSLATOR},
38   {"type", TYPE},
39   {"lyric", LYRIC},
40   {"key", KEY},
41   {"melodic" , MELODIC},
42   {"melodic_request", MELODIC_REQUEST},
43   {"meter", METER},
44   {"midi", MIDI},
45   {"mm", MM_T},
46   {"multi", MULTI},
47   {"header", HEADER},
48   {"notenames", NOTENAMES},
49   {"octave", OCTAVE},
50   {"output", OUTPUT},
51   {"partial", PARTIAL},
52   {"paper", PAPER},
53   {"property", PROPERTY},
54   {"pt", PT_T},
55   {"score", SCORE},
56   {"script", SCRIPT},
57   {"shape", SHAPE},
58   {"skip", SKIP},
59   {"staff", STAFF},
60   {"table", TABLE},
61   {"spandynamic", SPANDYNAMIC},
62   {"symboltables", SYMBOLTABLES},
63   {"tempo", TEMPO},
64   {"texid", TEXID},
65   {"textstyle", TEXTSTYLE},
66   {"transpose", TRANSPOSE},
67   {"version", VERSION},
68   {"grouping", GROUPING},
69   {0,0}
70 };
71
72 My_lily_lexer::My_lily_lexer()
73 {
74   keytable_p_ = new Keyword_table (the_key_tab);
75   identifier_p_dict_p_ = new Dictionary<Identifier*>;
76   errorlevel_i_ = 0;
77   post_quotes_b_ = false;
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   if (!identifier_p_dict_p_->elt_b (s))
91     return 0;
92
93   return (*identifier_p_dict_p_)[s];
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)
111 {
112   Identifier *old = lookup_identifier (name_str);
113   if  (old)
114     {
115       old->warning(_("redeclaration of \\") + name_str);
116       delete old;
117     }
118   (*identifier_p_dict_p_)[name_str] = i;
119 }
120
121 My_lily_lexer::~My_lily_lexer()
122 {
123   delete keytable_p_;
124
125   for (Assoc_iter<String,Identifier*>
126          ai (*identifier_p_dict_p_); ai.ok(); ai++)
127     {
128       DOUT << "deleting: " << ai.key()<<'\n';
129       delete ai.val();
130     }
131   delete note_tab_p_;
132   delete identifier_p_dict_p_;
133 }
134 void
135 My_lily_lexer::print_declarations (bool init_b) const
136 {
137   for (Assoc_iter<String,Identifier*> ai (*identifier_p_dict_p_);
138        ai.ok(); ai++)
139     {
140       if (ai.val()->init_b_ == init_b)
141         {
142           DOUT << ai.key() << '=';
143           ai.val()->print ();
144         }
145     }
146 }
147
148 void
149 My_lily_lexer::LexerError (char const *s)
150 {
151   if (include_stack_.empty())
152     {
153       *mlog << _("error at EOF") << s << '\n';
154     }
155   else
156     {
157       errorlevel_i_ |= 1;
158       Input spot (source_file_l(),here_ch_C());
159       spot.error (s);
160     }
161 }
162
163 Melodic_req*
164 My_lily_lexer::lookup_melodic_req_l (String s)
165 {
166   return note_tab_p_->get_l (s);
167 }
168
169 void
170 My_lily_lexer::add_notename (String s, Melodic_req *p)
171 {
172   note_tab_p_->add (s,p);
173 }
174
175 void
176 My_lily_lexer::clear_notenames()
177 {
178   delete note_tab_p_;
179   note_tab_p_ = new Notename_table;
180 }
181
182 char
183 My_lily_lexer::escaped_char(char c) const
184 {
185   switch(c)
186     {
187     case 'n':
188       return '\n';
189     case 't':
190       return '\t';
191
192     case '\'':
193     case '\"':
194     case '\\':
195       return c;
196     }
197   return 0;
198 }