Skip to content

Add assertions to the DSL #154

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 8 commits into from
Feb 21, 2022
Merged

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented Feb 10, 2022

This adds an Anchor type that handles the different kinds of assertions supported by regular expression literals. Some spelling are different — instead of separate \w and \W assertions, this design provides just Anchor.wordBoundary as well as an inverted property that is available on all assertions. (This property should perhaps be named negated instead.) That is, \W in a regex literal maps to Anchor.wordBoundary.inverted.

A lookahead(isNegative:) function provides the positive/negative lookahead functionality from regex literals.

Comment on lines 47 to 53
case .startOfSubject: fatalError("Not yet supported")
case .endOfSubjectBeforeNewline: fatalError("Not yet supported")
case .endOfSubject: fatalError("Not yet supported")
case .firstMatchingPositionInSubject: fatalError("Not yet supported")
case .textSegmentBoundary: return .notTextSegment
case .startOfLine: fatalError("Not yet supported")
case .endOfLine: fatalError("Not yet supported")
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't currently have a representation for these negated assertions in the AST, since things like notWordBoundary are represented as specific individual cases. These fatalError'd cases don't have a regex literal equivalent, but would be available if we use an API like Assertion.wordBoundary.inverted.

Copy link
Member

Choose a reason for hiding this comment

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

Why are we going to an AST here?

Copy link
Member Author

Choose a reason for hiding this comment

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

DSLTree tracks assertions using AST.Atom.AssertionKind right now.

@natecook1000 natecook1000 requested a review from rxwei February 10, 2022 17:38
case endOfLine
case wordBoundary
case lookahead(DSLTree.Node)
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this meant to be a listing of built-in assertions, or are each of these the kinds of assertions someone could write?

Comment on lines 47 to 53
case .startOfSubject: fatalError("Not yet supported")
case .endOfSubjectBeforeNewline: fatalError("Not yet supported")
case .endOfSubject: fatalError("Not yet supported")
case .firstMatchingPositionInSubject: fatalError("Not yet supported")
case .textSegmentBoundary: return .notTextSegment
case .startOfLine: fatalError("Not yet supported")
case .endOfLine: fatalError("Not yet supported")
Copy link
Member

Choose a reason for hiding this comment

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

Why are we going to an AST here?

("aaaaabc", nil),
captureType: Substring.self, ==)
{
Assertion.startOfLine
Copy link
Member

Choose a reason for hiding this comment

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

Many of the built-in ones are more commonly called "anchors", which might be worth considering too.

@milseman milseman marked this pull request as draft February 11, 2022 00:42
`lookahead` is moved to a free function, since it isn't an anchor like the others
@natecook1000 natecook1000 marked this pull request as ready for review February 18, 2022 21:02
Copy link
Member

@milseman milseman left a comment

Choose a reason for hiding this comment

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

LGTM.

var result = self
result.isInverted.toggle()
return result
}
Copy link
Member

Choose a reason for hiding this comment

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

Would we want an isInverted then? Also, is it the case that all anchors can be inverted?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know that a property makes sense if we aren't going to also expose kind as public API, and the purpose of this is just to carry the regex wrapper.

Copy link
Member Author

Choose a reason for hiding this comment

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

Everything in this PR can be inverted, just need a little more plumbing. If we want to provide the functionality of a "reset match" assertion, that could just be a separate function or type, since it isn't an anchor anyway.

@natecook1000
Copy link
Member Author

@swift-ci Please test Linux platform

@natecook1000 natecook1000 changed the title [Draft] Add assertions to the DSL Add assertions to the DSL Feb 21, 2022
}

public func lookahead<R: RegexProtocol>(
isNegative: Bool = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

While generally Boolean properties should read like an assertion about the receiver, I'm not sure it's quite as useful for function parameters. In this case, isNegative is not forming a phrase with the base name to produce Boolean result, but a dictation by the caller to modify the behavior of the callee. As such, I wonder if we should call it negative instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, this is like the second parameter in split(separator: "-", omittingEmptySubsequences: false). 👍🏻

@natecook1000
Copy link
Member Author

@swift-ci Please test Linux platform

@natecook1000
Copy link
Member Author

@swift-ci Please test Linux platform

@natecook1000 natecook1000 merged commit f8e1dc2 into swiftlang:main Feb 21, 2022
@natecook1000 natecook1000 deleted the dsl_assertions branch February 21, 2022 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants