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