lilypond-0.1.45
[lilypond.git] / lib / includable-lexer.cc
blob3728476203d6c442f07a1db16d6f605f8506fb21
1 /*
2 includable-lexer.cc -- implement Includable_lexer
4 source file of the LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include "includable-lexer.hh"
9 #include "source-file.hh"
10 #include "source.hh"
12 #ifndef YY_BUF_SIZE
13 #define YY_BUF_SIZE 16384
14 #endif
16 Includable_lexer::Includable_lexer ()
18 yy_current_buffer = 0;
21 /** set the new input to s, remember old file.
23 void
24 Includable_lexer::new_input (String s, Sources * global_sources)
26 Source_file * sl = global_sources->get_file_l (s);
27 if (!sl)
29 String msg =_ ("Can't find file `") + s+ "'";
30 LexerError (msg.ch_C ());
31 return;
35 char_count_stack_.push (0);
36 if (yy_current_buffer)
37 state_stack_.push (yy_current_buffer);
38 cout << "[" << s<<flush;
39 include_stack_.push (sl);
42 ugh. We'd want to create a buffer from the bytes directly.
44 Whoops. The size argument to yy_create_buffer is not the
45 filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
48 yy_switch_to_buffer (yy_create_buffer (sl->istream_l (), YY_BUF_SIZE));
51 /** pop the inputstack. conceptually this is a destructor, but it
52 does not destruct the Source_file that Includable_lexer::new_input creates. */
53 bool
54 Includable_lexer::close_input ()
56 include_stack_.pop ();
57 char_count_stack_.pop ();
58 cout << "]"<<flush;
59 yy_delete_buffer (yy_current_buffer);
60 yy_current_buffer = 0;
61 if (state_stack_.empty ())
63 return false;
65 else
67 yy_switch_to_buffer (state_stack_.pop ());
68 return true;
72 char const*
73 Includable_lexer::here_ch_C ()
75 if (include_stack_.empty ())
76 return 0;
77 return include_stack_.top ()->ch_C () + char_count_stack_.top ();
80 Includable_lexer::~Includable_lexer ()
82 while (!include_stack_.empty ())
84 close_input ();
87 /**
88 Since we don't create the buffer state from the bytes directly, we
89 don't know about the location of the lexer. Add this as a
90 YY_USER_ACTION */
91 void
92 Includable_lexer::add_lexed_char (int count)
94 char_count_stack_.top () += count;
97 Source_file*
98 Includable_lexer::source_file_l () const
100 if (include_stack_.empty ())
101 return 0;
102 else
103 return include_stack_.top ();