summaryrefslogblamecommitdiff
path: root/lexer.re
blob: 4c189f18a6a6b49e738cbccd0cf1736cf27b2965 (plain) (tree)































































                                                           
/* vi: set ts=4 sw=4 noet ft=c : */

#include <stdbool.h>
#include <stdio.h>
#include <string.h>

/*!include:re2c "lexer.h"*/
#include "parser.h"

int fill_buffer(lexer_t *in, size_t need) {

	if (in->eof) {
		return 1;
	}

	size_t space_free = in->tok - in->buf;
	if (space_free < need) {
		return 2;
	}

	size_t space_used = in->lim - in->tok;
	memmove(in->buf, in->tok, space_used);
	in->lim -= space_free;
	in->cur -= space_free;
	in->tok -= space_free;

	in->lim += fread(in->lim, 1, space_free, in->file);

	if (in->lim < in->buf + BUFSIZE) {
		in->eof = 1;
		memset(in->lim, 0, YYMAXFILL);
		in->lim += YYMAXFILL;
	}

	return 0;
}

void init_lexer(lexer_t *in, FILE *file) {
	in->file = file;
	in->cur = in->tok = in->lim = in->buf + BUFSIZE;
	in->eof = false;

	fill_buffer(in, 1);
}

#define YYFILL(n) if (fill_buffer(in, n)) return 0
int lex(lexer_t *in) {
loop:

	/*!re2c
	re2c:define:YYCTYPE = char;
	re2c:define:YYCURSOR = in->cur;
	re2c:define:YYLIMIT = in->lim;

	* { return 0; }
	[(] { return LPAREN; }
	[)] { return RPAREN; }
	[,] { return SEP; }
	[a-zA-Z]+ { return ID; }
	[0-9]+ { return INT; }
	[ ]+ { goto loop; }

	*/
}