Skip to content

Parse PCRE callouts, backtracking directives, and .NET balanced captures #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2022
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
14 changes: 14 additions & 0 deletions Sources/_MatchingEngine/Regex/AST/AST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ extension AST {

return self.children?.any(\.hasCapture) ?? false
}

/// Whether this AST node may be used as the operand of a quantifier such as
/// `?`, `+` or `*`.
public var isQuantifiable: Bool {
switch self {
case .atom(let a):
return a.isQuantifiable
case .group, .conditional, .customCharacterClass:
return true
case .alternation, .concatenation, .quantification, .quote, .trivia,
.empty, .groupTransform:
return false
}
}
Copy link
Member

@milseman milseman Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used to emit a diagnostic for backreference directives that aren't quantifiable:

aea6d9d#diff-4f8c3a04c147c57dc383b837f459ee51c8bec6de78d061d54ac7e228f5228dc5R1328-R1334

}

// MARK: - AST types
Expand Down
75 changes: 73 additions & 2 deletions Sources/_MatchingEngine/Regex/AST/Atom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ extension AST {
// References
case backreference(Reference)
case subpattern(Reference)

// (?C)
case callout(Callout)

// (*ACCEPT), (*FAIL), ...
case backtrackingDirective(BacktrackingDirective)
}
}
}
Expand Down Expand Up @@ -443,6 +449,59 @@ extension AST.Atom {
}
}

extension AST.Atom {
public struct Callout: Hashable {
public enum Argument: Hashable {
case number(Int)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these references? That is, could they be relative or are they always absolute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they're just plain arguments to the callout function. As I understand it, PCRE expects you to give it a single callout function, and it gets called with the argument specified to let you differentiate which call it is.

case string(String)
}
public var arg: AST.Located<Argument>
public init(_ arg: AST.Located<Argument>) {
self.arg = arg
}
}
}

extension AST.Atom {
public struct BacktrackingDirective: Hashable {
public enum Kind: Hashable {
/// (*ACCEPT)
case accept

/// (*FAIL)
case fail

/// (*MARK:NAME)
case mark

/// (*COMMIT)
case commit

/// (*PRUNE)
case prune

/// (*SKIP)
case skip

/// (*THEN)
case then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a newline to separate cases? Is there anywhere in the repo where these terms or concepts are defined or have a quick blurb?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, and no there currently is not. Should I elaborate on the comments here? Or split out a separate document to explain some of the more obscure features?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the comment at first, but I do think a document sounds like a really good idea. That can serve as fodder for pitches too. I suspect the very first fully-realized pitch will be the literal-interior syntax.

}
public var kind: AST.Located<Kind>
public var name: AST.Located<String>?

public init(_ kind: AST.Located<Kind>, name: AST.Located<String>?) {
self.kind = kind
self.name = name
}

public var isQuantifiable: Bool {
// As per http://pcre.org/current/doc/html/pcre2pattern.html#SEC29, only
// (*ACCEPT) is quantifiable.
kind.value == .accept
}
}
}

extension AST.Atom {
/// Retrieve the character value of the atom if it represents a literal
/// character or unicode scalar, nil otherwise.
Expand All @@ -458,7 +517,8 @@ extension AST.Atom {
fallthrough

case .property, .escaped, .any, .startOfLine, .endOfLine,
.backreference, .subpattern, .namedCharacter:
.backreference, .subpattern, .namedCharacter, .callout,
.backtrackingDirective:
return nil
}
}
Expand All @@ -483,10 +543,21 @@ extension AST.Atom {
return "\\M-\\C-\(x)"

case .property, .escaped, .any, .startOfLine, .endOfLine,
.backreference, .subpattern, .namedCharacter:
.backreference, .subpattern, .namedCharacter, .callout,
.backtrackingDirective:
return nil
}
}

public var isQuantifiable: Bool {
switch kind {
case .backtrackingDirective(let b):
return b.isQuantifiable
// TODO: Are callouts quantifiable?
default:
return true
}
}
}

