]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 0.0.43
[lilypond.git] / lily / my-lily-lexer.cc
1 #include <strstream.h>
2
3 #include "interval.hh"
4 #include "identifier.hh"
5 #include "assoc-iter.hh"
6 #include "input-file.hh"
7 #include "out/parser.hh"
8 #include "keyword.hh"
9 #include "assoc.hh"
10 #include "my-lily-lexer.hh"
11 #include "debug.hh"
12 #include "source-file.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     "duration", DURATIONCOMMAND,
21     "dynamic", DYNAMIC,
22     "geometric", GEOMETRIC,
23     "in", IN_T,
24     "lyrics", LYRICS,
25     "key", KEY,
26     "melodic" , MELODIC,
27     "meter", METER,
28     "midi", MIDI,
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     "tempo", TEMPO,
46     "texid", TEXID,
47     "textstyle", TEXTSTYLE,
48     "transpose", TRANSPOSE,
49     "unitspace", UNITSPACE,
50     "width", WIDTH,
51     "music", MUSIC,
52     "grouping", GROUPING,
53     0,0
54 };
55
56 My_lily_lexer::My_lily_lexer()
57 {
58     keytable_p_ = new Keyword_table(the_key_tab);
59     identifier_assoc_p_ = new Assoc<String, Identifier*>;
60     errorlevel_i_ = 0;
61 }
62
63 int
64 My_lily_lexer::lookup_keyword(String s)
65 {
66     return keytable_p_->lookup(s);
67 }
68
69 Identifier*
70 My_lily_lexer::lookup_identifier(String s)
71 {
72     if (!identifier_assoc_p_->elt_query(s))
73         return 0;
74     
75     return (*identifier_assoc_p_)[s];
76 }
77
78 char const*
79 My_lily_lexer::here_ch_c_l()
80 {
81     return include_stack_.top()->sourcefile_l_->ch_c_l() + yyin->tellg();
82 }
83
84 void
85 My_lily_lexer::add_identifier(Identifier*i)
86 {
87     delete lookup_identifier(i->name);
88     (*identifier_assoc_p_)[i->name] = i;
89 }
90
91 My_lily_lexer::~My_lily_lexer()
92 {
93     delete keytable_p_;
94
95     for (Assoc_iter<String,Identifier*>
96              ai(*identifier_assoc_p_); ai.ok(); ai++) {
97         mtor << "deleting: " << ai.key()<<'\n';
98         Identifier *i_p = ai.val();
99         if (!i_p->accessed_b_ && !i_p->init_b_)
100             warning("Variable not used", i_p->defined_ch_C_);
101         
102         delete ai.val();
103     }
104     delete identifier_assoc_p_;
105 }
106 void
107 My_lily_lexer::print_init_declarations()const
108 {
109     for (Assoc_iter<String,Identifier*> ai(*identifier_assoc_p_); ai.ok(); 
110          ai++) {
111         if (ai.val()->init_b_)
112             ai.val()->print();
113     }
114 }
115 void
116 My_lily_lexer::print_user_declarations()const
117 {
118     for (Assoc_iter<String,Identifier*> ai(*identifier_assoc_p_); ai.ok(); ai++) {
119         if (!ai.val()->init_b_)
120             ai.val()->print();
121     }
122 }
123
124 String
125 My_lily_lexer::spot()const
126 {
127     return include_stack_.top()->name +  ": " + String( lineno() );
128 }
129
130 void
131 My_lily_lexer::LexerError(char const *s)
132 {
133     if (lexer->include_stack_.empty()) {
134         *mlog << "error at EOF" << s << '\n';
135     } else {
136         char const* ch_c_l = here_ch_c_l();
137         if ( ch_c_l ) {
138             ch_c_l--;
139             while ( ( *ch_c_l == ' ' ) || ( *ch_c_l == '\t' ) || ( *ch_c_l == '\n' ) )
140                     ch_c_l--;
141             ch_c_l++;
142         }
143         errorlevel_i_ |= 1;
144         error( s, ch_c_l );
145     }
146 }
147
148 // set the  new input to s, remember old file.
149 void
150 My_lily_lexer::new_input(String s)
151 {    
152    if (!include_stack_.empty()) {
153         include_stack_.top()->line = lineno();
154              // should this be saved at all?
155         include_stack_.top()->defined_ch_c_l_ = defined_ch_c_l;
156    }
157
158    Input_file *newin = new Input_file(s);
159    include_stack_.push(newin);
160    switch_streams(newin->is);
161
162    yylineno = 1;
163 }
164
165 // pop the inputstack.
166 bool
167 My_lily_lexer::close_input()
168 {
169     Input_file *old = include_stack_.pop();
170      bool ok =  true;
171     if (include_stack_.empty()) {
172         ok = false;
173     } else {
174         Input_file *i = include_stack_.top();
175         switch_streams(i->is);
176         yylineno = i->line;     
177         defined_ch_c_l = i->defined_ch_c_l_;
178     }
179     delete old;
180     return ok;
181 }