lilypond-1.5.9
[lilypond.git] / lily / my-lily-lexer.cc
blob9c07514277d086f51970fbc136023dbfc25d241b
1 /*
2 my-lily-lexer.cc -- implement My_lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <strstream.h>
10 #include <ctype.h>
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
16 #include "lily-guile.hh"
17 #include "parser.hh"
18 #include "keyword.hh"
19 #include "my-lily-lexer.hh"
20 #include "debug.hh"
21 #include "source-file.hh"
22 #include "main.hh"
23 #include "scope.hh"
24 #include "input.hh"
25 #include "moment.hh"
27 static Keyword_ent the_key_tab[]={
28 {"alias", ALIAS},
29 {"apply", APPLY},
30 {"arpeggio", ARPEGGIO },
31 {"autochange", AUTOCHANGE},
32 {"spanrequest", SPANREQUEST},
33 {"commandspanrequest", COMMANDSPANREQUEST},
34 {"simultaneous", SIMULTANEOUS},
35 {"sequential", SEQUENTIAL},
36 {"accepts", ACCEPTS},
37 {"alternative", ALTERNATIVE},
38 {"bar", BAR},
39 {"breathe", BREATHE},
40 {"char", CHAR_T},
41 {"chordmodifiers", CHORDMODIFIERS},
42 {"chords", CHORDS},
43 {"clef", CLEF},
44 {"cm", CM_T},
45 {"consists", CONSISTS},
46 {"consistsend", CONSISTSEND},
47 {"context", CONTEXT},
48 {"default", DEFAULT},
49 {"denies", DENIES},
50 {"duration", DURATION},
51 {"dynamicscript", DYNAMICSCRIPT},
52 {"elementdescriptions", ELEMENTDESCRIPTIONS},
53 {"font", FONT},
54 {"grace", GRACE},
55 {"ngrace", NGRACE},
56 {"glissando", GLISSANDO},
57 {"header", HEADER},
58 {"in", IN_T},
59 {"lyrics", LYRICS},
60 {"key", KEY},
61 {"mark", MARK},
62 {"pitch", PITCH},
63 {"time", TIME_T},
64 {"times", TIMES},
65 {"midi", MIDI},
66 {"mm", MM_T},
67 {"name", NAME},
68 {"pitchnames", PITCHNAMES},
69 {"notes", NOTES},
70 {"outputproperty", OUTPUTPROPERTY},
71 {"override", OVERRIDE},
72 {"set", SET},
73 {"revert", REVERT},
74 {"partial", PARTIAL},
75 {"paper", PAPER},
76 {"penalty", PENALTY},
77 {"property", PROPERTY},
78 {"pt", PT_T},
79 {"relative", RELATIVE},
80 {"remove", REMOVE},
81 {"repeat", REPEAT},
82 {"addlyrics", ADDLYRICS},
83 {"partcombine", PARTCOMBINE},
84 {"score", SCORE},
85 {"script", SCRIPT},
86 {"stylesheet", STYLESHEET},
87 {"skip", SKIP},
88 {"tempo", TEMPO},
89 {"translator", TRANSLATOR},
90 {"transpose", TRANSPOSE},
91 {"type", TYPE},
92 {"unset", UNSET},
93 {0,0}
96 My_lily_lexer::My_lily_lexer ()
98 keytable_p_ = new Keyword_table (the_key_tab);
99 toplevel_variable_tab_ = new Scheme_hash_table ;
100 scope_p_ = new Scope (toplevel_variable_tab_);
102 scope_l_arr_.push (scope_p_);
104 errorlevel_i_ = 0;
105 main_input_b_ = false;
109 My_lily_lexer::lookup_keyword (String s)
111 return keytable_p_->lookup (s.ch_C ());
115 My_lily_lexer::lookup_identifier (String s)
117 SCM sym = ly_symbol2scm (s.ch_C ());
119 for (int i = scope_l_arr_.size (); i--;)
121 SCM val = SCM_UNSPECIFIED;
122 if (scope_l_arr_[i]->try_retrieve (sym, &val))
123 return val;
125 return SCM_UNSPECIFIED;
128 void
129 My_lily_lexer::start_main_input ()
131 new_input (main_input_str_, source_global_l);
132 allow_includes_b_ = allow_includes_b_ && ! (safe_global_b);
135 void
136 My_lily_lexer::set_identifier (String name_str, SCM s)
138 if (lookup_keyword (name_str) >= 0)
140 warning (_f ("Identifier name is a keyword: `%s'", name_str));
143 scope_l_arr_.top ()->set (name_str, s);
146 My_lily_lexer::~My_lily_lexer ()
148 delete keytable_p_;
149 scm_gc_unprotect_object (toplevel_variable_tab_->self_scm ());
150 delete scope_p_ ;
155 void
156 My_lily_lexer::LexerError (char const *s)
158 if (include_stack_.empty ())
160 progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
162 else
164 errorlevel_i_ |= 1;
165 Input spot (source_file_l (),here_ch_C ());
166 spot.error (s);
170 char
171 My_lily_lexer::escaped_char (char c) const
173 switch (c)
175 case 'n':
176 return '\n';
177 case 't':
178 return '\t';
180 case '\'':
181 case '\"':
182 case '\\':
183 return c;
185 return 0;
188 Input
189 My_lily_lexer::here_input () const
191 Source_file * f_l= source_file_l ();
192 return Input (f_l, (char*)here_ch_C ());