]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.3.103
[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   {"header", HEADER},
52   {"in", IN_T},
53   {"lyrics", LYRICS},
54   {"key", KEY},
55   {"mark", MARK},
56   {"musicalpitch", MUSICAL_PITCH},
57   {"time", TIME_T},
58   {"times", TIMES},
59   {"midi", MIDI},
60   {"mm", MM_T},
61   {"name", NAME},
62   {"notenames", NOTENAMES},
63   {"notes", NOTES},
64   {"outputproperty", OUTPUTPROPERTY},
65   {"push", PUSH},
66   {"pop", POP},
67   {"partial", PARTIAL},
68   {"paper", PAPER},
69   {"penalty", PENALTY},
70   {"property", PROPERTY},
71   {"pt", PT_T},
72   {"relative", RELATIVE},
73   {"remove", REMOVE},
74   {"repeat", REPEAT},
75   {"addlyrics", ADDLYRICS},
76   {"partcombine", PARTCOMBINE},
77   {"score", SCORE},
78   {"script", SCRIPT},
79   {"stylesheet", STYLESHEET},
80   {"skip", SKIP},
81   {"textscript", TEXTSCRIPT},
82   {"tempo", TEMPO},
83   {"translator", TRANSLATOR},
84   {"transpose", TRANSPOSE},
85   {"type", TYPE},
86   {0,0}
87 };
88
89 My_lily_lexer::My_lily_lexer()
90 {
91   keytable_p_ = new Keyword_table (the_key_tab);
92   toplevel_scope_p_ = new Scope;
93   scope_l_arr_.push (toplevel_scope_p_);
94   errorlevel_i_ = 0;
95   main_input_b_ = false;
96 }
97
98 int
99 My_lily_lexer::lookup_keyword (String s)
100 {
101   return keytable_p_->lookup (s.ch_C ());
102 }
103
104 SCM
105 My_lily_lexer::lookup_identifier (String s)
106 {
107   SCM sym = ly_symbol2scm (s.ch_C());
108   
109   for (int i = scope_l_arr_.size (); i--; )
110     {
111       SCM val = SCM_UNSPECIFIED;
112       if (scope_l_arr_[i]->try_retrieve (sym, &val))
113         return val;
114     }
115   return SCM_UNSPECIFIED;
116 }
117
118 void
119 My_lily_lexer::start_main_input ()
120 {  
121   new_input (main_input_str_, source_global_l);
122   allow_includes_b_ = allow_includes_b_ &&  !(safe_global_b);
123 }
124
125 void
126 My_lily_lexer::set_identifier (String name_str, SCM s)
127 {
128   if (lookup_keyword (name_str) >= 0)
129     {
130       warning (  _f ("Identifier name is a keyword: `%s'", name_str));
131     }
132   
133   scope_l_arr_.top ()->set (name_str, s);
134 }
135
136 My_lily_lexer::~My_lily_lexer()
137 {
138   delete keytable_p_;
139   delete toplevel_scope_p_ ;
140 }
141
142
143
144 void
145 My_lily_lexer::LexerError (char const *s)
146 {
147   if (include_stack_.empty())
148     {
149       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
150     }
151   else
152     {
153       errorlevel_i_ |= 1;
154       Input spot (source_file_l(),here_ch_C());
155       spot.error (s);
156     }
157 }
158
159 char
160 My_lily_lexer::escaped_char(char c) const
161 {
162   switch(c)
163     {
164     case 'n':
165       return '\n';
166     case 't':
167       return '\t';
168
169     case '\'':
170     case '\"':
171     case '\\':
172       return c;
173     }
174   return 0;
175 }
176
177 Input
178 My_lily_lexer::here_input () const
179 {
180   Source_file * f_l= source_file_l ();
181   return Input (f_l, (char*)here_ch_C ());
182 }