/*
 * NAME
 *	yacc - how to use yacc
 *
 * DESCRIPTION
 *	This cookbook describes how to use yacc.
 *
 *	You will have to add "-d" to the [yacc_flags] variable
 *	if you want %.h files generated.
 *
 *	If a y.output file is constructed, it will be moved to %.list
 *
 * RECIPES
 *	%.c %.h: %.y	applied if -d in [yacc_flags]
 *	%.c: %.y	applied if -d not in [yacc_flags]
 *
 * VARIABLES
 *	yacc_src	Yacc source files in the current directory.
 *	dot_src		Source files constructable in the current directory
 *			(unioned with existing setting, if necessary).
 *	dot_obj		Object files constructable in the current directory
 *			(unioned with existing setting, if necessary).
 *	dot_clean	Files which may be removed from the current directory
 *			in a clean target.
 *	dot_lint_obj	Lint object files constructable in the current directory
 *			(unioned with existing setting, if necessary).
 *
 * MANIFEST: cookbook for using yacc
 */

#pragma once

#include "c"

if [not [defined yacc]] then
	yacc = yacc;
if [not [defined yacc_flags]] then
	yacc_flags = ;
yacc_src = [glob *.y];
cc_src = [stringset [cc_src] - [fromto %.y %.c [yacc_src]]];
dot_src =
	[stringset
		[dot_src] [yacc_src]
	-
		[fromto %.y %.c [yacc_src]] [fromto %.y %.s [yacc_src]]
	];
dot_obj = [stringset [dot_obj] [fromto %.y %.o [yacc_src]]];
dot_clean =
	[stringset
		[dot_clean]
		[fromto %.y %.o [yacc_src]]
		[fromto %.y %.c [yacc_src]]
		[fromto %.y %.list [yacc_src]]
		[fromto %.y %.ln [yacc_src]]
		[fromto %.y %.s [yacc_src]]
		y.tab.c y.tab.h y.output
	];
dot_lint_obj = [stringset [dot_lint_obj] [fromto %.y %.ln [yacc_src]]];

%.c %.h: %.y
	if [in -d [yacc_flags]]
	single-thread y.tab.c y.tab.h y.output
{
	if [exists %.list] then
		rm -f %.list
			set clearstat;
	if [exists y.output] then
		rm -f y.output
			set clearstat;
	[yacc] [yacc_flags] %.y;
	mv y.tab.c %.c;
	mv y.tab.h %.h;
	if [exists y.output] then
		mv y.output %.list
			set clearstat;
}

%.c: %.y
	if [not [in -d [yacc_flags]]]
	single-thread y.tab.c y.output
{
	if [exists %.list] then
		rm -f %.list
			set clearstat;
	if [exists y.output] then
		rm -f y.output
			set clearstat;
	[yacc] [yacc_flags] %.y;
	mv y.tab.c %.c;
	if [exists y.output] then
		mv y.output %.list
			set clearstat;
}
