Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / C#

Script Engine Implemented by C# and Regular Expression

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
21 Nov 2009CPOL2 min read 24.8K   853   24  
Script engine to execute script codes, which is built by C# and regular expression
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Zadesoft.Library.Script
{
    public abstract class AbstractParseRout
    {
        private readonly List<WordTokenMap> _maps;

        private readonly IToken _startToken;

        public AbstractParseRout(IToken startToken)
        {
            if (startToken == null)
            {
                throw new ArgumentNullException("start token cannot be null");
            }

            _startToken = startToken;
            _maps = new List<WordTokenMap>();
        }

        public abstract int Priority { get; }

        public IToken StartToken
        {
            get
            {
                return _startToken;
            }
        }

        public IToken[] AvailableEndToken
        {
            get
            {
                List<IToken> tokens = new List<IToken>();

                foreach (WordTokenMap map in _maps)
                {
                    tokens.Add(map.Token);
                }

                return tokens.ToArray();
            }
        }

        public bool DoesMatchStartToken(IToken token)
        {
            if (token == null)
            {
                return false;
            }

            return _startToken.GetType().Equals(token.GetType());
        }

        public IToken Accept(string word)
        {
            foreach (WordTokenMap map in _maps)
            {
                if (map.DoesAnyWordMatch(word))
                {
                    return map.Token;
                }
            }

            return null;
        }

        internal class WordTokenMap
        {
            private List<string> _regexs;

            private IToken _token;

            public string[] Regexs
            {
                get
                {
                    return _regexs.ToArray();
                }
            }

            public IToken Token
            {
                get
                {
                    return _token;
                }
            }

            public WordTokenMap(IToken token)
            {
                if (_token == null)
                {
                    throw new ArgumentNullException("token cannot be null");
                }

                _token = token;
                _regexs = new List<string>();
            }

            public void AddWord(string regex)
            {
                if (!string.IsNullOrEmpty(regex))
                {
                    _regexs.Add(regex);
                }
            }

            public bool DoesAnyWordMatch(string str)
            {
                foreach (string regex in _regexs)
                {
                    if (Regex.IsMatch(str, regex))
                    {
                        return true;
                    }
                }

                return false;
            }
        }
    }
}

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) Totaldata Web Data Mining(extractweb.com)
China China
Just develop simple softwares. C# is my favorite prgramming languare, though I hope to create a new one...

Comments and Discussions