2002->2003
[lilypond.git] / lily / my-lily-lexer.cc
blob2463a121e75b14dce307198767b490dd892bf849
1 /*
2 my-lily-lexer.cc -- implement My_lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <ctype.h>
10 #include <sstream>
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
15 #include "input-file-results.hh"
16 #include "lily-guile.hh"
17 #include "parser.hh"
18 #include "keyword.hh"
19 #include "my-lily-lexer.hh"
20 #include "warn.hh"
21 #include "source-file.hh"
22 #include "main.hh"
23 #include "input.hh"
24 #include "moment.hh"
25 #include "ly-modules.hh"
28 static Keyword_ent the_key_tab[]={
29 {"alias", ALIAS},
30 {"apply", APPLY},
31 {"applycontext", APPLYCONTEXT},
32 {"autochange", AUTOCHANGE},
33 {"simultaneous", SIMULTANEOUS},
34 {"sequential", SEQUENTIAL},
35 {"accepts", ACCEPTS},
36 {"alternative", ALTERNATIVE},
37 {"bar", BAR},
38 {"breathe", BREATHE},
39 {"chordmodifiers", CHORDMODIFIERS},
40 {"chordnames", CHORDNAMES},
41 {"chords", CHORDS},
42 {"clef", CLEF},
43 {"consists", CONSISTS},
44 {"consistsend", CONSISTSEND},
45 {"context", CONTEXT},
46 {"default", DEFAULT},
47 {"denies", DENIES},
48 {"duration", DURATION},
49 {"grobdescriptions", GROBDESCRIPTIONS},
50 {"figures",FIGURES},
51 {"grace", GRACE},
52 {"header", HEADER},
53 {"lyrics", LYRICS},
54 {"key", KEY},
55 {"mark", MARK},
56 {"markup", MARKUP},
57 {"once", ONCE},
58 {"pitch", PITCH},
59 {"time", TIME_T},
60 {"times", TIMES},
61 {"midi", MIDI},
62 {"name", NAME},
63 {"pitchnames", PITCHNAMES},
64 {"notes", NOTES},
65 {"outputproperty", OUTPUTPROPERTY},
66 {"override", OVERRIDE},
67 {"set", SET},
68 {"rest", REST},
69 {"revert", REVERT},
70 {"partial", PARTIAL},
71 {"paper", PAPER},
72 {"property", PROPERTY},
73 {"relative", RELATIVE},
74 {"remove", REMOVE},
75 {"repeat", REPEAT},
76 {"addlyrics", ADDLYRICS},
77 {"partcombine", PARTCOMBINE},
78 {"score", SCORE},
79 {"skip", SKIP},
80 {"tempo", TEMPO},
81 {"translator", TRANSLATOR},
82 {"transpose", TRANSPOSE},
83 {"type", TYPE},
84 {"unset", UNSET},
85 {0,0}
89 My_lily_lexer::My_lily_lexer ()
91 keytable_ = new Keyword_table (the_key_tab);
92 scopes_ = SCM_EOL;
94 add_scope(ly_make_anonymous_module());
95 errorlevel_ =0;
97 main_input_b_ = false;
100 void
101 My_lily_lexer::add_scope (SCM module)
103 ly_reexport_module (scm_current_module());
104 scm_set_current_module (module);
105 for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
108 UGH. how to do this more neatly?
110 SCM expr = scm_list_n (ly_symbol2scm ("module-use!"),
111 module, scm_list_n (ly_symbol2scm ("module-public-interface"),
112 gh_car (s), SCM_UNDEFINED),
113 SCM_UNDEFINED);
115 scm_primitive_eval(expr);
118 scopes_ = scm_cons (module, scopes_);
122 My_lily_lexer::remove_scope ()
124 SCM sc = gh_car (scopes_);
125 scopes_ = gh_cdr (scopes_);
126 scm_set_current_module (gh_car (scopes_));
128 return sc;
133 My_lily_lexer::lookup_keyword (String s)
135 return keytable_->lookup (s.to_str0 ());
139 My_lily_lexer::lookup_identifier (String s)
141 SCM sym = ly_symbol2scm (s.to_str0());
142 for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
144 SCM var = ly_module_lookup (gh_car (s), sym);
145 if (var != SCM_BOOL_F)
146 return scm_variable_ref(var);
149 return SCM_UNSPECIFIED;
152 void
153 My_lily_lexer::start_main_input ()
155 new_input (main_input_string_, &global_input_file->sources_);
156 allow_includes_b_ = allow_includes_b_ && ! (safe_global_b);
159 void
160 My_lily_lexer::set_identifier (SCM name, SCM s)
162 assert (gh_string_p (name));
164 if (lookup_keyword (ly_scm2string (name)) >= 0)
166 size_t sz;
167 char * str = gh_scm2newstr (name, &sz) ;
168 warning (_f ("Identifier name is a keyword: `%s'", str));
169 free (str);
172 SCM sym = scm_string_to_symbol (name);
173 SCM mod = gh_car (scopes_);
175 scm_module_define (mod, sym, s);
178 My_lily_lexer::~My_lily_lexer ()
180 delete keytable_;
185 void
186 My_lily_lexer::LexerError (char const *s)
188 if (include_stack_.empty ())
190 progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
192 else
194 errorlevel_ |= 1;
195 Input spot (get_source_file (), here_str0 ());
196 spot.error (s);
200 char
201 My_lily_lexer::escaped_char (char c) const
203 switch (c)
205 case 'n':
206 return '\n';
207 case 't':
208 return '\t';
210 case '\'':
211 case '\"':
212 case '\\':
213 return c;
215 return 0;
218 Input
219 My_lily_lexer::here_input () const
221 Source_file * f= get_source_file ();
222 return Input (f, (char*)here_str0 ());
225 void
226 My_lily_lexer::prepare_for_next_token ()
228 last_input_ = here_input();
231 #if 0
233 My_lily_lexer::scan_markup_word (String s)
236 TODO: better implementation:
238 - make a table of markup functions, for quicker lookup
240 - error handling.
243 SCM s = scm_c_eval_str ((s + "-markup").to_str0());
244 yylval.scm = s;
245 return MARKUP_HEAD;
247 #endif