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