]> git.donarmstrong.com Git - lilypond.git/blob - lib/includable-lexer.cc
release: 0.0.68pre
[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 `" + s+ "'");
25         return; 
26     }
27
28     
29     char_count_stack_.push(0);
30     if (yy_current_buffer) 
31         state_stack_.push(yy_current_buffer);
32     cout << "[" << s<<flush;
33     include_stack_.push(sl);    
34     
35     // ugh. We'd want to create a buffer from the bytes directly.
36     yy_switch_to_buffer(yy_create_buffer( sl->istream_l(), sl->length_off() )); 
37 }
38
39 /** pop the inputstack.  conceptually this is a destructor, but it
40   does not destruct the Source_file that Includable_lexer::new_input creates.  */
41 bool
42 Includable_lexer::close_input()
43 {
44     include_stack_.pop();
45     char_count_stack_.pop();
46     cout << "]"<<flush;
47     yy_delete_buffer(yy_current_buffer );
48     yy_current_buffer = 0;
49     if (state_stack_.empty()) {
50         return false;
51     }else {
52         yy_switch_to_buffer(state_stack_.pop());
53         return true;
54     }    
55 }
56
57 char const*
58 Includable_lexer::here_ch_C()
59 {
60     if (include_stack_.empty())
61         return 0;
62     return include_stack_.top()->ch_C() + char_count_stack_.top();
63 }
64
65 Includable_lexer::~Includable_lexer()
66 {
67     while (!include_stack_.empty()) {
68         close_input();
69     };
70 }
71 /** 
72   Since we don't create the buffer state from the bytes directly, we
73   don't know about the location of the lexer. Add this as a
74   YY_USER_ACTION */
75 void
76 Includable_lexer::add_lexed_char(int count)
77 {
78     char_count_stack_.top() += count;
79 }
80
81 Source_file*
82 Includable_lexer::source_file_l()const
83 {
84     if (include_stack_.empty())
85         return 0;
86     else
87         return include_stack_.top();
88 }