]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 0.1.63
[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 #include "scope.hh"
24
25 static Keyword_ent the_key_tab[]={
26   {"accepts", ACCEPTS},
27   {"bar", BAR},
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   {"penalty", PENALTY},
54   {"property", PROPERTY},
55   {"pt", PT_T},
56   {"score", SCORE},
57   {"script", SCRIPT},
58   {"shape", SHAPE},
59   {"skip", SKIP},
60   {"staff", STAFF},
61   {"table", TABLE},
62   {"spandynamic", SPANDYNAMIC},
63   {"symboltables", SYMBOLTABLES},
64   {"tempo", TEMPO},
65   {"texid", TEXID},
66   {"textstyle", TEXTSTYLE},
67   {"transpose", TRANSPOSE},
68   {"version", VERSION},
69   {"grouping", GROUPING},
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 (unique_b)
117         old->warning(_("redeclaration of \\") + name_str);
118       delete old;
119     }
120   (*scope_l_arr_.top ())[name_str] = i;
121 }
122
123 My_lily_lexer::~My_lily_lexer()
124 {
125   delete keytable_p_;
126   delete toplevel_scope_p_ ;
127
128
129   delete note_tab_p_;
130 }
131
132 void
133 My_lily_lexer::print_declarations (bool init_b) const
134 {
135   for (int i=scope_l_arr_.size (); i--; )
136     {
137       DOUT << "Scope no. " << i << "\n";
138       scope_l_arr_[i]->print ();
139     }
140 }
141
142 void
143 My_lily_lexer::LexerError (char const *s)
144 {
145   if (include_stack_.empty())
146     {
147       *mlog << _("error at EOF") << s << '\n';
148     }
149   else
150     {
151       errorlevel_i_ |= 1;
152       Input spot (source_file_l(),here_ch_C());
153       spot.error (s);
154     }
155 }
156
157 Melodic_req*
158 My_lily_lexer::lookup_melodic_req_l (String s)
159 {
160   return note_tab_p_->get_l (s);
161 }
162
163 void
164 My_lily_lexer::add_notename (String s, Melodic_req *p)
165 {
166   note_tab_p_->add (s,p);
167 }
168
169 void
170 My_lily_lexer::clear_notenames()
171 {
172   delete note_tab_p_;
173   note_tab_p_ = new Notename_table;
174 }
175
176 char
177 My_lily_lexer::escaped_char(char c) const
178 {
179   switch(c)
180     {
181     case 'n':
182       return '\n';
183     case 't':
184       return '\t';
185
186     case '\'':
187     case '\"':
188     case '\\':
189       return c;
190     }
191   return 0;
192 }