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