tlang-interperter-cs/tlanglib/Lexer.cs

241 lines
8.0 KiB
C#
Raw Normal View History

2023-03-10 10:12:36 +00:00
using System.Collections.Generic;
using System.IO;
2023-03-09 19:40:14 +00:00
using System.Text;
namespace tlang
{
2023-03-10 04:33:58 +00:00
public class Lexer
2023-03-09 19:40:14 +00:00
{
TextReader reader;
public Lexer(string fName) : this(new StreamReader(fName))
{
}
public Lexer(TextReader reader)
{
this.reader = reader;
}
2023-03-10 04:33:58 +00:00
List<LexToken> tokens=new List<LexToken>();
2023-03-09 19:40:14 +00:00
public List<LexToken> Tokens
{
get{
2023-03-10 04:33:58 +00:00
if(!set)
2023-03-09 19:40:14 +00:00
{
2023-03-10 04:33:58 +00:00
set=true;
2023-03-09 19:40:14 +00:00
tokens = _getTokens();
}
return tokens;
}
}
2023-03-10 04:33:58 +00:00
bool set=false;
2023-03-09 19:40:14 +00:00
List<LexToken> _tokens = new List<LexToken>();
StringBuilder b = new StringBuilder();
private void FlushBuilder()
{
if(b.Length > 0)
{
_tokens.Add(LexToken.FromGeneralToken(b.ToString()));
b.Clear();
}
}
bool escaped=false;
private char getChar()
{
int read = reader.Read();
if(read == '\\')
{
escaped = true;
read = reader.Read();
if(read == 'x')
{
2023-03-10 04:33:58 +00:00
return (char)short.Parse($"0x{(char)reader.Read()}{(char)reader.Read()}",System.Globalization.NumberStyles.AllowHexSpecifier);
2023-03-09 19:40:14 +00:00
}
else if(read == 'n')
{
return '\n';
}
else if(read == 't')
{
return '\t';
}
else if(read == 'r')
{
return '\r';
}
return (char)read;
}
escaped=false;
return (char)read;
}
private List<LexToken> _getTokens()
{
int read;
while((read = reader.Read()) != -1)
{
int next=reader.Peek();
switch(read)
{
case '\"':
FlushBuilder();
//"Some \"Some String\" Is OK"
while(true)
{
char r = getChar();
if(r == -1 || (!escaped && r == '\"'))
{
break;
}
b.Append(r);
}
_tokens.Add(LexToken.FromString(b.ToString()));
b.Clear();
break;
case '\'':
FlushBuilder();
2023-03-10 04:33:58 +00:00
_tokens.Add(LexToken.FromChar(getChar().ToString()));
2023-03-09 19:40:14 +00:00
reader.Read();
break;
case '#':
while(true)
{
read = reader.Read();
if(read == -1) return _tokens;
if(read == '\n')
{
break;
}
}
break;
case '/':
FlushBuilder();
if(next == '=')
{
reader.Read();
_tokens.Add(LexToken.FromGeneralToken($"{(char)read}{(char)next}"));
}
else if(next == '/')
{
reader.Read();
while(true)
{
read = reader.Read();
if(read == -1) return _tokens;
if(read == '\n')
{
break;
}
}
}
else if(next == '*')
{
reader.Read();
bool lastIsAstrick=false;
while(true)
{
read = reader.Read();
if(read == -1) return _tokens;
if(read == '*')
{
lastIsAstrick=true;
continue;
}
else if(read == '/')
{
if(lastIsAstrick)
{
break;
}
}
lastIsAstrick=false;
}
}
else
{
_tokens.Add(LexToken.FromGeneralToken(read));
}
break;
case '^':
case '*':
case '%':
FlushBuilder();
if(next == '=')
{
reader.Read();
_tokens.Add(LexToken.FromGeneralToken($"{(char)read}{(char)next}"));
}
else
{
_tokens.Add(LexToken.FromGeneralToken(read));
}
break;
case '|':
case '&':
case '<':
case '>':
case '+':
case '-':
case '!':
FlushBuilder();
if(next == read || next == '=')
{
reader.Read();
_tokens.Add(LexToken.FromGeneralToken($"{(char)read}{(char)next}"));
}
else
{
_tokens.Add(LexToken.FromGeneralToken(read));
}
break;
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
case '.':
case '?':
case ':':
case ',':
case ';':
2023-06-08 18:14:35 +00:00
FlushBuilder();
_tokens.Add(LexToken.FromGeneralToken(read));
break;
2023-03-09 19:40:14 +00:00
case '=':
2023-06-08 18:14:35 +00:00
2023-03-09 19:40:14 +00:00
FlushBuilder();
2023-06-08 18:14:35 +00:00
if(next == read)
{
reader.Read();
_tokens.Add(LexToken.FromGeneralToken($"{(char)read}{(char)next}"));
}
else
{
_tokens.Add(LexToken.FromGeneralToken(read));
}
2023-03-09 19:40:14 +00:00
break;
case ' ':
case '\n':
case '\t':
case '\r':
FlushBuilder();
break;
default:
b.Append((char)read);
break;
}
}
FlushBuilder();
return _tokens;
}
}
}