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

#include "nimbase.h"
#include <string.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Baselexer282024 Baselexer282024;
typedef struct TNimObject TNimObject;
typedef struct Streamobj149427 Streamobj149427;
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 TY213407[32];
struct  Baselexer282024  {
  TNimObject Sup;
NI bufpos;
NCSTRING buf;
NI buflen;
Streamobj149427* input;
NI linenumber;
NI sentinel;
NI linestart;
TY213407 refillchars;
};
typedef N_NIMCALL_PTR(void, TY149428) (Streamobj149427* s);
typedef N_NIMCALL_PTR(NIM_BOOL, TY149432) (Streamobj149427* s);
typedef N_NIMCALL_PTR(void, TY149436) (Streamobj149427* s, NI pos);
typedef N_NIMCALL_PTR(NI, TY149441) (Streamobj149427* s);
typedef N_NIMCALL_PTR(NI, TY149445) (Streamobj149427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(NI, TY149451) (Streamobj149427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY149457) (Streamobj149427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY149463) (Streamobj149427* s);
struct  Streamobj149427  {
  TNimObject Sup;
TY149428 closeimpl;
TY149432 atendimpl;
TY149436 setpositionimpl;
TY149441 getpositionimpl;
TY149445 readdataimpl;
TY149451 peekdataimpl;
TY149457 writedataimpl;
TY149463 flushimpl;
};
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NOCONV(void*, alloc_8401)(NI size);
N_NIMCALL(void, fillbuffer_282056)(Baselexer282024* L);
N_NIMCALL(NI, readdata_149580)(Streamobj149427* s, void* buffer, NI buflen);
N_NOCONV(void*, realloc_8433)(void* p, NI newsize);
N_NIMCALL(void, skiputf8bom_283607)(Baselexer282024* L);
N_NOCONV(void, dealloc_8448)(void* p);
N_NIMCALL(void, close_149491)(Streamobj149427* s);
N_NIMCALL(NI, fillbaselexer_282827)(Baselexer282024* L, NI pos);
extern TNimType NTI4011; /* RootObj */
TNimType NTI282024; /* BaseLexer */
extern TNimType NTI108; /* int */
extern TNimType NTI151; /* cstring */
extern TNimType NTI149425; /* Stream */
extern TNimType NTI213407; /* set[char] */

N_NIMCALL(void, fillbuffer_282056)(Baselexer282024* 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_149580((*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_149580((*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_283607)(Baselexer282024* 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_283635)(Baselexer282024* L, Streamobj149427* input, NI buflen, TY213407 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_282056(L);
	skiputf8bom_283607(L);
}

N_NIMCALL(void, close_282046)(Baselexer282024* L) {
	dealloc_8448(((void*) ((*L).buf)));
	close_149491((*L).input);
}

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

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

N_NIMCALL(NI, handlelf_283216)(Baselexer282024* L, NI pos) {
	NI result;
	result = 0;
	(*L).linenumber += ((NI) 1);
	result = fillbaselexer_282827(L, pos);
	(*L).linestart = result;
	return result;
}

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

