]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.5.29
[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 "scope.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   {"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_p_ = new Scope (toplevel_variable_tab_);
100   
101   scope_l_arr_.push (scope_p_);
102   
103   errorlevel_i_ = 0;
104   main_input_b_ = false;
105 }
106
107 int
108 My_lily_lexer::lookup_keyword (String s)
109 {
110   return keytable_p_->lookup (s.ch_C ());
111 }
112
113 SCM
114 My_lily_lexer::lookup_identifier (String s)
115 {
116   SCM sym = ly_symbol2scm (s.ch_C ());
117   
118   for (int i = scope_l_arr_.size (); i--;)
119     {
120       SCM val = SCM_UNSPECIFIED;
121       if (scope_l_arr_[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_str_, source_global_l);
131   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
132 }
133
134 void
135 My_lily_lexer::set_identifier (String name_str, SCM s)
136 {
137   if (lookup_keyword (name_str) >= 0)
138     {
139       warning (_f ("Identifier name is a keyword: `%s'", name_str));
140     }
141   
142   scope_l_arr_.top ()->set (name_str, s);
143 }
144
145 My_lily_lexer::~My_lily_lexer ()
146 {
147   delete keytable_p_;
148   scm_gc_unprotect_object (toplevel_variable_tab_->self_scm ());
149   delete scope_p_ ;
150 }
151
152
153
154 void
155 My_lily_lexer::LexerError (char const *s)
156 {
157   if (include_stack_.empty ())
158     {
159       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
160     }
161   else
162     {
163       errorlevel_i_ |= 1;
164       Input spot (source_file_l (),here_ch_C ());
165       spot.error (s);
166     }
167 }
168
169 char
170 My_lily_lexer::escaped_char (char c) const
171 {
172   switch (c)
173     {
174     case 'n':
175       return '\n';
176     case 't':
177       return '\t';
178
179     case '\'':
180     case '\"':
181     case '\\':
182       return c;
183     }
184   return 0;
185 }
186
187 Input
188 My_lily_lexer::here_input () const
189 {
190   Source_file * f_l= source_file_l ();
191   return Input (f_l, (char*)here_ch_C ());
192 }