]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.5.47
[lilypond.git] / lily / my-lily-lexer.cc
1 /*
2   my-lily-lexer.cc -- implement My_lily_lexer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <strstream.h>
10 #include <ctype.h>
11
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
15
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 "input.hh"
24 #include "moment.hh"
25
26 static Keyword_ent the_key_tab[]={
27   {"alias", ALIAS},
28   {"apply", APPLY},
29   {"arpeggio", ARPEGGIO },
30   {"autochange", AUTOCHANGE},
31   {"spanrequest", SPANREQUEST},
32   {"commandspanrequest", COMMANDSPANREQUEST},  
33   {"simultaneous", SIMULTANEOUS},
34   {"sequential", SEQUENTIAL},
35   {"accepts", ACCEPTS},
36   {"alternative", ALTERNATIVE},
37   {"bar", BAR},
38   {"breathe", BREATHE},
39   {"char", CHAR_T},
40   {"chordmodifiers", CHORDMODIFIERS},
41   {"chords", CHORDS},
42   {"clef", CLEF},
43   {"cm", CM_T},
44   {"consists", CONSISTS},
45   {"consistsend", CONSISTSEND},
46   {"context", CONTEXT},
47   {"default", DEFAULT},
48   {"denies", DENIES},
49   {"duration", DURATION},
50   {"dynamicscript", DYNAMICSCRIPT},
51   {"grobdescriptions", GROBDESCRIPTIONS},
52   {"figures",FIGURES},
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   {"rest", REST},
72   {"revert", REVERT},
73   {"partial", PARTIAL},
74   {"paper", PAPER},
75   {"penalty", PENALTY},
76   {"property", PROPERTY},
77   {"pt", PT_T},
78   {"relative", RELATIVE},
79   {"remove", REMOVE},
80   {"repeat", REPEAT},
81   {"addlyrics", ADDLYRICS},
82   {"partcombine", PARTCOMBINE},
83   {"score", SCORE},
84   {"script", SCRIPT},
85   {"stylesheet", STYLESHEET},
86   {"skip", SKIP},
87   {"tempo", TEMPO},
88   {"translator", TRANSLATOR},
89   {"transpose", TRANSPOSE},
90   {"type", TYPE},
91   {"unset", UNSET},
92   {0,0}
93 };
94
95 My_lily_lexer::My_lily_lexer ()
96 {
97   keytable_p_ = new Keyword_table (the_key_tab);
98   toplevel_variable_tab_ = new Scheme_hash_table ;
99   scope_l_arr_.push (toplevel_variable_tab_);
100   
101   errorlevel_i_ = 0;
102   main_input_b_ = false;
103 }
104
105 int
106 My_lily_lexer::lookup_keyword (String s)
107 {
108   return keytable_p_->lookup (s.ch_C ());
109 }
110
111 SCM
112 My_lily_lexer::lookup_identifier (String s)
113 {
114   SCM sym = ly_symbol2scm (s.ch_C ());
115   
116   for (int i = scope_l_arr_.size (); i--;)
117     {
118       SCM val = SCM_UNSPECIFIED;
119       if (scope_l_arr_[i]->try_retrieve (sym, &val))
120         return val;
121     }
122   return SCM_UNSPECIFIED;
123 }
124
125 void
126 My_lily_lexer::start_main_input ()
127 {  
128   new_input (main_input_str_, source_global_l);
129   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
130 }
131
132 void
133 My_lily_lexer::set_identifier (SCM name, SCM s)
134 {
135   assert (gh_string_p (name));
136   
137   if (lookup_keyword (ly_scm2string (name)) >= 0)
138     {
139       size_t sz;
140       char * str = gh_scm2newstr (name, &sz) ;
141       warning (_f ("Identifier name is a keyword: `%s'", str));
142       free  (str);
143     }
144   
145   scope_l_arr_.top ()->set (scm_string_to_symbol (name), s);
146 }
147
148 My_lily_lexer::~My_lily_lexer ()
149 {
150   delete keytable_p_;
151   scm_gc_unprotect_object (toplevel_variable_tab_->self_scm ());
152 }
153
154
155
156 void
157 My_lily_lexer::LexerError (char const *s)
158 {
159   if (include_stack_.empty ())
160     {
161       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
162     }
163   else
164     {
165       errorlevel_i_ |= 1;
166       Input spot (source_file_l (),here_ch_C ());
167       spot.error (s);
168     }
169 }
170
171 char
172 My_lily_lexer::escaped_char (char c) const
173 {
174   switch (c)
175     {
176     case 'n':
177       return '\n';
178     case 't':
179       return '\t';
180
181     case '\'':
182     case '\"':
183     case '\\':
184       return c;
185     }
186   return 0;
187 }
188
189 Input
190 My_lily_lexer::here_input () const
191 {
192   Source_file * f_l= source_file_l ();
193   return Input (f_l, (char*)here_ch_C ());
194 }