]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 0.1.65
[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   {"musical_pitch", MUSICAL_PITCH},
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   {"relative", RELATIVE},
57   {"score", SCORE},
58   {"script", SCRIPT},
59   {"shape", SHAPE},
60   {"skip", SKIP},
61   {"staff", STAFF},
62   {"table", TABLE},
63   {"spandynamic", SPANDYNAMIC},
64   {"symboltables", SYMBOLTABLES},
65   {"tempo", TEMPO},
66   {"texid", TEXID},
67   {"textstyle", TEXTSTYLE},
68   {"transpose", TRANSPOSE},
69   {"version", VERSION},
70   {"grouping", GROUPING},
71   {0,0}
72 };
73
74 My_lily_lexer::My_lily_lexer()
75 {
76   keytable_p_ = new Keyword_table (the_key_tab);
77   toplevel_scope_p_ = new Scope;
78   scope_l_arr_.push (toplevel_scope_p_);
79   errorlevel_i_ = 0;
80   note_tab_p_ = new Notename_table;
81 }
82
83 int
84 My_lily_lexer::lookup_keyword (String s)
85 {
86   return keytable_p_->lookup (s.ch_C ());
87 }
88
89 Identifier*
90 My_lily_lexer::lookup_identifier (String s)
91 {
92   for (int i = scope_l_arr_.size (); i--; )
93     if (scope_l_arr_[i]->elt_b (s))
94       return (*scope_l_arr_[i])[s];
95   return 0;
96 }
97
98 void
99 My_lily_lexer::start_main_input ()
100 {  
101   if (!monitor->silent_b ("InitDeclarations") && check_debug)
102     print_declarations (true);
103   if (!monitor->silent_b ("InitLexer") && check_debug)
104     set_debug (1);
105
106   new_input (main_input_str_, source_global_l);
107   
108   print_declarations(true);
109 }
110
111 void
112 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool unique_b)
113 {
114   Identifier *old = lookup_identifier (name_str);
115   if  (old)
116     {
117       if (unique_b)
118         old->warning(_("redeclaration of \\") + name_str);
119       delete old;
120     }
121   (*scope_l_arr_.top ())[name_str] = i;
122 }
123
124 My_lily_lexer::~My_lily_lexer()
125 {
126   delete keytable_p_;
127   delete toplevel_scope_p_ ;
128
129
130   delete note_tab_p_;
131 }
132
133 void
134 My_lily_lexer::print_declarations (bool init_b) const
135 {
136   for (int i=scope_l_arr_.size (); i--; )
137     {
138       DOUT << "Scope no. " << i << "\n";
139       scope_l_arr_[i]->print ();
140     }
141 }
142
143 void
144 My_lily_lexer::LexerError (char const *s)
145 {
146   if (include_stack_.empty())
147     {
148       *mlog << _("error at EOF") << s << '\n';
149     }
150   else
151     {
152       errorlevel_i_ |= 1;
153       Input spot (source_file_l(),here_ch_C());
154       spot.error (s);
155     }
156 }
157
158 Musical_pitch
159 My_lily_lexer::lookup_pitch (String s)
160 {
161   return (*note_tab_p_)[s];
162 }
163
164 bool
165 My_lily_lexer::notename_b (String s) const
166 {
167   return note_tab_p_->elt_b (s);
168 }
169
170 void
171 My_lily_lexer::add_notename (String s, Musical_pitch p)
172 {
173   (*note_tab_p_)[s] = p;
174 }
175
176 void
177 My_lily_lexer::clear_notenames()
178 {
179   delete note_tab_p_;
180   note_tab_p_ = new Notename_table;
181 }
182
183 char
184 My_lily_lexer::escaped_char(char c) const
185 {
186   switch(c)
187     {
188     case 'n':
189       return '\n';
190     case 't':
191       return '\t';
192
193     case '\'':
194     case '\"':
195     case '\\':
196       return c;
197     }
198   return 0;
199 }