Skip to content
Merged
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
39 changes: 16 additions & 23 deletions lib/beam_mcp/schema/ecma.ex
Original file line number Diff line number Diff line change
Expand Up @@ -961,36 +961,29 @@ defmodule BeamMCP.Schema.ECMA do
defp emit({:set, :any_of, ranges, []}), do: class_text(ranges)
defp emit({:set, :none_of, ranges, []}), do: class_text(complement(ranges))

defp emit({:set, :any_of, ranges, props}) do
parts =
if(ranges == [], do: [], else: [class_text(ranges)]) ++ Enum.map(props, &property_text/1)
# A class holding a property is one class of the library's, `\\p{...}` inside it as outside,
# so it matches one code point as the class does and, repeated, holds no memory for each
# repetition (a group does: `(?:\\p{L})*` met the frame bound where `\\p{L}*` does not). A
# negated one matches any code point in neither: under UTF the library's subject holds no
# surrogate, so the complement is ECMA-262's.
defp emit({:set, :any_of, ranges, props}),
do: ["[", range_text(ranges), Enum.map(props, &property_text/1), "]"]

["(?:", Enum.intersperse(parts, "|"), ")"]
end

defp emit({:set, :none_of, ranges, props}) do
[
"(?:",
Enum.map(props, &["(?!", property_text(&1), ")"]),
class_text(complement(ranges)),
")"
]
end
defp emit({:set, :none_of, ranges, props}),
do: ["[^", range_text(ranges), Enum.map(props, &property_text/1), "]"]

defp property_text({true, name}), do: ["\\p{", name, "}"]
defp property_text({false, name}), do: ["\\P{", name, "}"]

defp class_text([]), do: "(?:(?!))"

defp class_text(ranges) do
[
"[",
Enum.map(ranges, fn
{a, a} -> code_point(a)
{a, b} -> [code_point(a), "-", code_point(b)]
end),
"]"
]
defp class_text(ranges), do: ["[", range_text(ranges), "]"]

defp range_text(ranges) do
Enum.map(ranges, fn
{a, a} -> code_point(a)
{a, b} -> [code_point(a), "-", code_point(b)]
end)
end

defp code_point(c), do: ["\\x{", Integer.to_string(c, 16), "}"]
Expand Down
23 changes: 23 additions & 0 deletions test/beam_mcp/pattern_dialect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ defmodule BeamMCP.PatternDialectTest do
end
end

# A class holding a property is written as one class of the library's, as a plain class is,
# not as a group: a repeated group holds memory for each repetition, and 1.0.2 served these
# at any length the body allows.
test "a repeated property or class holding one is served at any length, as a plain class is" do
letters = String.duplicate("a", 200_000)
marks = String.duplicate("!", 200_000)

for {pattern, string} <- [
{"^\\p{L}*$", letters},
{"^\\P{L}*$", marks},
{"^[\\p{L}\\d]*$", letters},
{"^[^\\p{N}]*$", letters},
{"^[^\\p{L}a-z]+$", marks},
{"^\\p{Script=Latin}{1,65535}$", String.duplicate("a", 65_535)}
] do
assert matches?(pattern, string), pattern
assert matches?(pattern, string, pattern_dialect: :pcre), pattern
end

refute matches?("^[^\\p{L}a-z]+$", "!a")
refute matches?("^[\\p{L}\\d]*$", "a!")
end

test "the dialect is :ecma or :pcre, and anything else raises" do
for opts <- [[pattern_dialect: :ecma262], [pattern_dialect: "pcre"], [pattern_dialect: nil]] do
assert_raise ArgumentError, ~r/pattern_dialect must be :ecma or :pcre/, fn ->
Expand Down