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