]> git.donarmstrong.com Git - lilypond.git/blob - src/mylexer.cc
release: 0.0.38
[lilypond.git] / src / mylexer.cc
1 #include <strstream.h>
2
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"
16
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
58 };
59
60 int
61 My_flex_lexer::ret_notename(int *p, String text, int octave_mod)
62 {
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++;
71     }
72         
73     lookup_notename(p[0], p[1], text);
74     p[2] = octave_mod;
75     mtor << "notename: "<< text <<eol;
76     if (p[0] < 0) {
77
78         errorlevel_i_ |= 1;
79         error( String( "notename does not exist: " ) + YYText(), ch_c_l );
80         p[0] = p[1] = 0;
81     }
82     return NOTENAME;
83 }
84
85 My_flex_lexer::My_flex_lexer()
86 {
87     keytable = new Keyword_table(the_key_tab);
88     the_id_tab = new Assoc<String, Identifier*>;
89     defaulttab = 0;
90     errorlevel_i_ = 0;
91 }
92
93 int
94 My_flex_lexer::lookup_keyword(String s)
95 {
96     return keytable->lookup(s);
97 }
98
99 Identifier*
100 My_flex_lexer::lookup_identifier(String s)
101 {
102     if (!the_id_tab->elt_query(s))
103         return 0;
104     
105     return (*the_id_tab)[s];
106 }
107
108 char const*
109 My_flex_lexer::here_ch_c_l()
110 {
111     return include_stack.top()->sourcefile_l_->ch_c_l() + yyin->tellg();
112 }
113
114 void
115 My_flex_lexer::add_identifier(Identifier*i)
116 {
117     delete lookup_identifier(i->name);
118     (*the_id_tab)[i->name] = i;
119 }
120
121 My_flex_lexer::~My_flex_lexer()
122 {
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();
128     }
129     delete the_id_tab;
130 }
131 void
132 My_flex_lexer::print_declarations()const
133 {
134     for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
135         ai.val()->print();
136     }
137 }
138
139 String
140 My_flex_lexer::spot()const
141 {
142     return include_stack.top()->name +  ": " + String( lineno() );
143 }
144
145 void
146 My_flex_lexer::LexerError(const char *s)
147 {
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++;
157         }
158         errorlevel_i_ |= 1;
159         error( s, ch_c_l );
160     }
161 }
162
163 // set the  new input to s, remember old file.
164 void
165 My_flex_lexer::new_input(String s)
166 {    
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;
171    }
172
173    Input_file *newin = new Input_file(s);
174    include_stack.push(newin);
175    switch_streams(newin->is);
176
177    yylineno = 1;
178 }
179
180 // pop the inputstack.
181 bool
182 My_flex_lexer::close_input()
183 {
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_;
193     }
194     delete old;
195     return ok;
196 }