#include "kant parser dispatch.h"
#include "kant parser.h"
#include "window layer.h"
#include "environment.h"
#include "menus.h"
#include "resource utilities.h"
#include "text twiddling.h"
/* program globals.h is #included in kant parser dispatch.h */

enum ErrorTypes ParserDispatch(WindowPtr theWindow)
{
	enum ErrorType	parseError;
	TEHandle		hTE;
	short			index;
	OSErr			oe;
	short			oldRefNum, refNum;
	Boolean			alreadyOpen;
	
	if (!gUseDefault)
		if ((oe=OpenTheResFile(&gModuleFS, &oldRefNum, &refNum, &alreadyOpen, TRUE))!=noErr)
			return kModuleNotFound;
	
	gCustomCursor=FALSE;
	gInProgress=TRUE;
	AdjustMenus();
	DrawMenuBar();
	
	index=GetWindowIndex(theWindow);
	InitTheParser(theWindow, 0, 32767);
	parseError=ParseLoop();

	CloseTheResFile(oldRefNum, refNum, alreadyOpen);

	if (IndWindowExistsQQ(index))
	{
		hTE=GetWindowTE(theWindow);
		TESelView(hTE);
		AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
	}
	
	gInProgress=FALSE;
	AdjustMenus();
	DrawMenuBar();
	
	switch (parseError)
	{
		case kNoReferenceName:		return kParserNullReferenceName;
		case kBadReference:			return kParserReferenceNotFound;
		case kBadChoiceFormat:		return kParserBadChoiceFormat;
		case kBadOptionalFormat:	return kParserBadOptionalFormat;
		case kNoInstantiations:		return kParserNoInstantiations;
		case kBadBackslash:			return kParserIllegalBackslash;
		case kUserCancelled:		return userCancelErr;
	}
	
	return allsWell;
}
