]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
patch::: 1.3.111.jcn1
[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 "interval.hh"
13 #include "identifier.hh"
14 #include "lily-guile.hh"
15 #include "parser.hh"
16 #include "keyword.hh"
17 #include "my-lily-lexer.hh"
18 #include "debug.hh"
19 #include "source-file.hh"
20 #include "main.hh"
21 #include "scope.hh"
22 #include "input.hh"
23 #include "moment.hh"
24
25 static Keyword_ent the_key_tab[]={
26   {"apply", APPLY},
27   {"arpeggio", ARPEGGIO },
28   {"autochange", AUTOCHANGE},
29   {"spanrequest", SPANREQUEST},
30   {"commandspanrequest", COMMANDSPANREQUEST},  
31   {"simultaneous", SIMULTANEOUS},
32   {"sequential", SEQUENTIAL},
33   {"accepts", ACCEPTS},
34   {"alternative", ALTERNATIVE},
35   {"bar", BAR},
36   {"breathe", BREATHE},
37   {"char", CHAR_T},
38   {"chordmodifiers", CHORDMODIFIERS},
39   {"chords", CHORDS},
40   {"clef", CLEF},
41   {"cm", CM_T},
42   {"consists", CONSISTS},
43   {"consistsend", CONSISTSEND},
44   {"context", CONTEXT},
45   {"denies", DENIES},
46   {"duration", DURATION},
47   {"dynamicscript", DYNAMICSCRIPT},
48   {"elementdescriptions", ELEMENTDESCRIPTIONS},
49   {"font", FONT},
50   {"grace", GRACE},
51   {"glissando", GLISSANDO},
52   {"header", HEADER},
53   {"in", IN_T},
54   {"lyrics", LYRICS},
55   {"key", KEY},
56   {"mark", MARK},
57   {"musicalpitch", MUSICAL_PITCH},
58   {"time", TIME_T},
59   {"times", TIMES},
60   {"midi", MIDI},
61   {"mm", MM_T},
62   {"name", NAME},
63   {"pitchnames", PITCHNAMES},
64   {"notes", NOTES},
65   {"outputproperty", OUTPUTPROPERTY},
66   {"override", OVERRIDE},
67   {"set", SET},
68   {"revert", REVERT},
69   {"partial", PARTIAL},
70   {"paper", PAPER},
71   {"penalty", PENALTY},
72   {"property", PROPERTY},
73   {"pt", PT_T},
74   {"relative", RELATIVE},
75   {"remove", REMOVE},
76   {"repeat", REPEAT},
77   {"addlyrics", ADDLYRICS},
78   {"partcombine", PARTCOMBINE},
79   {"score", SCORE},
80   {"script", SCRIPT},
81   {"stylesheet", STYLESHEET},
82   {"skip", SKIP},
83   {"textscript", TEXTSCRIPT},
84   {"tempo", TEMPO},
85   {"translator", TRANSLATOR},
86   {"transpose", TRANSPOSE},
87   {"type", TYPE},
88   {0,0}
89 };
90
91 My_lily_lexer::My_lily_lexer()
92 {
93   keytable_p_ = new Keyword_table (the_key_tab);
94   toplevel_scope_p_ = new Scope;
95   scope_l_arr_.push (toplevel_scope_p_);
96   errorlevel_i_ = 0;
97   main_input_b_ = false;
98 }
99
100 int
101 My_lily_lexer::lookup_keyword (String s)
102 {
103   return keytable_p_->lookup (s.ch_C ());
104 }
105
106 SCM
107 My_lily_lexer::lookup_identifier (String s)
108 {
109   SCM sym = ly_symbol2scm (s.ch_C());
110   
111   for (int i = scope_l_arr_.size (); i--; )
112     {
113       SCM val = SCM_UNSPECIFIED;
114       if (scope_l_arr_[i]->try_retrieve (sym, &val))
115         return val;
116     }
117   return SCM_UNSPECIFIED;
118 }
119
120 void
121 My_lily_lexer::start_main_input ()
122 {  
123   new_input (main_input_str_, source_global_l);
124   allow_includes_b_ = allow_includes_b_ &&  !(safe_global_b);
125 }
126
127 void
128 My_lily_lexer::set_identifier (String name_str, SCM s)
129 {
130   if (lookup_keyword (name_str) >= 0)
131     {
132       warning (  _f ("Identifier name is a keyword: `%s'", name_str));
133     }
134   
135   scope_l_arr_.top ()->set (name_str, s);
136 }
137
138 My_lily_lexer::~My_lily_lexer()
139 {
140   delete keytable_p_;
141   delete toplevel_scope_p_ ;
142 }
143
144
145
146 void
147 My_lily_lexer::LexerError (char const *s)
148 {
149   if (include_stack_.empty())
150     {
151       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
152     }
153   else
154     {
155       errorlevel_i_ |= 1;
156       Input spot (source_file_l(),here_ch_C());
157       spot.error (s);
158     }
159 }
160
161 char
162 My_lily_lexer::escaped_char(char c) const
163 {
164   switch(c)
165     {
166     case 'n':
167       return '\n';
168     case 't':
169       return '\t';
170
171     case '\'':
172     case '\"':
173     case '\\':
174       return c;
175     }
176   return 0;
177 }
178
179 Input
180 My_lily_lexer::here_input () const
181 {
182   Source_file * f_l= source_file_l ();
183   return Input (f_l, (char*)here_ch_C ());
184 }