]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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 <ctype.h>
10
11 #include <sstream>
12
13 #include "lily-proto.hh"
14 #include "scm-hash.hh"
15 #include "interval.hh"
16 #include "input-file-results.hh"
17 #include "lily-guile.hh"
18 #include "parser.hh"
19 #include "keyword.hh"
20 #include "my-lily-lexer.hh"
21 #include "warn.hh"
22 #include "source-file.hh"
23 #include "main.hh"
24 #include "input.hh"
25 #include "moment.hh"
26
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   {"grobdescriptions", GROBDESCRIPTIONS},
53   {"figures",FIGURES},
54   {"grace", GRACE},
55   {"glissando", GLISSANDO},
56   {"header", HEADER},
57   {"in", IN_T},
58   {"lyrics", LYRICS},
59   {"key", KEY},
60   {"mark", MARK},
61   {"once", ONCE},
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   {"rest", REST},
74   {"revert", REVERT},
75   {"partial", PARTIAL},
76   {"paper", PAPER},
77   {"penalty", PENALTY},
78   {"property", PROPERTY},
79   {"pt", PT_T},
80   {"relative", RELATIVE},
81   {"remove", REMOVE},
82   {"repeat", REPEAT},
83   {"addlyrics", ADDLYRICS},
84   {"partcombine", PARTCOMBINE},
85   {"score", SCORE},
86   {"script", SCRIPT},
87   {"stylesheet", STYLESHEET},
88   {"skip", SKIP},
89   {"tempo", TEMPO},
90   {"translator", TRANSLATOR},
91   {"transpose", TRANSPOSE},
92   {"type", TYPE},
93   {"unset", UNSET},
94   {0,0}
95 };
96
97 My_lily_lexer::My_lily_lexer ()
98 {
99   keytable_ = new Keyword_table (the_key_tab);
100   toplevel_variable_tab_ = new Scheme_hash_table ;
101   scopes_.push (toplevel_variable_tab_);
102   
103   errorlevel_ = 0;
104   main_input_b_ = false;
105 }
106
107 int
108 My_lily_lexer::lookup_keyword (String s)
109 {
110   return keytable_->lookup (s.to_str0 ());
111 }
112
113 SCM
114 My_lily_lexer::lookup_identifier (String s)
115 {
116   SCM sym = ly_symbol2scm (s.to_str0 ());
117   
118   for (int i = scopes_.size (); i--;)
119     {
120       SCM val = SCM_UNSPECIFIED;
121       if (scopes_[i]->try_retrieve (sym, &val))
122         return val;
123     }
124   return SCM_UNSPECIFIED;
125 }
126
127 void
128 My_lily_lexer::start_main_input ()
129 {  
130   new_input (main_input_string_, &global_input_file->sources_);
131   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
132 }
133
134 void
135 My_lily_lexer::set_identifier (SCM name, SCM s)
136 {
137   assert (gh_string_p (name));
138   
139   if (lookup_keyword (ly_scm2string (name)) >= 0)
140     {
141       size_t sz;
142       char * str = gh_scm2newstr (name, &sz) ;
143       warning (_f ("Identifier name is a keyword: `%s'", str));
144       free  (str);
145     }
146   
147   scopes_.top ()->set (scm_string_to_symbol (name), s);
148 }
149
150 My_lily_lexer::~My_lily_lexer ()
151 {
152   delete keytable_;
153   scm_gc_unprotect_object (toplevel_variable_tab_->self_scm ());
154 }
155
156
157
158 void
159 My_lily_lexer::LexerError (char const *s)
160 {
161   if (include_stack_.empty ())
162     {
163       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
164     }
165   else
166     {
167       errorlevel_ |= 1;
168       Input spot (get_source_file (),here_str0 ());
169       spot.error (s);
170     }
171 }
172
173 char
174 My_lily_lexer::escaped_char (char c) const
175 {
176   switch (c)
177     {
178     case 'n':
179       return '\n';
180     case 't':
181       return '\t';
182
183     case '\'':
184     case '\"':
185     case '\\':
186       return c;
187     }
188   return 0;
189 }
190
191 Input
192 My_lily_lexer::here_input () const
193 {
194   Source_file * f= get_source_file ();
195   return Input (f, (char*)here_str0 ());
196 }