Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 136.5K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="none" email=""/>
//     <version>$Revision: 1965 $</version>
// </file>

using System;
using System.Collections.Generic;

namespace ICSharpCode.NRefactory
{
	public class PreprocessingDirective : AbstractSpecial
	{
		public static void VBToCSharp(IList<ISpecial> list)
		{
			for (int i = 0; i < list.Count; ++i) {
				if (list[i] is PreprocessingDirective)
					list[i] = VBToCSharp((PreprocessingDirective)list[i]);
			}
		}
		
		public static PreprocessingDirective VBToCSharp(PreprocessingDirective dir)
		{
			string cmd = dir.Cmd;
			string arg = dir.Arg;
			if (cmd.Equals("#End", StringComparison.InvariantCultureIgnoreCase)) {
				if (arg.ToLowerInvariant().StartsWith("region")) {
					cmd = "#endregion";
					arg = "";
				} else if ("if".Equals(arg, StringComparison.InvariantCultureIgnoreCase)) {
					cmd = "#endif";
					arg = "";
				}
			} else if (cmd.Equals("#Region", StringComparison.InvariantCultureIgnoreCase)) {
				cmd = "#region";
			} else if (cmd.Equals("#If", StringComparison.InvariantCultureIgnoreCase)) {
				if (arg.ToLowerInvariant().EndsWith(" then"))
					arg = arg.Substring(0, arg.Length - 5);
			}
			return new PreprocessingDirective(cmd, arg, dir.StartPosition, dir.EndPosition);
		}
		
		public static void CSharpToVB(List<ISpecial> list)
		{
			for (int i = 0; i < list.Count; ++i) {
				if (list[i] is PreprocessingDirective)
					list[i] = CSharpToVB((PreprocessingDirective)list[i]);
			}
		}
		
		public static PreprocessingDirective CSharpToVB(PreprocessingDirective dir)
		{
			string cmd = dir.Cmd;
			string arg = dir.Arg;
			switch (cmd) {
				case "#region":
					cmd = "#Region";
					if (!arg.StartsWith("\"")) {
						arg = "\"" + arg.Trim() + "\"";
					}
					break;
				case "#endregion":
					cmd = "#End";
					arg = "Region";
					break;
				case "#endif":
					cmd = "#End";
					arg = "If";
					break;
				case "#if":
					arg += " Then";
					break;
			}
			if (cmd.Length > 1) {
				cmd = cmd.Substring(0, 2).ToUpperInvariant() + cmd.Substring(2);
			}
			return new PreprocessingDirective(cmd, arg, dir.StartPosition, dir.EndPosition);
		}
		
		string cmd;
		string arg;
		
		public string Cmd {
			get {
				return cmd;
			}
			set {
				cmd = value;
			}
		}
		
		public string Arg {
			get {
				return arg;
			}
			set {
				arg = value;
			}
		}
		
		public override string ToString()
		{
			return String.Format("[PreProcessingDirective: Cmd = {0}, Arg = {1}]",
			                     Cmd,
			                     Arg);
		}
		
		public PreprocessingDirective(string cmd, string arg, Location start, Location end)
			: base(start, end)
		{
			this.cmd = cmd;
			this.arg = arg;
		}
		
		public override object AcceptVisitor(ISpecialVisitor visitor, object data)
		{
			return visitor.Visit(this, data);
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions