]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.0.11
[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   {"clef", CLEF},
31   {"cm", CM_T},
32   {"consists", CONSISTS},
33   {"duration", DURATION},
34   {"font", FONT},
35   {"grouping", GROUPING},
36   {"header", HEADER},
37   {"in", IN_T},
38   {"lyric", LYRIC},
39   {"key", KEY},
40   {"keysignature", KEYSIGNATURE},
41   {"mark", MARK},
42   {"musicalpitch", MUSICAL_PITCH},
43   {"time", TIME_T},
44   {"midi", MIDI},
45   {"mm", MM_T},
46   {"notenames", NOTENAMES},
47   {"notes" , NOTES},
48   {"output", OUTPUT},
49   {"partial", PARTIAL},
50   {"paper", PAPER},
51   {"penalty", PENALTY},
52   {"property", PROPERTY},
53   {"pt", PT_T},
54   {"relative", RELATIVE},
55   {"score", SCORE},
56   {"script", SCRIPT},
57   {"shape", SHAPE},
58   {"skip", SKIP},
59   {"table", TABLE},
60   {"spandynamic", SPANDYNAMIC},
61   {"symboltables", SYMBOLTABLES},
62   {"tempo", TEMPO},
63   {"translator", TRANSLATOR},
64   {"type", TYPE},
65   {"transpose", TRANSPOSE},
66   {"version", VERSION},
67   {0,0}
68 };
69
70 My_lily_lexer::My_lily_lexer()
71 {
72   keytable_p_ = new Keyword_table (the_key_tab);
73   toplevel_scope_p_ = new Scope;
74   scope_l_arr_.push (toplevel_scope_p_);
75   errorlevel_i_ = 0;
76   note_tab_p_ = new Notename_table;
77 }
78
79 int
80 My_lily_lexer::lookup_keyword (String s)
81 {
82   return keytable_p_->lookup (s.ch_C ());
83 }
84
85 Identifier*
86 My_lily_lexer::lookup_identifier (String s)
87 {
88   for (int i = scope_l_arr_.size (); i--; )
89     if (scope_l_arr_[i]->elt_b (s))
90       return (*scope_l_arr_[i])[s];
91   return 0;
92 }
93
94 void
95 My_lily_lexer::start_main_input ()
96 {  
97   if (!monitor->silent_b ("InitDeclarations") && check_debug)
98     print_declarations (true);
99   if (!monitor->silent_b ("InitLexer") && check_debug)
100     set_debug (1);
101
102   new_input (main_input_str_, source_global_l);
103   
104   print_declarations(true);
105 }
106
107 void
108 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool unique_b)
109 {
110   Identifier *old = lookup_identifier (name_str);
111   if  (old)
112     {
113 #if 0
114       if (unique_b)
115         old->warning(_f ("redeclaration of `\\%s\'", name_str));
116 #endif
117       delete old;
118     }
119   if (lookup_keyword (name_str) >= 0)
120     {
121       warning (  _f ("Identifier name is a keyword (`%s')", name_str));
122     }
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::set_notename_table(Notename_table *p)
179 {
180   delete note_tab_p_;
181   note_tab_p_ = p;
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 }