Click here to Skip to main content
15,893,814 members
Articles / Desktop Programming / Windows Forms

Easily Create Your Own Parser

Rate me:
Please Sign up or sign in to vote.
4.81/5 (56 votes)
9 Jul 2011CPOL7 min read 184.3K   9.5K   160  
Create a hand made parser in VB.NET or C# easily and fast
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

namespace TokenIcer
{
    public class Processor
    {
        private readonly Dictionary<string, string> _tokens;
        private string _inputString;
        private int _index;

        public string InputString
        {
            set
            {
                _inputString = value;
            }
        }

        public Processor()
        {
            _tokens = new Dictionary<string, string>();
            _index = 0;
            _inputString = string.Empty;
        }

        public void ResetProcessor()
        {
            _tokens.Clear();
            _inputString = string.Empty;
            _index = 0;
        }

        public string AddRegExToken(string regEx, string identifier)
        {
            if (_tokens.ContainsKey(identifier.Trim()))
                return "";

            identifier = identifier.Trim();
            if (CheckValidity(regEx) == false)
            {
                return GetInvalidMessage(regEx);
            }
            _tokens.Add(identifier, regEx);

            return "";
        }

        private string GetInvalidMessage(string regEx)
        {
            try
            {
                Regex r = new Regex(regEx);

                r.Match("");
            } catch (Exception e)
            {
                return e.Message;
            }

            return "";
        }

        private bool CheckValidity(string regEx)
        {
            try
            {
                Regex r = new Regex(regEx);

                r.Match("");
            } catch (Exception)
            {
                return false;
            }

            return true;
        }

        public Token GetToken()
        {
            if (_index >= _inputString.Length)
                return null;

            foreach (KeyValuePair<string, string> pair in _tokens)
            {
                Regex r = new Regex(pair.Value);
                Match m = r.Match(_inputString, _index);

                if (m.Success && m.Index == _index)
                {
                    if (m.Length == 0)
                        continue;
                    _index += m.Length;
                    return new Token(pair.Key, m.Value);
                }
            }
            _index++;
            return new Token("UNDEFINED", "");
        }
    }
}

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
Web Developer http://www.icemanind.com
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