<commit_before>#include <ofc/parse.h>



struct entry {
    char *keyword;
    int key_val;
};

static const struct entry io_stmt__keyword[] =
{
		{ "OPEN ",      OFC_PARSE_STMT_IO_OPEN      },
		{ "INQUIRE ",   OFC_PARSE_STMT_IO_INQUIRE   },
		{ "REWIND ",    OFC_PARSE_STMT_IO_REWIND    },
		{ "BACKSPACE ", OFC_PARSE_STMT_IO_BACKSPACE },
		{ "READ ",      OFC_PARSE_STMT_IO_READ      },
		{ "WRITE ",     OFC_PARSE_STMT_IO_WRITE     },
		{ "ENDFILE ",   OFC_PARSE_STMT_IO_END_FILE  },
		{ "CLOSE ",     OFC_PARSE_STMT_IO_CLOSE     },
		{ 0, 0 }
};

static bool ofc_parse_io__print_keyword(
ofc_colstr_t* cs, int key_val)
{
	int i;
	for(i = 0; io_stmt__keyword[i].keyword != 0; i++)
	{
		if (io_stmt__keyword[i].key_val == key_val)
		{
			return ofc_colstr_atomic_writef(cs, "%s",
        io_stmt__keyword[i].keyword);
		}
	}
	return false;
}

static unsigned ofc_parse_stmt__io(
	const ofc_sparse_t* src, const char* ptr,
	ofc_parse_debug_t* debug,
	ofc_parse_keyword_e keyword, bool iolist,
	bool force_brackets,
	ofc_parse_stmt_t* stmt)
{
	unsigned dpos = ofc_parse_debug_position(debug);

	unsigned i = ofc_parse_keyword(
		src, ptr, debug, keyword);
	if (i == 0) return 0;

	stmt->io.params = NULL;
	if (ptr[i] == '(')
	{
		i += 1;

		unsigned len;
		stmt->io.params = ofc_parse_call_arg_list_named(
			src, &ptr[i], debug, &len);
		if (stmt->io.params) i += len;

		if (ptr[i++] != ')')
		{
			ofc_parse_call_arg_list_delete(
				stmt->io.params);
			ofc_parse_debug_rewind(debug, dpos);
			return 0;
		}
	}
	else if (force_brackets)
	{
		ofc_parse_debug_rewind(debug, dpos);
		return 0;
	}
	else
	{
		unsigned len;
		ofc_parse_call_arg_t* unit
			= ofc_parse_call_arg(src, &ptr[i], debug, &len);
		if (!unit)
		{
			ofc_parse_debug_rewind(debug, dpos);
			return 0;
		}
		i += len;

		if (ptr[i] == ',')
			i += 1;

		stmt->io.params = ofc_parse_call_arg_list_wrap(unit);
		if (!stmt->io.params)
		{
			ofc_parse_call_arg_delete(unit);
			ofc_parse_debug_rewind(debug, dpos);
			return 0;
		}
	}

	stmt->io.iolist = NULL;
	if (iolist)
	{
		unsigned len;
		stmt->io.iolist = ofc_parse_iolist(
			src, &ptr[i], debug, &len);
		if (stmt->io.iolist) i += len;
	}

	return i;
}

bool ofc_parse_stmt_io_print(
	ofc_colstr_t* cs, const ofc_parse_stmt_t* stmt)
{
	if (!stmt)
		return false;

	if (!ofc_parse_io__print_keyword(cs, stmt->type))
		return false;

	if (!ofc_colstr_atomic_writef(cs, "(")
		|| !ofc_parse_call_arg_list_print(cs, stmt->io.params)
		|| !ofc_colstr_atomic_writef(cs, ")")
		|| !ofc_colstr_atomic_writef(cs, " "))
		return false;

	if (stmt->io.iolist)
		ofc_parse_iolist_print(cs, stmt->io.iolist);
	return true;
}