extension AST {
Expand Down
27 changes: 26 additions & 1 deletion Sources/_MatchingEngine/Regex/AST/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ extension AST {
// (?<name>...) (?'name'...) (?P<name>...)
case namedCapture(Located<String>)

// (?<name-priorName>) (?'name-priorName')
case balancedCapture(BalancedCapture)

// (?:...)
case nonCapture

Expand Down Expand Up @@ -79,7 +82,7 @@ extension AST {
extension AST.Group.Kind {
public var isCapturing: Bool {
switch self {
case .capture, .namedCapture: return true
case .capture, .namedCapture, .balancedCapture: return true
default: return false
}
}
Expand All @@ -103,6 +106,7 @@ extension AST.Group.Kind {
public var name: String? {
switch self {
case .namedCapture(let name): return name.value
case .balancedCapture(let b): return b.name?.value
default: return nil
}
}
Expand All @@ -121,5 +125,26 @@ extension AST.Group {
default: return nil
}
}
}

extension AST.Group {
public struct BalancedCapture: Hashable {
/// The name of the group, or nil if the group has no name.
public var name: AST.Located<String>?

/// The location of the `-` in the group.
public var dash: SourceLocation

/// The name of the prior group that the balancing group references.
public var priorName: AST.Located<String>

public init(
name: AST.Located<String>?, dash: SourceLocation,
priorName: AST.Located<String>
) {
self.name = name
self.dash = dash
self.priorName = priorName
}
}
}
3 changes: 3 additions & 0 deletions Sources/_MatchingEngine/Regex/Parse/CaptureStructure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ extension AST {
return .atom() + innerCaptures
case .namedCapture(let name):
return .atom(name: name.value) + innerCaptures
case .balancedCapture(let b):
return .atom(name: b.name?.value) + innerCaptures
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @rxwei who is fixing some bugs around this area

default:
precondition(!group.kind.value.isCapturing)
return innerCaptures
}
case .conditional(let c):
Expand Down
20 changes: 20 additions & 0 deletions Sources/_MatchingEngine/Regex/Parse/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ enum ParseError: Error, Hashable {

case cannotReferToWholePattern

case notQuantifiable

case backtrackingDirectiveMustHaveName(String)

case unknownGroupKind(String)
case unknownCalloutKind(String)

case invalidMatchingOption(Character)
case cannotRemoveMatchingOptionsAfterCaret
Expand All @@ -50,6 +55,9 @@ enum ParseError: Error, Hashable {
case emptyProperty

case expectedGroupSpecifier
case expectedGroupName
case groupNameMustBeAlphaNumeric
case groupNameCannotStartWithNumber
case cannotRemoveTextSegmentOptions
}

Expand Down Expand Up @@ -80,12 +88,18 @@ extension ParseError: CustomStringConvertible {
return "expected escape sequence"
case .cannotReferToWholePattern:
return "cannot refer to whole pattern here"
case .notQuantifiable:
return "expression is not quantifiable"
case .backtrackingDirectiveMustHaveName(let b):
return "backtracking directive '\(b)' must include name"
case let .tooManyBranchesInConditional(i):
return "expected 2 branches in conditional, have \(i)"
case let .unsupportedCondition(str):
return "\(str) cannot be used as condition"
case let .unknownGroupKind(str):
return "unknown group kind '(\(str)'"
case let .unknownCalloutKind(str):
return "unknown callout kind '\(str)'"
case let .invalidMatchingOption(c):
return "invalid matching option '\(c)'"
case .cannotRemoveMatchingOptionsAfterCaret:
Expand All @@ -102,6 +116,12 @@ extension ParseError: CustomStringConvertible {
return "empty property"
case .expectedGroupSpecifier:
return "expected group specifier"
case .expectedGroupName:
return "expected group name"
case .groupNameMustBeAlphaNumeric:
return "group name must only contain alphanumeric characters"
case .groupNameCannotStartWithNumber:
return "group name must not start with number"
case .cannotRemoveTextSegmentOptions:
return "text segment mode cannot be unset, only changed"
}
Expand Down
Loading