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

#include "nimbase.h"
#include <string.h>
#include <stdio.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Tbaselexer204015 Tbaselexer204015;
typedef struct TNimObject TNimObject;
typedef struct Tllstream203204 Tllstream203204;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
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;
};
struct  Tbaselexer204015  {
  TNimObject Sup;
NI bufpos;
NCSTRING buf;
NI buflen;
Tllstream203204* stream;
NI linenumber;
NI sentinel;
NI linestart;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  Tllstream203204  {
  TNimObject Sup;
NU8 kind;
FILE* f;
NimStringDesc* s;
NI rd;
NI wr;
NI lineoffset;
};
N_NOCONV(void*, alloc_7401)(NI size);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(void, fillbuffer_204058)(Tbaselexer204015* L);
N_NIMCALL(NI, llstreamread_203530)(Tllstream203204* s, void* buf, NI buflen);
N_NOCONV(void*, realloc_7433)(void* p, NI newsize);
N_NIMCALL(void, skiputf8bom_205410)(Tbaselexer204015* L);
N_NIMCALL(NI, fillbaselexer_204809)(Tbaselexer204015* L, NI pos);
N_NOCONV(void, dealloc_7448)(void* p);
N_NIMCALL(void, llstreamclose_203277)(Tllstream203204* s);
extern TNimType NTI3608; /* RootObj */
TNimType NTI204015; /* TBaseLexer */
extern TNimType NTI104; /* int */
extern TNimType NTI140; /* cstring */
extern TNimType NTI203206; /* PLLStream */

N_NIMCALL(void, fillbuffer_204058)(Tbaselexer204015* 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 = llstreamread_203530((*L).stream, ((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 = !((((NU8)((*L).buf[s])) == ((NU8)(13)) || ((NU8)((*L).buf[s])) == ((NU8)(10))));
						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 = llstreamread_203530((*L).stream, ((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_205410)(Tbaselexer204015* 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, openbaselexer_204023)(Tbaselexer204015* L, Tllstream203204* inputstream, NI buflen) {
	void* LOC1;
	(*L).bufpos = ((NI) 0);
	(*L).buflen = buflen;
	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);
	unsureAsgnRef((void**) (&(*L).stream), inputstream);
	fillbuffer_204058(L);
	skiputf8bom_205410(L);
}

N_NIMCALL(NI, getcolnumber_204038)(Tbaselexer204015* L, NI pos) {
	NI result;
	result = 0;
	result = ((NI)(pos - (*L).linestart) > 0? ((NI)(pos - (*L).linestart)) : -((NI)(pos - (*L).linestart)));
	return result;
}

N_NIMCALL(NI, fillbaselexer_204809)(Tbaselexer204015* L, NI pos) {
	NI result;
	result = 0;
	{
		if (!(pos < (*L).sentinel)) goto LA3;
		result = (NI)(pos + ((NI) 1));
	}
	goto LA1;
	LA3: ;
	{
		fillbuffer_204058(L);
		(*L).bufpos = ((NI) 0);
		result = ((NI) 0);
	}
	LA1: ;
	(*L).linestart = result;
	return result;
}

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

N_NIMCALL(NI, handlelf_204047)(Tbaselexer204015* L, NI pos) {
	NI result;
	result = 0;
	(*L).linenumber += ((NI) 1);
	result = fillbaselexer_204809(L, pos);
	return result;
}

N_NIMCALL(void, closebaselexer_204030)(Tbaselexer204015* L) {
	dealloc_7448(((void*) ((*L).buf)));
	llstreamclose_203277((*L).stream);
}
NIM_EXTERNC N_NOINLINE(void, compiler_nimlexbaseInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, compiler_nimlexbaseDatInit000)(void) {
static TNimNode* TMP1211[7];
static TNimNode TMP795[8];
NTI204015.size = sizeof(Tbaselexer204015);
NTI204015.kind = 17;
NTI204015.base = (&NTI3608);
TMP1211[0] = &TMP795[1];
TMP795[1].kind = 1;
TMP795[1].offset = offsetof(Tbaselexer204015, bufpos);
TMP795[1].typ = (&NTI104);
TMP795[1].name = "bufpos";
TMP1211[1] = &TMP795[2];
TMP795[2].kind = 1;
TMP795[2].offset = offsetof(Tbaselexer204015, buf);
TMP795[2].typ = (&NTI140);
TMP795[2].name = "buf";
TMP1211[2] = &TMP795[3];
TMP795[3].kind = 1;
TMP795[3].offset = offsetof(Tbaselexer204015, buflen);
TMP795[3].typ = (&NTI104);
TMP795[3].name = "bufLen";
TMP1211[3] = &TMP795[4];
TMP795[4].kind = 1;
TMP795[4].offset = offsetof(Tbaselexer204015, stream);
TMP795[4].typ = (&NTI203206);
TMP795[4].name = "stream";
TMP1211[4] = &TMP795[5];
TMP795[5].kind = 1;
TMP795[5].offset = offsetof(Tbaselexer204015, linenumber);
TMP795[5].typ = (&NTI104);
TMP795[5].name = "lineNumber";
TMP1211[5] = &TMP795[6];
TMP795[6].kind = 1;
TMP795[6].offset = offsetof(Tbaselexer204015, sentinel);
TMP795[6].typ = (&NTI104);
TMP795[6].name = "sentinel";
TMP1211[6] = &TMP795[7];
TMP795[7].kind = 1;
TMP795[7].offset = offsetof(Tbaselexer204015, linestart);
TMP795[7].typ = (&NTI104);
TMP795[7].name = "lineStart";
TMP795[0].len = 7; TMP795[0].kind = 2; TMP795[0].sons = &TMP1211[0];
NTI204015.node = &TMP795[0];
}

