lilypond-0.0.40
[lilypond.git] / src / mylexer.cc
blob7da6dddec843d7413426c724287cb7aea2763ee9
1 #include <strstream.h>
3 #include "interval.hh"
4 #include "identparent.hh"
5 #include "assoc-iter.hh"
6 #include "lexer.hh"
7 #include "input-file.hh"
8 #include "parser.hh"
9 #include "keyword.hh"
10 #include "assoc.hh"
11 #include "lexer.hh"
12 #include "debug.hh"
13 #include "notename.hh"
14 #include "source-file.hh"
15 #include "parseconstruct.hh"
17 static Keyword_ent the_key_tab[]={
18 "bar", BAR,
19 "cadenza", CADENZA,
20 "clef", CLEF,
21 "cm", CM_T,
22 "command", COMMAND,
23 "commands", COMMANDS,
24 "duration", DURATIONCOMMAND,
25 "geometric", GEOMETRIC,
26 "goto", GOTO,
27 "in", IN_T,
28 "key", KEY,
29 "meter", METER,
30 "midi", MIDI,
31 "mm", MM_T,
32 "multivoice", MULTIVOICE,
33 "octave", OCTAVECOMMAND,
34 "output", OUTPUT,
35 "partial", PARTIAL,
36 "paper", PAPER,
37 "plet", PLET,
38 "pt", PT_T,
39 "score", SCORE,
40 "script", SCRIPT,
41 "skip", SKIP,
42 "staff", STAFF,
43 "start", START_T,
44 "stem", STEM,
45 "table", TABLE,
46 "symboltables", SYMBOLTABLES,
47 "notenames", NOTENAMES,
48 "tempo", TEMPO,
49 "texid", TEXID,
50 "textstyle", TEXTSTYLE,
51 "unitspace", UNITSPACE,
52 "voice", VOICE,
53 "voices", VOICES,
54 "width", WIDTH,
55 "music", MUSIC,
56 "grouping", GROUPING,
57 0,0
60 int
61 My_flex_lexer::ret_notename(int *p, String text, int octave_mod)
63 text = text.lower_str();
64 char const* ch_c_l = here_ch_c_l();
65 if ( ch_c_l ) {
66 ch_c_l--;
67 while ( ( *ch_c_l == ' ' )
68 || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
69 ch_c_l--;
70 ch_c_l++;
73 lookup_notename(p[0], p[1], text);
74 p[2] = octave_mod;
75 mtor << "notename: "<< text <<eol;
76 if (p[0] < 0) {
78 errorlevel_i_ |= 1;
79 error( String( "notename does not exist: " ) + YYText(), ch_c_l );
80 p[0] = p[1] = 0;
82 return NOTENAME;
85 My_flex_lexer::My_flex_lexer()
87 keytable = new Keyword_table(the_key_tab);
88 the_id_tab = new Assoc<String, Identifier*>;
89 defaulttab = 0;
90 errorlevel_i_ = 0;
93 int
94 My_flex_lexer::lookup_keyword(String s)
96 return keytable->lookup(s);
99 Identifier*
100 My_flex_lexer::lookup_identifier(String s)
102 if (!the_id_tab->elt_query(s))
103 return 0;
105 return (*the_id_tab)[s];
108 char const*
109 My_flex_lexer::here_ch_c_l()
111 return include_stack.top()->sourcefile_l_->ch_c_l() + yyin->tellg();
114 void
115 My_flex_lexer::add_identifier(Identifier*i)
117 delete lookup_identifier(i->name);
118 (*the_id_tab)[i->name] = i;
121 My_flex_lexer::~My_flex_lexer()
123 delete keytable;
124 delete defaulttab;
125 for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
126 mtor << "deleting: " << ai.key()<<'\n';
127 delete ai.val();
129 delete the_id_tab;
131 void
132 My_flex_lexer::print_declarations()const
134 for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
135 ai.val()->print();
139 String
140 My_flex_lexer::spot()const
142 return include_stack.top()->name + ": " + String( lineno() );
145 void
146 My_flex_lexer::LexerError(const char *s)
148 if (lexer->include_stack.empty()) {
149 *mlog << "error at EOF" << s << '\n';
150 } else {
151 char const* ch_c_l = here_ch_c_l();
152 if ( ch_c_l ) {
153 ch_c_l--;
154 while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
155 ch_c_l--;
156 ch_c_l++;
158 errorlevel_i_ |= 1;
159 error( s, ch_c_l );
163 // set the new input to s, remember old file.
164 void
165 My_flex_lexer::new_input(String s)
167 if (!include_stack.empty()) {
168 include_stack.top()->line = lineno();
169 // should this be saved at all?
170 include_stack.top()->defined_ch_c_l_ = defined_ch_c_l;
173 Input_file *newin = new Input_file(s);
174 include_stack.push(newin);
175 switch_streams(newin->is);
177 yylineno = 1;
180 // pop the inputstack.
181 bool
182 My_flex_lexer::close_input()
184 Input_file *old = include_stack.pop();
185 bool ok = true;
186 if (include_stack.empty()) {
187 ok = false;
188 } else {
189 Input_file *i = include_stack.top();
190 switch_streams(i->is);
191 yylineno = i->line;
192 defined_ch_c_l = i->defined_ch_c_l_;
194 delete old;
195 return ok;