/*
	This file is part of Warzone 2100.
	Copyright (C) 1999-2004  Eidos Interactive
	Copyright (C) 2005-2019  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
%defines
%{

#include "lib/framework/frame.h"
#include "lib/gamelib/parser.h"
#include "lib/sound/audio.h"
#include "lib/framework/lexer_input.h"

extern int audp_lex(void);
extern int audp_lex_destroy(void);
extern int audp_get_lineno(void);
extern char* audp_get_text(void);
extern void audp_set_extra(YY_EXTRA_TYPE user_defined);

void yyerror(const char* fmt);

%}

%name-prefix="audp_"

%union {
	float		fval;
	long		ival;
	bool            bval;
	char*		sval;
}

	/* value tokens */
%token <fval> FLOAT_T
%token <ival> INTEGER
%token <sval> QTEXT /* Text with double quotes surrounding it */

%destructor {
#ifndef WZ_OS_WIN
	// Force type checking by the compiler
	char * const s = $$;

	if (s)
		free(s);
#endif
} QTEXT

	/* keywords */
%token ONESHOT
%token LOOP
%token AUDIO
%token AUDIO_MODULE

	/* rule types */
%type <bval> looping

%%

data_file:				module_file
						;

module_file:			module_file data_list |
						data_list
						;

data_list:				audio_module
						;

audio_header:			AUDIO_MODULE '{'
						;

audio_module:			audio_header audio_list '}' |
						audio_header  '}'
						;

audio_list:				audio_list audio_track |
						audio_track
						;
	/*
	 * unsigned int audio_SetTrackVals(const char* fileName, bool loop, unsigned int volume, unsigned int audibleRadius)
	 */

audio_track:			AUDIO QTEXT looping INTEGER INTEGER
				{
					audio_SetTrackVals($2, $3, $4, $5);
					free($2);
				}
				;

looping:			LOOP
				{ $$ = true; }
				| ONESHOT
				{ $$ = false; }
				;

%%

/* A simple error reporting routine */
void yyerror(const char* msg)
{
	debug(LOG_ERROR, "RES file parse error:\n%s at line %d\nToken: %d, Text: '%s'\n", msg, audp_get_lineno(), audp_char, audp_get_text());
}

/** Read an audio properties file
 */
bool ParseResourceFile(PHYSFS_file* fileHandle)
{
	bool retval;
	lexerinput_t input;
	input.type = LEXINPUT_PHYSFS;
	input.input.physfsfile = fileHandle;

	audp_set_extra(&input);

	retval = (audp_parse() == 0);
	audp_lex_destroy();

	return retval;
}
