lilypond-0.0.32
[lilypond.git] / src / mylexer.cc
blob7af820ae449efcfc859a7d71a7c6e225b36ab1b6
1 #include <strstream.h>
2 #include "interval.hh"
3 #include "identparent.hh"
4 #include "associter.hh"
5 #include "lexer.hh"
6 #include "parser.hh"
7 #include "keyword.hh"
8 #include "assoc.hh"
9 #include "lexer.hh"
10 #include "debug.hh"
11 #include "notename.hh"
12 #include "sourcefile.hh"
13 #include "parseconstruct.hh"
15 static Keyword_ent the_key_tab[]={
16 "bar", BAR,
17 "cadenza", CADENZA,
18 "clef", CLEF,
19 "cm", CM_T,
20 "command", COMMAND,
21 "commands", COMMANDS,
22 "duration", DURATIONCOMMAND,
23 "geometric", GEOMETRIC,
24 "goto", GOTO,
25 "in", IN_T,
26 "key", KEY,
28 "meter", METER,
29 "mm", MM_T,
30 "multivoice", MULTIVOICE,
31 "octave", OCTAVECOMMAND,
32 "output", OUTPUT,
33 "partial", PARTIAL,
34 "paper", PAPER,
35 "plet", PLET,
36 "pt", PT_T,
37 "score", SCORE,
38 "script", SCRIPT,
39 "skip", SKIP,
40 "staff", STAFF,
41 "start", START_T,
42 "stem", STEM,
43 "table", TABLE,
44 "symboltables", SYMBOLTABLES,
45 "notenames", NOTENAMES,
46 "texid", TEXID,
47 "textstyle", TEXTSTYLE,
48 "unitspace", UNITSPACE,
49 "voice", VOICE,
50 "voices", VOICES,
51 "width", WIDTH,
52 "music", MUSIC,
53 "grouping", GROUPING,
54 0,0
57 int
58 My_flex_lexer::ret_notename(int *p, String text, int octave_mod)
60 text.lower();
61 char const* ch_c_l = here_ch_c_l();
62 ch_c_l--;
63 while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
64 ch_c_l--;
65 ch_c_l++;
67 lookup_notename(p[0], p[1], text);
68 p[2] = octave_mod;
69 mtor << "notename: "<< text <<eol;
70 if (p[0] < 0) {
72 errorlevel_i_ |= 1;
73 warning( String( "notename does not exist: " ) + YYText(), ch_c_l );
74 p[0] = p[1] = 0;
76 return NOTENAME;
79 My_flex_lexer::My_flex_lexer()
81 keytable = new Keyword_table(the_key_tab);
82 the_id_tab = new Assoc<String, Identifier*>;
83 defaulttab = 0;
84 data_ch_c_l_ = 0;
85 errorlevel_i_ = 0;
88 int
89 My_flex_lexer::lookup_keyword(String s)
91 return keytable->lookup(s);
94 Identifier*
95 My_flex_lexer::lookup_identifier(String s)
97 if (!the_id_tab->elt_query(s))
98 return 0;
100 return (*the_id_tab)[s];
103 char const*
104 My_flex_lexer::here_ch_c_l()
106 return data_ch_c_l_ ? data_ch_c_l_ + yyin->tellg() : 0;
109 void
110 My_flex_lexer::add_identifier(Identifier*i)
112 delete lookup_identifier(i->name);
113 (*the_id_tab)[i->name] = i;
116 My_flex_lexer::~My_flex_lexer()
118 delete keytable;
119 delete defaulttab;
120 for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
121 mtor << "deleting: " << ai.key()<<'\n';
122 delete ai.val();
124 delete the_id_tab;
127 String
128 My_flex_lexer::spot()const
130 return include_stack.top()->name + ": " + String( lineno() );
133 void
134 My_flex_lexer::LexerError(const char *s)
136 if (lexer->include_stack.empty()) {
137 *mlog << "error at EOF" << s << '\n';
138 } else {
139 char const* ch_c_l = here_ch_c_l();
140 if ( ch_c_l ) {
141 ch_c_l--;
142 while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
143 ch_c_l--;
144 ch_c_l++;
146 errorlevel_i_ |= 1;
147 warning( s, ch_c_l );
151 // set the new input to s, remember old file.
152 void
153 My_flex_lexer::new_input(String s)
155 if (!include_stack.empty()) {
156 include_stack.top()->line = lineno();
157 // should this be saved at all?
158 include_stack.top()->defined_ch_c_l_ = defined_ch_c_l;
161 Input_file *newin = new Input_file(s);
162 include_stack.push(newin);
163 switch_streams(newin->is);
164 if ( newin->sourcefile_l_ )
165 data_ch_c_l_ = newin->sourcefile_l_->ch_c_l();
166 else
167 data_ch_c_l_ = 0;
169 yylineno = 1;
172 // pop the inputstack.
173 bool
174 My_flex_lexer::close_input()
176 Input_file *old = include_stack.pop();
177 bool ok = true;
178 if (include_stack.empty()) {
179 ok = false;
180 } else {
181 Input_file *i = include_stack.top();
182 switch_streams(i->is);
183 yylineno = i->line;
184 defined_ch_c_l = i->defined_ch_c_l_;
186 delete old;
187 return ok;