/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.35 from the
 * contents of Lexer.xs. Do not edit this file, edit Lexer.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "lib\\\\Compiler\\\\Lexer.xs"
#include <lexer.hpp>
#ifdef __cplusplus
extern "C" {
#endif
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"
#undef  dNOOP // Hack to work around "error: declaration of 'Perl___notused' has a different language linkage" error message on clang.
#define dNOOP

#define new_Array() (AV*)sv_2mortal((SV*)newAV())
#define new_Hash() (HV*)sv_2mortal((SV*)newHV())
#define new_String(s, len) sv_2mortal(newSVpv(s, len))
#define new_Int(u) sv_2mortal(newSVuv(u))
#define new_Ref(sv) sv_2mortal(newRV_inc((SV*)sv))
#define set(e) SvREFCNT_inc(e)
#define get_value(hash, key) *hv_fetchs(hash, key, strlen(key))
#ifdef __cplusplus
};
#endif

typedef Lexer * Compiler_Lexer;

#line 37 "lib\\Compiler\\Lexer.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 181 "lib\\Compiler\\Lexer.c"

XS_EUPXS(XS_Compiler__Lexer__new); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Compiler__Lexer__new)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "classname, _options");
    {
	char *	classname = (char *)SvPV_nolen(ST(0))
;
	HV *	_options;
	Compiler_Lexer	RETVAL;

	STMT_START {
		SV* const xsub_tmp_sv = ST(1);
		SvGETMAGIC(xsub_tmp_sv);
		if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVHV){
		    _options = (HV*)SvRV(xsub_tmp_sv);
		}
		else{
		    Perl_croak_nocontext("%s: %s is not a HASH reference",
				"Compiler::Lexer::_new",
				"_options");
		}
	} STMT_END
;
#line 35 "lib\\\\Compiler\\\\Lexer.xs"
{
	const char *filename = SvPVX(get_value(_options, "filename"));
	bool verbose = SvIVX(get_value(_options, "verbose"));
	Lexer *lexer = new Lexer(filename, verbose);
	RETVAL = lexer;
}
#line 215 "lib\\Compiler\\Lexer.c"
	{
	    SV * RETVALSV;
	    RETVALSV = sv_newmortal();
		    sv_setref_pv(RETVALSV, "Compiler::Lexer",
		    (void*)RETVAL);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Compiler__Lexer_DESTROY); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Compiler__Lexer_DESTROY)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    {
	Compiler_Lexer	self;

                if (sv_derived_from(ST(0), "Compiler::Lexer")) {
                        IV tmp = SvIV((SV*)SvRV(ST(0)));
                        self = INT2PTR(Compiler_Lexer, tmp);
                }
                else
                        croak("self is not of type Compiler::Lexer")
;
#line 48 "lib\\\\Compiler\\\\Lexer.xs"
{
	delete self;
}
#line 248 "lib\\Compiler\\Lexer.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Compiler__Lexer_tokenize); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Compiler__Lexer_tokenize)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "self, script");
    {
	Compiler_Lexer	self;
	const char *	script = (const char *)SvPV_nolen(ST(1))
;
	AV *	RETVAL;

                if (sv_derived_from(ST(0), "Compiler::Lexer")) {
                        IV tmp = SvIV((SV*)SvRV(ST(0)));
                        self = INT2PTR(Compiler_Lexer, tmp);
                }
                else
                        croak("self is not of type Compiler::Lexer")
;
#line 57 "lib\\\\Compiler\\\\Lexer.xs"
{
	Tokens *tokens = self->tokenize((char *)script);
	AV* ret  = new_Array();
	size_t size = tokens->size();
	for (size_t i = 0; i < size; i++) {
		Token *token = tokens->at(i);
		HV *hash = (HV*)new_Hash();
		(void)hv_stores(hash, "stype", set(new_Int(token->stype)));
		(void)hv_stores(hash, "type", set(new_Int(token->info.type)));
		(void)hv_stores(hash, "kind", set(new_Int(token->info.kind)));
		(void)hv_stores(hash, "line", set(new_Int(token->finfo.start_line_num)));
		(void)hv_stores(hash, "has_warnings", set(new_Int(token->info.has_warnings)));
		(void)hv_stores(hash, "name", set(new_String(token->info.name, strlen(token->info.name))));
		(void)hv_stores(hash, "data", set(new_String(token->_data, strlen(token->_data))));
		HV *stash = (HV *)gv_stashpv("Compiler::Lexer::Token", sizeof("Compiler::Lexer::Token"));
		av_push(ret, set(sv_bless(new_Ref(hash), stash)));
	}
	self->clearContext();
    RETVAL = ret;
}
#line 294 "lib\\Compiler\\Lexer.c"
	{
	    SV * RETVALSV;
	    RETVALSV = newRV((SV*)RETVAL);
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Compiler__Lexer_get_groups_by_syntax_level); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Compiler__Lexer_get_groups_by_syntax_level)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "self, tokens_, syntax_level");
    {
	Compiler_Lexer	self;
	AV *	tokens_;
	int	syntax_level = (int)SvIV(ST(2))
;
	AV *	RETVAL;

                if (sv_derived_from(ST(0), "Compiler::Lexer")) {
                        IV tmp = SvIV((SV*)SvRV(ST(0)));
                        self = INT2PTR(Compiler_Lexer, tmp);
                }
                else
                        croak("self is not of type Compiler::Lexer")
;

	STMT_START {
		SV* const xsub_tmp_sv = ST(1);
		SvGETMAGIC(xsub_tmp_sv);
		if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVAV){
		    tokens_ = (AV*)SvRV(xsub_tmp_sv);
		}
		else{
		    Perl_croak_nocontext("%s: %s is not an ARRAY reference",
				"Compiler::Lexer::get_groups_by_syntax_level",
				"tokens_");
		}
	} STMT_END
