File: Sema.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (466 lines) | stat: -rw-r--r-- 15,160 bytes parent folder | download
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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
//
//===----------------------------------------------------------------------===//

/// Validate a regex AST for semantic validity. Once bytecode is emitted at
/// compile time, this could potentially be subsumed by the bytecode generator.
fileprivate struct RegexValidator {
  let ast: AST
  let captures: CaptureList
  var diags = Diagnostics()

  init(_ ast: AST) {
    self.ast = ast
    self.captures = ast.captureList
  }

  mutating func error(_ kind: ParseError, at loc: SourceLocation) {
    diags.error(kind, at: loc)
  }
  mutating func unreachable(_ str: String, at loc: SourceLocation) {
    diags.fatal(.unreachable(str), at: loc)
  }
}

extension String {
  fileprivate var quoted: String { "'\(self)'" }
}

extension RegexValidator {
  mutating func validate() -> AST {
    for opt in ast.globalOptions?.options ?? [] {
      validateGlobalMatchingOption(opt)
    }
    validateCaptures()
    validateNode(ast.root)

    var result = ast
    result.diags.append(contentsOf: diags)
    return result
  }

  /// Called when some piece of invalid AST is encountered. We want to ensure
  /// an error was emitted.
  mutating func expectInvalid(at loc: SourceLocation) {
    guard ast.diags.hasAnyError else {
      unreachable("Invalid, but no error emitted?", at: loc)
      return
    }
  }

  mutating func validateGlobalMatchingOption(_ opt: AST.GlobalMatchingOption) {
    switch opt.kind {
    case .limitDepth, .limitHeap, .limitMatch, .notEmpty, .notEmptyAtStart,
        .noAutoPossess, .noDotStarAnchor, .noJIT, .noStartOpt, .utfMode,
        .unicodeProperties:
      // These are PCRE specific, and not something we're likely to ever
      // support.
      error(.unsupported("global matching option"), at: opt.location)

    case .newlineMatching:
      // We have implemented the correct behavior for multi-line literals, but
      // these should also affect '.' and '\N' matching, which we haven't
      // implemented.
      error(.unsupported("newline matching mode"), at: opt.location)

    case .newlineSequenceMatching:
      // We haven't yet implemented the '\R' matching specifics of these.
      error(.unsupported("newline sequence matching mode"), at: opt.location)
    }
  }

  mutating func validateCaptures() {
    // TODO: Should this be validated when creating the capture list?
    var usedNames = Set<String>()
    for capture in captures.captures {
      guard let name = capture.name else { continue }
      if !usedNames.insert(name).inserted {
        error(.duplicateNamedCapture(name), at: capture.location)
      }
    }
  }

  mutating func validateReference(_ ref: AST.Reference) {
    if let recLevel = ref.recursionLevel {
      error(.unsupported("recursion level"), at: recLevel.location)
    }
    switch ref.kind {
    case .absolute(let num):
      guard let i = num.value else {
        // Should have already been diagnosed.
        expectInvalid(at: ref.innerLoc)
        break
      }
      if i >= captures.captures.count {
        error(.invalidReference(i), at: ref.innerLoc)
      }
    case .named(let name):
      // An empty name is already invalid, so don't bother validating.
      guard !name.isEmpty else { break }
      if !captures.hasCapture(named: name) {
        error(.invalidNamedReference(name), at: ref.innerLoc)
      }
    case .relative(let num):
      guard let _ = num.value else {
        // Should have already been diagnosed.
        expectInvalid(at: ref.innerLoc)
        break
      }
      error(.unsupported("relative capture reference"), at: ref.innerLoc)
    }
  }

  mutating func validateMatchingOption(_ opt: AST.MatchingOption) {
    let loc = opt.location
    switch opt.kind {
    case .allowDuplicateGroupNames:
      // Not currently supported as we need to figure out what to do with
      // the capture type.
      error(.unsupported("duplicate group naming"), at: loc)

    case .unicodeWordBoundaries:
      error(.unsupported("unicode word boundary mode"), at: loc)

    case .textSegmentWordMode, .textSegmentGraphemeMode:
      error(.unsupported("text segment mode"), at: loc)

    case .byteSemantics:
      error(.unsupported("byte semantic mode"), at: loc)

    case .unicodeScalarSemantics:
      error(.unsupported("unicode scalar semantic mode"), at: loc)

    case .graphemeClusterSemantics:
      error(.unsupported("grapheme semantic mode"), at: loc)

    case .caseInsensitive, .possessiveByDefault, .reluctantByDefault,
        .singleLine, .multiline, .namedCapturesOnly, .extended, .extraExtended,
        .asciiOnlyDigit, .asciiOnlyWord, .asciiOnlySpace, .asciiOnlyPOSIXProps,
        .nsreCompatibleDot:
      break
    }
  }

  mutating func validateMatchingOptions(_ opts: AST.MatchingOptionSequence) {
    for opt in opts.adding {
      validateMatchingOption(opt)
    }
    for opt in opts.removing {
      validateMatchingOption(opt)
    }
  }

