#region Copyright and License
//
// Fizzler - CSS Selector Engine for Microsoft .NET Framework
// Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#endregion
namespace Fizzler
{
using System;
///
/// Represent a token and optionally any text associated with it.
///
public struct Token : IEquatable
{
///
/// Gets the kind/type/class of the token.
///
public TokenKind Kind { get; private set; }
///
/// Gets text, if any, associated with the token.
///
public string Text { get; private set; }
private Token(TokenKind kind) : this(kind, null) {}
private Token(TokenKind kind, string text) : this()
{
Kind = kind;
Text = text;
}
///
/// Creates an end-of-input token.
///
public static Token Eoi()
{
return new Token(TokenKind.Eoi);
}
private static readonly Token _star = Char('*');
private static readonly Token _dot = Char('.');
private static readonly Token _colon = Char(':');
private static readonly Token _comma = Char(',');
private static readonly Token _rightParenthesis = Char(')');
private static readonly Token _equals = Char('=');
private static readonly Token _pipe = Char('|');
private static readonly Token _leftBracket = Char('[');
private static readonly Token _rightBracket = Char(']');
///
/// Creates a star token.
///
public static Token Star()
{
return _star;
}
///
/// Creates a dot token.
///
public static Token Dot()
{
return _dot;
}
///
/// Creates a colon token.
///
public static Token Colon()
{
return _colon;
}
///
/// Creates a comma token.
///
public static Token Comma()
{
return _comma;
}
///
/// Creates a right parenthesis token.
///
public static Token RightParenthesis()
{
return _rightParenthesis;
}
///
/// Creates an equals token.
///
public static Token Equals()
{
return _equals;
}
///
/// Creates a left bracket token.
///
public static Token LeftBracket()
{
return _leftBracket;
}
///
/// Creates a right bracket token.
///
public static Token RightBracket()
{
return _rightBracket;
}
///
/// Creates a pipe (vertical line) token.
///
public static Token Pipe()
{
return _pipe;
}
///
/// Creates a plus token.
///
public static Token Plus()
{
return new Token(TokenKind.Plus);
}
///
/// Creates a greater token.
///
public static Token Greater()
{
return new Token(TokenKind.Greater);
}
///
/// Creates an includes token.
///
public static Token Includes()
{
return new Token(TokenKind.Includes);
}
///
/// Creates a dash-match token.
///
public static Token DashMatch()
{
return new Token(TokenKind.DashMatch);
}
///
/// Creates a prefix-match token.
///
public static Token PrefixMatch()
{
return new Token(TokenKind.PrefixMatch);
}
///
/// Creates a suffix-match token.
///
public static Token SuffixMatch()
{
return new Token(TokenKind.SuffixMatch);
}
///
/// Creates a substring-match token.
///
public static Token SubstringMatch()
{
return new Token(TokenKind.SubstringMatch);
}
///
/// Creates a general sibling token.
///
public static Token Tilde()
{
return new Token(TokenKind.Tilde);
}
///
/// Creates an identifier token.
///
public static Token Ident(string text)
{
ValidateTextArgument(text);
return new Token(TokenKind.Ident, text);
}
///
/// Creates an integer token.
///
public static Token Integer(string text)
{
ValidateTextArgument(text);
return new Token(TokenKind.Integer, text);
}
///
/// Creates a hash-name token.
///
public static Token Hash(string text)
{
ValidateTextArgument(text);
return new Token(TokenKind.Hash, text);
}
///
/// Creates a white-space token.
///
public static Token WhiteSpace(string space)
{
ValidateTextArgument(space);
return new Token(TokenKind.WhiteSpace, space);
}
///
/// Creates a string token.
///
public static Token String(string text)
{
return new Token(TokenKind.String, text ?? string.Empty);
}
///
/// Creates a function token.
///
public static Token Function(string text)
{
ValidateTextArgument(text);
return new Token(TokenKind.Function, text);
}
///
/// Creates an arbitrary character token.
///
public static Token Char(char ch)
{
return new Token(TokenKind.Char, ch.ToString());
}
///
/// Indicates whether this instance and a specified object are equal.
///
public override bool Equals(object obj)
{
return obj != null && obj is Token && Equals((Token) obj);
}
///
/// Returns the hash code for this instance.
///
public override int GetHashCode()
{
return Text == null ? Kind.GetHashCode() : Kind.GetHashCode() ^ Text.GetHashCode();
}
///
/// Indicates whether the current object is equal to another object of the same type.
///
public bool Equals(Token other)
{
return Kind == other.Kind && Text == other.Text;
}
///
/// Gets a string representation of the token.
///
public override string ToString()
{
return Text == null ? Kind.ToString() : Kind + ": " + Text;
}
///
/// Performs a logical comparison of the two tokens to determine
/// whether they are equal.
///
public static bool operator==(Token a, Token b)
{
return a.Equals(b);
}
///
/// Performs a logical comparison of the two tokens to determine
/// whether they are inequal.
///
public static bool operator !=(Token a, Token b)
{
return !a.Equals(b);
}
private static void ValidateTextArgument(string text)
{
if (text == null) throw new ArgumentNullException("text");
if (text.Length == 0) throw new ArgumentException(null, "text");
}
}
}