regex/regex/internal/Match.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
a38196612dd92cbcf3e30043149c5c13f12c7f3c- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
//! Private Regex match implementation.
// Private immutable Match construction for the matcher.
// `bounds` holds 2 * (captureCount + 1) entries: the start and end Unicode
// scalar offsets of group g at 2g and 2g + 1 as a half-open span, both null
// when the group did not participate in the selected match. `texts` holds
// captureCount + 1 entries: the captured String of each participating
// group, possibly empty, or null. Group 0 is the whole match and always
// participates. A repeated group reports its last selected participation.
// `captureNames` and `groupNames` are the Pattern's capture metadata: the
// frozen Map from each name to its group and the frozen Array holding each
// group's name or null. The Match retains no subject and no compiled
// program, so the transfer family can snapshot it without matching again.
create: (transfer) => {
newMatch: (captureNames, groupNames, bounds, texts) => {
groupTotal: groupNames.size()
((bounds.size() == (2 * groupTotal)) && (texts.size() == groupTotal)).ifFalse(() => {
Error().signal()
})
capturedBounds: Array(...bounds)
capturedBounds.freeze()
capturedTexts: Array(...texts)
capturedTexts.freeze()
groupIndex: 0
while(groupIndex < groupTotal) {
first: capturedBounds[2 * groupIndex]
last: capturedBounds[2 * groupIndex + 1]
captured: capturedTexts[groupIndex]
absent: (first === null) && (last === null) && (captured === null)
present: Integer.recognizes(first) && Integer.recognizes(last) &&
String.recognizes(captured)
if(present) {
((first >= 0) && (first <= last) && (captured.size() == (last - first))).ifFalse(() => {
Error().signal()
})
}
((absent || present) && (present || (groupIndex > 0))).ifFalse(() => {
Error().signal()
})
groupIndex = groupIndex + 1
}
// Answers the group number selected by an Integer number or a capture name.
selectGroup: (key) => {
selected: null
if(Integer.recognizes(key)) {
if((key >= 0) && (key < groupTotal)) {
selected = key
}
}
if(String.recognizes(key)) {
if(captureNames.containsKey(key)) {
selected = captureNames[key]
}
}
if(selected === null) {
Error().signal()
}
selected
}
result: {
captureCount: groupTotal - 1
group: (key) => {
capturedTexts[selectGroup(key)]
}
start: (key) => {
capturedBounds[2 * selectGroup(key)]
}
end: (key) => {
capturedBounds[2 * selectGroup(key) + 1]
}
// Fresh snapshots: mutating them never alters the Match.
groups: () => {
Array(...capturedTexts)
}
namedGroups: () => {
snapshot: Map()
for(groupName in groupNames) {
(groupName === null).ifFalse(() => {
snapshot[groupName] = capturedTexts[captureNames[groupName]]
})
}
snapshot
}
}
// Minted as a frozen Regex transfer-family value with these slots.
transfer.mintMatch(result, capturedBounds, capturedTexts, groupNames)
}
{
newMatch: newMatch
}
}