]> git.donarmstrong.com Git - lilypond.git/blob - src/mylexer.cc
ea86b3094b4d4d1a6b1b3901917eb6ba6076a247
[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 "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"
14
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,
27
28     "meter", METER,
29     "midi", MIDI,
30     "mm", MM_T,
31     "multivoice", MULTIVOICE,
32     "octave", OCTAVECOMMAND,
33     "output", OUTPUT,
34     "partial", PARTIAL,
35     "paper", PAPER,
36     "plet", PLET,
37     "pt", PT_T,
38     "score", SCORE,
39     "script", SCRIPT,
40     "skip", SKIP,
41     "staff", STAFF,
42     "start", START_T,
43     "stem", STEM,
44     "table", TABLE,
45     "symboltables", SYMBOLTABLES,
46     "notenames", NOTENAMES,
47     "tempo", TEMPO,
48     "texid", TEXID,
49     "textstyle", TEXTSTYLE,
50     "unitspace", UNITSPACE,
51     "voice", VOICE,
52     "voices", VOICES,
53     "width", WIDTH,
54     "music", MUSIC,
55     "grouping", GROUPING,
56     0,0
57 };
58
59 int
60 My_flex_lexer::ret_notename(int *p, String text, int octave_mod)
61 {
62     text.lower();
63     char const* ch_c_l = here_ch_c_l();
64     if ( ch_c_l ) {
65         ch_c_l--;
66         while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
67             ch_c_l--;
68         ch_c_l++;
69     }
70         
71     lookup_notename(p[0], p[1], text);
72     p[2] = octave_mod;
73     mtor << "notename: "<< text <<eol;
74     if (p[0] < 0) {
75
76         errorlevel_i_ |= 1;
77         error( String( "notename does not exist: " ) + YYText(), ch_c_l );
78         p[0] = p[1] = 0;
79     }
80     return NOTENAME;
81 }
82
83 My_flex_lexer::My_flex_lexer()
84 {
85     keytable = new Keyword_table(the_key_tab);
86     the_id_tab = new Assoc<String, Identifier*>;
87     defaulttab = 0;
88     errorlevel_i_ = 0;
89 }
90
91 int
92 My_flex_lexer::lookup_keyword(String s)
93 {
94     return keytable->lookup(s);
95 }
96
97 Identifier*
98 My_flex_lexer::lookup_identifier(String s)
99 {
100     if (!the_id_tab->elt_query(s))
101         return 0;
102     
103     return (*the_id_tab)[s];
104 }
105
106 char const*
107 My_flex_lexer::here_ch_c_l()
108 {
109     return include_stack.top()->sourcefile_l_->ch_c_l() + yyin->tellg();
110 }
111
112 void
113 My_flex_lexer::add_identifier(Identifier*i)
114 {
115     delete lookup_identifier(i->name);
116     (*the_id_tab)[i->name] = i;
117 }
118
119 My_flex_lexer::~My_flex_lexer()
120 {
121     delete keytable;
122     delete defaulttab;
123     for (Assoc_iter<String,Identifier*> ai(*the_id_tab); ai.ok(); ai++) {
124         mtor << "deleting: " << ai.key()<<'\n';
125         delete ai.val();
126     }
127     delete the_id_tab;
128 }
129
130 String
131 My_flex_lexer::spot()const
132 {
133     return include_stack.top()->name +  ": " + String( lineno() );
134 }
135
136 void
137 My_flex_lexer::LexerError(const char *s)
138 {
139     if (lexer->include_stack.empty()) {
140         *mlog << "error at EOF" << s << '\n';
141     } else {
142         char const* ch_c_l = here_ch_c_l();
143         if ( ch_c_l ) {
144             ch_c_l--;
145             while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
146                     ch_c_l--;
147             ch_c_l++;
148         }
149         errorlevel_i_ |= 1;
150         error( s, ch_c_l );
151     }
152 }
153
154 // set the  new input to s, remember old file.
155 void
156 My_flex_lexer::new_input(String s)
157 {    
158    if (!include_stack.empty()) {
159         include_stack.top()->line = lineno();
160              // should this be saved at all?
161         include_stack.top()->defined_ch_c_l_ = defined_ch_c_l;
162    }
163
164    Input_file *newin = new Input_file(s);
165    include_stack.push(newin);
166    switch_streams(newin->is);
167
168    yylineno = 1;
169 }
170
171 // pop the inputstack.
172 bool
173 My_flex_lexer::close_input()
174 {
175     Input_file *old = include_stack.pop();
176      bool ok =  true;
177     if (include_stack.empty()) {
178         ok = false;
179     } else {
180         Input_file *i = include_stack.top();
181         switch_streams(i->is);
182         yylineno = i->line;     
183         defined_ch_c_l = i->defined_ch_c_l_;
184     }
185     delete old;
186     return ok;
187 }