7 typedef struct _Scanner {
35 void scan_init( Scanner *s, FILE *file )
37 memset (s, '\0', sizeof(Scanner));
44 #define TK_NO_TOKEN (-1)
47 #define TK_Identifier 130
51 #define ret_tok( _tok ) token = _tok; s->data = s->ts
53 int scan( Scanner *s )
55 int token = TK_NO_TOKEN;
59 if ( s->p == s->pe ) {
60 printf("scanner: need more data\n");
65 /* There is data that needs to be shifted over. */
66 printf("scanner: buffer broken mid token\n");
67 s->have = s->pe - s->ts;
68 memmove( s->buf, s->ts, s->have );
69 s->te -= (s->ts-s->buf);
73 s->p = s->buf + s->have;
74 space = BUFSIZE - s->have;
77 /* We filled up the buffer trying to scan a token. */
78 printf("scanner: out of buffer space\n");
83 printf("scanner: end of file\n");
88 readlen = fread( s->p, 1, space, s->file );
89 if ( readlen < space )
93 s->pe = s->p + readlen;
106 ( [a-zA-Z_] [a-zA-Z0-9_]* ) =>
107 { ret_tok( TK_Identifier ); fbreak; };
112 '"' ( [^\\"] | '\\' any ) * '"' =>
113 { ret_tok( TK_String ); fbreak; };
117 { ret_tok( TK_Number ); fbreak; };
121 { ret_tok( TK_EOF ); fbreak; };
125 { ret_tok( *s->p ); fbreak; };
132 if ( s->cs == Scanner_error )
135 if ( token != TK_NO_TOKEN ) {
136 s->len = s->p - s->data;
143 int main (int argc, char** argv)
148 scan_init(&ss, stdin);
152 if ( tok == TK_EOF ) {
153 printf ("parser: EOF\n");
156 else if ( tok == TK_ERR ) {
157 printf ("parser: ERR\n");
161 printf ("parser: %d \"", tok);
162 fwrite ( ss.data, 1, ss.len, stdout );