/* 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>
#include <stdio.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Tbaselexer213024 Tbaselexer213024;
typedef struct TNimObject TNimObject;
typedef struct Tllstream212204 Tllstream212204;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
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;
};
struct  Tbaselexer213024  {
  TNimObject Sup;
NI bufpos;
NCSTRING buf;
NI buflen;
Tllstream212204* stream;
NI linenumber;
NI sentinel;
NI linestart;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  Tllstream212204  {
  TNimObject Sup;
NU8 kind;
FILE* f;
NimStringDesc* s;
NI rd;
NI wr;
NI lineoffset;
};
N_NOCONV(void*, alloc_8401)(NI size);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(void, fillbuffer_213097)(Tbaselexer213024* L);
N_NIMCALL(NI, llstreamread_212584)(Tllstream212204* s, void* buf, NI buflen);
N_NOCONV(void*, realloc_8433)(void* p, NI newsize);
N_NIMCALL(void, skiputf8bom_214416)(Tbaselexer213024* L);
N_NIMCALL(NI, fillbaselexer_213827)(Tbaselexer213024* L, NI pos);
N_NOCONV(void, dealloc_8448)(void* p);
N_NIMCALL(void, llstreamclose_212310)(Tllstream212204* s);
extern TNimType NTI4011; /* RootObj */
TNimType NTI213024; /* TBaseLexer */
extern TNimType NTI108; /* int */
extern TNimType NTI151; /* cstring */
extern TNimType NTI212206; /* PLLStream */

N_NIMCALL(void, fillbuffer_213097)(Tbaselexer213024* 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_212584((*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_8433(((void*) ((*L).buf)), ((NI) ((NI)((*L).buflen * ((NI) 1)))));
					(*L).buf = ((NCSTRING) (LOC22));
					LOC23 = 0;
					LOC23 = llstreamread_212584((*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_214416)(Tbaselexer213024* 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_213035)(Tbaselexer213024* L, Tllstream212204* inputstream, NI buflen) {
	void* LOC1;
	(*L).bufpos = ((NI) 0);
	(*L).buflen = buflen;
	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);
	unsureAsgnRef((void**) (&(*L).stream), inputstream);
	fillbuffer_213097(L);
	skiputf8bom_214416(L);
}

N_NIMCALL(NI, getcolnumber_213059)(Tbaselexer213024* 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_213827)(Tbaselexer213024* L, NI pos) {
	NI result;
	result = 0;
	{
		if (!(pos < (*L).sentinel)) goto LA3;
		result = (NI)(pos + ((NI) 1));
	}
	goto LA1;
	LA3: ;
	{
		fillbuffer_213097(L);
		(*L).bufpos = ((NI) 0);
		result = ((NI) 0);
	}
	LA1: ;
	(*L).linestart = result;
	return result;
}

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

N_NIMCALL(NI, handlelf_213074)(Tbaselexer213024* L, NI pos) {
	NI result;
	result = 0;
	(*L).linenumber += ((NI) 1);
	result = fillbaselexer_213827(L, pos);
	return result;
}

N_NIMCALL(void, closebaselexer_213045)(Tbaselexer213024* L) {
	dealloc_8448(((void*) ((*L).buf)));
	llstreamclose_212310((*L).stream);
}
NIM_EXTERNC N_NOINLINE(void, compiler_nimlexbaseInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, compiler_nimlexbaseDatInit000)(void) {
static TNimNode* TMP1172[7];
static TNimNode TMP789[8];
NTI213024.size = sizeof(Tbaselexer213024);
NTI213024.kind = 17;
NTI213024.base = (&NTI4011);
TMP1172[0] = &TMP789[1];
TMP789[1].kind = 1;
TMP789[1].offset = offsetof(Tbaselexer213024, bufpos);
TMP789[1].typ = (&NTI108);
TMP789[1].name = "bufpos";
TMP1172[1] = &TMP789[2];
TMP789[2].kind = 1;
TMP789[2].offset = offsetof(Tbaselexer213024, buf);
TMP789[2].typ = (&NTI151);
TMP789[2].name = "buf";
TMP1172[2] = &TMP789[3];
TMP789[3].kind = 1;
TMP789[3].offset = offsetof(Tbaselexer213024, buflen);
TMP789[3].typ = (&NTI108);
TMP789[3].name = "bufLen";
TMP1172[3] = &TMP789[4];
TMP789[4].kind = 1;
TMP789[4].offset = offsetof(Tbaselexer213024, stream);
TMP789[4].typ = (&NTI212206);
TMP789[4].name = "stream";
TMP1172[4] = &TMP789[5];
TMP789[5].kind = 1;
TMP789[5].offset = offsetof(Tbaselexer213024, linenumber);
TMP789[5].typ = (&NTI108);
TMP789[5].name = "lineNumber";
TMP1172[5] = &TMP789[6];
TMP789[6].kind = 1;
TMP789[6].offset = offsetof(Tbaselexer213024, sentinel);
TMP789[6].typ = (&NTI108);
TMP789[6].name = "sentinel";
TMP1172[6] = &TMP789[7];
TMP789[7].kind = 1;
TMP789[7].offset = offsetof(Tbaselexer213024, linestart);
TMP789[7].typ = (&NTI108);
TMP789[7].name = "lineStart";
TMP789[0].len = 7; TMP789[0].kind = 2; TMP789[0].sons = &TMP1172[0];
NTI213024.node = &TMP789[0];
}

