/* Generated by Nim Compiler v0.13.0 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 64

#include "nimbase.h"
#include <string.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Baselexer267009 Baselexer267009;
typedef struct TNimObject TNimObject;
typedef struct Streamobj147418 Streamobj147418;
typedef N_NIMCALL_PTR(void, TY3489) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3494) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3489 marker;
TY3494 deepcopy;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  TNimObject  {
TNimType* m_type;
};
typedef NU8 TY204404[32];
struct  Baselexer267009  {
  TNimObject Sup;
NI bufpos;
NCSTRING buf;
NI buflen;
Streamobj147418* input;
NI linenumber;
NI sentinel;
NI linestart;
TY204404 refillchars;
};
typedef N_NIMCALL_PTR(void, TY147419) (Streamobj147418* s);
typedef N_NIMCALL_PTR(NIM_BOOL, TY147423) (Streamobj147418* s);
typedef N_NIMCALL_PTR(void, TY147427) (Streamobj147418* s, NI pos);
typedef N_NIMCALL_PTR(NI, TY147432) (Streamobj147418* s);
typedef N_NIMCALL_PTR(NI, TY147436) (Streamobj147418* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(NI, TY147442) (Streamobj147418* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY147448) (Streamobj147418* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY147454) (Streamobj147418* s);
struct  Streamobj147418  {
  TNimObject Sup;
TY147419 closeimpl;
TY147423 atendimpl;
TY147427 setpositionimpl;
TY147432 getpositionimpl;
TY147436 readdataimpl;
TY147442 peekdataimpl;
TY147448 writedataimpl;
TY147454 flushimpl;
};
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NOCONV(void*, alloc_7401)(NI size);
N_NIMCALL(void, fillbuffer_267201)(Baselexer267009* L);
N_NIMCALL(NI, readdata_147511)(Streamobj147418* s, void* buffer, NI buflen);
N_NOCONV(void*, realloc_7433)(void* p, NI newsize);
N_NIMCALL(void, skiputf8bom_269201)(Baselexer267009* L);
N_NOCONV(void, dealloc_7448)(void* p);
N_NIMCALL(void, close_147470)(Streamobj147418* s);
N_NIMCALL(NI, fillbaselexer_268409)(Baselexer267009* L, NI pos);
extern TNimType NTI3608; /* RootObj */
TNimType NTI267009; /* BaseLexer */
extern TNimType NTI104; /* int */
extern TNimType NTI140; /* cstring */
extern TNimType NTI147416; /* Stream */
extern TNimType NTI204404; /* set[char] */

N_NIMCALL(void, fillbuffer_267201)(Baselexer267009* L) {
	NI charsread;
	NI tocopy;
	NI s;
	NI oldbuflen;
	NI LOC5;
	charsread = 0;
	tocopy = 0;
	s = 0;
	oldbuflen = 0;
	tocopy = (NI)((NI)((*L).buflen - (*L).sentinel) - ((NI) 1));
	{
		if (!(((NI) 0) < tocopy)) goto LA3;
		memmove(((void*) ((*L).buf)), ((void*) ((&(*L).buf[(NI)((*L).sentinel + ((NI) 1))]))), ((NI) ((NI)(tocopy * ((NI) 1)))));
	}
	LA3: ;
	LOC5 = 0;
	LOC5 = readdata_147511((*L).input, ((void*) ((&(*L).buf[tocopy]))), (NI)((NI)((*L).sentinel + ((NI) 1)) * ((NI) 1)));
	charsread = (NI)(LOC5 / ((NI) 1));
	s = (NI)(tocopy + charsread);
	{
		if (!(charsread < (NI)((*L).sentinel + ((NI) 1)))) goto LA8;
		(*L).buf[s] = 0;
		(*L).sentinel = s;
	}
	goto LA6;
	LA8: ;
	{
		s -= ((NI) 1);
		{
			while (1) {
				{
					while (1) {
						NIM_BOOL LOC15;
						LOC15 = 0;
						LOC15 = (((NI) 0) <= s);
						if (!(LOC15)) goto LA16;
						LOC15 = !((((*L).refillchars[(NU)(((NU8)((*L).buf[s])))>>3] &(1U<<((NU)(((NU8)((*L).buf[s])))&7U)))!=0));
						LA16: ;
						if (!LOC15) goto LA14;
						s -= ((NI) 1);
					} LA14: ;
				}
				{
					if (!(((NI) 0) <= s)) goto LA19;
					(*L).sentinel = s;
					goto LA11;
				}
				goto LA17;
				LA19: ;
				{
					void* LOC22;
					NI LOC23;
					oldbuflen = (*L).buflen;
					(*L).buflen = (NI)((*L).buflen * ((NI) 2));
					LOC22 = 0;
					LOC22 = realloc_7433(((void*) ((*L).buf)), ((NI) ((NI)((*L).buflen * ((NI) 1)))));
					(*L).buf = ((NCSTRING) (LOC22));
					LOC23 = 0;
					LOC23 = readdata_147511((*L).input, ((void*) ((&(*L).buf[oldbuflen]))), (NI)(oldbuflen * ((NI) 1)));
					charsread = (NI)(LOC23 / ((NI) 1));
					{
						if (!(charsread < oldbuflen)) goto LA26;
						(*L).buf[(NI)(oldbuflen + charsread)] = 0;
						(*L).sentinel = (NI)(oldbuflen + charsread);
						goto LA11;
					}
					LA26: ;
					s = (NI)((*L).buflen - ((NI) 1));
				}
				LA17: ;
			}
		} LA11: ;
	}
	LA6: ;
}

