2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "lily-lexer.hh"
15 #include "international.hh"
16 #include "interval.hh"
21 #include "scm-hash.hh"
22 #include "source-file.hh"
24 #include "program-option.hh"
25 #include "lily-parser.hh"
27 static Keyword_ent the_key_tab
[]
30 {"addlyrics", ADDLYRICS
},
32 {"alternative", ALTERNATIVE
},
34 {"bookpart", BOOKPART
},
36 {"chordmode", CHORDMODE
},
38 {"consists", CONSISTS
},
41 {"defaultchild", DEFAULTCHILD
},
43 {"description", DESCRIPTION
},
44 {"drummode", DRUMMODE
},
46 {"figuremode", FIGUREMODE
},
48 {"grobdescriptions", GROBDESCRIPTIONS
},
52 {"lyricmode", LYRICMODE
},
54 {"lyricsto", LYRICSTO
},
57 {"markuplines", MARKUPLINES
},
61 {"notemode", NOTEMODE
},
62 {"objectid", OBJECTID
},
64 {"override", OVERRIDE
},
67 {"relative", RELATIVE
},
73 {"sequential", SEQUENTIAL
},
75 {"simultaneous", SIMULTANEOUS
},
80 {"transpose", TRANSPOSE
},
87 Lily_lexer::Lily_lexer (Sources
*sources
, Lily_parser
*parser
)
90 keytable_
= new Keyword_table (the_key_tab
);
91 chordmodifier_tab_
= SCM_EOL
;
92 pitchname_tab_stack_
= SCM_EOL
;
96 is_main_input_
= false;
97 start_module_
= SCM_EOL
;
98 chord_repetition_
= Chord_repetition ();
101 add_scope (ly_make_anonymous_module (false));
102 push_note_state (scm_c_make_hash_table (0));
103 chordmodifier_tab_
= scm_make_vector (scm_from_int (1), SCM_EOL
);
106 Lily_lexer::Lily_lexer (Lily_lexer
const &src
, Lily_parser
*parser
)
107 : Includable_lexer ()
110 keytable_
= (src
.keytable_
) ? new Keyword_table (*src
.keytable_
) : 0;
111 chordmodifier_tab_
= src
.chordmodifier_tab_
;
112 pitchname_tab_stack_
= src
.pitchname_tab_stack_
;
113 sources_
= src
.sources_
;
114 start_module_
= SCM_EOL
;
115 chord_repetition_
= Chord_repetition ();
117 error_level_
= src
.error_level_
;
118 is_main_input_
= src
.is_main_input_
;
124 SCM scopes
= SCM_EOL
;
126 for (SCM s
= src
.scopes_
; scm_is_pair (s
); s
= scm_cdr (s
))
128 SCM newmod
= ly_make_anonymous_module (false);
129 ly_module_copy (newmod
, scm_car (s
));
130 *tail
= scm_cons (newmod
, SCM_EOL
);
131 tail
= SCM_CDRLOC (*tail
);
135 push_note_state (scm_c_make_hash_table (0));
138 Lily_lexer::~Lily_lexer ()
144 Lily_lexer::add_scope (SCM module
)
146 ly_reexport_module (scm_current_module ());
147 if (!scm_is_pair (scopes_
))
148 start_module_
= scm_current_module ();
150 for (SCM s
= scopes_
; scm_is_pair (s
); s
= scm_cdr (s
))
151 ly_use_module (module
, scm_car (s
));
152 scopes_
= scm_cons (module
, scopes_
);
154 set_current_scope ();
157 Lily_lexer::has_scope () const
159 return scm_is_pair (scopes_
);
163 Lily_lexer::remove_scope ()
165 SCM sc
= scm_car (scopes_
);
166 scopes_
= scm_cdr (scopes_
);
167 set_current_scope ();
172 Lily_lexer::set_current_scope ()
174 SCM old
= scm_current_module ();
176 if (scm_is_pair (scopes_
))
177 scm_set_current_module (scm_car (scopes_
));
179 scm_set_current_module (start_module_
);
185 Lily_lexer::lookup_keyword (string s
)
187 return keytable_
->lookup (s
.c_str ());
191 Lily_lexer::keyword_list () const
198 for (vsize i
= 0; i
< keytable_
->table_
.size (); i
++)
200 *tail
= scm_acons (scm_from_locale_string (keytable_
->table_
[i
].name_
),
201 scm_from_int (keytable_
->table_
[i
].tokcode_
),
204 tail
= SCM_CDRLOC (*tail
);
211 Lily_lexer::lookup_identifier_symbol (SCM sym
)
213 for (SCM s
= scopes_
; scm_is_pair (s
); s
= scm_cdr (s
))
215 SCM var
= ly_module_lookup (scm_car (s
), sym
);
216 if (var
!= SCM_BOOL_F
)
217 return scm_variable_ref (var
);
220 return SCM_UNDEFINED
;
224 Lily_lexer::lookup_identifier (string name
)
226 return lookup_identifier_symbol (ly_symbol2scm (name
.c_str ()));
230 Lily_lexer::start_main_input ()
232 yy_flex_debug
= get_program_option ("debug-lexer");
233 parser_
->set_yydebug (get_program_option ("debug-parser"));
236 new_input (main_input_name_
, sources_
);
238 scm_module_define (scm_car (scopes_
),
239 ly_symbol2scm ("input-file-name"),
240 ly_string2scm (main_input_name_
));
244 Lily_lexer::new_input (string str
, string d
, Sources
*ss
)
246 Includable_lexer::new_input (str
, d
, ss
);
250 Lily_lexer::new_input (string str
, Sources
*ss
)
252 if (is_main_input_
&& be_safe_global
)
254 LexerError (_ ("include files are not allowed in safe mode").c_str ());
258 Includable_lexer::new_input (str
, ss
);
262 Lily_lexer::set_identifier (SCM name
, SCM s
)
265 if (scm_is_string (name
))
266 sym
= scm_string_to_symbol (name
);
268 if (scm_is_symbol (sym
))
270 if (lookup_keyword (ly_symbol2string (sym
)) >= 0)
272 string symstr
= ly_symbol2string (sym
);
273 warning (_f ("identifier name is a keyword: `%s'", symstr
.c_str ()));
276 SCM mod
= scm_car (scopes_
);
278 scm_module_define (mod
, sym
, s
);
281 programming_error ("identifier is not a symbol");
285 Lily_lexer::LexerError (char const *s
)
287 if (include_stack_
.empty ())
288 message (_f ("error at EOF: %s", s
) + "\n");
292 Input
spot (*lexloc
);
298 Lily_lexer::escaped_char (char c
) const
315 Lily_lexer::here_input () const
317 return Input (*lexloc
);
321 Lily_lexer::prepare_for_next_token ()
323 last_input_
= here_input ();
327 Since we don't create the buffer state from the bytes directly, we
328 don't know about the location of the lexer. Add this as a
331 Lily_lexer::add_lexed_char (int count
)
333 char const *start
= here_str0 ();
334 lexloc
->set (get_source_file (),
335 start
, start
+ count
);
336 char_count_stack_
.back () += count
;
339 #include "ly-smobs.icc"
341 IMPLEMENT_SMOBS (Lily_lexer
);
342 IMPLEMENT_TYPE_P (Lily_lexer
, "ly:lily-lexer?");
343 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer
);
346 Lily_lexer::mark_smob (SCM s
)
348 ASSERT_LIVE_IS_ALLOWED ();
350 Lily_lexer
*lexer
= (Lily_lexer
*) SCM_CELL_WORD_1 (s
);
352 scm_gc_mark (lexer
->chordmodifier_tab_
);
354 scm_gc_mark (lexer
->parser_
->self_scm ());
355 scm_gc_mark (lexer
->pitchname_tab_stack_
);
356 scm_gc_mark (lexer
->start_module_
);
357 return lexer
->scopes_
;
361 Lily_lexer::print_smob (SCM s
, SCM port
, scm_print_state
*)
363 Lily_lexer
*lexer
= Lily_lexer::unsmob (s
);
365 scm_puts ("#<Lily_lexer ", port
);
366 scm_display (lexer
->scopes_
, port
);
367 scm_puts (" >", port
);