]> git.donarmstrong.com Git - lilypond.git/blob - lib/includable-lexer.cc
release: 0.0.47
[lilypond.git] / lib / includable-lexer.cc
1 /*
2   includable-lexer.cc -- implement Includable_lexer
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include "includable-lexer.hh"
9 #include "source-file.hh"
10 #include "source.hh"
11
12 Includable_lexer::Includable_lexer()
13 {
14     yy_current_buffer = 0;
15 }
16
17 /** set the  new input to s, remember old file.
18 */
19 void
20 Includable_lexer::new_input(String s, Sources  * global_sources)
21 {
22     Source_file * sl = global_sources->get_file_l(s);
23     if (!sl) {
24         LexerError("can't find file");
25         return; 
26     }
27     char_count_stack_.push(0);
28     if (yy_current_buffer) 
29         state_stack_.push(yy_current_buffer);
30     cout << "[" << s<<flush;
31     include_stack_.push(sl);    
32     
33     // ugh. We'd want to create a buffer from the bytes directly.
34     yy_switch_to_buffer(yy_create_buffer( sl->istream_l(), sl->length_off() )); 
35 }
36
37 /** pop the inputstack.  conceptually this is a destructor, but it
38   does not destruct the Source_file that Includable_lexer::new_input creates.  */
39 bool
40 Includable_lexer::close_input()
41 {
42     include_stack_.pop();
43     char_count_stack_.pop();
44     cout << "]"<<flush;
45     yy_delete_buffer(yy_current_buffer );
46     if (state_stack_.empty()) {
47         yy_current_buffer = 0;
48         return false;
49     }else {
50         yy_switch_to_buffer(state_stack_.pop());
51         return true;
52     }    
53 }
54
55 char const*
56 Includable_lexer::here_ch_C()
57 {
58     if (include_stack_.empty())
59         return 0;
60     return include_stack_.top()->ch_C() + char_count_stack_.top();
61 }
62
63 Includable_lexer::~Includable_lexer()
64 {
65     while (!include_stack_.empty()) {
66         close_input();
67     };
68 }
69 /** 
70   Since we don't create the buffer state from the bytes directly, we
71   don't know about the location of the lexer. Add this as a
72   YY_USER_ACTION */
73 void
74 Includable_lexer::add_lexed_char(int count)
75 {
76     char_count_stack_.top() += count;
77 }
78
79 Source_file*
80 Includable_lexer::source_file_l()const
81 {
82     return include_stack_.top();
83 }