/* Generated by Nim Compiler v0.16.0 */
/*   (c) 2017 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, amd64, gcc */
/* Command for C compiler:
   gcc -c  -w -O3 -fno-strict-aliasing -fPIC  -I/build/nim/src/Nim-0.16.0/lib -o lib/nimcache/stdlib_lexbase.o lib/nimcache/stdlib_lexbase.c */
#define NIM_INTBITS 64

#include "nimbase.h"
#include <string.h>
#undef linux
typedef struct Baselexer_B6cGbTPhMsqMovc9cOcIHRw Baselexer_B6cGbTPhMsqMovc9cOcIHRw;
typedef struct Streamobj_RShkcoDOPnoCVhtpN1hDPQ Streamobj_RShkcoDOPnoCVhtpN1hDPQ;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef NU8 TY_nmiMWKVIe46vacnhAFrQvw_Set[32];
typedef NU8 Tnimkind_jIBKr1ejBgsfM33Kxw4j7A;
typedef NU8 Tnimtypeflag_v8QUszD1sWlSIWZz7mC4bQ_Set;
typedef N_NIMCALL_PTR(void, TY_ojoeKfW4VYIm36I9cpDTQIg) (void* p0, NI op0);
typedef N_NIMCALL_PTR(void*, TY_WSm2xU5ARYv9aAR4l0z9c9auQ) (void* p0);
struct  TNimType  {
NI size;
Tnimkind_jIBKr1ejBgsfM33Kxw4j7A kind;
Tnimtypeflag_v8QUszD1sWlSIWZz7mC4bQ_Set flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY_ojoeKfW4VYIm36I9cpDTQIg marker;
TY_WSm2xU5ARYv9aAR4l0z9c9auQ deepcopy;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Baselexer_B6cGbTPhMsqMovc9cOcIHRw  {
  TNimObject Sup;
NI bufpos;
NCSTRING buf;
NI buflen;
Streamobj_RShkcoDOPnoCVhtpN1hDPQ* input;
NI linenumber;
NI sentinel;
NI linestart;
TY_nmiMWKVIe46vacnhAFrQvw_Set refillchars;
};
typedef N_NIMCALL_PTR(void, TY_cnkJEp9bna8U6yi9aVW9cAJ0Q) (Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0);
typedef N_NIMCALL_PTR(NIM_BOOL, TY_KjFcmLOhf4VQvVcyLJWXZw) (Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0);
typedef N_NIMCALL_PTR(void, TY_jlOYFw9cHausuCbPoWx2iiA) (Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0, NI pos0);
typedef N_NIMCALL_PTR(NI, TY_Xzm85qBLb9cf9cu8VOfAr8PQ) (Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0);
typedef N_NIMCALL_PTR(NI, TY_tjaJtoUzLe1BMUbItlcGhQ) (Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0, void* buffer0, NI buflen0);
typedef N_NIMCALL_PTR(void, TY_b9bsIuILxTLVZHPvJBW187A) (Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0, void* buffer0, NI buflen0);
struct  Streamobj_RShkcoDOPnoCVhtpN1hDPQ  {
  TNimObject Sup;
TY_cnkJEp9bna8U6yi9aVW9cAJ0Q closeimpl;
TY_KjFcmLOhf4VQvVcyLJWXZw atendimpl;
TY_jlOYFw9cHausuCbPoWx2iiA setpositionimpl;
TY_Xzm85qBLb9cf9cu8VOfAr8PQ getpositionimpl;
TY_tjaJtoUzLe1BMUbItlcGhQ readdataimpl;
TY_tjaJtoUzLe1BMUbItlcGhQ peekdataimpl;
TY_b9bsIuILxTLVZHPvJBW187A writedataimpl;
TY_cnkJEp9bna8U6yi9aVW9cAJ0Q flushimpl;
};
typedef NU8 Tnimnodekind_unfNsxrcATrufDZmpBq4HQ;
struct  TNimNode  {
Tnimnodekind_unfNsxrcATrufDZmpBq4HQ kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
N_NIMCALL(void, unsureAsgnRef)(void** dest0, void* src0);
N_LIB_EXPORT N_NOCONV(void*, nimrtl_alloc)(NI size0);
N_NIMCALL(void, fillbuffer_QbedUCk2XwI9c7bbRzXBY0g)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0);
static N_INLINE(void, movemem_E1xtACub5WcDa3vbrIXbwglexbase)(void* dest0, void* source0, NI size0);
N_NIMCALL(NI, readdata_Gnwdizh7H5Q9cYivUoGHrVA)(Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0, void* buffer0, NI buflen0);
N_LIB_EXPORT N_NOCONV(void*, nimrtl_realloc)(void* p0, NI newsize0);
N_NIMCALL(void, skiputf8bom_BMPUUaJVkFzDJfE3dUzOCw)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0);
N_LIB_EXPORT N_NOCONV(void, nimrtl_dealloc)(void* p0);
N_NIMCALL(void, close_4ed7IXme7lqtClFI7s2sTQ)(Streamobj_RShkcoDOPnoCVhtpN1hDPQ* s0);
N_NIMCALL(NI, fillbaselexer_IwUEvMBOLRY9cqyar6XKEaw_2)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0, NI pos0);

