Skip to content

Commit

Permalink
Add regex builder annotation in Regex and RegexModifier protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kodlian committed May 1, 2021
1 parent 34cfdfc commit a751196
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ This brings SwiftRegexDSL, a Declarative Structured Language for regular express
struct ThisIsARegex: Regex {
let shouldMatchLine: Bool

@RegexBuilder
var body: Regex {
"Hello"
WhiteSpace()
Expand Down Expand Up @@ -45,12 +44,11 @@ dependencies: [
```

## How to?
Like SwiftUI view you declare your regex as a struct preferably in a separate file and use an annotation `@RegexBuilder`.
Like SwiftUI view you declare your regex as a struct preferably in a separate file and use the body to build the regex.
```swift
import SwiftRegexDSL

struct MyRegex: Regex {
@RegexBuilder
var body: Regex {
Digit()
.oneOrMore()
Expand All @@ -60,6 +58,15 @@ struct MyRegex: Regex {

```

You can also annotate any variables or functions with `@RegexBuilder`:
```
@RegexBuilder
var digits: Regex {
Digit()
..exactly(10)
}
```

### Pattern
SwiftRegexDSL implements the most common pattern found in the [`ICU`](http://userguide.icu-project.org/strings/regexp) API supported by the Swift Foundation.

Expand All @@ -68,7 +75,6 @@ Most common character classes and special characters are supported such as `AnyC

Of course, you can add any strings in your regex body, either directly or by using the `Text` regex.
```swift
@RegexBuilder
var body: Regex {
"Title"
Text("-")
Expand All @@ -81,7 +87,6 @@ Take notice that a `String` is not a `Regex` component per se, but rather an exp
#### Quantifier
You can attach a quantifier using the `quantified(...)` modifier or any shortcuts `zeroOrMore`, `oneOrMore`, `zeroOrOne`, `exactly` to specify the number of occurrences a pattern should match.
```swift
@RegexBuilder
var body: Regex {
Text("-")
.zeroOrOne()
Expand Down Expand Up @@ -135,7 +140,6 @@ struct DomainRegex: Regex { ... }
struct ExtensionRegex: Regex { ... }

struct HostRegex: Regex {
@RegexBuilder
var body: Regex {
DomainRegex()
"."
Expand All @@ -153,7 +157,6 @@ struct TitleRegex: Regex {

let shouldStartWithDigit: Bool

@RegexBuilder
var body: Regex {
if shouldStartWithDigit {
Digit()
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftRegexDSL/Engine/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation
/// base Regular Expression protocol. The regex is built using other regex by composition in the body.
/// ```swift
/// struct MyRegex: Regex {
/// @RegexBuilder
/// var body: Regex {
/// Digit()
/// .oneOrMore()
Expand All @@ -20,7 +19,7 @@ import Foundation
/// ```
public protocol Regex {
/// Body of the regex that is built it using regex.
/// It is generally annotated with a `@RegexBuilder` property to apply the SwiftRegexDSL to the expression.
@RegexBuilder
var body: Regex { get }

/// Applies a modifier to a regex and returns a new regex.
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftRegexDSL/Engine/RegexModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation

/// A modifier that you apply to a regex or another regex modifier, producing a different version of the original value.
public protocol RegexModifier {
@RegexBuilder
func body(content: Regex) -> Regex
}

Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftRegexDSL/Group/PatternOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public struct OptionsModifier: RegexModifier {
let enabledOptions: PatternOptions
let disabledOptions: PatternOptions

@RegexBuilder
public func body(content: Regex) -> Regex {
UnsafeText("(?")
if !enabledOptions.isEmpty {
Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftRegexDSL/Quantifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public struct QuantifiedPattern: Regex {
let base: Regex
let quantifier: Quantifier

@RegexBuilder
public var body: Regex {
base
UnsafeText(quantifier.value)
Expand Down
4 changes: 0 additions & 4 deletions Tests/SwiftRegexDSLTests/ControlFlowAndLoopTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ final class ControlFlowAndLoopTests: XCTestCase {
struct OptionalContent: Regex {
let content: Regex?

@RegexBuilder
var body: Regex {
"a"
if let content = content {
Expand All @@ -32,7 +31,6 @@ final class ControlFlowAndLoopTests: XCTestCase {
struct OptionalContent: Regex {
let content: Regex?

@RegexBuilder
var body: Regex {
"a"
content
Expand All @@ -46,7 +44,6 @@ final class ControlFlowAndLoopTests: XCTestCase {

func testForLoop() {
struct LoopRegex: Regex {
@RegexBuilder
var body: Regex {
for character in ["a", "b", "c", "d"] {
character
Expand All @@ -61,7 +58,6 @@ final class ControlFlowAndLoopTests: XCTestCase {
struct IfElseRegex: Regex {
let displayNumber: Bool

@RegexBuilder
var body: Regex {
"a"
if displayNumber {
Expand Down

0 comments on commit a751196

Please sign in to comment.