Skip to content

Commit 9e95b1a

Browse files
authored
Merge pull request #38 from kareman/cleanup
Cleanup
2 parents c7bf5d5 + 2149fd3 commit 9e95b1a

File tree

12 files changed

+123
-135
lines changed

12 files changed

+123
-135
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/Patterns.xcscheme

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES"
30+
disableMainThreadChecker = "YES"
3031
codeCoverageEnabled = "YES">
3132
<Testables>
3233
<TestableReference

Playground/MyPlayground.playground/Contents.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// ℹ️ Please restart Xcode if autocomplete is not working.
55

66
import Patterns
7-
import PlaygroundSupport
8-
import SwiftUI
97

108
let text = """
119
I can eat glass and it doesn't hurt me.
@@ -27,8 +25,4 @@ let p = Capture(name: ">=6", letter.repeat(6...))
2725
/ Capture(name: "2...3", letter.repeat(2 ... 3))
2826
/ Capture(name: "1", letter)
2927

30-
let view = try ParserView(text: text, pattern: p)
31-
let size = NSSize(width: 600, height: 600)
32-
let hosting = NSHostingController(rootView: view)
33-
hosting.view.frame.size = size
34-
PlaygroundPage.current.setLiveView(hosting)
28+
try showParserView(pattern: p, withText: text)

Playground/MyPlayground.playground/Sources/General.swift

+9
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,12 @@ struct SingleLineLabel: NSViewRepresentable {
8888

8989
func updateNSView(_ nsView: TheUIView, context: Context) {}
9090
}
91+
92+
import PlaygroundSupport
93+
94+
public func showParserView<P: Patterns.Pattern>(ofSize size: NSSize = NSSize(width: 600, height: 600), pattern: P, withText text: String) throws {
95+
let view = try ParserView(text: text, pattern: pattern)
96+
let hosting = NSHostingController(rootView: view)
97+
hosting.view.frame.size = size
98+
PlaygroundPage.current.setLiveView(hosting)
99+
}

Playground/Playground.xcodeproj/project.pbxproj

+58-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 46;
6+
objectVersion = 52;
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
BA82FFEF249E5CAE00D38ABA /* SE0270_RangeSet in Frameworks */ = {isa = PBXBuildFile; productRef = BA82FFEE249E5CAE00D38ABA /* SE0270_RangeSet */; };
11+
BA82FFF1249E5D2400D38ABA /* Optimise Instructions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA82FFF0249E5D2400D38ABA /* Optimise Instructions.swift */; };
1012
BA92EA232495240C000A854B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA92EA222495240C000A854B /* AppDelegate.swift */; };
1113
BA92EA272495240D000A854B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BA92EA262495240D000A854B /* Assets.xcassets */; };
1214
BA92EA2A2495240D000A854B /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BA92EA292495240D000A854B /* Preview Assets.xcassets */; };
@@ -73,6 +75,7 @@
7375
/* End PBXCopyFilesBuildPhase section */
7476

7577
/* Begin PBXFileReference section */
78+
BA82FFF0249E5D2400D38ABA /* Optimise Instructions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Optimise Instructions.swift"; sourceTree = "<group>"; };
7679
BA92EA202495240C000A854B /* PlaygroundView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PlaygroundView.app; sourceTree = BUILT_PRODUCTS_DIR; };
7780
BA92EA222495240C000A854B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7881
BA92EA262495240D000A854B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -123,6 +126,7 @@
123126
isa = PBXFrameworksBuildPhase;
124127
buildActionMask = 0;
125128
files = (
129+
BA82FFEF249E5CAE00D38ABA /* SE0270_RangeSet in Frameworks */,
126130
);
127131
runOnlyForDeploymentPostprocessing = 0;
128132
};
@@ -195,6 +199,7 @@
195199
OBJ_13 /* Patterns */ = {
196200
isa = PBXGroup;
197201
children = (
202+
BA82FFF0249E5D2400D38ABA /* Optimise Instructions.swift */,
198203
OBJ_14 /* Atomic Patterns */,
199204
OBJ_19 /* Decoder.swift */,
200205
OBJ_20 /* General */,
@@ -327,6 +332,9 @@
327332
dependencies = (
328333
);
329334
name = Patterns;
335+
packageProductDependencies = (
336+
BA82FFEE249E5CAE00D38ABA /* SE0270_RangeSet */,
337+
);
330338
productName = Patterns;
331339
productReference = "Patterns::Patterns::Product" /* Patterns.framework */;
332340
productType = "com.apple.product-type.framework";
@@ -401,6 +409,9 @@
401409
Base,
402410
);
403411
mainGroup = OBJ_5;
412+
packageReferences = (
413+
BA82FFED249E5CAE00D38ABA /* XCRemoteSwiftPackageReference "swift-se0270-range-set" */,
414+
);
404415
productRefGroup = OBJ_39 /* Products */;
405416
projectDirPath = "";
406417
projectRoot = "";
@@ -453,6 +464,7 @@
453464
OBJ_56 /* And.swift in Sources */,
454465
OBJ_57 /* AnyPattern.swift in Sources */,
455466
OBJ_58 /* Capture.swift in Sources */,
467+
BA82FFF1249E5D2400D38ABA /* Optimise Instructions.swift in Sources */,
456468
OBJ_59 /* Choice.swift in Sources */,
457469
OBJ_60 /* Concatenation.swift in Sources */,
458470
OBJ_61 /* Not.swift in Sources */,
@@ -569,7 +581,10 @@
569581
GCC_WARN_UNUSED_FUNCTION = YES;
570582
GCC_WARN_UNUSED_VARIABLE = YES;
571583
INFOPLIST_FILE = PlaygroundView/Info.plist;
572-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
584+
LD_RUNPATH_SEARCH_PATHS = (
585+
"$(inherited)",
586+
"@executable_path/../Frameworks",
587+
);
573588
MACOSX_DEPLOYMENT_TARGET = 10.15;
574589
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
575590
MTL_FAST_MATH = YES;
@@ -629,7 +644,10 @@
629644
GCC_WARN_UNUSED_FUNCTION = YES;
630645
GCC_WARN_UNUSED_VARIABLE = YES;
631646
INFOPLIST_FILE = PlaygroundView/Info.plist;
632-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
647+
LD_RUNPATH_SEARCH_PATHS = (
648+
"$(inherited)",
649+
"@executable_path/../Frameworks",
650+
);
633651
MACOSX_DEPLOYMENT_TARGET = 10.15;
634652
MTL_ENABLE_DEBUG_INFO = NO;
635653
MTL_FAST_MATH = YES;
@@ -685,7 +703,8 @@
685703
SDKROOT = macosx;
686704
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator";
687705
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) SWIFT_PACKAGE";
688-
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
706+
SWIFT_COMPILATION_MODE = wholemodule;
707+
SWIFT_OPTIMIZATION_LEVEL = "-O";
689708
USE_HEADERMAP = NO;
690709
};
691710
name = Release;
@@ -701,7 +720,10 @@
701720
HEADER_SEARCH_PATHS = "$(inherited)";
702721
INFOPLIST_FILE = Playground.xcodeproj/Patterns_Info.plist;
703722
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
704-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
723+
LD_RUNPATH_SEARCH_PATHS = (
724+
"$(inherited)",
725+
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
726+
);
705727
MACOSX_DEPLOYMENT_TARGET = 10.10;
706728
OTHER_CFLAGS = "$(inherited)";
707729
OTHER_LDFLAGS = "$(inherited)";
@@ -729,7 +751,10 @@
729751
HEADER_SEARCH_PATHS = "$(inherited)";
730752
INFOPLIST_FILE = Playground.xcodeproj/Patterns_Info.plist;
731753
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
732-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
754+
LD_RUNPATH_SEARCH_PATHS = (
755+
"$(inherited)",
756+
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
757+
);
733758
MACOSX_DEPLOYMENT_TARGET = 10.10;
734759
OTHER_CFLAGS = "$(inherited)";
735760
OTHER_LDFLAGS = "$(inherited)";
@@ -775,7 +800,10 @@
775800
HEADER_SEARCH_PATHS = "$(inherited)";
776801
INFOPLIST_FILE = Playground.xcodeproj/Playground_Info.plist;
777802
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
778-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
803+
LD_RUNPATH_SEARCH_PATHS = (
804+
"$(inherited)",
805+
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
806+
);
779807
MACOSX_DEPLOYMENT_TARGET = 10.10;
780808
OTHER_CFLAGS = "$(inherited)";
781809
OTHER_LDFLAGS = "$(inherited)";
@@ -803,7 +831,10 @@
803831
HEADER_SEARCH_PATHS = "$(inherited)";
804832
INFOPLIST_FILE = Playground.xcodeproj/Playground_Info.plist;
805833
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
806-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
834+
LD_RUNPATH_SEARCH_PATHS = (
835+
"$(inherited)",
836+
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
837+
);
807838
MACOSX_DEPLOYMENT_TARGET = 10.10;
808839
OTHER_CFLAGS = "$(inherited)";
809840
OTHER_LDFLAGS = "$(inherited)";
@@ -896,6 +927,25 @@
896927
defaultConfigurationName = Release;
897928
};
898929
/* End XCConfigurationList section */
930+
931+
/* Begin XCRemoteSwiftPackageReference section */
932+
BA82FFED249E5CAE00D38ABA /* XCRemoteSwiftPackageReference "swift-se0270-range-set" */ = {
933+
isa = XCRemoteSwiftPackageReference;
934+
repositoryURL = "https://github.com/apple/swift-se0270-range-set";
935+
requirement = {
936+
kind = upToNextMajorVersion;
937+
minimumVersion = 1.0.0;
938+
};
939+
};
940+
/* End XCRemoteSwiftPackageReference section */
941+
942+
/* Begin XCSwiftPackageProductDependency section */
943+
BA82FFEE249E5CAE00D38ABA /* SE0270_RangeSet */ = {
944+
isa = XCSwiftPackageProductDependency;
945+
package = BA82FFED249E5CAE00D38ABA /* XCRemoteSwiftPackageReference "swift-se0270-range-set" */;
946+
productName = SE0270_RangeSet;
947+
};
948+
/* End XCSwiftPackageProductDependency section */
899949
};
900950
rootObject = OBJ_1 /* Project object */;
901951
}

Playground/Playground.xcworkspace/contents.xcworkspacedata

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/Patterns/Operations on Patterns/Skip.swift

+5-23
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,17 @@
55
// Created by Kåre Morstøl on 25/05/2020.
66
//
77

8-
public struct Skip<Repeated: Pattern>: Pattern {
9-
public let repeatedPattern: Repeated?
10-
public let description: String
8+
public struct Skip: Pattern {
9+
public var description: String { "Skip()" }
1110

12-
public init(_ repeatedPattern: Repeated) {
13-
self.repeatedPattern = repeatedPattern
14-
self.description = "Skip(\(repeatedPattern))"
15-
}
11+
public init() {}
1612

1713
public func createInstructions(_ instructions: inout Instructions) throws {
1814
instructions.append(.skip)
1915
instructions.append(.jump(offset: 1)) // dummy
2016
}
2117
}
2218

23-
extension Skip where Repeated == AnyPattern {
24-
public init() {
25-
self.description = "Skip()"
26-
self.repeatedPattern = nil
27-
}
28-
}
29-
30-
extension Skip where Repeated == Literal {
31-
public init(_ repeatedPattern: Literal) {
32-
self.repeatedPattern = repeatedPattern
33-
self.description = "Skip(\(repeatedPattern))"
34-
}
35-
}
36-
3719
import SE0270_RangeSet
3820

3921
extension VMBacktrackEngine {
@@ -108,12 +90,12 @@ extension VMBacktrackEngine {
10890
i += offset
10991
case .elementEquals, .checkElement, .checkIndex, .moveIndex, .captureStart, .captureEnd, .call:
11092
i += 1
111-
case .commit, .choiceEnd, .return, .match, .skip:
93+
case .commit, .choiceEnd, .return, .match, .skip, .search:
11294
let dummyIndex = skipindex + 1
11395
instructions.moveSubranges(RangeSet(dummyIndex ..< (dummyIndex + 1)), to: i)
11496
instructions[i - 1] = .commit
11597
return
116-
case .fail, .function, .openCall:
98+
case .fail, .openCall:
11799
fatalError()
118100
}
119101
}

Sources/Patterns/Pattern And Instruction.swift

+8-14
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public enum Instruction<Input: BidirectionalCollection> where Input.Element: Has
3939
/// Continues with the instruction at `offset` relative to this instruction.
4040
case jump(offset: Distance)
4141

42-
/// Will be replaced by .search in preprocessing. Is never executed.
43-
case skip
44-
case function((Input, inout VMBacktrackEngine<Input>.Thread) -> Bool) // TODO: Replace with “search”
42+
/// Sets the input index to the output from the closure.
43+
/// If the output is nil, the instruction fails.
44+
case search((Input, Input.Index) -> Input.Index?)
4545

4646
/// Stores the current index as the beginning of capture `name`
4747
case captureStart(name: String?, atIndexOffset: Int)
@@ -50,7 +50,7 @@ public enum Instruction<Input: BidirectionalCollection> where Input.Element: Has
5050

5151
/// Stores a snapshot of the current state. If there is a future failure the snapshot will be restored
5252
/// and the instruction at `offset` (relative to this instruction) will be called.
53-
case choice(offset: Distance, atIndexOffset: Int) // TODO: remove atIndexOffset
53+
case choice(offset: Distance, atIndexOffset: Int)
5454
/// Signals the end of a choice. Doesn't do anything else.
5555
/// Used as a barrier across which instructions cannot be moved.
5656
case choiceEnd
@@ -72,6 +72,9 @@ public enum Instruction<Input: BidirectionalCollection> where Input.Element: Has
7272
/// Will not continue with further instructions.
7373
case match
7474

75+
/// Will be replaced in preprocessing. Is never executed.
76+
case skip
77+
7578
/// Succeeds anywhere except at the end of the input.
7679
static var any: Self { Self.checkElement { _ in true } } // TODO: make its own instruction
7780

@@ -91,15 +94,6 @@ public enum Instruction<Input: BidirectionalCollection> where Input.Element: Has
9194
.choice(offset: offset, atIndexOffset: 0)
9295
}
9396

94-
static func search(_ f: @escaping (Input, Input.Index) -> Input.Index?) -> Self {
95-
Self.function { (input: Input, thread: inout VMBacktrackEngine<Input>.Thread) -> Bool in
96-
guard let index = f(input, thread.inputIndex) else { return false }
97-
thread.inputIndex = index
98-
thread.instructionIndex += 1
99-
return true
100-
}
101-
}
102-
10397
/// The offset by which this instruction will move the input index.
10498
var movesIndexBy: Int? {
10599
switch self {
@@ -109,7 +103,7 @@ public enum Instruction<Input: BidirectionalCollection> where Input.Element: Has
109103
return 1
110104
case let .moveIndex(offset):
111105
return offset
112-
case .function, .choice, .jump, .openCall, .call, .return, .fail, .skip:
106+
case .search, .choice, .jump, .openCall, .call, .return, .fail, .skip:
113107
return nil
114108
}
115109
}

Sources/Patterns/Regex.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ extension RepeatPattern: RegexConvertible where Repeated: RegexConvertible {
5353
}
5454
}
5555

56-
extension Skip: RegexConvertible where Repeated: RegexConvertible {
57-
public var regex: String { repeatedPattern.map { "(?:\($0.regex))*?" } ?? ".*?" }
56+
extension Skip: RegexConvertible {
57+
public var regex: String { ".*?" }
5858
}

0 commit comments

Comments
 (0)