Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Acornima/Helpers/CodePointRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Globalization;
using System.Runtime.CompilerServices;

#pragma warning disable CA1805 // Do not initialize unnecessarily

namespace Acornima.Helpers;

#if DEBUG
Expand Down Expand Up @@ -202,6 +204,16 @@ public static KeyValuePair<int, int> CategoryFrom(UnicodeCategory category)

private readonly CodePointRange[][] _generalCategories = new CodePointRange[OtherCategory.Key + 1][];

// Caches for Script, Script_Extensions, and Binary property ranges
internal readonly Dictionary<string, CodePointRange[]> ScriptRangeCache = new(StringComparer.Ordinal);
internal readonly Dictionary<string, CodePointRange[]> ScriptExtensionsRangeCache = new(StringComparer.Ordinal);
internal readonly Dictionary<string, CodePointRange[]> BinaryRangeCache = new(StringComparer.Ordinal);

// Special-case binary property caches
internal CodePointRange[]? AnyRange;
internal CodePointRange[]? AsciiRange;
internal CodePointRange[]? AssignedRange;

public CodePointRange[] GetGeneralCategory(KeyValuePair<int, int> category)
{
ref var cachedRanges = ref _generalCategories[category.Key];
Expand Down
38 changes: 33 additions & 5 deletions src/Acornima/Tokenizer.RegExpParser.Unicode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,29 @@ private static bool ValidateUnicodeProperty(ReadOnlyMemory<char> expression, boo
return UnicodeProperties.IsAllowedGeneralCategoryValue(expression);
}

case "sc" or "Script" or "scx" or "Script_Extensions":
// Translating unicode properties other than General Categories is not implemented currently.
codePointRanges = default;
return UnicodeProperties.IsAllowedScriptValue(expression, parser._tokenizer._options._ecmaVersion);
case "sc" or "Script":
if (translateToRanges)
{
codePointRanges = UnicodeProperties.GetScriptRange(expression, parser.GetCodePointRangeCache());
return codePointRanges is not null;
}
else
{
codePointRanges = default;
return UnicodeProperties.IsAllowedScriptValue(expression, parser._tokenizer._options._ecmaVersion);
}

case "scx" or "Script_Extensions":
if (translateToRanges)
{
codePointRanges = UnicodeProperties.GetScriptExtensionsRange(expression, parser.GetCodePointRangeCache());
return codePointRanges is not null;
}
else
{
codePointRanges = default;
return UnicodeProperties.IsAllowedScriptValue(expression, parser._tokenizer._options._ecmaVersion);
}

default:
codePointRanges = default;
Expand All @@ -583,7 +602,16 @@ private static bool ValidateUnicodeProperty(ReadOnlyMemory<char> expression, boo
return true;
}

// Translating unicode properties other than General Categories is not implemented currently.
if (translateToRanges)
{
codePointRanges = UnicodeProperties.GetBinaryPropertyRange(expression, parser.GetCodePointRangeCache());
if (codePointRanges is not null)
{
return true;
}
}

// Check if it's a recognized binary property name (even if we couldn't translate it)
codePointRanges = default;
return UnicodeProperties.IsAllowedBinaryValue(expression, parser._tokenizer._options._ecmaVersion);
}
Expand Down
Loading
Loading