tlang-runtime-compiler/TLang.Lexer/LexToken.cs

78 lines
2.7 KiB
C#

using System;
namespace TLang.Lexer
{
public class LexToken : IEquatable<LexToken>
{
public static LexToken Empty => new LexToken();
public bool IsEmpty => Text.Length == 0 && !IsDocumentation && !IsChar && !IsString;
public static LexToken Documentation(string text)
{
LexToken token = new LexToken();
token.IsDocumentation =true;
token.IsChar = false;
token.IsString = false;
token.Text = text;
return token;
}
public static LexToken Token(string text)
{
LexToken token = new LexToken();
token.IsDocumentation=false;
token.IsChar = false;
token.IsString = false;
token.Text = text;
return token;
}
public static LexToken String(string text)
{
LexToken token = new LexToken();
token.IsDocumentation=false;
token.IsChar = false;
token.IsString = true;
token.Text = text;
return token;
}
public static LexToken Char(string text)
{
LexToken token = new LexToken();
token.IsDocumentation=false;
token.IsChar = true;
token.IsString = false;
token.Text = text;
return token;
}
public bool SpacesBetweenThisAndNext {get;set;} = false;
public LexToken WithLineInfo(LexLineInfo lineInfo)
{
Position = lineInfo.Clone();
return this;
}
public LexToken WithLineInfo(int line,int col,int offset,string filename="memory.tlang")
{
Position = new LexLineInfo(){Line = line,Column = col,Position = offset,FileName = filename};
return this;
}
public LexLineInfo Position {get;set;} = new LexLineInfo();
public string Text {get;set;}="";
public bool IsString {get;set;}=false;
public bool IsChar {get;set;}=false;
public bool IsDocumentation {get;set;}=false;
public bool SameToken(LexToken token)
{
return Text == token.Text && IsChar == token.IsChar && IsString == token.IsString && IsDocumentation == token.IsDocumentation;
}
public bool Equals(LexToken token)
{
return Text == token.Text && IsChar == token.IsChar && IsString == token.IsString && IsDocumentation == token.IsDocumentation && SpacesBetweenThisAndNext == token.SpacesBetweenThisAndNext && Position.Equals(token.Position);
}
public bool IsTokenWith(string text)
{
return !IsChar && !IsDocumentation && !IsString && Text == text;
}
}
}