]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
cff5ae13e698f155b5d202334d2d4ba70eb61390
[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   {"pitch", PITCH},
62   {"time", TIME_T},
63   {"times", TIMES},
64   {"midi", MIDI},
65   {"mm", MM_T},
66   {"name", NAME},
67   {"pitchnames", PITCHNAMES},
68   {"notes", NOTES},
69   {"outputproperty", OUTPUTPROPERTY},
70   {"override", OVERRIDE},
71   {"set", SET},
72   {"rest", REST},
73   {"revert", REVERT},
74   {"partial", PARTIAL},
75   {"paper", PAPER},
76   {"penalty", PENALTY},
77   {"property", PROPERTY},
78   {"pt", PT_T},
79   {"relative", RELATIVE},
80   {"remove", REMOVE},
81   {"repeat", REPEAT},
82   {"addlyrics", ADDLYRICS},
83   {"partcombine", PARTCOMBINE},
84   {"score", SCORE},
85   {"script", SCRIPT},
86   {"stylesheet", STYLESHEET},
87   {"skip", SKIP},
88   {"tempo", TEMPO},
89   {"translator", TRANSLATOR},
90   {"transpose", TRANSPOSE},
91   {"type", TYPE},
92   {"unset", UNSET},
93   {0,0}
94 };
95
96 My_lily_lexer::My_lily_lexer ()
97 {
98   keytable_ = new Keyword_table (the_key_tab);
99   toplevel_variable_tab_ = new Scheme_hash_table ;
100   scopes_.push (toplevel_variable_tab_);
101   
102   errorlevel_ = 0;
103   main_input_b_ = false;
104 }
105
106 int
107 My_lily_lexer::lookup_keyword (String s)
108 {
109   return keytable_->lookup (s.to_str0 ());
110 }
111
112 SCM
113 My_lily_lexer::lookup_identifier (String s)
114 {
115   SCM sym = ly_symbol2scm (s.to_str0 ());
116   
117   for (int i = scopes_.size (); i--;)
118     {
119       SCM val = SCM_UNSPECIFIED;
120       if (scopes_[i]->try_retrieve (sym, &val))
121         return val;
122     }
123   return SCM_UNSPECIFIED;
124 }
125
126 void
127 My_lily_lexer::start_main_input ()
128 {  
129   new_input (main_input_string_, &global_input_file->sources_);
130   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
131 }
132
133 void
134 My_lily_lexer::set_identifier (SCM name, SCM s)
135 {
136   assert (gh_string_p (name));
137   
138   if (lookup_keyword (ly_scm2string (name)) >= 0)
139     {
140       size_t sz;
141       char * str = gh_scm2newstr (name, &sz) ;
142       warning (_f ("Identifier name is a keyword: `%s'", str));
143       free  (str);
144     }
145   
146   scopes_.top ()->set (scm_string_to_symbol (name), s);
147 }
148
149 My_lily_lexer::~My_lily_lexer ()
150 {
151   delete keytable_;
152   scm_gc_unprotect_object (toplevel_variable_tab_->self_scm ());
153 }
154
155
156
157 void
158 My_lily_lexer::LexerError (char const *s)
159 {
160   if (include_stack_.empty ())
161     {
162       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
163     }
164   else
165     {
166       errorlevel_ |= 1;
167       Input spot (get_source_file (),here_str0 ());
168       spot.error (s);
169     }
170 }
171
172 char
173 My_lily_lexer::escaped_char (char c) const
174 {
175   switch (c)
176     {
177     case 'n':
178       return '\n';
179     case 't':
180       return '\t';
181
182     case '\'':
183     case '\"':
184     case '\\':
185       return c;
186     }
187   return 0;
188 }
189
190 Input
191 My_lily_lexer::here_input () const
192 {
193   Source_file * f= get_source_file ();
194   return Input (f, (char*)here_str0 ());
195 }