  mutating func validateBinaryProperty(
    _ prop: Unicode.BinaryProperty, at loc: SourceLocation
  ) {
    switch prop {
    case .asciiHexDigit, .alphabetic, .bidiControl, .bidiMirrored, .cased,
        .caseIgnorable, .changesWhenCasefolded, .changesWhenCasemapped,
        .changesWhenNFKCCasefolded, .changesWhenLowercased,
        .changesWhenTitlecased, .changesWhenUppercased, .dash, .deprecated,
        .defaultIgnorableCodePoint, .diacratic, .extender,
        .fullCompositionExclusion, .graphemeBase, .graphemeExtended, .hexDigit,
        .idContinue, .ideographic, .idStart, .idsBinaryOperator,
        .idsTrinaryOperator, .joinControl, .logicalOrderException, .lowercase,
        .math, .noncharacterCodePoint, .patternSyntax, .patternWhitespace,
        .quotationMark, .radical, .regionalIndicator, .softDotted,
        .sentenceTerminal, .terminalPunctuation, .unifiedIdiograph, .uppercase,
        .variationSelector, .whitespace, .xidContinue, .xidStart:
      break

    case .emojiModifierBase, .emojiModifier, .emoji, .emojiPresentation:
      // These are available on macOS 10.12.2, iOS 10.2, tvOS 10.1, watchOS 3.1.1.
      // TODO: We should ideally check deployment target for such conditionally
      // available properties.
      break

    case .expandsOnNFC, .expandsOnNFD, .expandsOnNFKD, .expandsOnNFKC:
      error(.deprecatedUnicode(prop.rawValue.quoted), at: loc)

    case .compositionExclusion, .emojiComponent,
        .extendedPictographic, .graphemeLink, .hyphen, .otherAlphabetic,
        .otherDefaultIgnorableCodePoint, .otherGraphemeExtended,
        .otherIDContinue, .otherIDStart, .otherLowercase, .otherMath,
        .otherUppercase, .prependedConcatenationMark:
      error(.unsupported(prop.rawValue.quoted), at: loc)
    }
  }

  mutating func validateCharacterProperty(
    _ prop: AST.Atom.CharacterProperty, at loc: SourceLocation
  ) {
    // TODO: We could re-add the .other case to diagnose unknown properties
    // here instead of in the parser.
    // TODO: Should we store an 'inner location' for the contents of `\p{...}`?
    switch prop.kind {
    case .binary(let b, _):
      validateBinaryProperty(b, at: loc)
    case .any, .assigned, .ascii, .generalCategory, .posix, .named, .script,
        .scriptExtension, .age, .numericType, .numericValue, .mapping, .ccc:
      break
    case .invalid:
      // Should have already been diagnosed.
      expectInvalid(at: loc)
    case .pcreSpecial:
      error(.unsupported("PCRE property"), at: loc)
    case .block:
      error(.unsupported("Unicode block property"), at: loc)
    case .javaSpecial:
      error(.unsupported("Java property"), at: loc)
    }
  }

  mutating func validateEscaped(
    _ esc: AST.Atom.EscapedBuiltin, at loc: SourceLocation
  ) {
    switch esc {
    case .resetStartOfMatch, .singleDataUnit, .trueAnychar,
        // '\N' needs to be emitted using 'emitDot'.
        .notNewline:
      error(.unsupported("'\\\(esc.character)'"), at: loc)

    // Character classes.
    case .decimalDigit, .notDecimalDigit, .whitespace, .notWhitespace,
        .wordCharacter, .notWordCharacter, .graphemeCluster,
        .horizontalWhitespace, .notHorizontalWhitespace,
        .verticalTab, .notVerticalTab:
      break

    case .newlineSequence:
      break

    // Assertions.
    case .wordBoundary, .notWordBoundary, .startOfSubject,
        .endOfSubjectBeforeNewline, .endOfSubject, .textSegment,
        .notTextSegment, .firstMatchingPositionInSubject:
      break

    // Literal escapes.
    case .alarm, .backspace, .escape, .formfeed, .newline, .carriageReturn,
        .tab:
      break
    }
  }

  mutating func validateAtom(_ atom: AST.Atom, inCustomCharacterClass: Bool) {
    switch atom.kind {
    case .escaped(let esc):
      validateEscaped(esc, at: atom.location)

    case .keyboardControl, .keyboardMeta, .keyboardMetaControl:
      // We need to implement the scalar computations for these.
      error(.unsupported("control sequence"), at: atom.location)

    case .property(let p):
      validateCharacterProperty(p, at: atom.location)

    case .backreference(let r):
      validateReference(r)

    case .subpattern:
      error(.unsupported("subpattern"), at: atom.location)

    case .callout:
      // These are PCRE and Oniguruma specific, supporting them is future work.
      error(.unsupported("callout"), at: atom.location)

    case .backtrackingDirective:
      // These are PCRE-specific, and are unlikely to be fully supported.
      error(.unsupported("backtracking directive"), at: atom.location)

    case .changeMatchingOptions(let opts):
      validateMatchingOptions(opts)

    case .namedCharacter:
      // TODO: We should error on unknown Unicode scalar names.
      break

    case .scalarSequence:
      // Not currently supported in a custom character class.
      if inCustomCharacterClass {
        error(.unsupported("scalar sequence in custom character class"),
              at: atom.location)
      }

    case .char, .scalar, .caretAnchor, .dollarAnchor, .dot:
      break

    case .invalid:
      // Should have already been diagnosed.
      expectInvalid(at: atom.location)
      break
    }
  }