static N_INLINE(void, movemem_E1xtACub5WcDa3vbrIXbwglexbase)(void* dest0, void* source0, NI size0) {
	void* LOC1;
	LOC1 = (void*)0;
	LOC1 = memmove(dest0, source0, ((size_t) (size0)));
}

N_NIMCALL(void, fillbuffer_QbedUCk2XwI9c7bbRzXBY0g)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0) {
	NI charsread0;
	NI tocopy0;
	NI s0;
	NI oldbuflen0;
	NI LOC5;
	charsread0 = (NI)0;
	tocopy0 = (NI)0;
	s0 = (NI)0;
	oldbuflen0 = (NI)0;
	tocopy0 = (NI)((NI)((*L0).buflen - (*L0).sentinel) - ((NI) 1));
	{
		if (!(((NI) 0) < tocopy0)) goto LA3;
		movemem_E1xtACub5WcDa3vbrIXbwglexbase(((void*) ((*L0).buf)), ((void*) ((&(*L0).buf[(NI)((*L0).sentinel + ((NI) 1))]))), ((NI) ((NI)(tocopy0 * ((NI) 1)))));
	}
	LA3: ;
	LOC5 = (NI)0;
	LOC5 = readdata_Gnwdizh7H5Q9cYivUoGHrVA((*L0).input, ((void*) ((&(*L0).buf[tocopy0]))), (NI)((NI)((*L0).sentinel + ((NI) 1)) * ((NI) 1)));
	charsread0 = (NI)(LOC5 / ((NI) 1));
	s0 = (NI)(tocopy0 + charsread0);
	{
		if (!(charsread0 < (NI)((*L0).sentinel + ((NI) 1)))) goto LA8;
		(*L0).buf[s0] = 0;
		(*L0).sentinel = s0;
	}
	goto LA6;
	LA8: ;
	{
		s0 -= ((NI) 1);
		{
			while (1) {
				{
					while (1) {
						NIM_BOOL LOC15;
						LOC15 = (NIM_BOOL)0;
						LOC15 = (((NI) 0) <= s0);
						if (!(LOC15)) goto LA16;
						LOC15 = !((((*L0).refillchars[(NU)(((NU8)((*L0).buf[s0])))>>3] &(1U<<((NU)(((NU8)((*L0).buf[s0])))&7U)))!=0));
						LA16: ;
						if (!LOC15) goto LA14;
						s0 -= ((NI) 1);
					} LA14: ;
				}
				{
					if (!(((NI) 0) <= s0)) goto LA19;
					(*L0).sentinel = s0;
					goto LA11;
				}
				goto LA17;
				LA19: ;
				{
					void* LOC22;
					NI LOC23;
					oldbuflen0 = (*L0).buflen;
					(*L0).buflen = (NI)((*L0).buflen * ((NI) 2));
					LOC22 = (void*)0;
					LOC22 = nimrtl_realloc(((void*) ((*L0).buf)), ((NI) ((NI)((*L0).buflen * ((NI) 1)))));
					(*L0).buf = ((NCSTRING) (LOC22));
					LOC23 = (NI)0;
					LOC23 = readdata_Gnwdizh7H5Q9cYivUoGHrVA((*L0).input, ((void*) ((&(*L0).buf[oldbuflen0]))), (NI)(oldbuflen0 * ((NI) 1)));
					charsread0 = (NI)(LOC23 / ((NI) 1));
					{
						if (!(charsread0 < oldbuflen0)) goto LA26;
						(*L0).buf[(NI)(oldbuflen0 + charsread0)] = 0;
						(*L0).sentinel = (NI)(oldbuflen0 + charsread0);
						goto LA11;
					}
					LA26: ;
					s0 = (NI)((*L0).buflen - ((NI) 1));
				}
				LA17: ;
			}
		} LA11: ;
	}
	LA6: ;
}

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

