Skip to content
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

[DRAFT] Experimental conversion of Regex to BNF #803

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ let package = Package(
"_RegexParser",
"_StringProcessing"
]),
.executableTarget(
name: "Regex2BNF",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"_RegexParser"
],
swiftSettings: [availabilityDefinition]),
.executableTarget(
name: "RegexTester",
dependencies: [
Expand Down
89 changes: 89 additions & 0 deletions Sources/Regex2BNF/Regex2BNF.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import _RegexParser

@main
@available(SwiftStdlib 5.8, *)
struct Regex2BNF: ParsableCommand {
@Argument(help: "The regex pattern to convert to BNF.")
var pattern: String

@Flag(
name: [.customShort("e"), .customLong("examples")],
help: "Run several examples")
var runExamples = false

func convert(_ pattern: String) throws {
print("/\(pattern)/\n")
print(try _printAsBNF(inputRegex: pattern))
}

mutating func run() throws {
if runExamples {
// TODO: Turn into test cases
// print("[Examples")

// print("Single-scalar character literals:")
try convert("a")
try convert("Z")
try convert("あ")
try convert("日")
try convert("\u{301}")


// print("Multi-scalar character literals")
try convert("🧟‍♀️")
try convert("e\u{301}")

// print("Simple alternations")
try convert("a|b")
try convert("a|b|c|d")
try convert("a|🧟‍♀️\u{301}日|z")

// print("Simple quantifications")
try convert("a*")
try convert("a+")
try convert("a?")
try convert("a{2,10}")
try convert("a{,10}")
try convert("a{2,}")

// print("Grouping")
try convert("a(b|c)d")
try convert("a(?:b|c)d")
try convert("a(bcd|def(g|h)+)z")

// print("Dot")
try convert(".*")
try convert("(a|b)*.{3}(a|b)")

// print("Bultin character classes")
try convert(#"\(\d{3}\)\d{3}-\d{4}"#)
try convert(#"\s+keyword\s+"#)


// print("[Done]")

// Look at optimizer output, the quant child is very long
try convert("a(123456789)+b")

try convert("Hi the time right now is (AM|PM)")

try convert("a(b|c)*d{2,4}e?")
}
try convert(pattern)



}
}
152 changes: 76 additions & 76 deletions Sources/RegexBenchmark/BenchmarkChart.swift
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
//===----------------------------------------------------------------------===//
////===----------------------------------------------------------------------===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
////
////===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//#if os(macOS) && canImport(Charts)
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//import Charts
//import SwiftUI
//
// See https://swift.org/LICENSE.txt for license information
//struct BenchmarkChart: View {
// var comparisons: [BenchmarkResult.Comparison]
//
//===----------------------------------------------------------------------===//

#if os(macOS) && canImport(Charts)

import Charts
import SwiftUI

struct BenchmarkChart: View {
var comparisons: [BenchmarkResult.Comparison]

// Sort by normalized difference
var sortedComparisons: [BenchmarkResult.Comparison] {
comparisons.sorted { a, b in
a.normalizedDiff < b.normalizedDiff
}
}
var body: some View {
VStack(alignment: .leading) {
Chart {
ForEach(sortedComparisons) { comparison in
// Normalized runtime
BarMark(
x: .value("Name", comparison.name),
y: .value("Normalized runtime", comparison.normalizedDiff))
.foregroundStyle(LinearGradient(
colors: [.accentColor, comparison.diff?.seconds ?? 0 <= 0 ? .green : .yellow],
startPoint: .bottom,
endPoint: .top))
}
// Baseline
RuleMark(y: .value("Time", 1.0))
.foregroundStyle(.red)
.lineStyle(.init(lineWidth: 1, dash: [2]))
.annotation(position: .top, alignment: .leading) {
Text("Baseline").foregroundStyle(.red)
}

}
.frame(idealWidth: 800, idealHeight: 800)
.chartYScale(domain: 0...2.0)
.chartYAxis {
AxisMarks(values: .stride(by: 0.1))
}
.chartXAxis {
AxisMarks { value in
AxisGridLine()
AxisTick()
AxisValueLabel(value.as(String.self)!, orientation: .vertical)
}
}
}
}
}

struct BenchmarkResultApp: App {
static var comparisons: [BenchmarkResult.Comparison]?

var body: some Scene {
WindowGroup {
if let comparisons = Self.comparisons {
ScrollView {
BenchmarkChart(comparisons: comparisons)
}
} else {
Text("No data")
}
}
}
}

#endif
// // Sort by normalized difference
// var sortedComparisons: [BenchmarkResult.Comparison] {
// comparisons.sorted { a, b in
// a.normalizedDiff < b.normalizedDiff
// }
// }
// var body: some View {
// VStack(alignment: .leading) {
// Chart {
// ForEach(sortedComparisons) { comparison in
// // Normalized runtime
// BarMark(
// x: .value("Name", comparison.name),
// y: .value("Normalized runtime", comparison.normalizedDiff))
// .foregroundStyle(LinearGradient(
// colors: [.accentColor, comparison.diff?.seconds ?? 0 <= 0 ? .green : .yellow],
// startPoint: .bottom,
// endPoint: .top))
// }
// // Baseline
// RuleMark(y: .value("Time", 1.0))
// .foregroundStyle(.red)
// .lineStyle(.init(lineWidth: 1, dash: [2]))
// .annotation(position: .top, alignment: .leading) {
// Text("Baseline").foregroundStyle(.red)
// }
//
// }
// .frame(idealWidth: 800, idealHeight: 800)
// .chartYScale(domain: 0...2.0)
// .chartYAxis {
// AxisMarks(values: .stride(by: 0.1))
// }
// .chartXAxis {
// AxisMarks { value in
// AxisGridLine()
// AxisTick()
// AxisValueLabel(value.as(String.self)!, orientation: .vertical)
// }
// }
// }
// }
//}
//
//struct BenchmarkResultApp: App {
// static var comparisons: [BenchmarkResult.Comparison]?
//
// var body: some Scene {
// WindowGroup {
// if let comparisons = Self.comparisons {
// ScrollView {
// BenchmarkChart(comparisons: comparisons)
// }
// } else {
// Text("No data")
// }
// }
// }
//}
//
//#endif
20 changes: 10 additions & 10 deletions Sources/RegexBenchmark/BenchmarkResults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ extension BenchmarkRunner {
print(item)
}

#if os(macOS) && canImport(Charts)
if showChart {
print("""
=== Comparison chart =================================================================
Press Control-C to close...
""")
BenchmarkResultApp.comparisons = comparisons
BenchmarkResultApp.main()
}
#endif
// #if os(macOS) && canImport(Charts)
// if showChart {
// print("""
// === Comparison chart =================================================================
// Press Control-C to close...
// """)
// BenchmarkResultApp.comparisons = comparisons
// BenchmarkResultApp.main()
// }
// #endif
}

func saveComparisons(
Expand Down
Loading