|
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.
Just develop simple softwares. C# is my favorite prgramming languare, though I hope to create a new one...