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