Skip to content

[Integration] main (f779459) -> swift/main #400

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

Merged
merged 29 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e748aea
Add NegativeLookahead and Anchor comments (#372)
natecook1000 May 2, 2022
13342eb
Add matching support for `\p{Lc}`
hamishknight May 3, 2022
925f51b
Add parser support for `\p{L&}`
hamishknight May 3, 2022
ade8f01
Merge pull request #373 from hamishknight/case-in-prop
hamishknight May 3, 2022
c44efeb
Update ProposalOverview.md
milseman May 3, 2022
9801855
Add tests for AnyRegexOutput (#371)
milseman May 3, 2022
0e5cfa8
Rename noAutoCapture -> namedCapturesOnly
hamishknight May 4, 2022
2a4b3a6
Implement the `(?n)` option
hamishknight May 4, 2022
f22cb4f
Merge pull request #377 from hamishknight/named-captures-only
hamishknight May 4, 2022
6d833aa
Improve Unicode/UTS18 and semantic level support (#268)
natecook1000 May 5, 2022
09a385b
Support Unicode scalar names in `\p{name=...}` (#382)
natecook1000 May 6, 2022
39c0ed5
Modify DSL test to test for uncaptured backreference (#355)
natecook1000 May 6, 2022
9740416
Introduce ASTStage parameter to `parse`
hamishknight May 9, 2022
4b31736
Implement semantic diagnostics
hamishknight May 9, 2022
466b375
Validate capture lists
hamishknight May 9, 2022
c95e862
Address review feedback
hamishknight May 9, 2022
7f068dc
Merge pull request #379 from hamishknight/sema
hamishknight May 9, 2022
c16e389
Implement \R, \v, \h for character/scalar modes (#384)
natecook1000 May 9, 2022
c13980f
De-deprecate MatchingOptions.matchLevel (#390)
natecook1000 May 9, 2022
61965c3
Restrict character property fuzzy matching to "pattern whitespace"
hamishknight May 10, 2022
05e610a
Improve the wording of a diagnostic
hamishknight May 10, 2022
7752015
Introduce AST.Atom.Scalar
hamishknight May 10, 2022
f436cca
Introduce scalar sequences `\u{AA BB CC}`
hamishknight May 10, 2022
0597164
Fix invalid indexing
hamishknight May 10, 2022
0872d16
Fix source location tracking in `lexUntil`
hamishknight May 10, 2022
5b30c5b
Merge pull request #386 from hamishknight/multiscalar
hamishknight May 10, 2022
b209e4f
Tidy up build flags and fix implicit import circular dependency (#392)
rxwei May 10, 2022
f779459
Catch more unquantifiable elements (#391)
natecook1000 May 10, 2022
f37fc9b
Merge branch 'main' into main-merge
hamishknight May 11, 2022
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
Prev Previous commit
Next Next commit
Introduce scalar sequences \u{AA BB CC}
Allow a whitespace-separated list of scalars within
the `\u{...}` syntax. This is syntactic sugar that
gets implicitly splatted out, for example `\u{A B C}`
becomes `\u{A}\u{B}\u{C}`.
  • Loading branch information
hamishknight committed May 10, 2022
commit f436ccad016d2dddb1984b8104c88a9ae9ed21ac
36 changes: 32 additions & 4 deletions Sources/_RegexParser/Regex/AST/Atom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ extension AST {
/// \u{...}, \0dd, \x{...}, ...
case scalar(Scalar)

/// A whitespace-separated sequence of Unicode scalar values which are
/// implicitly splatted out.
///
/// `\u{A B C}` -> `\u{A}\u{B}\u{C}`
case scalarSequence(ScalarSequence)

/// A Unicode property, category, or script, including those written using
/// POSIX syntax.
///
Expand Down Expand Up @@ -84,6 +90,7 @@ extension AST.Atom {
switch kind {
case .char(let v): return v
case .scalar(let v): return v
case .scalarSequence(let v): return v
case .property(let v): return v
case .escaped(let v): return v
case .keyboardControl(let v): return v
Expand Down Expand Up @@ -116,6 +123,18 @@ extension AST.Atom {
self.location = location
}
}

public struct ScalarSequence: Hashable {
public var scalars: [Scalar]
public var trivia: [AST.Trivia]

public init(_ scalars: [Scalar], trivia: [AST.Trivia]) {
precondition(scalars.count > 1, "Expected multiple scalars")
self.scalars = scalars
self.trivia = trivia
}
public var scalarValues: [Unicode.Scalar] { scalars.map(\.value) }
}
}

extension AST.Atom {
Expand Down Expand Up @@ -725,8 +744,9 @@ extension AST.Atom {
// the AST? Or defer for the matching engine?
return nil

case .property, .any, .startOfLine, .endOfLine, .backreference, .subpattern,
.callout, .backtrackingDirective, .changeMatchingOptions:
case .scalarSequence, .property, .any, .startOfLine, .endOfLine,
.backreference, .subpattern, .callout, .backtrackingDirective,
.changeMatchingOptions:
return nil
}
}
Expand All @@ -748,13 +768,21 @@ extension AST.Atom {
/// A string literal representation of the atom, if possible.
///
/// Individual characters are returned as-is, and Unicode scalars are
/// presented using "\u{nnnn}" syntax.
/// presented using "\u{nn nn ...}" syntax.
public var literalStringValue: String? {
func scalarLiteral(_ u: [UnicodeScalar]) -> String {
let digits = u.map { String($0.value, radix: 16, uppercase: true) }
.joined(separator: " ")
return "\\u{\(digits)}"
}
switch kind {
case .char(let c):
return String(c)
case .scalar(let s):
return "\\u{\(String(s.value.value, radix: 16, uppercase: true))}"
return scalarLiteral([s.value])

case .scalarSequence(let s):
return scalarLiteral(s.scalarValues)

case .keyboardControl(let x):
return "\\C-\(x)"
Expand Down
61 changes: 59 additions & 2 deletions Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,54 @@ extension Source {
return try Source.validateUnicodeScalar(str, .hex)
}

/// Try to lex a seqence of hex digit unicode scalars.
///
/// UniScalarSequence -> Whitespace? UniScalarSequencElt+
/// UniScalarSequencElt -> HexDigit{1...} Whitespace?
///
mutating func expectUnicodeScalarSequence(
eating ending: Character
) throws -> AST.Atom.Kind {
try recordLoc { src in
var scalars = [AST.Atom.Scalar]()
var trivia = [AST.Trivia]()

// Eat up any leading whitespace.
if let t = src.lexWhitespace() { trivia.append(t) }

while true {
let str = src.lexUntil { src in
// Hit the ending, stop lexing.
if src.isEmpty || src.peek() == ending {
return true
}
// Eat up trailing whitespace, and stop lexing to record the scalar.
if let t = src.lexWhitespace() {
trivia.append(t)
return true
}
// Not the ending or trivia, must be a digit of the scalar.
return false
}
guard !str.value.isEmpty else { break }
scalars.append(try Source.validateUnicodeScalar(str, .hex))
}
guard !scalars.isEmpty else {
throw ParseError.expectedNumber("", kind: .hex)
}
try src.expect(ending)

if scalars.count == 1 {
return .scalar(scalars[0])
}
return .scalarSequence(.init(scalars, trivia: trivia))
}.value
}

/// Eat a scalar off the front, starting from after the
/// backslash and base character (e.g. `\u` or `\x`).
///
/// UniScalar -> 'u{' HexDigit{1...} '}'
/// UniScalar -> 'u{' UniScalarSequence '}'
/// | 'u' HexDigit{4}
/// | 'x{' HexDigit{1...} '}'
/// | 'x' HexDigit{0...2}
Expand All @@ -314,7 +358,10 @@ extension Source {
// TODO: PCRE offers a different behavior if PCRE2_ALT_BSUX is set.
switch base {
// Hex numbers.
case "u" where src.tryEat("{"), "x" where src.tryEat("{"):
case "u" where src.tryEat("{"):
return try src.expectUnicodeScalarSequence(eating: "}")

case "x" where src.tryEat("{"):
let str = try src.lexUntil(eating: "}")
return .scalar(try Source.validateUnicodeScalar(str, .hex))

Expand Down Expand Up @@ -598,6 +645,16 @@ extension Source {
// inside a custom character class (and only treats whitespace as
// non-semantic there for the extra-extended `(?xx)` mode). If we get a
// strict-PCRE mode, we'll need to add a case for that.
return lexWhitespace()
}

/// Try to consume whitespace as trivia
///
/// Whitespace -> WhitespaceChar+
///
/// Unlike `lexNonSemanticWhitespace`, this will always attempt to lex
/// whitespace.
mutating func lexWhitespace() -> AST.Trivia? {
let trivia: Located<String>? = recordLoc { src in
src.tryEatPrefix(\.isPatternWhitespace)?.string
}
Expand Down
17 changes: 12 additions & 5 deletions Sources/_RegexParser/Regex/Parse/Sema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ extension RegexValidator {
}
}

func validateAtom(_ atom: AST.Atom) throws {
func validateAtom(_ atom: AST.Atom, inCustomCharacterClass: Bool) throws {
switch atom.kind {
case .escaped(let esc):
try validateEscaped(esc, at: atom.location)
Expand Down Expand Up @@ -243,6 +243,13 @@ extension RegexValidator {
// TODO: We should error on unknown Unicode scalar names.
break

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

case .char, .scalar, .startOfLine, .endOfLine, .any:
break
}
Expand All @@ -260,8 +267,8 @@ extension RegexValidator {
let lhs = range.lhs
let rhs = range.rhs

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

guard lhs.isValidCharacterClassRangeBound else {
throw error(.invalidCharacterClassRangeOperand, at: lhs.location)
Expand Down Expand Up @@ -297,7 +304,7 @@ extension RegexValidator {
try validateCharacterClassRange(r)

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

case .setOperation(let lhs, _, let rhs):
for lh in lhs { try validateCharacterClassMember(lh) }
Expand Down Expand Up @@ -379,7 +386,7 @@ extension RegexValidator {
try validateQuantification(q)

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

case .customCharacterClass(let c):
try validateCustomCharacterClass(c)
Expand Down
3 changes: 3 additions & 0 deletions Sources/_RegexParser/Regex/Printing/DumpAST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ extension AST.Atom {
switch kind {
case .escaped(let c): return "\\\(c.character)"

case .scalarSequence(let s):
return s.scalars.map(\.value.halfWidthCornerQuoted).joined()

case .namedCharacter(let charName):
return "\\N{\(charName)}"

Expand Down
6 changes: 3 additions & 3 deletions Sources/_StringProcessing/ConsumerInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ extension AST.Atom {
// handled in emitAssertion
return nil

case .escaped, .keyboardControl, .keyboardMeta, .keyboardMetaControl,
.backreference, .subpattern, .callout, .backtrackingDirective,
.changeMatchingOptions:
case .scalarSequence, .escaped, .keyboardControl, .keyboardMeta,
.keyboardMetaControl, .backreference, .subpattern, .callout,
.backtrackingDirective, .changeMatchingOptions:
// FIXME: implement
return nil
}
Expand Down
20 changes: 11 additions & 9 deletions Sources/_StringProcessing/PrintAsPattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,19 @@ extension AST.Atom {
}

var _dslBase: String {
func scalarLiteral(_ s: UnicodeScalar) -> String {
let hex = String(s.value, radix: 16, uppercase: true)
return "\\u{\(hex)}"
}
switch kind {
case let .char(c):
return String(c)

case let .scalar(s):
let hex = String(s.value.value, radix: 16, uppercase: true)
return "\\u{\(hex)}"
return scalarLiteral(s.value)

case let .scalarSequence(seq):
return seq.scalarValues.map(scalarLiteral).joined()

case let .property(p):
return p._dslBase
Expand Down Expand Up @@ -769,13 +775,9 @@ extension AST.Atom {

var _regexBase: String {
switch kind {
case let .char(c):
return String(c)

case let .scalar(s):
let hex = String(s.value.value, radix: 16, uppercase: true)
return "\\u{\(hex)}"

case .char, .scalar, .scalarSequence:
return literalStringValue!

case let .property(p):
return p._regexBase

Expand Down
18 changes: 14 additions & 4 deletions Sources/_StringProcessing/Regex/ASTConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ extension AST.Node {
var result = ""
var idx = idx
while idx < astChildren.endIndex {
let atom: AST.Atom? = astChildren[idx].as()
guard let atom: AST.Atom = astChildren[idx].as() else { break }

// TODO: For printing, nice to coalesce
// scalars literals too. We likely need a different
// approach even before we have a better IR.
if let char = atom?.singleCharacter {
if let char = atom.singleCharacter {
result.append(char)
} else if let scalar = atom?.singleScalar {
} else if let scalar = atom.singleScalar {
result.append(Character(scalar))
} else if case .scalarSequence(let seq) = atom.kind {
result += seq.scalarValues.map(Character.init)
} else {
break
}
Expand Down Expand Up @@ -136,7 +138,15 @@ extension AST.Node {
return .trivia(v.contents)

case let .atom(v):
return .atom(v.dslTreeAtom)
switch v.kind {
case .scalarSequence(let seq):
// Scalar sequences are splatted into concatenated scalars, which
// becomes a quoted literal. Sequences nested in concatenations have
// already been coalesced, this just handles the lone atom case.
return .quotedLiteral(String(seq.scalarValues.map(Character.init)))
default:
return .atom(v.dslTreeAtom)
}

case let .customCharacterClass(ccc):
return .customCharacterClass(ccc.dslTreeClass)
Expand Down
20 changes: 18 additions & 2 deletions Sources/_StringProcessing/Utility/ASTBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,26 @@ func escaped(
atom(.escaped(e))
}
func scalar(_ s: Unicode.Scalar) -> AST.Node {
atom(.scalar(.init(s, .fake)))
.atom(scalar_a(s))
}
func scalar_a(_ s: Unicode.Scalar) -> AST.Atom {
atom_a(.scalar(.init(s, .fake)))
}
func scalar_m(_ s: Unicode.Scalar) -> AST.CustomCharacterClass.Member {
atom_m(.scalar(.init(s, .fake)))
.atom(scalar_a(s))
}

func scalarSeq(_ s: Unicode.Scalar...) -> AST.Node {
.atom(scalarSeq_a(s))
}
func scalarSeq_a(_ s: Unicode.Scalar...) -> AST.Atom {
scalarSeq_a(s)
}
func scalarSeq_a(_ s: [Unicode.Scalar]) -> AST.Atom {
atom_a(.scalarSequence(.init(s.map { .init($0, .fake) }, trivia: [])))
}
func scalarSeq_m(_ s: Unicode.Scalar...) -> AST.CustomCharacterClass.Member {
.atom(scalarSeq_a(s))
}

func backreference(_ r: AST.Reference.Kind, recursionLevel: Int? = nil) -> AST.Node {
Expand Down
21 changes: 19 additions & 2 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,20 @@ extension RegexTests {
firstMatchTest(#"\0707"#, input: "12387\u{1C7}xyz", match: "\u{1C7}")

// code point sequence
firstMatchTest(#"\u{61 62 63}"#, input: "123abcxyz", match: "abc", xfail: true)
firstMatchTest(#"\u{61 62 63}"#, input: "123abcxyz", match: "abc")
firstMatchTest(#"3\u{ 61 62 63 }"#, input: "123abcxyz", match: "3abc")
firstMatchTest(#"\u{61 62}\u{63}"#, input: "123abcxyz", match: "abc")
firstMatchTest(#"\u{61}\u{62 63}"#, input: "123abcxyz", match: "abc")
firstMatchTest(#"9|\u{61 62 63}"#, input: "123abcxyz", match: "abc")
firstMatchTest(#"(?:\u{61 62 63})"#, input: "123abcxyz", match: "abc")
firstMatchTest(#"23\u{61 62 63}xy"#, input: "123abcxyz", match: "23abcxy")

// o + horn + dot_below
firstMatchTest(
#"\u{006f 031b 0323}"#,
input: "\u{006f}\u{031b}\u{0323}",
match: "\u{006f}\u{031b}\u{0323}"
)

// Escape sequences that represent scalar values.
firstMatchTest(#"\a[\b]\e\f\n\r\t"#,
Expand Down Expand Up @@ -1405,6 +1418,9 @@ extension RegexTests {
firstMatchTest(#"\u{65}\u{301}$"#, input: eDecomposed, match: eDecomposed)
firstMatchTest(#"\u{65}\u{301}$"#, input: eComposed, match: eComposed)

firstMatchTest(#"\u{65 301}$"#, input: eDecomposed, match: eDecomposed)
firstMatchTest(#"\u{65 301}$"#, input: eComposed, match: eComposed)

// FIXME: Implicit \y at end of match
firstMatchTest(#"\u{65}"#, input: eDecomposed, match: nil,
xfail: true)
Expand Down Expand Up @@ -1516,7 +1532,8 @@ extension RegexTests {
firstMatchTest(#"🇰🇷"#, input: flag, match: flag)
firstMatchTest(#"[🇰🇷]"#, input: flag, match: flag)
firstMatchTest(#"\u{1F1F0}\u{1F1F7}"#, input: flag, match: flag)

firstMatchTest(#"\u{1F1F0 1F1F7}"#, input: flag, match: flag)

// First Unicode scalar followed by CCC of regional indicators
firstMatchTest(#"\u{1F1F0}[\u{1F1E6}-\u{1F1FF}]"#, input: flag, match: flag,
xfail: true)
Expand Down
Loading