Skip to content

Add repeatMatch functions to DSL #161

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
Feb 16, 2022
Merged

Conversation

natecook1000
Copy link
Member

These allow you to spell literal expressions like a{3,} or a{2,5} when using the DSL. I've gone with the repeatMatch name to align a bit with repeatElement in the stdlib, and because repeating(...) sounds too much to my ear like you have to repeat the matched text, not repeat the match, if that makes sense.

Usage:

let regex = Regex {
    repeatMatch(2...) {
        repeatMatch(CharacterClass.word, count: 3)
        CharacterClass.digit
    }
}
"abc1".matches(regex)        // false
"abc1def2".matches(regex)    // true
"abc1defg2".matches(regex)   // false

These allow you to spell literal expressions like `a{3,}` or `a{2,5}`
@natecook1000 natecook1000 marked this pull request as draft February 14, 2022 21:42
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, I'll let @rxwei comment on generator script.

My vote is for

repeat {
  ...
}

repeat(3...) {
  ...
}

I think repeat as a verb is fine alongside capture, but I'm not a picky grammar namer.

Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

LGTM. I'd vote for repeat as the name.

@@ -145,6 +145,34 @@ postfix operator .?
postfix operator .*
postfix operator .+

/// Generates a DSLTree node for a repeated range of the given DSLTree node.
/// Individual public API functions are in the generated Variadics.swift file.
internal func _repeatingNode(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: How about a static method DSLTree.Node.repeating(_:_:_:)

@natecook1000
Copy link
Member Author

I think repeat as a verb is fine alongside capture, but I'm not a picky grammar namer.

repeat is out, unfortunately, since it's a keyword, and people would need to write e.g. `repeat`(3...) { ... }. I've gone with repeating for now.

@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000 natecook1000 marked this pull request as ready for review February 16, 2022 16:36
@natecook1000 natecook1000 merged commit 52da66c into swiftlang:main Feb 16, 2022
@natecook1000 natecook1000 deleted the repeating branch February 16, 2022 16:36
itingliu pushed a commit to itingliu/swift-experimental-string-processing that referenced this pull request Feb 22, 2022
These allow you to spell literal expressions like `a{3,}` or `a{2,5}`
while using the Regex DSL.
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