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