]> git.donarmstrong.com Git - lilypond.git/blob - src/mylexer.cc
de6fcc7238c90ad370eae012f44424c1d76bc4a5
[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     "mark", MARK,
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
55 };
56
57 int
58 My_flex_lexer::ret_notename(int *p, String text, int octave_mod)
59 {
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++;
66         
67     lookup_notename(p[0], p[1], text);
68     p[2] = octave_mod;
69     mtor << "notename: "<< text <<eol;
70     if (p[0] < 0) {
71
72         errorlevel_i_ |= 1;
73         warning( String( "notename does not exist: " ) + YYText(), ch_c_l );
74         p[0] = p[1] = 0;
75     }
76     return NOTENAME;
77 }
78
79 My_flex_lexer::My_flex_lexer()
80 {
81     keytable = new Keyword_table(the_key_tab);
82     the_id_tab = new Assoc<String, Identifier*>;
83     defaulttab = 0;
84     data_ch_c_l_m = 0;
85     errorlevel_i_ = 0;
86 }
87
88 int
89 My_flex_lexer::lookup_keyword(String s)
90 {
91     return keytable->lookup(s);
92 }
93
94 Identifier*
95 My_flex_lexer::lookup_identifier(String s)
96 {
97     if (!the_id_tab->elt_query(s))
98         return 0;
99     
100     return (*the_id_tab)[s];
101 }
102
103 char const*
104 My_flex_lexer::here_ch_c_l()
105 {
106     return data_ch_c_l_m ? data_ch_c_l_m + yyin->tellg() : 0;
107 }
108
109 void
110 My_flex_lexer::add_identifier(Identifier*i)
111 {
112     delete lookup_identifier(i->name);
113     (*the_id_tab)[i->name] = i;
114 }
115
116 My_flex_lexer::~My_flex_lexer()
117 {
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();
123     }
124     delete the_id_tab;
125 }
126
127 String
128 My_flex_lexer::spot()const
129 {
130     return include_stack.top()->name +  ": " + String( lineno() );
131 }
132
133 void
134 My_flex_lexer::LexerError(const char *s)
135 {
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++;
145         }
146         errorlevel_i_ |= 1;
147         warning( s, ch_c_l );
148     }
149 }
150
151 // set the  new input to s, remember old file.
152 void
153 My_flex_lexer::new_input(String s)
154 {    
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_m = defined_ch_c_l;
159    }
160
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_m = newin->sourcefile_l_->ch_c_l();
166    else
167        data_ch_c_l_m = 0;
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_m;
185     }
186     delete old;
187     return ok;
188 }