]> git.donarmstrong.com Git - lilypond.git/blob - lily/includable-lexer.cc
release: 1.1.62
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <strstream.h>
10
11
12 #include "file-path.hh"
13 #include "includable-lexer.hh"
14 #include "source-file.hh"
15 #include "source.hh"
16 #include "debug.hh"
17
18 #ifndef YY_BUF_SIZE
19 #define YY_BUF_SIZE 16384
20 #endif
21
22 #ifndef YY_START
23 #define YY_START ((yy_start - 1) / 2)
24 #define YYSTATE YY_START
25 #endif
26
27 Includable_lexer::Includable_lexer ()
28 {
29   yy_current_buffer = 0;
30   allow_includes_b_ = true;
31 }
32
33 /** set the  new input to s, remember old file.
34 */
35 void
36 Includable_lexer::new_input (String s, Sources  * global_sources)
37 {
38   if (!allow_includes_b_)
39     {
40       LexerError ("include files are disallowed.");
41       return;
42     }
43   
44   Source_file * sl = global_sources->get_file_l (s);
45   if (!sl)
46     {
47       String msg = _f ("can't find file: `%s\'", s);
48       msg += _f ("\nSearch path is `%s'\n", global_sources->path_C_->str ().ch_C());
49       LexerError (msg.ch_C ());
50
51       return;
52     }
53   filename_str_arr_.push (sl->name_str ());
54
55   char_count_stack_.push (0);
56   if (yy_current_buffer)
57     state_stack_.push (yy_current_buffer);
58   *mlog << "[" << s<< flush;
59   include_stack_.push (sl);
60
61   /*
62     ugh. We'd want to create a buffer from the bytes directly.
63
64     Whoops. The size argument to yy_create_buffer is not the
65     filelength but a BUFFERSIZE. Maybe this is why reading stdin fucks up.
66
67     */
68   yy_switch_to_buffer (yy_create_buffer (sl->istream_l (), YY_BUF_SIZE));
69
70 }
71 void
72 Includable_lexer::new_input (String name, String data, Sources* sources)
73 {
74   Source_file* file = new Source_file (name, data);
75   sources->add (file);
76   filename_str_arr_.push (name);
77
78   char_count_stack_.push (0);
79   if (yy_current_buffer)
80     state_stack_.push (yy_current_buffer);
81   *mlog << "[" << name << flush;
82   include_stack_.push (file);
83
84   yy_switch_to_buffer (yy_create_buffer (file->istream_l (), YY_BUF_SIZE));
85 }
86
87 /** pop the inputstack.  conceptually this is a destructor, but it
88   does not destruct the Source_file that Includable_lexer::new_input creates.  */
89 bool
90 Includable_lexer::close_input ()
91 {
92   include_stack_.pop ();
93   char_count_stack_.pop ();
94   *mlog << "]"<<flush;
95   yy_delete_buffer (yy_current_buffer);
96   yy_current_buffer = 0;
97   if (state_stack_.empty ())
98     {
99       yy_current_buffer = 0;
100       return false;
101     }
102   else
103       {
104         yy_switch_to_buffer (state_stack_.pop ());
105         return true;
106       }
107 }
108
109 char const*
110 Includable_lexer::here_ch_C ()
111 {
112   if (include_stack_.empty ())
113     return 0;
114   return include_stack_.top ()->ch_C () + char_count_stack_.top ();
115 }
116
117 Includable_lexer::~Includable_lexer ()
118 {
119   while (!include_stack_.empty ())
120     {
121       close_input ();
122     }
123 }
124 /**
125   Since we don't create the buffer state from the bytes directly, we
126   don't know about the location of the lexer. Add this as a
127   YY_USER_ACTION */
128 void
129 Includable_lexer::add_lexed_char (int count)
130 {
131   char_count_stack_.top () += count;
132 }
133
134 Source_file*
135 Includable_lexer::source_file_l () const
136 {
137   if (include_stack_.empty ())
138     return 0;
139   else
140     return include_stack_.top ();
141 }