bool ofc_parse_stmt_print_accept_print(
	ofc_colstr_t* cs, const ofc_parse_stmt_t* stmt)
{
	if (!stmt)
		return false;

	const char* kwstr;
	switch (stmt->type)
	{
		case OFC_PARSE_STMT_IO_PRINT:
			kwstr = "PRINT";
			break;
		case OFC_PARSE_STMT_IO_TYPE:
			kwstr = "TYPE";
			break;
		case OFC_PARSE_STMT_IO_ACCEPT:
			kwstr = "ACCEPT";
			break;
		default:
			return false;
	}

	if (!ofc_colstr_atomic_writef(cs, "%s ", kwstr))
		return false;

	if (!(stmt->io_print.format_asterisk
		? ofc_colstr_atomic_writef(cs, "*")
		: ofc_parse_label_print(cs, stmt->io_print.format)))
		return false;

	if (stmt->io_print.iolist)
	{
		if (!ofc_colstr_atomic_writef(cs, ", ")
			|| !ofc_parse_iolist_print(
				cs, stmt->io_print.iolist))
			return false;
	}

	return true;
}

bool ofc_parse_stmt_define_file_print(
	ofc_colstr_t* cs, const ofc_parse_stmt_t* stmt)
{
	if (!stmt)
		return false;

	if (!ofc_colstr_atomic_writef(cs, "DEFINE FILE ")
		&& !ofc_parse_define_file_arg_list_print(
			cs, stmt->io_define_file.args))
		return false;
}

unsigned ofc_parse_stmt_io_open(
	const ofc_sparse_t* src, const char* ptr,
	ofc_parse_debug_t* debug,
	ofc_parse_stmt_t* stmt)
{
	unsigned i = ofc_parse_stmt__io(
		src, ptr, debug,
		OFC_PARSE_KEYWORD_OPEN, false, true,
		stmt);
	if (i == 0) return 0;

	stmt->type = OFC_PARSE_STMT_IO_OPEN;
	return i;
}

unsigned ofc_parse_stmt_io_inquire(
	const ofc_sparse_t* src, const char* ptr,
	ofc_parse_debug_t* debug,
	ofc_parse_stmt_t* stmt)<commit_msg>Fix minor bug in DEFINE FILE printing<commit_after>#include <ofc/parse.h>



struct entry {
    char *keyword;
    int key_val;
};

static const struct entry io_stmt__keyword[] =
{
		{ "OPEN ",      OFC_PARSE_STMT_IO_OPEN      },
		{ "INQUIRE ",   OFC_PARSE_STMT_IO_INQUIRE   },
		{ "REWIND ",    OFC_PARSE_STMT_IO_REWIND    },
		{ "BACKSPACE ", OFC_PARSE_STMT_IO_BACKSPACE },
		{ "READ ",      OFC_PARSE_STMT_IO_READ      },
		{ "WRITE ",     OFC_PARSE_STMT_IO_WRITE     },
		{ "ENDFILE ",   OFC_PARSE_STMT_IO_END_FILE  },
		{ "CLOSE ",     OFC_PARSE_STMT_IO_CLOSE     },
		{ 0, 0 }
};

static bool ofc_parse_io__print_keyword(
ofc_colstr_t* cs, int key_val)
{
	int i;
	for(i = 0; io_stmt__keyword[i].keyword != 0; i++)
	{
		if (io_stmt__keyword[i].key_val == key_val)
		{
			return ofc_colstr_atomic_writef(cs, "%s",
        io_stmt__keyword[i].keyword);
		}
	}
	return false;
}