N_NIMCALL(void, skiputf8bom_269201)(Baselexer267009* L) {
	{
		NIM_BOOL LOC3;
		NIM_BOOL LOC4;
		LOC3 = 0;
		LOC4 = 0;
		LOC4 = ((NU8)((*L).buf[((NI) 0)]) == (NU8)(239));
		if (!(LOC4)) goto LA5;
		LOC4 = ((NU8)((*L).buf[((NI) 1)]) == (NU8)(187));
		LA5: ;
		LOC3 = LOC4;
		if (!(LOC3)) goto LA6;
		LOC3 = ((NU8)((*L).buf[((NI) 2)]) == (NU8)(191));
		LA6: ;
		if (!LOC3) goto LA7;
		(*L).bufpos += ((NI) 3);
		(*L).linestart += ((NI) 3);
	}
	LA7: ;
}

N_NIMCALL(void, open_269223)(Baselexer267009* L, Streamobj147418* input, NI buflen, TY204404 refillchars) {
	void* LOC1;
	unsureAsgnRef((void**) (&(*L).input), input);
	(*L).bufpos = ((NI) 0);
	(*L).buflen = buflen;
	memcpy((void*)(*L).refillchars, (NIM_CONST void*)refillchars, 32);
	LOC1 = 0;
	LOC1 = alloc_7401(((NI) ((NI)(buflen * ((NI) 1)))));
	(*L).buf = ((NCSTRING) (LOC1));
	(*L).sentinel = (NI)(buflen - ((NI) 1));
	(*L).linestart = ((NI) 0);
	(*L).linenumber = ((NI) 1);
	fillbuffer_267201(L);
	skiputf8bom_269201(L);
}

N_NIMCALL(void, close_267022)(Baselexer267009* L) {
	dealloc_7448(((void*) ((*L).buf)));
	close_147470((*L).input);
}

N_NIMCALL(NI, fillbaselexer_268409)(Baselexer267009* L, NI pos) {
	NI result;
	result = 0;
	{
		if (!(pos < (*L).sentinel)) goto LA3;
		result = (NI)(pos + ((NI) 1));
	}
	goto LA1;
	LA3: ;
	{
		fillbuffer_267201(L);
		(*L).bufpos = ((NI) 0);
		result = ((NI) 0);
	}
	LA1: ;
	return result;
}

N_NIMCALL(NI, handlecr_268601)(Baselexer267009* L, NI pos) {
	NI result;
	result = 0;
	(*L).linenumber += ((NI) 1);
	result = fillbaselexer_268409(L, pos);
	{
		if (!((NU8)((*L).buf[result]) == (NU8)(10))) goto LA3;
		result = fillbaselexer_268409(L, result);
	}
	LA3: ;
	(*L).linestart = result;
	return result;
}

N_NIMCALL(NI, handlelf_268810)(Baselexer267009* L, NI pos) {
	NI result;
	result = 0;
	(*L).linenumber += ((NI) 1);
	result = fillbaselexer_268409(L, pos);
	(*L).linestart = result;
	return result;
}

N_NIMCALL(NI, getcolnumber_269802)(Baselexer267009* L, NI pos) {
	NI result;
	result = 0;
	result = ((NI)(pos - (*L).linestart) > 0? ((NI)(pos - (*L).linestart)) : -((NI)(pos - (*L).linestart)));
	return result;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_lexbaseInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_lexbaseDatInit000)(void) {
static TNimNode* TMP2833[8];
static TNimNode TMP951[9];
NTI267009.size = sizeof(Baselexer267009);
NTI267009.kind = 17;
NTI267009.base = (&NTI3608);
TMP2833[0] = &TMP951[1];
TMP951[1].kind = 1;
TMP951[1].offset = offsetof(Baselexer267009, bufpos);
TMP951[1].typ = (&NTI104);
TMP951[1].name = "bufpos";
TMP2833[1] = &TMP951[2];
TMP951[2].kind = 1;
TMP951[2].offset = offsetof(Baselexer267009, buf);
TMP951[2].typ = (&NTI140);
TMP951[2].name = "buf";
TMP2833[2] = &TMP951[3];
TMP951[3].kind = 1;
TMP951[3].offset = offsetof(Baselexer267009, buflen);
TMP951[3].typ = (&NTI104);
TMP951[3].name = "bufLen";
TMP2833[3] = &TMP951[4];
TMP951[4].kind = 1;
TMP951[4].offset = offsetof(Baselexer267009, input);
TMP951[4].typ = (&NTI147416);
TMP951[4].name = "input";
TMP2833[4] = &TMP951[5];
TMP951[5].kind = 1;
TMP951[5].offset = offsetof(Baselexer267009, linenumber);
TMP951[5].typ = (&NTI104);
TMP951[5].name = "lineNumber";
TMP2833[5] = &TMP951[6];
TMP951[6].kind = 1;
TMP951[6].offset = offsetof(Baselexer267009, sentinel);
TMP951[6].typ = (&NTI104);
TMP951[6].name = "sentinel";
TMP2833[6] = &TMP951[7];
TMP951[7].kind = 1;
TMP951[7].offset = offsetof(Baselexer267009, linestart);
TMP951[7].typ = (&NTI104);
TMP951[7].name = "lineStart";
TMP2833[7] = &TMP951[8];
TMP951[8].kind = 1;
TMP951[8].offset = offsetof(Baselexer267009, refillchars);
TMP951[8].typ = (&NTI204404);
TMP951[8].name = "refillChars";
TMP951[0].len = 8; TMP951[0].kind = 2; TMP951[0].sons = &TMP2833[0];
NTI267009.node = &TMP951[0];
}