;
#line 86 "lib\\\\Compiler\\\\Lexer.xs"
{
	int tokens_size = av_len(tokens_);
	if (tokens_size < 0) {
		RETVAL = NULL;
		return;
	}
	Tokens tks;
	for (int i = 0; i <= tokens_size; i++) {
		SV *token_ = (SV *)*av_fetch(tokens_, i, FALSE);
		if (sv_isa(token_, "Compiler::Lexer::Token")) {
			token_ = SvRV(token_);
		}
		HV *token = (HV *)token_;
		const char *name = SvPVX(get_value(token, "name"));
		const char *data = SvPVX(get_value(token, "data"));
		int line = SvIVX(get_value(token, "line"));
		int has_warnings = SvIVX(get_value(token, "has_warnings"));
		Enum::Token::Type::Type type = (Enum::Token::Type::Type)SvIVX(get_value(token, "type"));
		Enum::Token::Kind::Kind kind = (Enum::Token::Kind::Kind)SvIVX(get_value(token, "kind"));
		FileInfo finfo;
		finfo.start_line_num = line;
		finfo.end_line_num = line;
		finfo.filename = self->finfo.filename;
		TokenInfo info;
		info.type = type;
		info.kind = kind;
		info.name = name;
		info.data = data;
		info.has_warnings = has_warnings;
		Token *tk = new Token(std::string(data), finfo);
		tk->info = info;
		tk->type = type;
		tk->_data = data;
		tks.push_back(tk);
	}
	self->grouping(&tks);
	self->prepare(&tks);
	//self->dump(&tks);
	Token *root = self->parseSyntax(NULL, &tks);
	//self->dumpSyntax(root, 0);
	self->parseSpecificStmt(root);
	//self->dumpSyntax(root, 0);
	self->setIndent(root, 0);
	size_t block_id = 0;
	self->setBlockIDWithDepthFirst(root, &block_id);
	Tokens *stmts = self->getTokensBySyntaxLevel(root, (Enum::Parser::Syntax::Type)syntax_level);
	AV* ret  = new_Array();
	for (size_t i = 0; i < stmts->size(); i++) {
		Token *stmt = stmts->at(i);
		const char *src = stmt->deparse();
		size_t len = strlen(src);
		HV *hash = (HV*)new_Hash();
		(void)hv_stores(hash, "src", set(new_String(src, len)));
		(void)hv_stores(hash, "token_num", set(new_Int(stmt->total_token_num)));
		(void)hv_stores(hash, "indent", set(new_Int(stmt->finfo.indent)));
		(void)hv_stores(hash, "block_id", set(new_Int(stmt->finfo.block_id)));
		(void)hv_stores(hash, "start_line", set(new_Int(stmt->finfo.start_line_num)));
		(void)hv_stores(hash, "end_line", set(new_Int(stmt->finfo.end_line_num)));
		(void)hv_stores(hash, "has_warnings", set(new_Int(stmt->info.has_warnings)));
		av_push(ret, set(new_Ref(hash)));
	}
	RETVAL = ret;
}
#line 404 "lib\\Compiler\\Lexer.c"
	{
	    SV * RETVALSV;
	    RETVALSV = newRV((SV*)RETVAL);
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Compiler__Lexer_get_used_modules); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Compiler__Lexer_get_used_modules)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "self, script");
    {
	Compiler_Lexer	self;
	const char *	script = (const char *)SvPV_nolen(ST(1))
;
	AV *	RETVAL;

                if (sv_derived_from(ST(0), "Compiler::Lexer")) {
                        IV tmp = SvIV((SV*)SvRV(ST(0)));
                        self = INT2PTR(Compiler_Lexer, tmp);
                }
                else
                        croak("self is not of type Compiler::Lexer")
;
#line 157 "lib\\\\Compiler\\\\Lexer.xs"
{
	Tokens *tokens = self->tokenize((char *)script);
	self->grouping(tokens);
	self->prepare(tokens);
	Token *root = self->parseSyntax(NULL, tokens);
	Modules *modules = self->getUsedModules(root);
	AV* ret = new_Array();
	for (size_t i = 0; i < modules->size(); i++) {
		Module *module = modules->at(i);
		const char *module_name = module->name;
		const char *module_args = module->args;
		size_t module_name_len = strlen(module_name);
		size_t module_args_len = (module_args) ? strlen(module_args) : 0;
		HV *hash = (HV*)new_Hash();
		(void)hv_stores(hash, "name", set(new_String(module_name, module_name_len)));
		(void)hv_stores(hash, "args", set(new_String(module_args, module_args_len)));
		av_push(ret, set(new_Ref(hash)));
	}
	self->clearContext();
	RETVAL = ret;
}
#line 457 "lib\\Compiler\\Lexer.c"
	{
	    SV * RETVALSV;
	    RETVALSV = newRV((SV*)RETVAL);
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Compiler__Lexer_deparse); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Compiler__Lexer_deparse)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "filename, script");
    {
	const char *	filename = (const char *)SvPV_nolen(ST(0))
;
	const char *	script = (const char *)SvPV_nolen(ST(1))
;
	SV *	RETVAL;
#line 186 "lib\\\\Compiler\\\\Lexer.xs"
{
	Lexer lexer(filename, false);
	Tokens *tokens = lexer.tokenize((char *)script);
	lexer.grouping(tokens);
	lexer.prepare(tokens);
	Token *root = lexer.parseSyntax(NULL, tokens);
	const char *src = root->deparse();
	size_t len = strlen(src) + 1;
	size_t token_size = tokens->size();
	RETVAL = newSVpv(src, len);
}
#line 493 "lib\\Compiler\\Lexer.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Compiler__Lexer); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Compiler__Lexer)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if (PERL_REVISION == 5 && PERL_VERSION < 9)
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        newXS_deffile("Compiler::Lexer::_new", XS_Compiler__Lexer__new);
        newXS_deffile("Compiler::Lexer::DESTROY", XS_Compiler__Lexer_DESTROY);
        newXS_deffile("Compiler::Lexer::tokenize", XS_Compiler__Lexer_tokenize);
        newXS_deffile("Compiler::Lexer::get_groups_by_syntax_level", XS_Compiler__Lexer_get_groups_by_syntax_level);
        newXS_deffile("Compiler::Lexer::get_used_modules", XS_Compiler__Lexer_get_used_modules);
        newXS_deffile("Compiler::Lexer::deparse", XS_Compiler__Lexer_deparse);
#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}