61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Collections.Generic;
|
|
namespace lexer.match
|
|
{
|
|
public delegate void MatchedPartNoticeDelegate(MatchedPart matchedPart);
|
|
|
|
public class MatchedPart
|
|
{
|
|
public Matchable Matchable { get; private set; }
|
|
public char[] MatchedCharacters { get; private set; }
|
|
|
|
public int Length { get { return this.MatchedCharacters.Length; } }
|
|
|
|
public MatchedPart(Matchable matchable, char[] characters)
|
|
{
|
|
this.Matchable = matchable;
|
|
this.MatchedCharacters = characters;
|
|
}
|
|
|
|
public String MatchedString { get { return new String(MatchedCharacters); } }
|
|
|
|
}
|
|
|
|
public static class MatchedPartArray {
|
|
|
|
public static int MatchedLength(this MatchedPart[] parts){
|
|
int l = 0;
|
|
foreach (MatchedPart p in parts)
|
|
l += p.Length;
|
|
return l;
|
|
}
|
|
|
|
public static char[] MatchingCharacters(this MatchedPart[] parts) {
|
|
if ((parts == null)||(parts.Length == 0)){
|
|
return new char[0];
|
|
}
|
|
List<char> characters = new List<char>();
|
|
foreach (MatchedPart mp in parts){
|
|
characters.AddRange(mp.MatchedCharacters);
|
|
}
|
|
return characters.ToArray();
|
|
}
|
|
|
|
|
|
public static String AsString(this char[] chars){
|
|
return new String(chars);
|
|
}
|
|
|
|
public static void Notice(this MatchedPart[] matchedParts,MatchedPartNoticeDelegate notice){
|
|
foreach (MatchedPart mpart in matchedParts){
|
|
if (mpart.Matchable.Notice){
|
|
notice(mpart);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|