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