Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Coco Custom Tool for Visual Studio.NET

, 29 Oct 2005 CPOL
Use the award winning Coco compiler's compiler directly from within Visual Studio
vscoco.zip
vsCoco
bin
VsCoco.dll
vsCoco.tlb
vsCocoRegistration.exe
Calculator
App.ico
Calc.atg
Calculator.csproj.user
Calculator.suo
VsCoCo
vsCoco.png
Coco
Coco.atg
Parser.frame
Scanner.frame
CustomToolGenerator
vsCoco
VsCoco.csproj.user
vsCocoRegistration
reg.ico
Thumbs.db
vsCocoRegistration.csproj.user
// vsCoco by Pascal GANAYE
// Based on Coco/R (Sep 26, 2005)

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.CodeDom;
using System.CodeDom.Compiler;
using CustomToolGenerator; // vs.net\Common7\IDE\Microsoft.VSDesigner.dll
using System.Windows.Forms;
using System.Resources;
using System.IO;
using System.Collections;
using EnvDTE;
using System.Text.RegularExpressions;
using at.jku.ssw.Coco;


namespace VsCoco
{
	/// <summary>
	/// Summary description for ResourceKeysGenerator
	/// </summary>
	/// 
	[Guid("D8231E80-4710-11DA-8CD6-0800200C9A66")]
	public class vsCoco : BaseCodeGeneratorWithSite
	{
		static public OutputWindowPane OWp;

		override protected byte[] GenerateCode(string inputFileName, string inputFileContent)
		{
			object serviceObject = GetService(typeof(EnvDTE.ProjectItem));
			EnvDTE.Project containingProject;
			if (serviceObject != null) 
			{
				containingProject = ((EnvDTE.ProjectItem) serviceObject).ContainingProject;
				DTE dte = containingProject.DTE;
				Window win  = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
				OutputWindow OW   =(OutputWindow) win.Object;
				OWp=null;
				for (int i=1;i<=OW.OutputWindowPanes.Count;i++)
				{
					if (OW.OutputWindowPanes.Item(i).Name=="VsCoco")
					{
						OWp=OW.OutputWindowPanes.Item(i);
						OWp.Clear();
					}
				}
				if (OWp==null)
				{
					OWp = OW.OutputWindowPanes.Add("VsCoco");
				}
			}
			vsCoco.srcFile=inputFileName;
			
			VsCoco.vsCoco.WriteLine("------ vsCoco/R (Oct 27, 2005)");
			try
			{
				FileInfo fi = new FileInfo(inputFileName);
				if (!fi.Exists) throw new System.IO.FileNotFoundException("Source file not found.",inputFileName);
				MemoryStream memIn = new MemoryStream();
				byte[] b=System.Text.Encoding.ASCII.GetBytes(inputFileContent);
				memIn.Write(b,0,b.Length);
				memIn.Seek(0,0);
				Scanner scanner = new vsCocoScanner(memIn);
			
				MemoryStream memOut = new MemoryStream();
				StreamWriter gen = new StreamWriter(memOut);
				DateTime now=DateTime.Now;
				gen.WriteLine("// Compiled by vsCoco on {0} {1}",now.ToShortDateString(),now.ToLongTimeString());
				Parser parser = new Parser(scanner);
				parser.tab = new Tab(parser);
				parser.dfa = new DFA(parser);
				parser.dfa.gen = gen;
				parser.pgen = new ParserGen(parser);
				parser.pgen.gen = gen;

				parser.pgen.srcName = fi.FullName;
				parser.tab.srcName = fi.FullName;
				scanner.srcFile = fi.FullName;
				parser.tab.srcDir = fi.Directory.FullName;
				parser.tab.nsName = this.FileNameSpace;
				parser.tab.frameDir = inputFileName;

				parser.Parse();

				vsCoco.WriteLine(String.Format("{0} errors detected", parser.errors.count));

				gen.Close();			
				return memOut.ToArray();
			}
			catch(Exception ex)
			{
				vsCoco.WriteError(0,0,vsCoco.ErrorLevel.error,"Fatal error :" + ex.Message);
				return System.Text.ASCIIEncoding.ASCII.GetBytes("// Fatal error : " + ex.Message);
			}
		}

		public static string srcFile;

		public static void WriteLine(string s)
		{
			OWp.OutputString(s);
			OWp.OutputString("\r\n");
		}

		public enum ErrorLevel
		{
			info,
			warning,
			error
		}

		public static void WriteError(int lin, int col, ErrorLevel errorLevel , 
			string errorMessage)
		{
			OWp.OutputString(string.Format("{0}({1},{2}): {3} {4}\r\n",
				new object[] {srcFile,lin,col,
					(errorLevel==vsCoco.ErrorLevel.info?"":
					(errorLevel==vsCoco.ErrorLevel.warning?"Warning:":"Error"))
				,errorMessage}));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pascal Ganaye
Software Developer (Senior)
United Kingdom United Kingdom
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 29 Oct 2005
Article Copyright 2005 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid