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