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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// TODO: Round-tripping tests
extension AST {
/// Renders using Swift's preferred regex literal syntax.
public func renderAsCanonical(
showDelimiters delimiters: Bool = false,
terminateLine: Bool = false
) -> String {
var printer = PrettyPrinter()
printer.printAsCanonical(
self,
delimiters: delimiters,
terminateLine: terminateLine)
return printer.finish()
}
}
extension AST.Node {
/// Renders using Swift's preferred regex literal syntax.
public func renderAsCanonical(
showDelimiters delimiters: Bool = false,
terminateLine: Bool = false
) -> String {
AST(self, globalOptions: nil, diags: Diagnostics()).renderAsCanonical(
showDelimiters: delimiters, terminateLine: terminateLine)
}
}
extension PrettyPrinter {
/// Outputs a regular expression abstract syntax tree in canonical form,
/// indenting and terminating the line, and updating its internal state.
///
/// - Parameter ast: The abstract syntax tree of the regular expression being output.
/// - Parameter delimiters: Whether to include commas between items.
/// - Parameter terminateLine: Whether to include terminate the line.
public mutating func printAsCanonical(
_ ast: AST,
delimiters: Bool = false,
terminateLine terminate: Bool = true
) {
indent()
if delimiters { output("'/") }
if let opts = ast.globalOptions {
outputAsCanonical(opts)
}
outputAsCanonical(ast.root)
if delimiters { output("/'") }
if terminate {
terminateLine()
}
}
/// Outputs a regular expression abstract syntax tree in canonical form,
/// without indentation, line termation, or affecting its internal state.
mutating func outputAsCanonical(_ ast: AST.Node) {
switch ast {
case let .alternation(a):
for idx in a.children.indices {
outputAsCanonical(a.children[idx])
if a.children.index(after: idx) != a.children.endIndex {
output("|")
}
}
case let .concatenation(c):
c.children.forEach { outputAsCanonical($0) }
case let .group(g):
output(g.kind.value._canonicalBase)
outputAsCanonical(g.child)
output(")")
case let .conditional(c):
output("(")
outputAsCanonical(c.condition)
outputAsCanonical(c.trueBranch)
output("|")
outputAsCanonical(c.falseBranch)
case let .quantification(q):
outputAsCanonical(q.child)
output(q.amount.value._canonicalBase)
output(q.kind.value._canonicalBase)
case let .quote(q):
output(q._canonicalBase)
case let .trivia(t):
output(t._canonicalBase)
case let .interpolation(i):
output(i._canonicalBase)
case let .atom(a):
output(a._canonicalBase)
case let .customCharacterClass(ccc):
outputAsCanonical(ccc)
case let .absentFunction(abs):
outputAsCanonical(abs)
case .empty:
output("")
}
}
mutating func outputAsCanonical(
_ ccc: AST.CustomCharacterClass
) {
output(ccc.start.value._canonicalBase)
ccc.members.forEach { outputAsCanonical($0) }
output("]")
}
mutating func outputAsCanonical(
_ member: AST.CustomCharacterClass.Member
) {
// TODO: Do we need grouping or special escape rules?
switch member {
case .custom(let ccc):
outputAsCanonical(ccc)
case .range(let r):
output(r.lhs._canonicalBase)
output("-")
output(r.rhs._canonicalBase)
case .atom(let a):
output(a._canonicalBase)
case .quote(let q):
output(q._canonicalBase)
case .trivia(let t):
output(t._canonicalBase)
case .setOperation:
output("/* TODO: set operation \(self) */")
}
}
mutating func outputAsCanonical(_ condition: AST.Conditional.Condition) {
output("(/*TODO: conditional \(condition) */)")
}
mutating func outputAsCanonical(_ abs: AST.AbsentFunction) {
output("(?~")
switch abs.kind {
case .repeater(let a):
outputAsCanonical(a)
case .expression(let a, _, let child):
output("|")
outputAsCanonical(a)
output("|")
outputAsCanonical(child)
case .stopper(let a):
output("|")
outputAsCanonical(a)
case .clearer:
output("|")
}
output(")")
}
mutating func outputAsCanonical(_ opts: AST.GlobalMatchingOptionSequence) {
for opt in opts.options {
output(opt._canonicalBase)
}
}
}
extension AST.Quote {
var _canonicalBase: String {
// TODO: Is this really what we want?
"\\Q\(literal)\\E"
}
}
extension AST.Interpolation {
var _canonicalBase: String {
"<{\(contents)}>"
}
}
extension AST.Group.Kind {
var _canonicalBase: String {
switch self {
case .capture: return "("
case .namedCapture(let n): return "(?<\(n.value)>"
case .balancedCapture(let b): return "(?<\(b._canonicalBase)>"
case .nonCapture: return "(?:"
case .nonCaptureReset: return "(?|"
case .atomicNonCapturing: return "(?>"
case .lookahead: return "(?="
case .negativeLookahead: return "(?!"
case .nonAtomicLookahead: return "(?*"
case .lookbehind: return "(?<="
case .negativeLookbehind: return "(?<!"
case .nonAtomicLookbehind: return "(?<*"
case .scriptRun: return "(*sr:"
case .atomicScriptRun: return "(*asr:"
case .changeMatchingOptions:
return "(/* TODO: matchign options in canonical form */"
}
}
}
extension AST.Quantification.Amount {
var _canonicalBase: String {
switch self {
case .zeroOrMore: return "*"
case .oneOrMore: return "+"
case .zeroOrOne: return "?"
case let .exactly(n): return "{\(n._canonicalBase)}"
case let .nOrMore(n): return "{\(n._canonicalBase),}"
case let .upToN(n): return "{,\(n._canonicalBase)}"
case let .range(lower, upper):
return "{\(lower),\(upper)}"
}
}
}
extension AST.Quantification.Kind {
var _canonicalBase: String { self.rawValue }
}
extension AST.Atom.Number {
var _canonicalBase: String {
value.map { "\($0)" } ?? "<#number#>"
}
}
extension AST.Atom {
var _canonicalBase: String {
if let lit = self.literalStringValue {
// FIXME: We may have to re-introduce escapes
// For example, `\.` will come back as "." instead
// For now, catch the most common offender
if lit == "." { return "\\." }
return lit
}
switch self.kind {
case .caretAnchor:
return "^"
case .dollarAnchor:
return "$"
case .escaped(let e):
return "\\\(e.character)"
case .backreference(let br):
return br._canonicalBase
default:
return "/* TODO: atom \(self) */"
}
}
}
extension AST.Reference {
var _canonicalBase: String {
if self.recursesWholePattern {
return "(?R)"
}
switch kind {
case .absolute(let i):
// TODO: Which should we prefer, this or `\g{n}`?
return "\\\(i)"
case .relative:
return "/* TODO: relative reference \(self) */"
case .named:
return "/* TODO: named reference \(self) */"
}
}
}
extension AST.CustomCharacterClass.Start {
var _canonicalBase: String { self.rawValue }
}
extension AST.Group.BalancedCapture {
var _canonicalBase: String {
"\(name?.value ?? "")-\(priorName.value)"
}
}
extension AST.GlobalMatchingOption.NewlineMatching {
var _canonicalBase: String {
switch self {
case .carriageReturnOnly: return "CR"
case .linefeedOnly: return "LF"
case .carriageAndLinefeedOnly: return "CRLF"
case .anyCarriageReturnOrLinefeed: return "ANYCRLF"
case .anyUnicode: return "ANY"
case .nulCharacter: return "NUL"
}
}
}
extension AST.GlobalMatchingOption.NewlineSequenceMatching {
var _canonicalBase: String {
switch self {
case .anyCarriageReturnOrLinefeed: return "BSR_ANYCRLF"
case .anyUnicode: return "BSR_UNICODE"
}
}
}
extension AST.GlobalMatchingOption.Kind {
var _canonicalBase: String {
switch self {
case .limitDepth(let i): return "LIMIT_DEPTH=\(i._canonicalBase)"
case .limitHeap(let i): return "LIMIT_HEAP=\(i._canonicalBase)"
case .limitMatch(let i): return "LIMIT_MATCH=\(i._canonicalBase)"
case .notEmpty: return "NOTEMPTY"
case .notEmptyAtStart: return "NOTEMPTY_ATSTART"
case .noAutoPossess: return "NO_AUTO_POSSESS"
case .noDotStarAnchor: return "NO_DOTSTAR_ANCHOR"
case .noJIT: return "NO_JIT"
case .noStartOpt: return "NO_START_OPT"
case .utfMode: return "UTF"
case .unicodeProperties: return "UCP"
case .newlineMatching(let m): return m._canonicalBase
case .newlineSequenceMatching(let m): return m._canonicalBase
}
}
}
extension AST.GlobalMatchingOption {
var _canonicalBase: String { "(*\(kind._canonicalBase))"}
}
extension AST.Trivia {
var _canonicalBase: String {
// TODO: We might want to output comments...
""
}
}
|