188 lines
3.3 KiB
C#
188 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Linq;
|
|
namespace lexer.buffer
|
|
{
|
|
public class CharacterBuffer
|
|
{
|
|
char[] characters;
|
|
int position;
|
|
|
|
Stack<int> positionStack = new Stack<int>();
|
|
|
|
public CharacterBuffer(char[] characters)
|
|
{
|
|
this.characters = new char[characters.Length];
|
|
Array.Copy(characters, this.characters, characters.Length);
|
|
}
|
|
public CharacterBuffer(String characters)
|
|
: this(characters.ToCharArray())
|
|
{
|
|
}
|
|
|
|
public void Push()
|
|
{
|
|
positionStack.Push(position);
|
|
}
|
|
public void Pop()
|
|
{
|
|
this.position = positionStack.Pop();
|
|
}
|
|
|
|
public char this[int n]
|
|
{
|
|
get { return this.characters[n]; }
|
|
}
|
|
|
|
public char Current
|
|
{
|
|
get { return CharAt(this.position); }
|
|
}
|
|
|
|
public char Last { get { return CharAt(this.position - 1); } }
|
|
public char Next { get { return CharAt(this.position + 1); } }
|
|
|
|
public char CharAt(int position)
|
|
{
|
|
if (position >= this.characters.Length)
|
|
return (char)0xFFFF;
|
|
if (position < 0)
|
|
return (char)0;
|
|
return this.characters[position];
|
|
}
|
|
|
|
public int Position
|
|
{
|
|
get { return this.position; }
|
|
set { this.position = value; }
|
|
}
|
|
|
|
public char NextNext
|
|
{
|
|
get
|
|
{
|
|
if ((position + 1) >= this.characters.Length)
|
|
return (char)0xFFFF;
|
|
|
|
return this.characters[this.position + 2];
|
|
}
|
|
}
|
|
|
|
public char next(int n)
|
|
{
|
|
if ((position + n) >= this.characters.Length)
|
|
return (char)0xFFFF;
|
|
return this.characters[this.position + n];
|
|
}
|
|
|
|
public char MoveNext()
|
|
{
|
|
this.position++;
|
|
if (EndOfBuffer)
|
|
return (char)0;
|
|
|
|
return this.characters[this.position];
|
|
}
|
|
|
|
public bool EndOfBuffer
|
|
{
|
|
get { return this.position >= this.characters.Length; }
|
|
}
|
|
|
|
public char[] getSection()
|
|
{
|
|
int start = positionStack.Last();
|
|
return getSection(start, position - start);
|
|
}
|
|
|
|
public char[] getSection(int start, int len)
|
|
{
|
|
char[] result = new char[len];
|
|
|
|
for (int n = 0; n < len; n++)
|
|
{
|
|
result[n] = this.characters[start + n];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public char[] find(char ch)
|
|
{
|
|
return find(new char[] { ch });
|
|
}
|
|
public char[] find(char[] ch)
|
|
{
|
|
List<char> characters = new List<char>();
|
|
while (!EndOfBuffer)
|
|
{
|
|
foreach (char c in ch)
|
|
{
|
|
if (Current == c)
|
|
{
|
|
MoveNext();
|
|
return characters.ToArray();
|
|
}
|
|
}
|
|
characters.Add(Current);
|
|
MoveNext();
|
|
}
|
|
return characters.ToArray();
|
|
}
|
|
|
|
public char[] findUnescaped(char find)
|
|
{
|
|
return findUnescaped(new char[] { find });
|
|
}
|
|
|
|
public char[] findUnescaped(char[] find)
|
|
{
|
|
int n;
|
|
|
|
for (n = 0; !EndOfBuffer; n++)
|
|
{
|
|
foreach (char f in find)
|
|
{
|
|
if (f == Current)
|
|
{
|
|
char[] r = getSection(position - n, n);
|
|
MoveNext();
|
|
return r;
|
|
}
|
|
}
|
|
|
|
if (Current == '\\')
|
|
{
|
|
MoveNext();
|
|
n++;
|
|
}
|
|
MoveNext();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public char[] findSymbol()
|
|
{
|
|
List<char> characters = new List<char>();
|
|
while (!EndOfBuffer)
|
|
{
|
|
if (
|
|
!char.IsDigit(Current) &&
|
|
!char.IsLetter(Current) &&
|
|
(Current != '_')
|
|
)
|
|
break;
|
|
|
|
characters.Add(Current);
|
|
MoveNext();
|
|
}
|
|
#if DEBUG
|
|
Console.WriteLine("findSymbol() = {0}",new String(characters.ToArray()));
|
|
#endif
|
|
return characters.ToArray();
|
|
}
|
|
|
|
}
|
|
}
|