static unsigned ofc_parse_stmt__io(
	const ofc_sparse_t* src, const char* ptr,
	ofc_parse_debug_t* debug,
	ofc_parse_keyword_e keyword, bool iolist,
	bool force_brackets,
	ofc_parse_stmt_t* stmt)
{
	unsigned dpos = ofc_parse_debug_position(debug);

	unsigned i = ofc_parse_keyword(
		src, ptr, debug, keyword);
	if (i == 0) return 0;

	stmt->io.params = NULL;
	if (ptr[i] == '(')
	{
		i += 1;

		unsigned len;
		stmt->io.params = ofc_parse_call_arg_list_named(
			src, &ptr[i], debug, &len);
		if (stmt->io.params) i += len;

		if (ptr[i++] != ')')
		{
			ofc_parse_call_arg_list_delete(
				stmt->io.params);
			ofc_parse_debug_rewind(debug, dpos);
			return 0;
		}
	}
	else if (force_brackets)
	{
		ofc_parse_debug_rewind(debug, dpos);
		return 0;
	}
	else
	{
		unsigned len;
		ofc_parse_call_arg_t* unit
			= ofc_parse_call_arg(src, &ptr[i], debug, &len);
		if (!unit)
		{
			ofc_parse_debug_rewind(debug, dpos);
			return 0;
		}
		i += len;

		if (ptr[i] == ',')
			i += 1;

		stmt->io.params = ofc_parse_call_arg_list_wrap(unit);
		if (!stmt->io.params)
		{
			ofc_parse_call_arg_delete(unit);
			ofc_parse_debug_rewind(debug, dpos);
			return 0;
		}
	}

	stmt->io.iolist = NULL;
	if (iolist)
	{
		unsigned len;
		stmt->io.iolist = ofc_parse_iolist(
			src, &ptr[i], debug, &len);
		if (stmt->io.iolist) i += len;
	}

	return i;
}

bool ofc_parse_stmt_io_print(
	ofc_colstr_t* cs, const ofc_parse_stmt_t* stmt)
{
	if (!stmt)
		return false;

	if (!ofc_parse_io__print_keyword(cs, stmt->type))
		return false;

	if (!ofc_colstr_atomic_writef(cs, "(")
		|| !ofc_parse_call_arg_list_print(cs, stmt->io.params)
		|| !ofc_colstr_atomic_writef(cs, ")")
		|| !ofc_colstr_atomic_writef(cs, " "))
		return false;

	if (stmt->io.iolist)
		ofc_parse_iolist_print(cs, stmt->io.iolist);
	return true;
}

bool ofc_parse_stmt_print_accept_print(
	ofc_colstr_t* cs, const ofc_parse_stmt_t* stmt)
{
	if (!stmt)
		return false;

	const char* kwstr;
	switch (stmt->type)
	{
		case OFC_PARSE_STMT_IO_PRINT:
			kwstr = "PRINT";
			break;
		case OFC_PARSE_STMT_IO_TYPE:
			kwstr = "TYPE";
			break;
		case OFC_PARSE_STMT_IO_ACCEPT:
			kwstr = "ACCEPT";
			break;
		default:
			return false;
	}

	if (!ofc_colstr_atomic_writef(cs, "%s ", kwstr))
		return false;

	if (!(stmt->io_print.format_asterisk
		? ofc_colstr_atomic_writef(cs, "*")
		: ofc_parse_label_print(cs, stmt->io_print.format)))
		return false;

	if (stmt->io_print.iolist)
	{
		if (!ofc_colstr_atomic_writef(cs, ", ")
			|| !ofc_parse_iolist_print(
				cs, stmt->io_print.iolist))
			return false;
	}

	return true;
}

bool ofc_parse_stmt_define_file_print(
	ofc_colstr_t* cs, const ofc_parse_stmt_t* stmt)
{
	if (!stmt)
		return false;

	return (ofc_colstr_atomic_writef(cs, "DEFINE FILE ")
		&& ofc_parse_define_file_arg_list_print(
			cs, stmt->io_define_file.args));
}

unsigned ofc_parse_stmt_io_open(
	const ofc_sparse_t* src, const char* ptr,
	ofc_parse_debug_t* debug,
	ofc_parse_stmt_t* stmt)
{
	unsigned i = ofc_parse_stmt__io(
		src, ptr, debug,
		OFC_PARSE_KEYWORD_OPEN, false, true,
		stmt);
	if (i == 0) return 0;

	stmt->type = OFC_PARSE_STMT_IO_OPEN;
	return i;
}

unsigned ofc_parse_stmt_io_inquire(
	const ofc_sparse_t* src, const char* ptr,
	ofc_parse_debug_t* debug,
	ofc_parse_stmt_t* stmt)
{'fraction_non_alphanumeric': 0.13323660984161528, 'fraction_numerical': 0.0033853222101317857, 'mean_word_length': 4.040828762949421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2442915', 'n_tokens_mistral': 3975, 'n_tokens_neox': 3652, 'n_words': 643}