]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
a20e1df14a7662d8909cc02a2707b97bee7b9323
[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   {"font", FONT},
48   {"grace", GRACE},
49   {"header", HEADER},
50   {"in", IN_T},
51   {"lyrics", LYRICS},
52   {"key", KEY},
53   {"mark", MARK},
54   {"musicalpitch", MUSICAL_PITCH},
55   {"time", TIME_T},
56   {"times", TIMES},
57   {"midi", MIDI},
58   {"mm", MM_T},
59   {"name", NAME},
60   {"notenames", NOTENAMES},
61   {"notes", NOTES},
62   {"outputproperty", OUTPUTPROPERTY},
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     {
108       SCM val = SCM_UNSPECIFIED;
109       if (scope_l_arr_[i]->try_retrieve (sym, &val))
110         return val;
111     }
112   return SCM_UNSPECIFIED;
113 }
114
115 void
116 My_lily_lexer::start_main_input ()
117 {  
118   new_input (main_input_str_, source_global_l);
119   allow_includes_b_ = allow_includes_b_ &&  !(safe_global_b);
120 }
121
122 void
123 My_lily_lexer::set_identifier (String name_str, SCM s)
124 {
125   if (lookup_keyword (name_str) >= 0)
126     {
127       warning (  _f ("Identifier name is a keyword: `%s'", name_str));
128     }
129   
130   scope_l_arr_.top ()->set (name_str, s);
131 }
132
133 My_lily_lexer::~My_lily_lexer()
134 {
135   delete keytable_p_;
136   delete toplevel_scope_p_ ;
137 }
138
139
140
141 void
142 My_lily_lexer::LexerError (char const *s)
143 {
144   if (include_stack_.empty())
145     {
146       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
147     }
148   else
149     {
150       errorlevel_i_ |= 1;
151       Input spot (source_file_l(),here_ch_C());
152       spot.error (s);
153     }
154 }
155
156 char
157 My_lily_lexer::escaped_char(char c) const
158 {
159   switch(c)
160     {
161     case 'n':
162       return '\n';
163     case 't':
164       return '\t';
165
166     case '\'':
167     case '\"':
168     case '\\':
169       return c;
170     }
171   return 0;
172 }
173
174 Input
175 My_lily_lexer::here_input () const
176 {
177   Source_file * f_l= source_file_l ();
178   return Input (f_l, (char*)here_ch_C ());
179 }