extern "C" __declspec(dllexport) wchar_t * __stdcall process(const wchar_t* inputFileName, const wchar_t * inputFileContent, int * hasErrors)
{
	hostLang = &hostLangCSharp;
	wstringstream out;
	wstringstream errors;

	pErrorStream = &errors;
	gblErrorCount = 0;

	InputData id;

	id.inputFileName = inputFileName;
	id.outputFileName = inputFileName;
	id.inStream = new wstringstream(inputFileContent);
	id.outStream = &out;

	InputItem *firstInputItem = new InputItem;
	firstInputItem->type = InputItem::HostData;
	firstInputItem->loc.fileName = id.inputFileName;
	firstInputItem->loc.line = 1;
	firstInputItem->loc.col = 1;
	id.inputItems.append(firstInputItem);

	Scanner scanner(id, id.inputFileName, *id.inStream, 0, 0, 0, false);
	scanner.do_scan();

	id.terminateAllParsers();
	id.prepareMachineGen();
	id.generateReduced();
	id.verifyWritesHaveData();
	id.writeOutput();

	wchar_t * result = NULL;

	if (gblErrorCount > 0)
	{
		*hasErrors = true;
		int length = (errors.str().length() + 1) * sizeof(wchar_t);
		result = (wchar_t*)::CoTaskMemAlloc(length);
		wcscpy_s(result, errors.str().length() + 1, errors.str().c_str());
	}
	else
	{
		*hasErrors = false;
		int length = (out.str().length() + 1) * sizeof(wchar_t);
		result = (wchar_t*)::CoTaskMemAlloc(length);
		wcscpy_s(result, out.str().length() + 1, out.str().c_str());
	}

	return result;
}
Exemple #2
0
void process( InputData &id )
{
	/* Open the input file for reading. */
	assert( id.inputFileName != 0 );
	ifstream *inFile = new ifstream( id.inputFileName );
	if ( ! inFile->is_open() )
		error() << "could not open " << id.inputFileName << " for reading" << endp;

	/* Used for just a few things. */
	std::ostringstream hostData;

	/* Make the first input item. */
	InputItem *firstInputItem = new InputItem;
	firstInputItem->type = InputItem::HostData;
	firstInputItem->loc.fileName = id.inputFileName;
	firstInputItem->loc.line = 1;
	firstInputItem->loc.col = 1;
	id.inputItems.append( firstInputItem );

	Scanner scanner( id, id.inputFileName, *inFile, 0, 0, 0, false );
	scanner.do_scan();

	/* Finished, final check for errors.. */
	if ( gblErrorCount > 0 )
		exit(1);

	/* Now send EOF to all parsers. */
	id.terminateAllParsers();

	/* Bail on above error. */
	if ( gblErrorCount > 0 )
		exit(1);

	/* Locate the backend program */
	/* Compiles machines. */
	id.prepareMachineGen();

	if ( gblErrorCount > 0 )
		exit(1);

	id.makeOutputStream();

	/* Generates the reduced machine, which we use to write output. */
	if ( !generateXML ) {
		id.generateReduced();

		if ( gblErrorCount > 0 )
			exit(1);
	}

	id.verifyWritesHaveData();
	if ( gblErrorCount > 0 )
		exit(1);

	/*
	 * From this point on we should not be reporting any errors.
	 */

	id.openOutput();
	id.writeOutput();

	/* Close the input and the intermediate file. */
	delete inFile;

	/* If writing to a file, delete the ostream, causing it to flush.
	 * Standard out is flushed automatically. */
	if ( id.outputFileName != 0 ) {
		delete id.outStream;
		delete id.outFilter;
	}

	assert( gblErrorCount == 0 );
}