55 lines
995 B
C#
55 lines
995 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
namespace lexer.match
|
|
{
|
|
public class Alternative : Sequence
|
|
{
|
|
public Alternative()
|
|
{
|
|
}
|
|
|
|
public override MatchedPart[] Match(buffer.CharacterBuffer chbuffer)
|
|
{
|
|
MatchedPart[] matchedParts = null;
|
|
int len = -1;
|
|
|
|
foreach (Matchable m in matchables)
|
|
{
|
|
chbuffer.Push();
|
|
|
|
MatchedPart[] mparts = m.MatchNoticeable(chbuffer);
|
|
if (mparts != null)
|
|
{
|
|
int mplen = mparts.MatchedLength();
|
|
if ((matchedParts == null) || (mplen > len))
|
|
{
|
|
matchedParts = mparts;
|
|
len = mplen;
|
|
}
|
|
}
|
|
chbuffer.Pop();
|
|
}
|
|
|
|
return matchedParts;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("( ");
|
|
for (int n = 0; n < this.matchables.Count; n++)
|
|
{
|
|
if (n > 0)
|
|
sb.Append(" | ");
|
|
|
|
Matchable m = this.matchables[n];
|
|
sb.Append(m.ToString(true));
|
|
}
|
|
sb.Append(")");
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|