mootGenericLexer.h
Go to the documentation of this file.
1 /* -*- Mode: C++ -*- */
2 
3 /*
4  libmoot : moocow's part-of-speech tagging library
5  Copyright (C) 2003-2009 by Bryan Jurish <moocow@cpan.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 3 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /*--------------------------------------------------------------------------
23  * File: mootGenericLexer.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moocow's PoS tagger : generic lexer routines
27  *--------------------------------------------------------------------------*/
28 
34 #ifndef _MOOT_GENERIC_LEXER_H
35 #define _MOOT_GENERIC_LEXER_H
36 
37 #include <mootBufferIO.h> //-- (indirectly) includes mootCIO.h
38 
39 namespace moot {
40  using namespace std;
41 
90 class GenericLexer {
91 public:
92  /*--------------------------------------------------------------------------
93  * mootGenericLexer: Statics
94  */
95  /*static const int MGL_DEFAULT_BUFFER_SIZE = 8192;*/ //-- old: flex++ now uses 16384
96  static const int MGL_DEFAULT_BUFFER_SIZE = 16384;
97 
98 public:
99  /*--------------------------------------------------------------------
100  * mootGenericLexer: Data
101  */
102  /*------------------------------------------------------------*/
105  mootio::mistream *mglin;
106  mootio::mostream *mglout;
108  bool mgl_in_created;
109  bool mgl_out_created;
111 
112  /*------------------------------------------------------------*/
115  size_t theLine;
116  size_t theColumn;
119 
120  /*------------------------------------------------------------*/
123  std::string lexname;
125 
126 
127  /*------------------------------------------------------------*/
130  std::string tokbuf;
131  bool tokbuf_clear;
133 
134 public:
135  /*--------------------------------------------------------------------
136  * mootGenericLexer: Methods
137  */
138 
139  /*------------------------------------------------------------*/
143  GenericLexer(const std::string &myname="moot::GenericLexer",
144  size_t line=0, size_t column=0, mootio::ByteOffset byte=0)
145  : mglin(NULL),
146  mglout(NULL),
147  mgl_in_created(false),
148  mgl_out_created(false),
149  theLine(line),
150  theColumn(column),
151  theByte(byte),
152  lexname(myname),
153  tokbuf_clear(false)
154  {};
155 
157  virtual ~GenericLexer(void);
158 
160  virtual void reset(void);
161 
163  virtual void clear(bool clear_input=true, bool clear_output=true);
165 
166  /*------------------------------------------------------------*/
169  virtual void *mgl_yy_current_buffer_p(void);
170  //{ return (&yy_current_buffer); }
176  virtual void mgl_begin(int stateno);
177  //{ BEGIN(stateno); };
178 
180  virtual void mgl_yy_delete_buffer(void *buf)
181  {
182  yycarp("abstract method mgl_yy_delete_buffer() called!");
183  abort();
184  };
185 
187  virtual void *mgl_yy_create_buffer(int size, FILE *unused=stdin) =0;
188 
190  virtual void mgl_yy_switch_to_buffer(void *buf) =0;
191 
193  virtual void mgl_yy_init_buffer(void *buf, FILE *unused=stdin) =0;
195 
196  /*------------------------------------------------------------*/
200  virtual void from_mstream(mootio::mistream *in=NULL);
201 
203  virtual void from_filename(const std::string &filename);
204 
206  virtual void from_file(FILE *in=stdin);
207 
209  virtual void from_buffer(const char *buf, size_t len);
210 
212  inline void from_string(const char *s) {
213  from_buffer(s, strlen(s));
214  };
216  inline void from_string(const std::string &s) {
217  from_buffer(s.data(), s.size());
218  };
219 
221  inline void select_streams(FILE *in, FILE *out=stdout, const char *myname=NULL) {
222  from_file(in);
223  to_file(out);
224  if (myname) lexname = myname;
225  };
226 
228  inline void select_string(const char *in, FILE *out=stderr, const char *myname=NULL) {
229  from_string(in);
230  to_file(out);
231  if (myname) lexname = myname;
232  };
234 
235  /*------------------------------------------------------------*/
239  virtual void to_mstream(mootio::mostream *out=NULL);
240 
242  virtual void to_filename(const std::string &filename);
243 
245  virtual void to_file(FILE *out=stdout);
247 
248 
249  /*------------------------------------------------------------*/
253  inline int yyinput(char *buffer, int &result, int max_size)
254  {
255  return (result = (mglin ? mglin->read(buffer, max_size) : 0));
256  };
258 
259  /*------------------------------------------------------------*/
263  inline void tokbuf_append(const char *text, size_t len)
264  {
265  if (tokbuf_clear) {
266  tokbuf.assign(text,len);
267  tokbuf_clear = false;
268  } else {
269  tokbuf.append(text,len);
270  }
271  };
273 
274  /*------------------------------------------------------------*/
278  inline void add_columns(ByteOffset len)
279  {
280  theColumn += len;
281  theByte += len;
282  };
283 
285  inline void add_lines(ByteOffset len=1)
286  {
287  theLine += len;
288  theByte += len;
289  theColumn = 0;
290  };
292 
293 
294  /*------------------------------------------------------------*/
298  virtual void yycarp(const char *fmt, ...);
300 }; //-- /class mootGenericLexer
301 
302 }; //-- /namespace moot
303 
304 #endif // _MOOT_GENERIC_LEXER_H
Definition: mootAssocVector.h:39
Abstract base class for Flex++ lexers.
Definition: mootGenericLexer.h:77
Abstract base class for output stream wrappers.
Definition: mootIO.h:194
moot::OffsetT ByteOffset
typedef for (byte) offsets (may be unsigned)
Definition: mootIO.h:55
mootio abstraction layer for C char* buffers
virtual ByteCount read(char *buf, size_t n)
Definition: mootIO.h:156
Abstract base class for input stream wrappers.
Definition: mootIO.h:129