]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.3.118
[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--2000 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   {"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   {"denies", DENIES},
48   {"duration", DURATION},
49   {"dynamicscript", DYNAMICSCRIPT},
50   {"elementdescriptions", ELEMENTDESCRIPTIONS},
51   {"font", FONT},
52   {"grace", GRACE},
53   {"glissando", GLISSANDO},
54   {"header", HEADER},
55   {"in", IN_T},
56   {"lyrics", LYRICS},
57   {"key", KEY},
58   {"mark", MARK},
59   {"musicalpitch", MUSICAL_PITCH},
60   {"time", TIME_T},
61   {"times", TIMES},
62   {"midi", MIDI},
63   {"mm", MM_T},
64   {"name", NAME},
65   {"pitchnames", PITCHNAMES},
66   {"notes", NOTES},
67   {"outputproperty", OUTPUTPROPERTY},
68   {"override", OVERRIDE},
69   {"set", SET},
70   {"revert", REVERT},
71   {"partial", PARTIAL},
72   {"paper", PAPER},
73   {"penalty", PENALTY},
74   {"property", PROPERTY},
75   {"pt", PT_T},
76   {"relative", RELATIVE},
77   {"remove", REMOVE},
78   {"repeat", REPEAT},
79   {"addlyrics", ADDLYRICS},
80   {"partcombine", PARTCOMBINE},
81   {"score", SCORE},
82   {"script", SCRIPT},
83   {"stylesheet", STYLESHEET},
84   {"skip", SKIP},
85   {"textscript", TEXTSCRIPT},
86   {"tempo", TEMPO},
87   {"translator", TRANSLATOR},
88   {"transpose", TRANSPOSE},
89   {"type", TYPE},
90   {0,0}
91 };
92
93 My_lily_lexer::My_lily_lexer()
94 {
95   keytable_p_ = new Keyword_table (the_key_tab);
96   toplevel_variable_tab_ = new Scheme_hash_table ;
97   scope_p_ = new Scope (toplevel_variable_tab_);
98   
99   scope_l_arr_.push (scope_p_);
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 (String name_str, SCM s)
134 {
135   if (lookup_keyword (name_str) >= 0)
136     {
137       warning (  _f ("Identifier name is a keyword: `%s'", name_str));
138     }
139   
140   scope_l_arr_.top ()->set (name_str, s);
141 }
142
143 My_lily_lexer::~My_lily_lexer()
144 {
145   delete keytable_p_;
146   scm_unprotect_object (toplevel_variable_tab_->self_scm ());
147   delete scope_p_ ;
148 }
149
150
151
152 void
153 My_lily_lexer::LexerError (char const *s)
154 {
155   if (include_stack_.empty())
156     {
157       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
158     }
159   else
160     {
161       errorlevel_i_ |= 1;
162       Input spot (source_file_l(),here_ch_C());
163       spot.error (s);
164     }
165 }
166
167 char
168 My_lily_lexer::escaped_char(char c) const
169 {
170   switch(c)
171     {
172     case 'n':
173       return '\n';
174     case 't':
175       return '\t';
176
177     case '\'':
178     case '\"':
179     case '\\':
180       return c;
181     }
182   return 0;
183 }
184
185 Input
186 My_lily_lexer::here_input () const
187 {
188   Source_file * f_l= source_file_l ();
189   return Input (f_l, (char*)here_ch_C ());
190 }