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