Skip to content

Provide literal pattern for convertible regexes #670

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
Dec 13, 2023
Merged

Conversation

natecook1000
Copy link
Member

This adds an (SPI) API for converting a Regex instance to a regex literal when possible, whether the regex starts as a literal, a run-time-constructed regex, or using the RegexBuilder syntax. The _literalPattern is round-trippable, so a regex created from a _literalPattern will have that same _literalPattern, but it could be different from the original pattern used to create a regex.

This is an alternative to #667, in that a library using Regex as a dependency could create a composed printable regex type with the required semantics.

}
}

fileprivate struct LiteralPrinter {
Copy link
Member

Choose a reason for hiding this comment

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

Should this replace printAsCanonical, or merge them?

Copy link
Member Author

Choose a reason for hiding this comment

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

This doesn't necessarily need to replace printAsCanonical, since they work on different things. If we have the AST -> DSL conversion, though, then this should be able to print anything, and we wouldn't need the existing AST printer any more. Not sure if we need the headache of keeping both in sync

@@ -30,7 +44,8 @@ func _firstMatch(
) throws -> (String, [String?])? {
var regex = try Regex(regexStr, syntax: syntax).matchingSemantics(semanticLevel)
let result = try regex.firstMatch(in: input)

try _roundTripLiteral(regexStr, syntax: syntax)
Copy link
Member

Choose a reason for hiding this comment

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

Long term, we'll want to sink this deeper into the library.

For this, I think we'd also want to test parity for parse only tests.

@milseman
Copy link
Member

What needs to be done to get this further along?

@natecook1000 natecook1000 marked this pull request as ready for review July 6, 2023 18:39
@natecook1000 natecook1000 force-pushed the literal_printer branch 3 times, most recently from 00a7c42 to 2d1ba4d Compare July 6, 2023 19:59
@natecook1000
Copy link
Member Author

@swift-ci Please test

This adds an (SPI) API for converting a `Regex` instance to a regex
literal when possible, whether the regex starts as a literal, a
run-time-constructed regex, or using the RegexBuilder syntax.
The `_literalPattern` is round-trippable, so a regex created from
a `_literalPattern` will have that same `_literalPattern`, but it
could be different from the original pattern used to create a regex.
This adds another confirmation test, as part of the parse testing.
When a parsing test is expected to succeed, and contains supported
syntax, this checks that a round-tripped literal pattern maintains
its identity.
Add an example of a pattern that is different from, but equivalent
to, the original regex.
@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000
Copy link
Member Author

@swift-ci Please test Windows platform

@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000 natecook1000 merged commit b0cd8f1 into main Dec 13, 2023
@natecook1000 natecook1000 deleted the literal_printer branch December 13, 2023 20:26
natecook1000 added a commit that referenced this pull request Dec 13, 2023
This adds an (SPI) API for converting a `Regex` instance to a regex
literal when possible, whether the regex starts as a literal, a
run-time-constructed regex, or using the RegexBuilder syntax.
The `_literalPattern` is round-trippable, so a regex created from
a `_literalPattern` will have that same `_literalPattern`, but it
could be different from the original pattern used to create a regex.

This adds another confirmation test, as part of the parse testing.
When a parsing test is expected to succeed, and contains supported
syntax, this checks that a round-tripped literal pattern maintains
its identity.
natecook1000 added a commit that referenced this pull request Jan 9, 2024
This adds an (SPI) API for converting a `Regex` instance to a regex
literal when possible, whether the regex starts as a literal, a
run-time-constructed regex, or using the RegexBuilder syntax.
The `_literalPattern` is round-trippable, so a regex created from
a `_literalPattern` will have that same `_literalPattern`, but it
could be different from the original pattern used to create a regex.

This adds another confirmation test, as part of the parse testing.
When a parsing test is expected to succeed, and contains supported
syntax, this checks that a round-tripped literal pattern maintains
its identity.
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.

2 participants