xref: /netbsd-src/external/bsd/flex/dist/src/FlexLexer.h (revision 7977e68669c3b784eb65c933a2bd99785638ae00)
1 /*	$NetBSD: FlexLexer.h,v 1.3 2017/01/02 17:45:27 christos Exp $	*/
2 
3 // -*-C++-*-
4 // FlexLexer.h -- define interfaces for lexical analyzer classes generated
5 // by flex
6 
7 // Copyright (c) 1993 The Regents of the University of California.
8 // All rights reserved.
9 //
10 // This code is derived from software contributed to Berkeley by
11 // Kent Williams and Tom Epperly.
12 //
13 //  Redistribution and use in source and binary forms, with or without
14 //  modification, are permitted provided that the following conditions
15 //  are met:
16 
17 //  1. Redistributions of source code must retain the above copyright
18 //  notice, this list of conditions and the following disclaimer.
19 //  2. Redistributions in binary form must reproduce the above copyright
20 //  notice, this list of conditions and the following disclaimer in the
21 //  documentation and/or other materials provided with the distribution.
22 
23 //  Neither the name of the University nor the names of its contributors
24 //  may be used to endorse or promote products derived from this software
25 //  without specific prior written permission.
26 
27 //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
28 //  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
29 //  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 //  PURPOSE.
31 
32 // This file defines FlexLexer, an abstract class which specifies the
33 // external interface provided to flex C++ lexer objects, and yyFlexLexer,
34 // which defines a particular lexer class.
35 //
36 // If you want to create multiple lexer classes, you use the -P flag
37 // to rename each yyFlexLexer to some other xxFlexLexer.  You then
38 // include <FlexLexer.h> in your other sources once per lexer class:
39 //
40 //      #undef yyFlexLexer
41 //      #define yyFlexLexer xxFlexLexer
42 //      #include <FlexLexer.h>
43 //
44 //      #undef yyFlexLexer
45 //      #define yyFlexLexer zzFlexLexer
46 //      #include <FlexLexer.h>
47 //      ...
48 
49 #ifndef __FLEX_LEXER_H
50 // Never included before - need to define base class.
51 #define __FLEX_LEXER_H
52 
53 #include <iostream>
54 
55 extern "C++" {
56 
57 struct yy_buffer_state;
58 typedef int yy_state_type;
59 
60 class FlexLexer
61 {
62 public:
~FlexLexer()63   virtual ~FlexLexer()        { }
64 
YYText()65   const char* YYText() const  { return yytext; }
YYLeng()66   int YYLeng()        const   { return yyleng; }
67 
68   virtual void
69   yy_switch_to_buffer( yy_buffer_state* new_buffer ) = 0;
70   virtual yy_buffer_state* yy_create_buffer( std::istream* s, int size ) = 0;
71   virtual yy_buffer_state* yy_create_buffer( std::istream& s, int size ) = 0;
72   virtual void yy_delete_buffer( yy_buffer_state* b ) = 0;
73   virtual void yyrestart( std::istream* s ) = 0;
74   virtual void yyrestart( std::istream& s ) = 0;
75 
76   virtual int yylex() = 0;
77 
78   // Call yylex with new input/output sources.
yylex(std::istream & new_in,std::ostream & new_out)79   int yylex( std::istream& new_in, std::ostream& new_out )
80   {
81     switch_streams( new_in, new_out );
82     return yylex();
83   }
84 
85   int yylex( std::istream* new_in, std::ostream* new_out = 0)
86   {
87     switch_streams( new_in, new_out );
88     return yylex();
89   }
90 
91   // Switch to new input/output streams.  A nil stream pointer
92   // indicates "keep the current one".
93   virtual void switch_streams( std::istream* new_in,
94                                std::ostream* new_out ) = 0;
95   virtual void switch_streams( std::istream& new_in,
96                                std::ostream& new_out ) = 0;
97 
lineno()98   int lineno() const          { return yylineno; }
99 
debug()100   int debug() const           { return yy_flex_debug; }
set_debug(int flag)101   void set_debug( int flag )  { yy_flex_debug = flag; }
102 
103 protected:
104   char* yytext;
105   int yyleng;
106   int yylineno;       // only maintained if you use %option yylineno
107   int yy_flex_debug;  // only has effect with -d or "%option debug"
108 };
109 
110 }
111 #endif // FLEXLEXER_H
112 
113 #if defined(yyFlexLexer) || ! defined(yyFlexLexerOnce)
114 // Either this is the first time through (yyFlexLexerOnce not defined),
115 // or this is a repeated include to define a different flavor of
116 // yyFlexLexer, as discussed in the flex manual.
117 # define yyFlexLexerOnce
118 
119 extern "C++" {
120 
121 class yyFlexLexer : public FlexLexer {
122 public:
123   // arg_yyin and arg_yyout default to the cin and cout, but we
124   // only make that assignment when initializing in yylex().
125   yyFlexLexer( std::istream& arg_yyin, std::ostream& arg_yyout );
126   yyFlexLexer( std::istream* arg_yyin = 0, std::ostream* arg_yyout = 0 );
127 private:
128   void ctor_common();
129 
130 public:
131 
132   virtual ~yyFlexLexer();
133 
134   void yy_switch_to_buffer( yy_buffer_state* new_buffer );
135   yy_buffer_state* yy_create_buffer( std::istream* s, int size );
136   yy_buffer_state* yy_create_buffer( std::istream& s, int size );
137   void yy_delete_buffer( yy_buffer_state* b );
138   void yyrestart( std::istream* s );
139   void yyrestart( std::istream& s );
140 
141   void yypush_buffer_state( yy_buffer_state* new_buffer );
142   void yypop_buffer_state();
143 
144   virtual int yylex();
145   virtual void switch_streams( std::istream& new_in, std::ostream& new_out );
146   virtual void switch_streams( std::istream* new_in = 0, std::ostream* new_out = 0 );
147   virtual int yywrap();
148 
149 protected:
150   virtual int LexerInput( char* buf, int max_size );
151   virtual void LexerOutput( const char* buf, int size );
152   virtual void LexerError( const char* msg );
153 
154   void yyunput( int c, char* buf_ptr );
155   int yyinput();
156 
157   void yy_load_buffer_state();
158   void yy_init_buffer( yy_buffer_state* b, std::istream& s );
159   void yy_flush_buffer( yy_buffer_state* b );
160 
161   int yy_start_stack_ptr;
162   int yy_start_stack_depth;
163   int* yy_start_stack;
164 
165   void yy_push_state( int new_state );
166   void yy_pop_state();
167   int yy_top_state();
168 
169   yy_state_type yy_get_previous_state();
170   yy_state_type yy_try_NUL_trans( yy_state_type current_state );
171   int yy_get_next_buffer();
172 
173   std::istream yyin;  // input source for default LexerInput
174   std::ostream yyout; // output sink for default LexerOutput
175 
176   // yy_hold_char holds the character lost when yytext is formed.
177   char yy_hold_char;
178 
179   // Number of characters read into yy_ch_buf.
180   int yy_n_chars;
181 
182   // Points to current character in buffer.
183   char* yy_c_buf_p;
184 
185   int yy_init;                // whether we need to initialize
186   int yy_start;               // start state number
187 
188   // Flag which is used to allow yywrap()'s to do buffer switches
189   // instead of setting up a fresh yyin.  A bit of a hack ...
190   int yy_did_buffer_switch_on_eof;
191 
192 
193   size_t yy_buffer_stack_top; /**< index of top of stack. */
194   size_t yy_buffer_stack_max; /**< capacity of stack. */
195   yy_buffer_state ** yy_buffer_stack; /**< Stack as an array. */
196   void yyensure_buffer_stack(void);
197 
198   // The following are not always needed, but may be depending
199   // on use of certain flex features (like REJECT or yymore()).
200 
201   yy_state_type yy_last_accepting_state;
202   char* yy_last_accepting_cpos;
203 
204   yy_state_type* yy_state_buf;
205   yy_state_type* yy_state_ptr;
206 
207   char* yy_full_match;
208   int* yy_full_state;
209   int yy_full_lp;
210 
211   int yy_lp;
212   int yy_looking_for_trail_begin;
213 
214   int yy_more_flag;
215   int yy_more_len;
216   int yy_more_offset;
217   int yy_prev_more_offset;
218 };
219 
220 }
221 
222 #endif // yyFlexLexer || ! yyFlexLexerOnce
223