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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
@_spi(_Unicode)
import Swift
@_implementationOnly import _RegexParser
extension Compiler {
struct ByteCodeGen {
var options: MatchingOptions
var builder = MEProgram.Builder()
/// A Boolean indicating whether the first matchable atom has been emitted.
/// This is used to determine whether to apply initial options.
var hasEmittedFirstMatchableAtom = false
private let compileOptions: _CompileOptions
fileprivate var optimizationsEnabled: Bool {
!compileOptions.contains(.disableOptimizations)
}
init(
options: MatchingOptions,
compileOptions: _CompileOptions,
captureList: CaptureList
) {
self.options = options
self.compileOptions = compileOptions
self.builder.captureList = captureList
self.builder.enableTracing = compileOptions.contains(.enableTracing)
self.builder.enableMetrics = compileOptions.contains(.enableMetrics)
}
}
}
extension Compiler.ByteCodeGen {
mutating func emitRoot(_ root: DSLTree.Node) throws -> MEProgram {
// The whole match (`.0` element of output) is equivalent to an implicit
// capture over the entire regex.
try emitNode(.capture(name: nil, reference: nil, root))
builder.canOnlyMatchAtStart = root.canOnlyMatchAtStart()
builder.buildAccept()
return try builder.assemble()
}
}
fileprivate extension Compiler.ByteCodeGen {
mutating func emitAtom(_ a: DSLTree.Atom) throws {
defer {
if a.isMatchable {
hasEmittedFirstMatchableAtom = true
}
}
switch a {
case .any:
emitAny()
case .anyNonNewline:
emitAnyNonNewline()
case .dot:
try emitDot()
case let .char(c):
emitCharacter(c)
case let .scalar(s):
if options.semanticLevel == .graphemeCluster {
emitCharacter(Character(s))
} else {
emitMatchScalar(s)
}
case let .characterClass(cc):
emitCharacterClass(cc)
case let .assertion(kind):
try emitAssertion(kind)
case let .backreference(ref):
try emitBackreference(ref.ast)
case let .symbolicReference(id):
builder.buildUnresolvedReference(
id: id, isScalarMode: options.semanticLevel == .unicodeScalar)
case let .changeMatchingOptions(optionSequence):
if !hasEmittedFirstMatchableAtom {
builder.initialOptions.apply(optionSequence.ast)
}
options.apply(optionSequence.ast)
case let .unconverted(astAtom):
if let consumer = try astAtom.ast.generateConsumer(options) {
builder.buildConsume(by: consumer)
} else {
throw Unsupported("\(astAtom.ast._patternBase)")
}
}
}
mutating func emitQuotedLiteral(_ s: String) {
guard options.semanticLevel == .graphemeCluster else {
for char in s {
for scalar in char.unicodeScalars {
emitMatchScalar(scalar)
}
}
return
}
// Fast path for eliding boundary checks for an all ascii quoted literal
if optimizationsEnabled && s.allSatisfy(\.isASCII) && !s.isEmpty {
let lastIdx = s.unicodeScalars.indices.last!
for idx in s.unicodeScalars.indices {
let boundaryCheck = idx == lastIdx
let scalar = s.unicodeScalars[idx]
if options.isCaseInsensitive && scalar.properties.isCased {
builder.buildMatchScalarCaseInsensitive(scalar, boundaryCheck: boundaryCheck)
} else {
builder.buildMatchScalar(scalar, boundaryCheck: boundaryCheck)
}
}
return
}
for c in s { emitCharacter(c) }
}
mutating func emitBackreference(
_ ref: AST.Reference
) throws {
if ref.recursesWholePattern {
// TODO: A recursive call isn't a backreference, but
// we could in theory match the whole match so far...
throw Unsupported("Backreference kind: \(ref)")
}
switch ref.kind {
case .absolute(let n):
guard let i = n.value else {
throw Unreachable("Expected a value")
}
builder.buildBackreference(
.init(i), isScalarMode: options.semanticLevel == .unicodeScalar)
case .named(let name):
try builder.buildNamedReference(
name, isScalarMode: options.semanticLevel == .unicodeScalar)
case .relative:
throw Unsupported("Backreference kind: \(ref)")
#if RESILIENT_LIBRARIES
@unknown default:
fatalError()
#endif
}
}
mutating func emitAssertion(
_ kind: DSLTree.Atom.Assertion
) throws {
if kind == .resetStartOfMatch {
throw Unsupported(#"\K (reset/keep assertion)"#)
}
builder.buildAssert(
by: kind,
options.anchorsMatchNewlines,
options.usesSimpleUnicodeBoundaries,
options.usesASCIIWord,
options.semanticLevel)
}
mutating func emitCharacterClass(_ cc: DSLTree.Atom.CharacterClass) {
builder.buildMatchBuiltin(model: cc.asRuntimeModel(options))
}
mutating func emitMatchScalar(_ s: UnicodeScalar) {
assert(options.semanticLevel == .unicodeScalar)
if options.isCaseInsensitive && s.properties.isCased {
builder.buildMatchScalarCaseInsensitive(s, boundaryCheck: false)
} else {
builder.buildMatchScalar(s, boundaryCheck: false)
}
}
mutating func emitCharacter(_ c: Character) {
// Unicode scalar mode matches the specific scalars that comprise a character
if options.semanticLevel == .unicodeScalar {
for scalar in c.unicodeScalars {
emitMatchScalar(scalar)
}
return
}
if options.isCaseInsensitive && c.isCased {
if optimizationsEnabled && c.isASCII {
// c.isCased ensures that c is not CR-LF,
// so we know that c is a single scalar
assert(c.unicodeScalars.count == 1)
builder.buildMatchScalarCaseInsensitive(
c.unicodeScalars.last!,
boundaryCheck: true)
} else {
builder.buildMatch(c, isCaseInsensitive: true)
}
return
}
if optimizationsEnabled && c.isASCII {
let lastIdx = c.unicodeScalars.indices.last!
for idx in c.unicodeScalars.indices {
builder.buildMatchScalar(c.unicodeScalars[idx], boundaryCheck: idx == lastIdx)
}
return
}
builder.buildMatch(c, isCaseInsensitive: false)
}
mutating func emitAny() {
switch options.semanticLevel {
case .graphemeCluster:
builder.buildAdvance(1)
case .unicodeScalar:
builder.buildAdvanceUnicodeScalar(1)
}
}
mutating func emitAnyNonNewline() {
switch options.semanticLevel {
case .graphemeCluster:
builder.buildConsumeNonNewline()
case .unicodeScalar:
builder.buildConsumeScalarNonNewline()
}
}
mutating func emitDot() throws {
if options.dotMatchesNewline {
if options.usesNSRECompatibleDot {
try emitAlternation([
.atom(.characterClass(.newlineSequence)),
.atom(.anyNonNewline)])
} else {
emitAny()
}
} else {
emitAnyNonNewline()
}
}
mutating func emitAlternationGen<C: BidirectionalCollection>(
_ elements: C,
withBacktracking: Bool,
_ body: (inout Compiler.ByteCodeGen, C.Element) throws -> Void
) rethrows {
// Alternation: p0 | p1 | ... | pn
// save next_p1
// <code for p0>
// branch done
// next_p1:
// save next_p2
// <code for p1>
// branch done
// next_p2:
// save next_p...
// <code for p2>
// branch done
// ...
// next_pn:
// <code for pn>
// done:
let done = builder.makeAddress()
for element in elements.dropLast() {
let next = builder.makeAddress()
builder.buildSave(next)
try body(&self, element)
if !withBacktracking {
builder.buildClear()
}
builder.buildBranch(to: done)
builder.label(next)
}
try body(&self, elements.last!)
builder.label(done)
}
mutating func emitAlternation(
_ children: [DSLTree.Node]
) throws {
try emitAlternationGen(children, withBacktracking: true) {
try $0.emitNode($1)
}
}
mutating func emitConcatenationComponent(
_ node: DSLTree.Node
) throws {
// TODO: Should we do anything special since we can
// be glueing sub-grapheme components together?
try emitNode(node)
}
mutating func emitPositiveLookahead(_ child: DSLTree.Node) throws {
/*
save(restoringAt: success)
save(restoringAt: intercept)
<sub-pattern> // failure restores at intercept
clearThrough(intercept) // remove intercept and any leftovers from <sub-pattern>
fail(preservingCaptures: true) // ->success
intercept:
clearSavePoint // remove success
fail // propagate failure
success:
...
*/
let intercept = builder.makeAddress()
let success = builder.makeAddress()
builder.buildSave(success)
builder.buildSave(intercept)
try emitNode(child)
builder.buildClearThrough(intercept)
builder.buildFail(preservingCaptures: true) // Lookahead succeeds here
builder.label(intercept)
builder.buildClear()
builder.buildFail()
builder.label(success)
}
mutating func emitNegativeLookahead(_ child: DSLTree.Node) throws {
/*
save(restoringAt: success)
save(restoringAt: intercept)
<sub-pattern> // failure restores at intercept
clearThrough(intercept) // remove intercept and any leftovers from <sub-pattern>
clearSavePoint // remove success
fail // propagate failure
intercept:
fail // ->success
success:
...
*/
let intercept = builder.makeAddress()
let success = builder.makeAddress()
builder.buildSave(success)
builder.buildSave(intercept)
try emitNode(child)
builder.buildClearThrough(intercept)
builder.buildClear()
builder.buildFail()
builder.label(intercept)
builder.buildFail()
builder.label(success)
}
mutating func emitLookaround(
_ kind: (forwards: Bool, positive: Bool),
_ child: DSLTree.Node
) throws {
guard kind.forwards else {
throw Unsupported("backwards assertions")
}
if kind.positive {
try emitPositiveLookahead(child)
} else {
try emitNegativeLookahead(child)
}
}
mutating func emitAtomicNoncapturingGroup(
_ child: DSLTree.Node
) throws {
/*
save(continuingAt: success)
save(restoringAt: intercept)
<sub-pattern> // failure restores at intercept
clearThrough(intercept) // remove intercept and any leftovers from <sub-pattern>
fail(preservingCaptures: true) // ->success
intercept:
clearSavePoint // remove success
fail // propagate failure
success:
...
*/
let intercept = builder.makeAddress()
let success = builder.makeAddress()
builder.buildSaveAddress(success)
builder.buildSave(intercept)
try emitNode(child)
builder.buildClearThrough(intercept)
builder.buildFail(preservingCaptures: true) // Atomic group succeeds here
builder.label(intercept)
builder.buildClear()
builder.buildFail()
builder.label(success)
}
mutating func emitMatcher(
_ matcher: @escaping _MatcherInterface
) -> ValueRegister {
// TODO: Consider emitting consumer interface if
// not captured. This may mean we should store
// an existential instead of a closure...
let matcher = builder.makeMatcherFunction { input, start, range in
try matcher(input, start, range)
}
let valReg = builder.makeValueRegister()
builder.buildMatcher(matcher, into: valReg)
return valReg
}
mutating func emitNoncapturingGroup(
_ kind: AST.Group.Kind,
_ child: DSLTree.Node
) throws {
assert(!kind.isCapturing)
options.beginScope()
defer { options.endScope() }
if let lookaround = kind.lookaroundKind {
try emitLookaround(lookaround, child)
return
}
switch kind {
case .lookahead, .negativeLookahead,
.lookbehind, .negativeLookbehind:
throw Unreachable("TODO: reason")
case .capture, .namedCapture, .balancedCapture:
throw Unreachable("These should produce a capture node")
case .changeMatchingOptions(let optionSequence):
if !hasEmittedFirstMatchableAtom {
builder.initialOptions.apply(optionSequence)
}
options.apply(optionSequence)
try emitNode(child)
case .atomicNonCapturing:
try emitAtomicNoncapturingGroup(child)
default:
// FIXME: Other kinds...
try emitNode(child)
}
}
mutating func emitQuantification(
_ amount: AST.Quantification.Amount,
_ kind: DSLTree.QuantificationKind,
_ child: DSLTree.Node
) throws {
let updatedKind: AST.Quantification.Kind
switch kind {
case .explicit(let kind):
updatedKind = kind.ast
case .syntax(let kind):
updatedKind = kind.ast.applying(options)
case .default:
updatedKind = options.defaultQuantificationKind
}
let (low, high) = amount.bounds
guard let low = low else {
throw Unreachable("Must have a lower bound")
}
switch (low, high) {
case (_, 0):
// TODO: Should error out earlier, maybe DSL and parser
// has validation logic?
return
case let (n, m?) where n > m:
// TODO: Should error out earlier, maybe DSL and parser
// has validation logic?
return
case let (n, m) where m == nil || n <= m!:
// Ok
break
default:
throw Unreachable("TODO: reason")
}
// Compiler and/or parser should enforce these invariants
// before we are called
assert(high != 0)
assert((0...(high ?? Int.max)).contains(low))
let maxExtraTrips: Int?
if let h = high {
maxExtraTrips = h - low
} else {
maxExtraTrips = nil
}
let minTrips = low
assert((maxExtraTrips ?? 1) >= 0)
if tryEmitFastQuant(child, updatedKind, minTrips, maxExtraTrips) {
return
}
// The below is a general algorithm for bounded and unbounded
// quantification. It can be specialized when the min
// is 0 or 1, or when extra trips is 1 or unbounded.
//
// Stuff inside `<` and `>` are decided at compile time,
// while run-time values stored in registers start with a `%`
_ = """
min-trip-count control block:
if %minTrips is zero:
goto exit-policy control block
else:
decrement %minTrips and fallthrough
loop-body:
<if can't guarantee forward progress && maxExtraTrips = nil>:
mov currentPosition %pos
evaluate the subexpression
<if can't guarantee forward progress && maxExtraTrips = nil>:
if %pos is currentPosition:
goto exit
goto min-trip-count control block
exit-policy control block:
if %maxExtraTrips is zero:
goto exit
else:
decrement %maxExtraTrips and fallthrough
<if eager>:
save exit and goto loop-body
<if possessive>:
ratchet and goto loop
<if reluctant>:
save loop-body and fallthrough (i.e. goto exit)
exit
... the rest of the program ...
"""
// Specialization based on `minTrips` for 0 or 1:
_ = """
min-trip-count control block:
<if minTrips == 0>:
goto exit-policy
<if minTrips == 1>:
/* fallthrough */
loop-body:
evaluate the subexpression
<if minTrips <= 1>
/* fallthrough */
"""
// Specialization based on `maxExtraTrips` for 0 or unbounded
_ = """
exit-policy control block:
<if maxExtraTrips == 0>:
goto exit
<if maxExtraTrips == .unbounded>:
/* fallthrough */
"""
/*
NOTE: These specializations don't emit the optimal
code layout (e.g. fallthrough vs goto), but that's better
done later (not prematurely) and certainly better
done by an optimizing compiler.
NOTE: We're intentionally emitting essentially the same
algorithm for all quantifications for now, for better
testing and surfacing difficult bugs. We can specialize
for other things, like `.*`, later.
When it comes time for optimizing, we can also look into
quantification instructions (e.g. reduce save-point traffic)
*/
let minTripsControl = builder.makeAddress()
let loopBody = builder.makeAddress()
let exitPolicy = builder.makeAddress()
let exit = builder.makeAddress()
// We'll need registers if we're (non-trivially) bounded
let minTripsReg: IntRegister?
if minTrips > 1 {
minTripsReg = builder.makeIntRegister(
initialValue: minTrips)
} else {
minTripsReg = nil
}
let maxExtraTripsReg: IntRegister?
if (maxExtraTrips ?? 0) > 0 {
maxExtraTripsReg = builder.makeIntRegister(
initialValue: maxExtraTrips!)
} else {
maxExtraTripsReg = nil
}
// Set up a dummy save point for possessive to update
if updatedKind == .possessive {
builder.pushEmptySavePoint()
}
// min-trip-count:
// condBranch(to: exitPolicy, ifZeroElseDecrement: %min)
builder.label(minTripsControl)
switch minTrips {
case 0: builder.buildBranch(to: exitPolicy)
case 1: break
default:
assert(minTripsReg != nil, "logic inconsistency")
builder.buildCondBranch(
to: exitPolicy, ifZeroElseDecrement: minTripsReg!)
}
// FIXME: Possessive needs a "dummy" save point to ratchet
// loop:
// <subexpression>
// branch min-trip-count
builder.label(loopBody)
// if we aren't sure if the child node will have forward progress and
// we have an unbounded quantification
let startPosition: PositionRegister?
let emitPositionChecking =
(!optimizationsEnabled || !child.guaranteesForwardProgress) &&
maxExtraTrips == nil
if emitPositionChecking {
startPosition = builder.makePositionRegister()
builder.buildMoveCurrentPosition(into: startPosition!)
} else {
startPosition = nil
}
try emitNode(child)
if emitPositionChecking {
// in all quantifier cases, no matter what minTrips or maxExtraTrips is,
// if we have a successful non-advancing match, branch to exit because it
// can match an arbitrary number of times
builder.buildCondBranch(to: exit, ifSamePositionAs: startPosition!)
}
if minTrips <= 1 {
// fallthrough
} else {
builder.buildBranch(to: minTripsControl)
}
// exit-policy:
// condBranch(to: exit, ifZeroElseDecrement: %maxExtraTrips)
// <eager: split(to: loop, saving: exit)>
// <possesive:
// clearSavePoint
// split(to: loop, saving: exit)>
// <reluctant: save(restoringAt: loop)
builder.label(exitPolicy)
switch maxExtraTrips {
case nil: break
case 0: builder.buildBranch(to: exit)
default:
assert(maxExtraTripsReg != nil, "logic inconsistency")
builder.buildCondBranch(
to: exit, ifZeroElseDecrement: maxExtraTripsReg!)
}
switch updatedKind {
case .eager:
builder.buildSplit(to: loopBody, saving: exit)
case .possessive:
builder.buildClear()
builder.buildSplit(to: loopBody, saving: exit)
case .reluctant:
builder.buildSave(loopBody)
// FIXME: Is this re-entrant? That is would nested
// quantification break if trying to restore to a prior
// iteration because the register got overwritten?
//
#if RESILIENT_LIBRARIES
@unknown default:
fatalError()
#endif
}
builder.label(exit)
}
/// Specialized quantification instruction for repetition of certain nodes in grapheme semantic mode
/// Allowed nodes are:
/// - single ascii scalar .char
/// - ascii .customCharacterClass
/// - single grapheme consumgin built in character classes
/// - .any, .anyNonNewline, .dot
mutating func tryEmitFastQuant(
_ child: DSLTree.Node,
_ kind: AST.Quantification.Kind,
_ minTrips: Int,
_ maxExtraTrips: Int?
) -> Bool {
let isScalarSemantics = options.semanticLevel == .unicodeScalar
guard optimizationsEnabled
&& minTrips <= QuantifyPayload.maxStorableTrips
&& maxExtraTrips ?? 0 <= QuantifyPayload.maxStorableTrips
&& kind != .reluctant else {
return false
}
switch child {
case .customCharacterClass(let ccc):
// ascii only custom character class
guard let bitset = ccc.asAsciiBitset(options) else {
return false
}
builder.buildQuantify(bitset: bitset, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .atom(let atom):
switch atom {
case .char(let c):
// Single scalar ascii value character
guard let val = c._singleScalarAsciiValue else {
return false
}
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .any:
builder.buildQuantifyAny(
matchesNewlines: true, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .anyNonNewline:
builder.buildQuantifyAny(
matchesNewlines: false, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .dot:
builder.buildQuantifyAny(
matchesNewlines: options.dotMatchesNewline, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .characterClass(let cc):
// Custom character class that consumes a single grapheme
let model = cc.asRuntimeModel(options)
builder.buildQuantify(
model: model,
kind,
minTrips,
maxExtraTrips,
isScalarSemantics: isScalarSemantics)
default:
return false
}
case .convertedRegexLiteral(let node, _):
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
case .nonCapturingGroup(let groupKind, let node):
// .nonCapture nonCapturingGroups are ignored during compilation
guard groupKind.ast == .nonCapture else {
return false
}
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
default:
return false
}
return true
}
/// Coalesce any adjacent scalar members in a custom character class together.
/// This is required in order to produce correct grapheme matching behavior.
func coalescingCustomCharacterClassMembers(
_ members: [DSLTree.CustomCharacterClass.Member]
) -> [DSLTree.CustomCharacterClass.Member] {
struct Accumulator {
/// A series of range operands. For example, in `[ab-cde-fg]`, this will
/// contain the strings `["ab", "cde", "fg"]`. From there, the resulting
/// ranges will be created.
private var rangeOperands: [String] = [""]
/// The current range operand.
private var current: String {
_read { yield rangeOperands[rangeOperands.count - 1] }
_modify { yield &rangeOperands[rangeOperands.count - 1] }
}
/// Try to accumulate a character class member, returning `true` if
/// successful, `false` otherwise.
mutating func tryAccumulate(
_ member: DSLTree.CustomCharacterClass.Member
) -> Bool {
switch member {
case .atom(let a):
guard let c = a.literalCharacterValue else { return false }
current.append(c)
return true
case .quotedLiteral(let str):
current += str
return true
case let .range(lhs, rhs):
guard let lhs = lhs.literalCharacterValue,
let rhs = rhs.literalCharacterValue
else { return false }
current.append(lhs)
rangeOperands.append(String(rhs))
return true
case .trivia:
// Trivia can be completely ignored if we've already coalesced
// something.
return !current.isEmpty
default:
return false
}
}
func finish() -> [DSLTree.CustomCharacterClass.Member] {
if rangeOperands.count == 1 {
// If we didn't have any additional range operands, this isn't a
// range, we can just form a standard quoted literal.
return [.quotedLiteral(current)]
}
var members = [DSLTree.CustomCharacterClass.Member]()
// We have other range operands, splice them together. For N operands
// we have N - 1 ranges.
for (i, lhs) in rangeOperands.dropLast().enumerated() {
let rhs = rangeOperands[i + 1]
// If this is the first operand we only need to drop the last
// character for its quoted members, otherwise this is both an LHS
// and RHS of a range, and as such needs both sides trimmed.
let leading = i == 0 ? lhs.dropLast() : lhs.dropFirst().dropLast()
if !leading.isEmpty {
members.append(.quotedLiteral(String(leading)))
}
members.append(.range(.char(lhs.last!), .char(rhs.first!)))
}
// We've handled everything except the quoted portion of the last
// operand, add it now.
let trailing = rangeOperands.last!.dropFirst()
if !trailing.isEmpty {
members.append(.quotedLiteral(String(trailing)))
}
return members
}
}
return members
.map { m -> DSLTree.CustomCharacterClass.Member in
// First we need to recursively coalsce any child character classes.
switch m {
case .custom(let ccc):
return .custom(coalescingCustomCharacterClass(ccc))
case .intersection(let lhs, let rhs):
return .intersection(
coalescingCustomCharacterClass(lhs),
coalescingCustomCharacterClass(rhs))
case .subtraction(let lhs, let rhs):
return .subtraction(
coalescingCustomCharacterClass(lhs),
coalescingCustomCharacterClass(rhs))
case .symmetricDifference(let lhs, let rhs):
return .symmetricDifference(
coalescingCustomCharacterClass(lhs),
coalescingCustomCharacterClass(rhs))
case .atom, .range, .quotedLiteral, .trivia:
return m
}
}
.coalescing(with: Accumulator(), into: { $0.finish() }) { accum, member in
accum.tryAccumulate(member)
}
}
/// Flatten quoted strings into sequences of atoms, so that the standard
/// CCC codegen will handle them.
func flatteningCustomCharacterClassMembers(
_ members: [DSLTree.CustomCharacterClass.Member]
) -> [DSLTree.CustomCharacterClass.Member] {
var characters: Set<Character> = []
var scalars: Set<UnicodeScalar> = []
var result: [DSLTree.CustomCharacterClass.Member] = []
for member in members {
switch member {
case .atom(let atom):
switch atom {
case let .char(char):
characters.insert(char)
case let .scalar(scalar):
scalars.insert(scalar)
default:
result.append(member)
}
case let .quotedLiteral(str):
characters.formUnion(str)
default:
result.append(member)
}
}
result.append(contentsOf: characters.map { .atom(.char($0)) })
result.append(contentsOf: scalars.map { .atom(.scalar($0)) })
return result
}
func coalescingCustomCharacterClass(
_ ccc: DSLTree.CustomCharacterClass
) -> DSLTree.CustomCharacterClass {
// This only needs to be done in grapheme semantic mode. In scalar semantic
// mode, we don't want to coalesce any scalars into a grapheme. This
// means that e.g `[e\u{301}-\u{302}]` remains a range between U+301 and
// U+302.
let members = options.semanticLevel == .graphemeCluster
? coalescingCustomCharacterClassMembers(ccc.members)
: ccc.members
return .init(
members: flatteningCustomCharacterClassMembers(members),
isInverted: ccc.isInverted)
}
mutating func emitCharacterInCCC(_ c: Character) {
switch options.semanticLevel {
case .graphemeCluster:
emitCharacter(c)
case .unicodeScalar:
// When in scalar mode, act like an alternation of the individual scalars
// that comprise a character.
emitAlternationGen(c.unicodeScalars, withBacktracking: false) {
$0.emitMatchScalar($1)
}
}
}
mutating func emitCCCMember(
_ member: DSLTree.CustomCharacterClass.Member
) throws {
switch member {
case .atom(let atom):
switch atom {
case .char(let c):
emitCharacterInCCC(c)
case .scalar(let s):
emitCharacterInCCC(Character(s))
default:
try emitAtom(atom)
}
case .custom(let ccc):
try emitCustomCharacterClass(ccc)
case .quotedLiteral:
fatalError("Removed in 'flatteningCustomCharacterClassMembers'")
case .range:
let consumer = try member.generateConsumer(options)
builder.buildConsume(by: consumer)
case .trivia:
return
// TODO: Can we decide when it's better to try `rhs` first?
// Intersection is trivial, since failure on either side propagates:
// - store current position
// - lhs
// - restore current position
// - rhs
case let .intersection(lhs, rhs):
let r = builder.makePositionRegister()
builder.buildMoveCurrentPosition(into: r)
try emitCustomCharacterClass(lhs)
builder.buildRestorePosition(from: r)
try emitCustomCharacterClass(rhs)
// TODO: Can we decide when it's better to try `rhs` first?
// For subtraction, failure in `lhs` propagates, while failure in `rhs` is
// swallowed/reversed:
// - store current position
// - lhs
// - save to end
// - restore current position
// - rhs
// - clear, fail (since both succeeded)
// - end: ...
case let .subtraction(lhs, rhs):
let r = builder.makePositionRegister()
let end = builder.makeAddress()
builder.buildMoveCurrentPosition(into: r)
try emitCustomCharacterClass(lhs) // no match here = failure, propagates
builder.buildSave(end)
builder.buildRestorePosition(from: r)
try emitCustomCharacterClass(rhs) // no match here = success, resumes at 'end'
builder.buildClear() // clears 'end'
builder.buildFail() // this failure propagates outward
builder.label(end)
// Symmetric difference always requires executing both `rhs` and `lhs`.
// Execute each, ignoring failure and storing the resulting position in a
// register. If those results are equal, fail. If they're different, use
// the position that is different from the starting position:
// - store current position as r0
// - save to lhsFail
// - lhs
// - clear lhsFail (and continue)
// - lhsFail: save position as r1
//
// - restore current position
// - save to rhsFail
// - rhs
// - clear rhsFail (and continue)
// - rhsFail: save position as r2
//
// - restore to resulting position from lhs (r1)
// - if equal to r2, goto fail (both sides had same result)
// - if equal to r0, goto advance (lhs failed)
// - goto end
// - advance: restore to resulting position from rhs (r2)
// - goto end
// - fail: fail
// - end: ...
case let .symmetricDifference(lhs, rhs):
let r0 = builder.makePositionRegister()
let r1 = builder.makePositionRegister()
let r2 = builder.makePositionRegister()
let lhsFail = builder.makeAddress()
let rhsFail = builder.makeAddress()
let advance = builder.makeAddress()
let fail = builder.makeAddress()
let end = builder.makeAddress()
builder.buildMoveCurrentPosition(into: r0)
builder.buildSave(lhsFail)
try emitCustomCharacterClass(lhs)
builder.buildClear()
builder.label(lhsFail)
builder.buildMoveCurrentPosition(into: r1)
builder.buildRestorePosition(from: r0)
builder.buildSave(rhsFail)
try emitCustomCharacterClass(rhs)
builder.buildClear()
builder.label(rhsFail)
builder.buildMoveCurrentPosition(into: r2)
// If r1 == r2, then fail
builder.buildRestorePosition(from: r1)
builder.buildCondBranch(to: fail, ifSamePositionAs: r2)
// If r1 == r0, then move to r2 before ending
builder.buildCondBranch(to: advance, ifSamePositionAs: r0)
builder.buildBranch(to: end)
builder.label(advance)
builder.buildRestorePosition(from: r2)
builder.buildBranch(to: end)
builder.label(fail)
builder.buildFail()
builder.label(end)
}
}
mutating func emitCustomCharacterClass(
_ ccc: DSLTree.CustomCharacterClass
) throws {
// Before emitting a custom character class in grapheme semantic mode, we
// need to coalesce together any adjacent characters and scalars, over which
// we can perform grapheme breaking. This includes e.g range bounds for
// `[e\u{301}-\u{302}]`.
let ccc = coalescingCustomCharacterClass(ccc)
if let asciiBitset = ccc.asAsciiBitset(options),
optimizationsEnabled {
if options.semanticLevel == .unicodeScalar {
builder.buildScalarMatchAsciiBitset(asciiBitset)
} else {
builder.buildMatchAsciiBitset(asciiBitset)
}
return
}
let updatedCCC: DSLTree.CustomCharacterClass
if optimizationsEnabled {
updatedCCC = ccc.coalescingASCIIMembers(options)
} else {
updatedCCC = ccc
}
let filteredMembers = updatedCCC.members.filter({!$0.isOnlyTrivia})
if updatedCCC.isInverted {
// inverted
// custom character class: p0 | p1 | ... | pn
// Try each member to make sure they all fail
// save next_p1
// <code for p0>
// clear, fail
// next_p1:
// save next_p2
// <code for p1>
// clear fail
// next_p2:
// save next_p...
// <code for p2>
// clear fail
// ...
// next_pn:
// save done
// <code for pn>
// clear fail
// done:
// step forward by 1
let done = builder.makeAddress()
for member in filteredMembers.dropLast() {
let next = builder.makeAddress()
builder.buildSave(next)
try emitCCCMember(member)
builder.buildClear()
builder.buildFail()
builder.label(next)
}
builder.buildSave(done)
try emitCCCMember(filteredMembers.last!)
builder.buildClear()
builder.buildFail()
builder.label(done)
// Consume a single unit for the inverted ccc
switch options.semanticLevel {
case .graphemeCluster:
builder.buildAdvance(1)
case .unicodeScalar:
builder.buildAdvanceUnicodeScalar(1)
}
return
}
// non inverted CCC
// Custom character class: p0 | p1 | ... | pn
// Very similar to alternation, but we don't keep backtracking save points
try emitAlternationGen(filteredMembers, withBacktracking: false) {
try $0.emitCCCMember($1)
}
}
mutating func emitConcatenation(_ children: [DSLTree.Node]) throws {
// Before emitting a concatenation, we need to flatten out any nested
// concatenations, and coalesce any adjacent characters and scalars, forming
// quoted literals of their contents, over which we can perform grapheme
// breaking.
func flatten(_ node: DSLTree.Node) -> [DSLTree.Node] {
switch node {
case .concatenation(let ch):
return ch.flatMap(flatten)
case .convertedRegexLiteral(let n, _), .ignoreCapturesInTypedOutput(let n):
return flatten(n)
default:
return [node]
}
}
let children = children
.flatMap(flatten)
.coalescing(with: "", into: DSLTree.Node.quotedLiteral) { str, node in
switch node {
case .atom(let a):
guard let c = a.literalCharacterValue else { return false }
str.append(c)
return true
case .quotedLiteral(let q):
str += q
return true
case .trivia:
// Trivia can be completely ignored if we've already coalesced
// something.
return !str.isEmpty
default:
return false
}
}
for child in children {
try emitConcatenationComponent(child)
}
}
@discardableResult
mutating func emitNode(_ node: DSLTree.Node) throws -> ValueRegister? {
switch node {
case let .orderedChoice(children):
try emitAlternation(children)
case let .concatenation(children):
try emitConcatenation(children)
case let .capture(name, refId, child, transform):
options.beginScope()
defer { options.endScope() }
let cap = builder.makeCapture(id: refId, name: name)
builder.buildBeginCapture(cap)
let value = try emitNode(child)
builder.buildEndCapture(cap)
// If the child node produced a custom capture value, e.g. the result of
// a matcher, this should override the captured substring.
if let value {
builder.buildMove(value, into: cap)
}
// If there's a capture transform, apply it now.
if let transform = transform {
let fn = builder.makeTransformFunction { input, cap in
// If it's a substring capture with no custom value, apply the
// transform directly to the substring to avoid existential traffic.
//
// FIXME: separate out this code path. This is fragile,
// slow, and these are clearly different constructs
if let range = cap.range, cap.value == nil {
return try transform(input[range])
}
let value = constructExistentialOutputComponent(
from: input,
component: cap.deconstructed,
optionalCount: 0)
return try transform(value)
}
builder.buildTransformCapture(cap, fn)
}
case let .nonCapturingGroup(kind, child):
try emitNoncapturingGroup(kind.ast, child)
case let .ignoreCapturesInTypedOutput(child):
try emitNode(child)
case .conditional:
throw Unsupported("Conditionals")
case let .quantification(amt, kind, child):
try emitQuantification(amt.ast, kind, child)
case let .customCharacterClass(ccc):
if ccc.containsDot {
if !ccc.isInverted {
try emitDot()
} else {
throw Unsupported("Inverted any")
}
} else {
try emitCustomCharacterClass(ccc)
}
case let .atom(a):
try emitAtom(a)
case let .quotedLiteral(s):
emitQuotedLiteral(s)
case let .convertedRegexLiteral(n, _):
return try emitNode(n)
case .absentFunction:
throw Unsupported("absent function")
case .consumer:
throw Unsupported("consumer")
case let .matcher(_, f):
return emitMatcher(f)
case .characterPredicate:
throw Unsupported("character predicates")
case .trivia, .empty:
return nil
}
return nil
}
}
extension DSLTree.Node {
/// A Boolean value indicating whether this node advances the match position
/// on a successful match.
///
/// For example, an alternation like `(a|b|c)` always advances the position
/// by a character, but `(a|b|)` has an empty branch, which matches without
/// advancing.
var guaranteesForwardProgress: Bool {
switch self {
case .orderedChoice(let children):
return children.allSatisfy { $0.guaranteesForwardProgress }
case .concatenation(let children):
return children.contains(where: { $0.guaranteesForwardProgress })
case .capture(_, _, let node, _):
return node.guaranteesForwardProgress
case .nonCapturingGroup(let kind, let child):
switch kind.ast {
case .lookahead, .negativeLookahead, .lookbehind, .negativeLookbehind:
return false
default: return child.guaranteesForwardProgress
}
case .atom(let atom):
switch atom {
case .changeMatchingOptions, .assertion: return false
// Captures may be nil so backreferences may be zero length matches
case .backreference: return false
default: return true
}
case .trivia, .empty:
return false
case .quotedLiteral(let string):
return !string.isEmpty
case .convertedRegexLiteral(let node, _):
return node.guaranteesForwardProgress
case .consumer, .matcher:
// Allow zero width consumers and matchers
return false
case .customCharacterClass(let ccc):
return ccc.guaranteesForwardProgress
case .quantification(let amount, _, let child):
let (atLeast, _) = amount.ast.bounds
return atLeast ?? 0 > 0 && child.guaranteesForwardProgress
default: return false
}
}
}
extension DSLTree.CustomCharacterClass {
/// We allow trivia into CustomCharacterClass, which could result in a CCC
/// that matches nothing, ie `(?x)[ ]`.
var guaranteesForwardProgress: Bool {
for m in members {
switch m {
case .trivia:
continue
case let .intersection(lhs, rhs):
return lhs.guaranteesForwardProgress && rhs.guaranteesForwardProgress
case let .subtraction(lhs, _):
return lhs.guaranteesForwardProgress
case let .symmetricDifference(lhs, rhs):
return lhs.guaranteesForwardProgress && rhs.guaranteesForwardProgress
default:
return true
}
}
return false
}
}
|