54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
using System;
|
|
using lexer.buffer;
|
|
using System.Collections.Generic;
|
|
namespace lexer.match
|
|
{
|
|
public class Expression : Matchable
|
|
{
|
|
public Matchable Matchable { get; private set; }
|
|
|
|
public int Minimum { get; private set; } = 0;
|
|
public int Maximum { get; private set; } = int.MaxValue;
|
|
|
|
public Expression(Matchable matchable,int minimum = 1,int maximum = 1)
|
|
{
|
|
this.Matchable = matchable;
|
|
this.Minimum = minimum;
|
|
this.Maximum = maximum;
|
|
}
|
|
|
|
public override MatchedPart[] Match(CharacterBuffer chbuffer){
|
|
List<MatchedPart> matchedParts = new List<MatchedPart>();
|
|
int n;
|
|
chbuffer.Push();
|
|
|
|
for (n = 0; n < Maximum;n++){
|
|
MatchedPart[] mparts = Matchable.MatchNoticeable(chbuffer);
|
|
if (mparts == null){
|
|
break;
|
|
}
|
|
matchedParts.AddRange(mparts);
|
|
chbuffer.Position += mparts.MatchedLength();
|
|
}
|
|
|
|
chbuffer.Pop();
|
|
|
|
if (n < Minimum){
|
|
return null;
|
|
}
|
|
|
|
return matchedParts.ToArray();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return String.Format("{0}{{{1},{2}}}",
|
|
this.Matchable.ToString(true),
|
|
Minimum == 0 ? "" : Minimum.ToString(),
|
|
Maximum == int.MaxValue ? "" : Maximum.ToString()
|
|
);
|
|
}
|
|
|
|
}
|
|
}
|