  mutating func validateCustomCharacterClass(_ c: AST.CustomCharacterClass) {
    for member in c.members {
      validateCharacterClassMember(member)
    }
  }

  mutating func validateCharacterClassRange(
    _ range: AST.CustomCharacterClass.Range
  ) {
    let lhs = range.lhs
    let rhs = range.rhs

    validateAtom(lhs, inCustomCharacterClass: true)
    validateAtom(rhs, inCustomCharacterClass: true)

    guard lhs.isValidCharacterClassRangeBound else {
      error(.invalidCharacterClassRangeOperand, at: lhs.location)
      return
    }
    guard rhs.isValidCharacterClassRangeBound else {
      error(.invalidCharacterClassRangeOperand, at: rhs.location)
      return
    }

    guard let lhsChar = lhs.literalCharacterValue else {
      error(
        .unsupported("character class range operand"), at: lhs.location)
      return
    }

    guard let rhsChar = rhs.literalCharacterValue else {
      error(
        .unsupported("character class range operand"), at: rhs.location)
      return
    }

    if lhsChar > rhsChar {
      error(
        .invalidCharacterRange(from: lhsChar, to: rhsChar), at: range.dashLoc)
    }
  }

  mutating func validateCharacterClassMember(
    _ member: AST.CustomCharacterClass.Member
  ) {
    switch member {
    case .custom(let c):
      validateCustomCharacterClass(c)

    case .range(let r):
      validateCharacterClassRange(r)

    case .atom(let a):
      validateAtom(a, inCustomCharacterClass: true)

    case .setOperation(let lhs, _, let rhs):
      for lh in lhs { validateCharacterClassMember(lh) }
      for rh in rhs { validateCharacterClassMember(rh) }

    case .quote, .trivia:
      break
    }
  }

  mutating func validateGroup(_ group: AST.Group) {
    let kind = group.kind
    if let name = kind.value.name, name.isEmpty {
      expectInvalid(at: kind.location)
    }
    switch kind.value {
    case .capture, .namedCapture, .nonCapture, .lookahead, .negativeLookahead,
        .atomicNonCapturing:
      break

    case .balancedCapture:
      // These are .NET specific, and kinda niche.
      error(.unsupported("balanced capture"), at: kind.location)

    case .nonCaptureReset:
      // We need to figure out how these interact with typed captures.
      error(.unsupported("branch reset group"), at: kind.location)

    case .nonAtomicLookahead:
      error(.unsupported("non-atomic lookahead"), at: kind.location)

    case .lookbehind, .negativeLookbehind, .nonAtomicLookbehind:
      error(.unsupported("lookbehind"), at: kind.location)

    case .scriptRun, .atomicScriptRun:
      error(.unsupported("script run"), at: kind.location)

    case .changeMatchingOptions(let opts):
      validateMatchingOptions(opts)
    }
    validateNode(group.child)
  }

  mutating func validateQuantification(_ quant: AST.Quantification) {
    validateNode(quant.child)
    if !quant.child.isQuantifiable {
      error(.notQuantifiable, at: quant.child.location)
    }
    switch quant.amount.value {
    case .range(let lhs, let rhs):
      guard let lhs = lhs.value, let rhs = rhs.value else {
        // Should have already been diagnosed.
        expectInvalid(at: quant.location)
        break
      }
      if lhs > rhs {
        error(.invalidQuantifierRange(lhs, rhs), at: quant.location)
      }
    case .zeroOrMore, .oneOrMore, .zeroOrOne, .exactly, .nOrMore, .upToN:
      break
    }
  }

  mutating func validateNode(_ node: AST.Node) {
    switch node {
    case .alternation(let a):
      for branch in a.children {
        validateNode(branch)
      }
    case .concatenation(let c):
      for child in c.children {
        validateNode(child)
      }

    case .group(let g):
      validateGroup(g)

    case .conditional(let c):
      // Note even once we get runtime support for this, we need to change the
      // parsing to incorporate what is specified in the syntax proposal.
      error(.unsupported("conditional"), at: c.location)

    case .quantification(let q):
      validateQuantification(q)

    case .atom(let a):
      validateAtom(a, inCustomCharacterClass: false)

    case .customCharacterClass(let c):
      validateCustomCharacterClass(c)

    case .absentFunction(let a):
      // These are Oniguruma specific.
      error(.unsupported("absent function"), at: a.location)

    case .interpolation(let i):
      // This is currently rejected in the parser for better diagnostics, but
      // reject here too until we get runtime support.
      error(.unsupported("interpolation"), at: i.location)

    case .quote, .trivia, .empty:
      break
    }
  }
}

/// Check a regex AST for semantic validity.
public func validate(_ ast: AST) -> AST {
  var validator = RegexValidator(ast)
  return validator.validate()
}