lilypond-1.3.145
[lilypond.git] / lily / my-lily-lexer.cc
blob37488771457f11ff0d503b0328f6497be8f869c1
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 {"denies", DENIES},
49 {"duration", DURATION},
50 {"dynamicscript", DYNAMICSCRIPT},
51 {"elementdescriptions", ELEMENTDESCRIPTIONS},
52 {"font", FONT},
53 {"grace", GRACE},
54 {"glissando", GLISSANDO},
55 {"header", HEADER},
56 {"in", IN_T},
57 {"lyrics", LYRICS},
58 {"key", KEY},
59 {"mark", MARK},
60 {"pitch", PITCH},
61 {"time", TIME_T},
62 {"times", TIMES},
63 {"midi", MIDI},
64 {"mm", MM_T},
65 {"name", NAME},
66 {"pitchnames", PITCHNAMES},
67 {"notes", NOTES},
68 {"outputproperty", OUTPUTPROPERTY},
69 {"override", OVERRIDE},
70 {"set", SET},
71 {"revert", REVERT},
72 {"partial", PARTIAL},
73 {"paper", PAPER},
74 {"penalty", PENALTY},
75 {"property", PROPERTY},
76 {"pt", PT_T},
77 {"relative", RELATIVE},
78 {"remove", REMOVE},
79 {"repeat", REPEAT},
80 {"addlyrics", ADDLYRICS},
81 {"partcombine", PARTCOMBINE},
82 {"score", SCORE},
83 {"script", SCRIPT},
84 {"stylesheet", STYLESHEET},
85 {"skip", SKIP},
86 {"tempo", TEMPO},
87 {"translator", TRANSLATOR},
88 {"transpose", TRANSPOSE},
89 {"type", TYPE},
90 {"unset", UNSET},
91 {0,0}
94 My_lily_lexer::My_lily_lexer ()
96 keytable_p_ = new Keyword_table (the_key_tab);
97 toplevel_variable_tab_ = new Scheme_hash_table ;
98 scope_p_ = new Scope (toplevel_variable_tab_);
100 scope_l_arr_.push (scope_p_);
102 errorlevel_i_ = 0;
103 main_input_b_ = false;
107 My_lily_lexer::lookup_keyword (String s)
109 return keytable_p_->lookup (s.ch_C ());
113 My_lily_lexer::lookup_identifier (String s)
115 SCM sym = ly_symbol2scm (s.ch_C ());
117 for (int i = scope_l_arr_.size (); i--;)
119 SCM val = SCM_UNSPECIFIED;
120 if (scope_l_arr_[i]->try_retrieve (sym, &val))
121 return val;
123 return SCM_UNSPECIFIED;
126 void
127 My_lily_lexer::start_main_input ()
129 new_input (main_input_str_, source_global_l);
130 allow_includes_b_ = allow_includes_b_ && ! (safe_global_b);
133 void
134 My_lily_lexer::set_identifier (String name_str, SCM s)
136 if (lookup_keyword (name_str) >= 0)
138 warning (_f ("Identifier name is a keyword: `%s'", name_str));
141 scope_l_arr_.top ()->set (name_str, s);
144 My_lily_lexer::~My_lily_lexer ()
146 delete keytable_p_;
147 scm_unprotect_object (toplevel_variable_tab_->self_scm ());
148 delete scope_p_ ;
153 void
154 My_lily_lexer::LexerError (char const *s)
156 if (include_stack_.empty ())
158 progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
160 else
162 errorlevel_i_ |= 1;
163 Input spot (source_file_l (),here_ch_C ());
164 spot.error (s);
168 char
169 My_lily_lexer::escaped_char (char c) const
171 switch (c)
173 case 'n':
174 return '\n';
175 case 't':
176 return '\t';
178 case '\'':
179 case '\"':
180 case '\\':
181 return c;
183 return 0;
186 Input
187 My_lily_lexer::here_input () const
189 Source_file * f_l= source_file_l ();
190 return Input (f_l, (char*)here_ch_C ());