Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Spart, a parser generator framework 100% C#

Rate me:
Please Sign up or sign in to vote.
4.76/5 (71 votes)
22 Dec 20038 min read 352K   5.2K   129  
Spart is the C# sister of Spirit. It lets you quickly create code parsers directly in your application.
/// Spart License (zlib/png)
/// 
/// 
/// Copyright (c) 2003 Jonathan de Halleux
/// 
/// This software is provided 'as-is', without any express or implied warranty. 
/// In no event will the authors be held liable for any damages arising from 
/// the use of this software.
/// 
/// Permission is granted to anyone to use this software for any purpose, 
/// including commercial applications, and to alter it and redistribute it 
/// freely, subject to the following restrictions:
/// 
/// 1. The origin of this software must not be misrepresented; you must not 
/// claim that you wrote the original software. If you use this software in a 
/// product, an acknowledgment in the product documentation would be 
/// appreciated but is not required.
/// 
/// 2. Altered source versions must be plainly marked as such, and must not be 
/// misrepresented as being the original software.
/// 
/// 3. This notice may not be removed or altered from any source distribution.
/// 
/// Author: Jonathan de Halleux
namespace Spart.Scanners
{
    using System;
	using System.IO;
	using Spart.Parsers;

	/// <summary>
	/// Scanner acting on a string.
	/// <seealso cref="IScanner"/>
	/// </summary>
    public class StringScanner : IScanner
    {
        private String m_InputString;
        private long m_Offset;
        private IFilter m_Filter;

		/// <summary>
		/// Creates a scanner on the string.
		/// </summary>
		/// <param name="inputString">Input string</param>
		/// <exception cref="ArgumentNullException">input string is null</exception>
		public StringScanner(String inputString)
        {
            if (inputString == null)
                throw new ArgumentNullException("inputString is null");
            m_InputString = inputString;
            Offset = 0;
            Filter = null;
        }               

		/// <summary>
		/// Creates a scanner on the string at a specified offset
		/// </summary>
		/// <param name="inputString">Input string</param>
		/// <exception cref="ArgumentNullException">input string is null</exception>
		/// <exception cref="ArgumentException">offset if out of range</exception>
		public StringScanner(String inputString, long offset)
        {
            if (inputString == null)
                throw new ArgumentNullException("inputString is null");
            if (offset >= inputString.Length)
                throw new ArgumentException("offset out of bounds");
            m_InputString = inputString;
            Offset = offset;
            Filter = null;
        }               
 
		/// <summary>
		/// the input string
		/// </summary>
        public String InputString  
        {
            get
            {
                return m_InputString;
            }
        }
 
		/// <summary>
		/// Current offset
		/// </summary>
        public long Offset  
        {
            get
            {
                return m_Offset;
            }
            set
            {
                if (value < 0 || value > InputString.Length)
                    throw new ArgumentOutOfRangeException("offset out of bounds");
                m_Offset = value;
            }
        }

		/// <summary>
		/// true if at the end of the string
		/// </summary>
        public bool AtEnd   
        {
            get
            { 
                return m_Offset == InputString.Length;
            }
        }

		/// <summary>
		/// Advance the cursor once
		/// </summary>
		/// <returns>true if not at end</returns>
		/// <exception cref="Exception">If called while AtEnd is true</exception>
        public bool Read()
        {
            if (AtEnd)
                throw new Exception("Scanner already at end");
            ++m_Offset;

            return !AtEnd;
        }
          
		/// <summary>
		/// Current character
		/// </summary>
		/// <returns>character at cursor position</returns>
        public char Peek()
        {
            if (Filter==null)
                return InputString[(int)Offset];
            else
                return Filter.Filter(InputString[(int)Offset]);
        }

		/// <summary>
		/// Extracts a substring 
		/// </summary>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public String Substring(long offset, int length)
		{
			String s=InputString.Substring((int)offset,Math.Min(length, InputString.Length-(int)offset));
			
			if (Filter != null)
				s=Filter.Filter(s);

			return s;
		}

		/// <summary>
		/// Moves the cursor to the offset position
		/// </summary>
		/// <param name="offset"></param>
        public void Seek(long offset)
        {
			if (offset < 0 || offset > InputString.Length)
				throw new ArgumentOutOfRangeException("offset");

            Offset = offset;
        }
        
		/// <summary>
		/// Current filter
		/// </summary>
        public IFilter Filter 
        {
            get
            {
                return m_Filter;
            }
            set 
            { 
                m_Filter = value;
            }
        } 

		/// <summary>
		/// Failure match
		/// </summary>
        public ParserMatch NoMatch
        {
            get
            {
                return new ParserMatch(this,0,-1);
            }
        }

		/// <summary>
		/// Empty match
		/// </summary>
        public ParserMatch EmptyMatch
        {
            get
            {
                return new ParserMatch(this,0,0);
            }
        }

		/// <summary>
		/// Creates a successful match
		/// </summary>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <returns></returns>
        public ParserMatch CreateMatch(long offset, int length)
        {
            return new ParserMatch(this,offset,length);
        }
    }
}

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
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions