]> git.donarmstrong.com Git - lilypond.git/blob - src/mylexer.cc
release: 0.0.34
[lilypond.git] / src / mylexer.cc
1 #include <strstream.h>
2 #include "interval.hh"
3 #include "identparent.hh"
4 #include "associter.hh"
5 #include "lexer.hh"
6 #include "inputfile.hh"
7 #include "parser.hh"
8 #include "keyword.hh"
9 #include "assoc.hh"
10 #include "lexer.hh"
11 #include "debug.hh"
12 #include "notename.hh"
13 #include "sourcefile.hh"
14 #include "parseconstruct.hh"
15
16 static Keyword_ent the_key_tab[]={
17     "bar", BAR,
18     "cadenza", CADENZA,
19     "clef", CLEF,
20     "cm", CM_T,
21     "command", COMMAND,
22     "commands", COMMANDS,
23     "duration", DURATIONCOMMAND,
24     "geometric", GEOMETRIC,
25     "goto", GOTO,
26     "in", IN_T,
27     "key", KEY,
28
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.lower();
64     char const* ch_c_l = here_ch_c_l();
65     if ( ch_c_l ) {
66         ch_c_l--;
67         while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
68             ch_c_l--;
69         ch_c_l++;
70     }
71         
72     lookup_notename(p[0], p[1], text);
73     p[2] = octave_mod;
74     mtor << "notename: "<< text <<eol;
75     if (p[0] < 0) {
76
77         errorlevel_i_ |= 1;
78         error( String( "notename does not exist: " ) + YYText(), ch_c_l );
79         p[0] = p[1] = 0;
80     }
81     return NOTENAME;
82 }
83
84 My_flex_lexer::My_flex_lexer()
85 {
86     keytable = new Keyword_table(the_key_tab);
87     the_id_tab = new Assoc<String, Identifier*>;
88     defaulttab = 0;
89     errorlevel_i_ = 0;
90 }
91
92 int
93 My_flex_lexer::lookup_keyword(String s)
94 {
95     return keytable->lookup(s);
96 }
97
98 Identifier*
99 My_flex_lexer::lookup_identifier(String s)
100 {
101     if (!the_id_tab->elt_query(s))
102         return 0;
103     
104     return (*the_id_tab)[s];
105 }
106
107 char const*
108 My_flex_lexer::here_ch_c_l()
109 {
110     return include_stack.top()->sourcefile_l_->ch_c_l() + yyin->tellg();
111 }
112
113 void
114 My_flex_lexer::add_identifier(Identifier*i)
115 {
116     delete lookup_identifier(i->name);
117     (*the_id_tab)[i->name] = i;
118 }
119
120 My_flex_lexer::~My_flex_lexer()
121 {
122     delete keytable;
123     delete defaulttab;
124     for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
125         mtor << "deleting: " << ai.key()<<'\n';
126         delete ai.val();
127     }
128     delete the_id_tab;
129 }
130
131 String
132 My_flex_lexer::spot()const
133 {
134     return include_stack.top()->name +  ": " + String( lineno() );
135 }
136
137 void
138 My_flex_lexer::LexerError(const char *s)
139 {
140     if (lexer->include_stack.empty()) {
141         *mlog << "error at EOF" << s << '\n';
142     } else {
143         char const* ch_c_l = here_ch_c_l();
144         if ( ch_c_l ) {
145             ch_c_l--;
146             while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
147                     ch_c_l--;
148             ch_c_l++;
149         }
150         errorlevel_i_ |= 1;
151         error( s, ch_c_l );
152     }
153 }
154
155 // set the  new input to s, remember old file.
156 void
157 My_flex_lexer::new_input(String s)
158 {    
159    if (!include_stack.empty()) {
160         include_stack.top()->line = lineno();
161              // should this be saved at all?
162         include_stack.top()->defined_ch_c_l_ = defined_ch_c_l;
163    }
164
165    Input_file *newin = new Input_file(s);
166    include_stack.push(newin);
167    switch_streams(newin->is);
168
169    yylineno = 1;
170 }
171
172 // pop the inputstack.
173 bool
174 My_flex_lexer::close_input()
175 {
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_;
185     }
186     delete old;
187     return ok;
188 }