/* Generated by Nim Compiler v0.12.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 Baselexer284024 Baselexer284024;
typedef struct TNimObject TNimObject;
typedef struct Streamobj152427 Streamobj152427;
typedef N_NIMCALL_PTR(void, TY3889) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3894) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3889 marker;
TY3894 deepcopy;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  TNimObject  {
TNimType* m_type;
};
typedef NU8 TY215407[32];
struct  Baselexer284024  {
  TNimObject Sup;
NI bufpos;
NCSTRING buf;
NI buflen;
Streamobj152427* input;
NI linenumber;
NI sentinel;
NI linestart;
TY215407 refillchars;
};
typedef N_NIMCALL_PTR(void, TY152428) (Streamobj152427* s);
typedef N_NIMCALL_PTR(NIM_BOOL, TY152432) (Streamobj152427* s);
typedef N_NIMCALL_PTR(void, TY152436) (Streamobj152427* s, NI pos);
typedef N_NIMCALL_PTR(NI, TY152441) (Streamobj152427* s);
typedef N_NIMCALL_PTR(NI, TY152445) (Streamobj152427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(NI, TY152451) (Streamobj152427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY152457) (Streamobj152427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY152463) (Streamobj152427* s);
struct  Streamobj152427  {
  TNimObject Sup;
TY152428 closeimpl;
TY152432 atendimpl;
TY152436 setpositionimpl;
TY152441 getpositionimpl;
TY152445 readdataimpl;
TY152451 peekdataimpl;
TY152457 writedataimpl;
TY152463 flushimpl;
};
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NOCONV(void*, alloc_8401)(NI size);
N_NIMCALL(void, fillbuffer_284056)(Baselexer284024* L);
N_NIMCALL(NI, readdata_152580)(Streamobj152427* s, void* buffer, NI buflen);
N_NOCONV(void*, realloc_8433)(void* p, NI newsize);
N_NIMCALL(void, skiputf8bom_285607)(Baselexer284024* L);
N_NOCONV(void, dealloc_8448)(void* p);
N_NIMCALL(void, close_152491)(Streamobj152427* s);
N_NIMCALL(NI, fillbaselexer_284827)(Baselexer284024* L, NI pos);
extern TNimType NTI4011; /* RootObj */
TNimType NTI284024; /* BaseLexer */
extern TNimType NTI108; /* int */
extern TNimType NTI151; /* cstring */
extern TNimType NTI152425; /* Stream */
extern TNimType NTI215407; /* set[char] */

N_NIMCALL(void, fillbuffer_284056)(Baselexer284024* 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_152580((*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[((NU8)((*L).buf[s]))/8] &(1<<(((NU8)((*L).buf[s]))%8)))!=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_8433(((void*) ((*L).buf)), ((NI) ((NI)((*L).buflen * ((NI) 1)))));
					(*L).buf = ((NCSTRING) (LOC22));
					LOC23 = 0;
					LOC23 = readdata_152580((*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_285607)(Baselexer284024* 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_285635)(Baselexer284024* L, Streamobj152427* input, NI buflen, TY215407 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_8401(((NI) ((NI)(buflen * ((NI) 1)))));
	(*L).buf = ((NCSTRING) (LOC1));
	(*L).sentinel = (NI)(buflen - ((NI) 1));
	(*L).linestart = ((NI) 0);
	(*L).linenumber = ((NI) 1);
	fillbuffer_284056(L);
	skiputf8bom_285607(L);
}

N_NIMCALL(void, close_284046)(Baselexer284024* L) {
	dealloc_8448(((void*) ((*L).buf)));
	close_152491((*L).input);
}

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

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

N_NIMCALL(NI, handlelf_285216)(Baselexer284024* L, NI pos) {
	NI result;
	result = 0;
	(*L).linenumber += ((NI) 1);
	result = fillbaselexer_284827(L, pos);
	(*L).linestart = result;
	return result;
}

N_NIMCALL(NI, getcolnumber_286008)(Baselexer284024* 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* TMP2793[8];
static TNimNode TMP944[9];
NTI284024.size = sizeof(Baselexer284024);
NTI284024.kind = 17;
NTI284024.base = (&NTI4011);
TMP2793[0] = &TMP944[1];
TMP944[1].kind = 1;
TMP944[1].offset = offsetof(Baselexer284024, bufpos);
TMP944[1].typ = (&NTI108);
TMP944[1].name = "bufpos";
TMP2793[1] = &TMP944[2];
TMP944[2].kind = 1;
TMP944[2].offset = offsetof(Baselexer284024, buf);
TMP944[2].typ = (&NTI151);
TMP944[2].name = "buf";
TMP2793[2] = &TMP944[3];
TMP944[3].kind = 1;
TMP944[3].offset = offsetof(Baselexer284024, buflen);
TMP944[3].typ = (&NTI108);
TMP944[3].name = "bufLen";
TMP2793[3] = &TMP944[4];
TMP944[4].kind = 1;
TMP944[4].offset = offsetof(Baselexer284024, input);
TMP944[4].typ = (&NTI152425);
TMP944[4].name = "input";
TMP2793[4] = &TMP944[5];
TMP944[5].kind = 1;
TMP944[5].offset = offsetof(Baselexer284024, linenumber);
TMP944[5].typ = (&NTI108);
TMP944[5].name = "lineNumber";
TMP2793[5] = &TMP944[6];
TMP944[6].kind = 1;
TMP944[6].offset = offsetof(Baselexer284024, sentinel);
TMP944[6].typ = (&NTI108);
TMP944[6].name = "sentinel";
TMP2793[6] = &TMP944[7];
TMP944[7].kind = 1;
TMP944[7].offset = offsetof(Baselexer284024, linestart);
TMP944[7].typ = (&NTI108);
TMP944[7].name = "lineStart";
TMP2793[7] = &TMP944[8];
TMP944[8].kind = 1;
TMP944[8].offset = offsetof(Baselexer284024, refillchars);
TMP944[8].typ = (&NTI215407);
TMP944[8].name = "refillChars";
TMP944[0].len = 8; TMP944[0].kind = 2; TMP944[0].sons = &TMP2793[0];
NTI284024.node = &TMP944[0];
}

