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