]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
patch::: 1.1.10.jcn1: koorden
[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   {"alternative", ALTERNATIVE},
29   {"bar", BAR},
30   {"cadenza", CADENZA},
31   {"chords", CHORDS},
32   {"clef", CLEF},
33   {"cm", CM_T},
34   {"consists", CONSISTS},
35   {"duration", DURATION},
36   {"font", FONT},
37   {"grouping", GROUPING},
38   {"header", HEADER},
39   {"in", IN_T},
40   {"lyrics", LYRICS},
41   {"key", KEY},
42   {"keysignature", KEYSIGNATURE},
43   {"mark", MARK},
44   {"musicalpitch", MUSICAL_PITCH},
45   {"time", TIME_T},
46   {"times", TIMES},
47   {"midi", MIDI},
48   {"mm", MM_T},
49   {"name", NAME},
50   {"notenames", NOTENAMES},
51   {"notes" , NOTES},
52   {"partial", PARTIAL},
53   {"paper", PAPER},
54   {"penalty", PENALTY},
55   {"property", PROPERTY},
56   {"pt", PT_T},
57   {"relative", RELATIVE},
58   {"remove", REMOVE},
59   {"repeat", REPEAT},
60   {"scm", SCM_T},
61   {"scmfile", SCMFILE},
62   {"score", SCORE},
63   {"script", SCRIPT},
64   {"shape", SHAPE},
65   {"skip", SKIP},
66   {"table", TABLE},
67   {"spandynamic", SPANDYNAMIC},
68   {"symboltables", SYMBOLTABLES},
69   {"tempo", TEMPO},
70   {"translator", TRANSLATOR},
71   {"type", TYPE},
72   {"transpose", TRANSPOSE},
73   {"version", VERSION},
74   {0,0}
75 };
76
77 My_lily_lexer::My_lily_lexer()
78 {
79   keytable_p_ = new Keyword_table (the_key_tab);
80   toplevel_scope_p_ = new Scope;
81   scope_l_arr_.push (toplevel_scope_p_);
82   errorlevel_i_ = 0;
83   note_tab_p_ = new Notename_table;
84 }
85
86 int
87 My_lily_lexer::lookup_keyword (String s)
88 {
89   return keytable_p_->lookup (s.ch_C ());
90 }
91
92 Identifier*
93 My_lily_lexer::lookup_identifier (String s)
94 {
95   for (int i = scope_l_arr_.size (); i--; )
96     if (scope_l_arr_[i]->elem_b (s))
97       return (*scope_l_arr_[i])[s];
98   return 0;
99 }
100
101 void
102 My_lily_lexer::start_main_input ()
103 {  
104   if (!monitor->silent_b ("InitDeclarations") && check_debug)
105     print_declarations (true);
106   if (!monitor->silent_b ("InitLexer") && check_debug)
107     set_debug (1);
108
109   new_input (main_input_str_, source_global_l);
110   
111   print_declarations(true);
112 }
113
114 void
115 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool )
116 {
117   Identifier *old =0;
118   if (scope_l_arr_.top ()->elem_b (name_str))
119     old = scope_l_arr_.top ()->elem(name_str);
120  
121    
122   if  (old)
123     {
124 #if 0
125       if (unique_b)
126         old->warning(_f ("redeclaration of `\\%s\'", name_str));
127 #endif
128       delete old;
129     }
130   if (lookup_keyword (name_str) >= 0)
131     {
132       warning (  _f ("Identifier name is a keyword (`%s')", name_str));
133     }
134   
135   (*scope_l_arr_.top ())[name_str] = i;
136 }
137
138 My_lily_lexer::~My_lily_lexer()
139 {
140   delete keytable_p_;
141   delete toplevel_scope_p_ ;
142   delete note_tab_p_;
143 }
144
145 void
146 My_lily_lexer::print_declarations (bool ) const
147 {
148   for (int i=scope_l_arr_.size (); i--; )
149     {
150       DOUT << "Scope no. " << i << '\n';
151       scope_l_arr_[i]->print ();
152     }
153 }
154
155 void
156 My_lily_lexer::LexerError (char const *s)
157 {
158   if (include_stack_.empty())
159     {
160       *mlog << _f ("error at EOF: %s", s) << endl;
161     }
162   else
163     {
164       errorlevel_i_ |= 1;
165       Input spot (source_file_l(),here_ch_C());
166       spot.error (s);
167     }
168 }
169
170 Musical_pitch
171 My_lily_lexer::lookup_pitch (String s)
172 {
173   return (*note_tab_p_)[s];
174 }
175
176 bool
177 My_lily_lexer::notename_b (String s) const
178 {
179   return note_tab_p_->elem_b (s);
180 }
181
182 void
183 My_lily_lexer::add_notename (String s, Musical_pitch p)
184 {
185   (*note_tab_p_)[s] = p;
186 }
187
188 void
189 My_lily_lexer::set_notename_table(Notename_table *p)
190 {
191   delete note_tab_p_;
192   note_tab_p_ = p;
193 }
194
195 char
196 My_lily_lexer::escaped_char(char c) const
197 {
198   switch(c)
199     {
200     case 'n':
201       return '\n';
202     case 't':
203       return '\t';
204
205     case '\'':
206     case '\"':
207     case '\\':
208       return c;
209     }
210   return 0;
211 }