1 #ifndef _CPPSCAN1_H 2 #define _CPPSCAN1_H 3 4 #include <iostream> 5 #include <cstdlib> 6 #include <cstring> 7 8 using namespace std; 9 10 #define BUFSIZE 2048 11 12 #define TK_Dlit 192 13 #define TK_Slit 193 14 #define TK_Float 194 15 #define TK_Id 195 16 #define TK_NameSep 197 17 #define TK_Arrow 211 18 #define TK_PlusPlus 212 19 #define TK_MinusMinus 213 20 #define TK_ArrowStar 214 21 #define TK_DotStar 215 22 #define TK_ShiftLeft 216 23 #define TK_ShiftRight 217 24 #define TK_IntegerDecimal 218 25 #define TK_IntegerOctal 219 26 #define TK_IntegerHex 220 27 #define TK_EqualsEquals 223 28 #define TK_NotEquals 224 29 #define TK_AndAnd 225 30 #define TK_OrOr 226 31 #define TK_MultAssign 227 32 #define TK_DivAssign 228 33 #define TK_PercentAssign 229 34 #define TK_PlusAssign 230 35 #define TK_MinusAssign 231 36 #define TK_AmpAssign 232 37 #define TK_CaretAssign 233 38 #define TK_BarAssign 234 39 #define TK_DotDotDot 240 40 41 /* A growable buffer for collecting headers. */ 42 struct Buffer 43 { BufferBuffer44 Buffer() : data(0), allocated(0), length(0) { } BufferBuffer45 Buffer( const Buffer &other ) { 46 data = (char*)malloc( other.allocated ); 47 memcpy( data, other.data, other.length ); 48 allocated = other.allocated; 49 length = other.length; 50 } ~BufferBuffer51 ~Buffer() { empty(); } 52 appendBuffer53 void append( char p ) { 54 if ( ++length > allocated ) 55 upAllocate( length*2 ); 56 data[length-1] = p; 57 } appendBuffer58 void append( char *str, int len ) { 59 if ( (length += len) > allocated ) 60 upAllocate( length*2 ); 61 memcpy( data+length-len, str, len ); 62 } 63 clearBuffer64 void clear() { length = 0; } 65 void upAllocate( int len ); 66 void empty(); 67 68 char *data; 69 int allocated; 70 int length; 71 }; 72 73 74 struct Scanner 75 { ScannerScanner76 Scanner( std::ostream &out ) 77 : out(out) { } 78 79 std::ostream &out; 80 81 int line, col; 82 int tokStart; 83 int inlineDepth; 84 int count; 85 Buffer tokBuf; 86 Buffer nonTokBuf; 87 passScanner88 void pass(char c) { nonTokBuf.append(c); } bufScanner89 void buf(char c) { tokBuf.append(c); } 90 void token( int id ); 91 92 int cs, stack, top; 93 94 // Initialize the machine. Invokes any init statement blocks. Returns 0 95 // if the machine begins in a non-accepting state and 1 if the machine 96 // begins in an accepting state. 97 void init( ); 98 99 // Execute the machine on a block of data. Returns -1 if after processing 100 // the data, the machine is in the error state and can never accept, 0 if 101 // the machine is in a non-accepting state and 1 if the machine is in an 102 // accepting state. 103 int execute( const char *data, int len ); 104 105 // Indicate that there is no more data. Returns -1 if the machine finishes 106 // in the error state and does not accept, 0 if the machine finishes 107 // in any other non-accepting state and 1 if the machine finishes in an 108 // accepting state. 109 int finish( ); 110 }; 111 112 #endif 113