48 lines
1020 B
C#
48 lines
1020 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
namespace sharp.parser
|
|
{
|
|
public class TokenDefinition
|
|
{
|
|
public String Name { get; private set; }
|
|
public LexerPathSegment[] PathHeads { get { return this.pathHeads.ToArray(); } }
|
|
|
|
private List<LexerPathSegment> pathHeads = new List<LexerPathSegment>();
|
|
|
|
public TokenDefinition(string name)
|
|
{
|
|
this.Name = name;
|
|
}
|
|
public TokenDefinition(string name,LexerPathSegment pathHead)
|
|
{
|
|
this.Name = name;
|
|
this.pathHeads.Add(pathHead);
|
|
}
|
|
public TokenDefinition(string name,LexerPathSegment[] pathHeads)
|
|
{
|
|
this.Name = name;
|
|
this.pathHeads.AddRange(pathHeads);
|
|
}
|
|
|
|
|
|
public Token tryParse(CharBuffer buffer){
|
|
int cpos = buffer.Position;
|
|
|
|
foreach (LexerPathSegment head in pathHeads){
|
|
int n = head.walk(buffer);
|
|
if (n > 0){
|
|
Token t = new Token(this,buffer,n);
|
|
return t;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return string.Format("[TokenDefinition: Name={0}]", Name);
|
|
}
|
|
}
|
|
}
|