N_NIMCALL(void, open_xmgaYJZNnO6QcDtac9bs3Vg)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0, Streamobj_RShkcoDOPnoCVhtpN1hDPQ* input0, NI buflen0, TY_nmiMWKVIe46vacnhAFrQvw_Set refillchars0) {
	void* LOC1;
	unsureAsgnRef((void**) (&(*L0).input), input0);
	(*L0).bufpos = ((NI) 0);
	(*L0).buflen = buflen0;
	memcpy((void*)(*L0).refillchars, (NIM_CONST void*)refillchars0, 32);
	LOC1 = (void*)0;
	LOC1 = nimrtl_alloc(((NI) ((NI)(buflen0 * ((NI) 1)))));
	(*L0).buf = ((NCSTRING) (LOC1));
	(*L0).sentinel = (NI)(buflen0 - ((NI) 1));
	(*L0).linestart = ((NI) 0);
	(*L0).linenumber = ((NI) 1);
	fillbuffer_QbedUCk2XwI9c7bbRzXBY0g(L0);
	skiputf8bom_BMPUUaJVkFzDJfE3dUzOCw(L0);
}

N_NIMCALL(void, close_QbedUCk2XwI9c7bbRzXBY0g_2)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0) {
	nimrtl_dealloc(((void*) ((*L0).buf)));
	close_4ed7IXme7lqtClFI7s2sTQ((*L0).input);
}

N_NIMCALL(NI, getcolnumber_9cj186ckGjLqyndkoW7bBdg)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0, NI pos0) {
	NI result0;
	result0 = (NI)0;
	result0 = ((NI)(pos0 - (*L0).linestart) > 0? ((NI)(pos0 - (*L0).linestart)) : -((NI)(pos0 - (*L0).linestart)));
	return result0;
}

N_NIMCALL(NI, fillbaselexer_IwUEvMBOLRY9cqyar6XKEaw_2)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0, NI pos0) {
	NI result0;
	result0 = (NI)0;
	{
		if (!(pos0 < (*L0).sentinel)) goto LA3;
		result0 = (NI)(pos0 + ((NI) 1));
	}
	goto LA1;
	LA3: ;
	{
		fillbuffer_QbedUCk2XwI9c7bbRzXBY0g(L0);
		(*L0).bufpos = ((NI) 0);
		result0 = ((NI) 0);
	}
	LA1: ;
	return result0;
}

N_NIMCALL(NI, handlecr_IwUEvMBOLRY9cqyar6XKEaw)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0, NI pos0) {
	NI result0;
	result0 = (NI)0;
	(*L0).linenumber += ((NI) 1);
	result0 = fillbaselexer_IwUEvMBOLRY9cqyar6XKEaw_2(L0, pos0);
	{
		if (!((NU8)((*L0).buf[result0]) == (NU8)(10))) goto LA3;
		result0 = fillbaselexer_IwUEvMBOLRY9cqyar6XKEaw_2(L0, result0);
	}
	LA3: ;
	(*L0).linestart = result0;
	return result0;
}

N_NIMCALL(NI, handlelf_IwUEvMBOLRY9cqyar6XKEaw_3)(Baselexer_B6cGbTPhMsqMovc9cOcIHRw* L0, NI pos0) {
	NI result0;
	result0 = (NI)0;
	(*L0).linenumber += ((NI) 1);
	result0 = fillbaselexer_IwUEvMBOLRY9cqyar6XKEaw_2(L0, pos0);
	(*L0).linestart = result0;
	return result0;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_lexbaseInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_lexbaseDatInit000)(void) {
}

