ln.parse/ln.parse/tokenizer/RegularExpressionMatcher.cs

38 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ln.parse.tokenizer
{
public class RegularExpressionMatcher : TokenMatcher
{
Regex regex;
Func<SourceBuffer, int, int, Token> createTokenDelegate;
public RegularExpressionMatcher(string pattern,Func<SourceBuffer,int,int,Token> createTokenDelegate)
:this(pattern)
{
this.createTokenDelegate = createTokenDelegate;
}
protected RegularExpressionMatcher(string pattern)
{
regex = new Regex(pattern);
}
public virtual Token CreateToken(SourceBuffer sourceBuffer, int start, int length) => createTokenDelegate(sourceBuffer, start, length);
public override bool Match(SourceBuffer sourceBuffer,out Token token)
{
Match match = regex.Match(sourceBuffer.GetCurrentText());
if ((match != null) && match.Success && (match.Index == 0))
{
token = CreateToken(sourceBuffer, sourceBuffer.LinearPosition, match.Length);
return true;
}
token = null;
return false;
}
}
}