Click here to Skip to main content
15,886,799 members
Articles / Containers / Virtual Machine

Conscript: An embeddable, compiled scripting language for .NET

Rate me:
Please Sign up or sign in to vote.
4.97/5 (58 votes)
5 Sep 2008CPOL15 min read 158.6K   1.4K   141  
An API for enhancing any .NET application with a scripting language
using System;
using System.Collections.Generic;
using System.Text;

namespace Conscript.Compiler
{
    /// <summary>
    /// Represents a lexing token generated by the script lexer and used
    /// by the parser.
    /// </summary>
    public class Token
    {
        #region Private Variables

        private TokenType m_tokenType;
        private object m_objectLexeme;
        private int m_iSourceLine;
        private int m_iSourceChar;
        private String m_strSourceLine;

        #endregion

        #region Public Methods

        /// <summary>
        /// Creats a token with the given <see cref="TokenType"/>, lexeme,
        /// source line and character position and source text for the given
        /// line.
        /// </summary>
        /// <param name="tokenType">The token's <see cref="TokenType"/>.
        /// </param>
        /// <param name="objectLexeme">The lexeme associated with the token.
        /// </param>
        /// <param name="iSourceLine">The source line number where the token
        /// occurs.</param>
        /// <param name="iSourceChar">The source character number where the
        /// token occurs.</param>
        /// <param name="strSourceLine">The source text line where the token
        /// occurs.</param>
        public Token(TokenType tokenType, object objectLexeme,
            int iSourceLine, int iSourceChar, String strSourceLine)
        {
            m_tokenType = tokenType;
            m_objectLexeme = objectLexeme;
            m_iSourceLine = iSourceLine;
            m_iSourceChar = Math.Max(0, iSourceChar - objectLexeme.ToString().Length - 1);
            m_strSourceLine = strSourceLine;
        }

        /// <summary>
        /// Returns a string representation of the token.
        /// </summary>
        /// <returns>String representation of the token.</returns>
        public override String ToString()
        {
            return m_tokenType + " (\"" + m_objectLexeme.ToString() + "\")";
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The token's <see cref="TokenType"/>.
        /// </summary>
        public TokenType Type
        {
            get { return m_tokenType; }
        }

        /// <summary>
        /// The lexeme associated with the token.
        /// </summary>
        public object Lexeme
        {
            get { return m_objectLexeme; }
        }

        /// <summary>
        /// The source line number where the token occurs.
        /// </summary>
        public int SourceLine
        {
            get { return m_iSourceLine; }
        }

        /// <summary>
        /// The source character position where the token occurs.
        /// </summary>
        public int SourceCharacter
        {
            get { return m_iSourceChar; }
        }

        /// <summary>
        /// The source text line where the token occurs.
        /// </summary>
        public String SourceText
        {
            get { return m_strSourceLine; }
        }

        #endregion
    }
}

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 (Senior)
Malta Malta
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions