1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// swift run PatternConverter <regex>
import ArgumentParser
import _RegexParser
@_spi(PatternConverter) import _StringProcessing
@main
struct PatternConverter: ParsableCommand {
@Argument(help: "The regex to convert")
var regex: String
@Flag(help: "Whether to use the experimental syntax")
var experimentalSyntax: Bool = false
@Flag(help: "Whether to show rendered source ranges")
var renderSourceRanges: Bool = false
@Flag(help: "Whether to show canonical regex literal")
var showCanonical: Bool = false
@Flag(help: "Whether to skip result builder DSL")
var skipDSL: Bool = false
@Option(help: "Limit (from top-down) the conversion levels")
var topDownConversionLimit: Int?
@Option(help: "(TODO) Limit (from bottom-up) the conversion levels")
var bottomUpConversionLimit: Int?
func run() throws {
print("""
NOTE: This tool is experimental and its output is not
necessarily compilable.
""")
let delim = experimentalSyntax ? "|" : "/"
print("Converting '\(delim)\(regex)\(delim)'")
let ast = try _RegexParser.parse(
regex, experimentalSyntax ? .experimental : .traditional)
// Show rendered source ranges
if renderSourceRanges {
print()
print(regex)
print(ast._render(in: regex).joined(separator: "\n"))
print()
}
if showCanonical {
print("Canonical:")
print()
print(ast.renderAsCanonical())
print()
}
print()
if !skipDSL {
let render = renderAsBuilderDSL(
ast: ast,
maxTopDownLevels: topDownConversionLimit,
minBottomUpLevels: bottomUpConversionLimit
)
print(render)
}
return
}
}
|