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