Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public final class ReggieFlags {
// Keep these separate from java.util.regex.Pattern's values so an accidental JDK flag is never
// silently interpreted as a different Reggie flag.
public static final int NONE = 0;
public static final int CASE_INSENSITIVE = 1 << 24;
public static final int MULTILINE = 1 << 25;
public static final int DOTALL = 1 << 26;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,44 @@ boolean matchIntoBounded(
return true;
}

MatchWorkspace newMatchWorkspace() {
return newWorkspace();
}

int groupCount() {
return groupCount;
}

int groupIndex(String name) {
Objects.requireNonNull(name, "name");
Integer index = nameToIndex.get(name);
if (index == null) {
throw new IllegalArgumentException("unknown group name: " + name);
}
return index;
}

boolean matchIntoBounded(
CharSequence input,
int start,
int end,
int[] groupStarts,
int[] groupEnds,
MatchWorkspace workspace) {
Objects.requireNonNull(input, "input");
Objects.requireNonNull(groupStarts, "groupStarts");
Objects.requireNonNull(groupEnds, "groupEnds");
Objects.requireNonNull(workspace, "workspace");
validateRegion(input, start, end);
if (groupStarts.length <= groupCount || groupEnds.length <= groupCount) {
throw new IndexOutOfBoundsException("group arrays too small for " + groupCount + " groups");
}
if (!matchesAt(input, start, end, workspace, true)) return false;
System.arraycopy(workspace.starts, 0, groupStarts, 0, groupCount + 1);
System.arraycopy(workspace.ends, 0, groupEnds, 0, groupCount + 1);
return true;
}

private MatchResult toMatchResult(String input, MatchWorkspace workspace) {
if (!nameToIndex.isEmpty()) {
return new NamedMatchResultImpl(
Expand Down Expand Up @@ -479,7 +517,7 @@ private static int maxOptionalDepth(Iterable<LinearTokenSequencePlan.Op> ops) {
return max;
}

private static final class MatchWorkspace {
static final class MatchWorkspace {
final int[] starts;
final int[] ends;
final int[][] optionalStarts;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2026-Present Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datadoghq.reggie.runtime;

/** Reason the native named linear-token-sequence profile did not admit a request. */
public enum ReggieCompilationRejection {
UNSUPPORTED_FLAGS,
SOURCE_INLINE_MODIFIER,
PARSE_FAILURE,
PLAN_UNAVAILABLE,
MISSING_NAMED_CAPTURE,
PROFILE_INELIGIBLE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2026-Present Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datadoghq.reggie.runtime;

import java.util.Objects;

/** Immutable outcome of a native named linear-token-sequence compilation request. */
public final class ReggieCompilationResult {
private final ReggieCompiledPattern pattern;
private final ReggieCompilationRejection rejection;

private ReggieCompilationResult(
ReggieCompiledPattern pattern, ReggieCompilationRejection rejection) {
if ((pattern == null) == (rejection == null)) {
throw new IllegalArgumentException("exactly one of pattern or rejection is required");
}
this.pattern = pattern;
this.rejection = rejection;
}

static ReggieCompilationResult admitted(ReggieCompiledPattern pattern) {
return new ReggieCompilationResult(Objects.requireNonNull(pattern, "pattern"), null);
}

static ReggieCompilationResult rejected(ReggieCompilationRejection rejection) {
return new ReggieCompilationResult(null, Objects.requireNonNull(rejection, "rejection"));
}

public boolean isAdmitted() {
return pattern != null;
}

public ReggieCompiledPattern pattern() {
if (pattern == null) {
throw new IllegalStateException("compilation was rejected: " + rejection);
}
return pattern;
}

public ReggieCompilationRejection rejection() {
if (rejection == null) {
throw new IllegalStateException("compilation was admitted");
}
return rejection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2026-Present Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datadoghq.reggie.runtime;

import com.datadoghq.reggie.ReggieFlags;

/** Flags supported by the native named linear-token-sequence compilation profile. */
public enum ReggieCompileFlag {
NONE(ReggieFlags.NONE),
DOTALL(ReggieFlags.DOTALL);

private final int reggieFlags;

ReggieCompileFlag(int reggieFlags) {
this.reggieFlags = reggieFlags;
}

int reggieFlags() {
return reggieFlags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2026-Present Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datadoghq.reggie.runtime;

import java.util.Objects;

/** Immutable request for the native named linear-token-sequence compilation profile. */
public record ReggieCompileRequest(String source, ReggieCompileFlag flag) {
public ReggieCompileRequest {
Objects.requireNonNull(source, "source");
Objects.requireNonNull(flag, "flag");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2026-Present Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datadoghq.reggie.runtime;

import java.util.Objects;

/**
* Immutable native compiled pattern for the named linear-token-sequence profile.
*
* <p>This API never selects another Reggie strategy and never delegates to the JDK.
*/
public final class ReggieCompiledPattern {
private final LinearTokenSequenceMatcher matcher;

private ReggieCompiledPattern(LinearTokenSequenceMatcher matcher) {
this.matcher = Objects.requireNonNull(matcher, "matcher");
}

/**
* Attempts native compilation without consulting the general compiler or either compiler cache.
*/
public static ReggieCompilationResult tryCompile(ReggieCompileRequest request) {
Objects.requireNonNull(request, "request");
RuntimeCompiler.NamedOnlyLtsCompilation compilation =
RuntimeCompiler.tryCompileNamedOnlyLinearTokenSequence(
request.source(), request.flag().reggieFlags());
if (compilation.matcher() != null) {
return ReggieCompilationResult.admitted(new ReggieCompiledPattern(compilation.matcher()));
}
return ReggieCompilationResult.rejected(
ReggieCompilationRejection.valueOf(compilation.rejection().name()));
}
Comment on lines +40 to +45

/** Creates a new single-thread-confined state object for matching this immutable pattern. */
public ReggieMatchState newState() {
return new ReggieMatchState(matcher);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2026-Present Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datadoghq.reggie.runtime;

import java.util.Arrays;
import java.util.Objects;

/**
* Single-thread-confined mutable matching state for a {@link ReggieCompiledPattern}.
*
* <p>The state retains spans only; it never retains the input sequence.
*/
public final class ReggieMatchState {
private final LinearTokenSequenceMatcher matcher;
private final LinearTokenSequenceMatcher.MatchWorkspace workspace;
private final int[] starts;
private final int[] ends;
private boolean matched;

ReggieMatchState(LinearTokenSequenceMatcher matcher) {
this.matcher = Objects.requireNonNull(matcher, "matcher");
this.workspace = matcher.newMatchWorkspace();
this.starts = new int[matcher.groupCount() + 1];
this.ends = new int[matcher.groupCount() + 1];
clear();
}

/**
* Attempts a full match inside {@code [start, end)}.
*
* <p>Prior state is cleared before validating the input or attempting the match.
*/
public boolean matches(CharSequence input, int start, int end) {
clear();
return matched = matcher.matchIntoBounded(input, start, end, starts, ends, workspace);
}

public int start() {
requireMatched();
return starts[0];
}

public int end() {
requireMatched();
return ends[0];
}

public int start(String name) {
requireMatched();
return starts[matcher.groupIndex(name)];
}

public int end(String name) {
requireMatched();
return ends[matcher.groupIndex(name)];
}

private void clear() {
Arrays.fill(starts, -1);
Arrays.fill(ends, -1);
matched = false;
}

private void requireMatched() {
if (!matched) {
throw new IllegalStateException("there is no current match");
}
}
}
Loading