Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

An Expression Parser for CodeDom

Rate me:
Please Sign up or sign in to vote.
4.78/5 (51 votes)
22 Apr 2009CPOL4 min read 140.6K   1.1K   74  
Tired of writing long, complicated CodeDom expressions? This parser's for you!
using System;
 
namespace CodeDomExpParser
{
	/// <summary>
	/// Represents a token that is parsed out by the <see cref="Tokenizer"/>.
	/// </summary>
	public sealed class Token
	{
		private string _Text;
		private object _ParsedObject;
		private TokenType _Type;
		private TokenPriority _Priority;

		/// <summary>
		/// The text that makes up the token.
		/// </summary>
		public string Text 
		{
			get { return _Text; }
		}

		/// <summary>
		/// If the token can be parsed into a type like an integer, this property holds that value.
		/// </summary>
		public object ParsedObject 
		{
			get { return _ParsedObject; }
		}

		/// <summary>
		/// Token type
		/// </summary>
		public TokenType Type 
		{
			get { return _Type; }
		}

		/// <summary>
		/// Token priority
		/// </summary>
		public TokenPriority Priority 
		{
			get { return _Priority; }
		}

		/// <summary>
		/// Constructor for tokens that are not parsed.
		/// </summary>
		/// <param name="text"></param>
		/// <param name="type"></param>
		/// <param name="priority"></param>
		public Token(string text, TokenType type, TokenPriority priority) 
		{
			_Text = text;
			_Type = type;
			_Priority = priority;
			_ParsedObject = text;
		}

		/// <summary>
		/// Constructor for tokens that are parsed.
		/// </summary>
		/// <param name="parsedObj"></param>
		/// <param name="type"></param>
		/// <param name="priority"></param>
		public Token(object parsedObj, TokenType type, TokenPriority priority)
		{
			_ParsedObject = parsedObj;
			_Text = ParsedObject.ToString();
			_Type = type;
			_Priority = priority;
		}

		/// <summary>
		/// The null token represents a state where the <see cref="Tokenizer"/> encountered an error
		/// or has not begun parsing yet.
		/// </summary>
		public static Token NullToken = new Token("", TokenType.NotAToken, TokenPriority.None);
	}
}

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)


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

Comments and Discussions