Skip to content

Commit 05c73ac

Browse files
committed
Prototype of experimental Regex -> BNF conversion
1 parent 4e10f38 commit 05c73ac

File tree

7 files changed

+839
-86
lines changed

7 files changed

+839
-86
lines changed

Package.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ let package = Package(
135135
"_RegexParser",
136136
"_StringProcessing"
137137
]),
138+
.executableTarget(
139+
name: "Regex2BNF",
140+
dependencies: [
141+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
142+
"_RegexParser"
143+
],
144+
swiftSettings: [availabilityDefinition]),
138145
.executableTarget(
139146
name: "RegexTester",
140147
dependencies: [

Sources/Regex2BNF/Regex2BNF.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import ArgumentParser
13+
import _RegexParser
14+
15+
@main
16+
@available(SwiftStdlib 5.8, *)
17+
struct Regex2BNF: ParsableCommand {
18+
@Argument(help: "The regex pattern to convert to BNF.")
19+
var pattern: String
20+
21+
@Flag(
22+
name: [.customShort("e"), .customLong("examples")],
23+
help: "Run several examples")
24+
var runExamples = false
25+
26+
func convert(_ pattern: String) throws {
27+
print("\n=== /\(pattern)/ ===\n")
28+
let ast = try _RegexParser.parse(pattern, .init())
29+
print(ast)
30+
print()
31+
print(try _printAsBNF(inputRegex: pattern))
32+
}
33+
34+
mutating func run() throws {
35+
if runExamples {
36+
// TODO: Turn into test cases
37+
print("[Examples")
38+
39+
print("Single-scalar character literals:")
40+
try convert("a")
41+
try convert("Z")
42+
try convert("")
43+
try convert("")
44+
try convert("\u{301}")
45+
46+
47+
print("Multi-scalar character literals")
48+
try convert("🧟‍♀️")
49+
try convert("e\u{301}")
50+
51+
print("Simple alternations")
52+
try convert("a|b")
53+
try convert("a|b|c|d")
54+
try convert("a|🧟‍♀️\u{301}日|z")
55+
56+
print("Simple quantifications")
57+
try convert("a*")
58+
try convert("a+")
59+
try convert("a?")
60+
try convert("a{2,10}")
61+
try convert("a{,10}")
62+
try convert("a{2,}")
63+
64+
print("Grouping")
65+
try convert("a(b|c)d")
66+
try convert("a(bcd|def(g|h)+)z")
67+
68+
print("Dot")
69+
// try convert(".*")
70+
// try convert("(a|b)*.{3}(a|b)")
71+
72+
73+
print("[Done]")
74+
}
75+
try convert(pattern)
76+
77+
78+
79+
}
80+
}
Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
//===----------------------------------------------------------------------===//
1+
////===----------------------------------------------------------------------===//
2+
////
3+
//// This source file is part of the Swift.org open source project
4+
////
5+
//// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
6+
//// Licensed under Apache License v2.0 with Runtime Library Exception
7+
////
8+
//// See https://swift.org/LICENSE.txt for license information
9+
////
10+
////===----------------------------------------------------------------------===//
211
//
3-
// This source file is part of the Swift.org open source project
12+
//#if os(macOS) && canImport(Charts)
413
//
5-
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
6-
// Licensed under Apache License v2.0 with Runtime Library Exception
14+
//import Charts
15+
//import SwiftUI
716
//
8-
// See https://swift.org/LICENSE.txt for license information
17+
//struct BenchmarkChart: View {
18+
// var comparisons: [BenchmarkResult.Comparison]
919
//
10-
//===----------------------------------------------------------------------===//
11-
12-
#if os(macOS) && canImport(Charts)
13-
14-
import Charts
15-
import SwiftUI
16-
17-
struct BenchmarkChart: View {
18-
var comparisons: [BenchmarkResult.Comparison]
19-
20-
// Sort by normalized difference
21-
var sortedComparisons: [BenchmarkResult.Comparison] {
22-
comparisons.sorted { a, b in
23-
a.normalizedDiff < b.normalizedDiff
24-
}
25-
}
26-
var body: some View {
27-
VStack(alignment: .leading) {
28-
Chart {
29-
ForEach(sortedComparisons) { comparison in
30-
// Normalized runtime
31-
BarMark(
32-
x: .value("Name", comparison.name),
33-
y: .value("Normalized runtime", comparison.normalizedDiff))
34-
.foregroundStyle(LinearGradient(
35-
colors: [.accentColor, comparison.diff?.seconds ?? 0 <= 0 ? .green : .yellow],
36-
startPoint: .bottom,
37-
endPoint: .top))
38-
}
39-
// Baseline
40-
RuleMark(y: .value("Time", 1.0))
41-
.foregroundStyle(.red)
42-
.lineStyle(.init(lineWidth: 1, dash: [2]))
43-
.annotation(position: .top, alignment: .leading) {
44-
Text("Baseline").foregroundStyle(.red)
45-
}
46-
47-
}
48-
.frame(idealWidth: 800, idealHeight: 800)
49-
.chartYScale(domain: 0...2.0)
50-
.chartYAxis {
51-
AxisMarks(values: .stride(by: 0.1))
52-
}
53-
.chartXAxis {
54-
AxisMarks { value in
55-
AxisGridLine()
56-
AxisTick()
57-
AxisValueLabel(value.as(String.self)!, orientation: .vertical)
58-
}
59-
}
60-
}
61-
}
62-
}
63-
64-
struct BenchmarkResultApp: App {
65-
static var comparisons: [BenchmarkResult.Comparison]?
66-
67-
var body: some Scene {
68-
WindowGroup {
69-
if let comparisons = Self.comparisons {
70-
ScrollView {
71-
BenchmarkChart(comparisons: comparisons)
72-
}
73-
} else {
74-
Text("No data")
75-
}
76-
}
77-
}
78-
}
79-
80-
#endif
20+
// // Sort by normalized difference
21+
// var sortedComparisons: [BenchmarkResult.Comparison] {
22+
// comparisons.sorted { a, b in
23+
// a.normalizedDiff < b.normalizedDiff
24+
// }
25+
// }
26+
// var body: some View {
27+
// VStack(alignment: .leading) {
28+
// Chart {
29+
// ForEach(sortedComparisons) { comparison in
30+
// // Normalized runtime
31+
// BarMark(
32+
// x: .value("Name", comparison.name),
33+
// y: .value("Normalized runtime", comparison.normalizedDiff))
34+
// .foregroundStyle(LinearGradient(
35+
// colors: [.accentColor, comparison.diff?.seconds ?? 0 <= 0 ? .green : .yellow],
36+
// startPoint: .bottom,
37+
// endPoint: .top))
38+
// }
39+
// // Baseline
40+
// RuleMark(y: .value("Time", 1.0))
41+
// .foregroundStyle(.red)
42+
// .lineStyle(.init(lineWidth: 1, dash: [2]))
43+
// .annotation(position: .top, alignment: .leading) {
44+
// Text("Baseline").foregroundStyle(.red)
45+
// }
46+
//
47+
// }
48+
// .frame(idealWidth: 800, idealHeight: 800)
49+
// .chartYScale(domain: 0...2.0)
50+
// .chartYAxis {
51+
// AxisMarks(values: .stride(by: 0.1))
52+
// }
53+
// .chartXAxis {
54+
// AxisMarks { value in
55+
// AxisGridLine()
56+
// AxisTick()
57+
// AxisValueLabel(value.as(String.self)!, orientation: .vertical)
58+
// }
59+
// }
60+
// }
61+
// }
62+
//}
63+
//
64+
//struct BenchmarkResultApp: App {
65+
// static var comparisons: [BenchmarkResult.Comparison]?
66+
//
67+
// var body: some Scene {
68+
// WindowGroup {
69+
// if let comparisons = Self.comparisons {
70+
// ScrollView {
71+
// BenchmarkChart(comparisons: comparisons)
72+
// }
73+
// } else {
74+
// Text("No data")
75+
// }
76+
// }
77+
// }
78+
//}
79+
//
80+
//#endif

Sources/RegexBenchmark/BenchmarkResults.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ extension BenchmarkRunner {
114114
print(item)
115115
}
116116

117-
#if os(macOS) && canImport(Charts)
118-
if showChart {
119-
print("""
120-
=== Comparison chart =================================================================
121-
Press Control-C to close...
122-
""")
123-
BenchmarkResultApp.comparisons = comparisons
124-
BenchmarkResultApp.main()
125-
}
126-
#endif
117+
// #if os(macOS) && canImport(Charts)
118+
// if showChart {
119+
// print("""
120+
// === Comparison chart =================================================================
121+
// Press Control-C to close...
122+
// """)
123+
// BenchmarkResultApp.comparisons = comparisons
124+
// BenchmarkResultApp.main()
125+
// }
126+
// #endif
127127
}
128128

129129
func saveComparisons(

0 commit comments

Comments
 (0)