switch root namespace name to NITLOAD
This commit is contained in:
@@ -0,0 +1,313 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="RegexTokenReader"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using Nitload.RegularAnalysis.Abstraction;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Permissions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Text
|
||||
{
|
||||
internal class RegexTokenReader : ITokenReader<RegexElementLabel>
|
||||
{
|
||||
|
||||
private static string _source;
|
||||
|
||||
private int position = 0;
|
||||
|
||||
public RegexTokenReader() { }
|
||||
|
||||
public RegexTokenReader(string source)
|
||||
{
|
||||
_source = source;
|
||||
}
|
||||
|
||||
public bool IsEmpty => string.IsNullOrEmpty(_source) || position >= _source.Length;
|
||||
|
||||
public Token<RegexElementLabel> ReadToken()
|
||||
{
|
||||
int pos = position;
|
||||
switch (_source[pos])
|
||||
{
|
||||
case '\r' or '\n':
|
||||
throw new NotSupportedException("Multi-line regular expressions are not supported yet.");
|
||||
case '\\':
|
||||
if (pos + 1 == _source.Length) {
|
||||
throw new InvalidOperationException("Unclosed char");
|
||||
}
|
||||
if (_source[pos + 1] == 'x')
|
||||
{
|
||||
string value = _source[(pos + 1)..(pos + 3)];
|
||||
pos += 4;
|
||||
if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result))
|
||||
{
|
||||
return new Token<RegexElementLabel>(result.ToString(), 0, pos, RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Failed to convert {value} to a valid ascii code point.");
|
||||
}
|
||||
} else if (_source[pos + 1] == 'u')
|
||||
{
|
||||
const int unicodeEscapeLength = 6;
|
||||
if (pos > _source.Length - unicodeEscapeLength)
|
||||
{
|
||||
throw new InvalidCastException(
|
||||
"A Unicode escape must contain exactly four hexadecimal digits.");
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> value = _source.AsSpan(pos + 2, 4);
|
||||
if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier,
|
||||
CultureInfo.InvariantCulture, out ushort code))
|
||||
{
|
||||
position = pos + unicodeEscapeLength;
|
||||
return new Token<RegexElementLabel>(((char)code).ToString(), 0, pos,
|
||||
RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException(
|
||||
$"Failed to convert {value.ToString()} to a UTF-16 code unit.");
|
||||
}
|
||||
} else if (_source[pos + 1] == 'p')
|
||||
{
|
||||
int i = pos + 2;
|
||||
if (_source[i] != '{') {
|
||||
throw new InvalidOperationException("Invalid Unicode property escape sequence.");
|
||||
}
|
||||
for (; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == '}')
|
||||
{
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, pos,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed Unicode property escape sequence.");
|
||||
} else if (_source[pos + 1] == '.')
|
||||
{
|
||||
position = pos + 2;
|
||||
return new Token<RegexElementLabel>(".", 0, pos, RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
if (_source[pos + 1] is 'n' or 'r') {
|
||||
throw new NotSupportedException("Multi-line regular expressions are not supported yet.");
|
||||
}
|
||||
position = pos + 2;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 2)], 0, pos,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
case '[':
|
||||
for (int i = pos + 1; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == ']' && _source[i - 1] != '\\')
|
||||
{
|
||||
int length = (i - pos - 1);
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, position,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed character set");
|
||||
case '|':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.ALT);
|
||||
case '(':
|
||||
|
||||
// very comlex here
|
||||
return ReadGroupBeginToken();
|
||||
case ')':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.GROUP_END);
|
||||
case '{':
|
||||
for (int i = pos + 1; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == '}')
|
||||
{
|
||||
int end = i + 1;
|
||||
if (end < _source.Length && _source[end] == '?') {
|
||||
end++;
|
||||
}
|
||||
position = end;
|
||||
return new Token<RegexElementLabel>(_source[pos..end], 0, pos,
|
||||
RegexElementLabel.COUNT);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed count specifier");
|
||||
case '*' or '+':
|
||||
int count = pos + 1 >= _source.Length ? 1 : (_source[pos + 1] == '?' ? 2 : 1);
|
||||
position = pos + count;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + count)], 0, pos, RegexElementLabel.COUNT);
|
||||
case '?':
|
||||
int optionalCount = pos + 1 < _source.Length && _source[pos + 1] == '?' ? 2 : 1;
|
||||
position = pos + optionalCount;
|
||||
return new Token<RegexElementLabel>(_source[pos..(pos + optionalCount)], 0, pos,
|
||||
RegexElementLabel.COUNT);
|
||||
case '.':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT_SET);
|
||||
default:
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryReadToken(out Token<RegexElementLabel> t)
|
||||
{
|
||||
if (IsEmpty)
|
||||
{
|
||||
t = Token<RegexElementLabel>.Empty;
|
||||
return false;
|
||||
}
|
||||
t = ReadToken();
|
||||
return Token<RegexElementLabel>.Empty != t;
|
||||
}
|
||||
|
||||
private Token<RegexElementLabel> ReadGroupBeginToken()
|
||||
{
|
||||
if (IsEmpty) {
|
||||
throw new InvalidOperationException("Source is null.");
|
||||
}
|
||||
|
||||
int start = position;
|
||||
|
||||
if (start >= _source.Length || _source[start] != '(') {
|
||||
throw new InvalidOperationException("ReadGroupBeginToken called when current char is not '('.");
|
||||
}
|
||||
|
||||
// Case 1: plain capturing group "("
|
||||
if (start + 1 >= _source.Length || _source[start + 1] != '?')
|
||||
{
|
||||
position = start + 1;
|
||||
return new Token<RegexElementLabel>("(", 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// From here we have "(?"
|
||||
if (start + 2 >= _source.Length)
|
||||
{
|
||||
throw new InvalidOperationException("Unfinished group prefix '(?'.");
|
||||
}
|
||||
|
||||
char c2 = _source[start + 2];
|
||||
|
||||
// Case 2: non-capturing group "(?:"
|
||||
if (c2 == ':')
|
||||
{
|
||||
position = start + 3;
|
||||
return new Token<RegexElementLabel>("(?:", 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// Assertions (write the first-level cases, but not supported yet)
|
||||
if (c2 == '=')
|
||||
{
|
||||
// "(?="
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Positive lookahead '(?=...)' is not supported yet.");
|
||||
}
|
||||
if (c2 == '!')
|
||||
{
|
||||
// "(?!"
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Negative lookahead '(?!...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Named capturing group or lookbehind starts with "(?<"
|
||||
if (c2 == '<')
|
||||
{
|
||||
if (start + 3 >= _source.Length) {
|
||||
throw new InvalidOperationException("Unfinished group prefix '(?<'.");
|
||||
}
|
||||
|
||||
char c3 = _source[start + 3];
|
||||
|
||||
// Look behind prefixes: "(?<=" and "(?<!"
|
||||
if (c3 == '=')
|
||||
{
|
||||
position = start + 4;
|
||||
throw new NotSupportedException("Positive look behind '(?<=...)' is not supported yet.");
|
||||
}
|
||||
if (c3 == '!')
|
||||
{
|
||||
position = start + 4;
|
||||
throw new NotSupportedException("Negative look behind '(?<!...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Otherwise: "(?<name>"
|
||||
int nameStart = start + 3;
|
||||
|
||||
// Read name until '>'
|
||||
int i = nameStart;
|
||||
|
||||
// Name must start with [A-Za-z_]
|
||||
if (i >= _source.Length)
|
||||
{
|
||||
throw new InvalidOperationException("Unfinished named group prefix.");
|
||||
}
|
||||
|
||||
char first = _source[i];
|
||||
if (!(first == '_' || char.IsLetter(first))) {
|
||||
throw new InvalidOperationException($"Invalid named group name start '{first}' at position {i}.");
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
while (i < _source.Length)
|
||||
{
|
||||
char ch = _source[i];
|
||||
if (ch == '>')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(ch == '_' || char.IsLetterOrDigit(ch))) {
|
||||
throw new InvalidOperationException($"Invalid character '{ch}' in group name at position {i}.");
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i >= _source.Length || _source[i] != '>') {
|
||||
throw new InvalidOperationException("Unclosed named group prefix; missing '>'.");
|
||||
}
|
||||
|
||||
// Consume "(?<name>"
|
||||
int consumedLen = i - start + 1; // include '>'
|
||||
string text = _source.Substring(start, consumedLen);
|
||||
position = start + consumedLen;
|
||||
|
||||
return new Token<RegexElementLabel>(text, 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// Optional: comments "(?#...)" or inline options "(?i:...)" etc.
|
||||
// First-level cases listed but not supported yet:
|
||||
if (c2 == '#')
|
||||
{
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Comment group '(?#...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Inline options: (?i:...), (?m:...), (?-i:...), (?im), etc.
|
||||
// Too many dialect differences; reserve the prefix but mark unsupported.
|
||||
if (char.IsLetter(c2) || c2 == '-')
|
||||
{
|
||||
// We don't know how long the option spec is here; do NOT advance position too much.
|
||||
// Keep position at '(' so caller can decide how to handle, or just throw.
|
||||
position = start;
|
||||
throw new NotSupportedException("Inline option groups like '(?i:...)' are not supported yet.");
|
||||
}
|
||||
|
||||
// Unknown "(?X" prefix
|
||||
position = start;
|
||||
throw new InvalidOperationException($"Unknown or unsupported group prefix starting at position {start}: '{_source.Substring(start, Math.Min(4, _source.Length - start))}'");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user