Skip to content

Commit edb72e8

Browse files
mbrandonwstephencelisrandomeizer
authored
Parser-printers (#194)
* wip * don't backtrack * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * rank one of errors * rank errors by most processed * nested * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * from * wip * wip * wip * Update benches * wip * idiomatic * wip * wip * wip * truncate * wip * wip * wip * wip * clean up * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Some docs and update tests to use public conversion * removed testable imports * lots of docs * wip * wip * wip * many terminator failure * wip * add loop error * force MapConversion to work only on printers * bye bye exactly * wip * wip * Added reversion to original if any parsers in `Zip` variations throw an error (#107) * Added reversion to original if any parsers in `Zip` variations throw an error. * Updated ParserBuilderTests to check correct consumption of the input * wip * fix * wip * wip * wip * wip * wip * wip * fix * wip * wip * wip * wip * Bump package tools version * wip * fix * backtrack printing * wip * wip * wip * wip * wip * New VoidMap parser and updated race parser. * wip * pullback * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * docs * wip * wip * wip * wip * wip * wip * wiop * print tests for PrefixUpTo/Through * Printer prepending instead of appending (#145) * Pointing `swift-custom-dump` to the "main" branch, since `0.4.0` hasn't been released publicly yet. * Switched printing to `prepend` rather than `append` Consequentially, all parsers that compose multiple parsers work backwards through provided parsers, and the `input` is built up from the back to the front. This allows print to check that the output is correct for cases like `Rest`, `Prefix`, `Not`, and `Peek`. * Added `Many` tests, fixed terminator bug * Whitespace cleanup * Fixed typo in test name. * Updated `End` to check input when printing. Added related tests. * Switched `Literal` printing to append rather than insert They now append into a copy of themselves, then append the `input`. * Updated `PrependableCollection` to use `append` internally * Improved error output for OneOfMany * Adds backtracking to `Optionally` * Added unit test to check backtracking * Updated the `testBacktracking` case Makes it clearer under what circumstances it will fail without backtracking. * Added backtracking to Optionally Also updated documentation about backtracking in general. * Removed incorrectly merged test case. * Added `Expect` Which can be both parsed and printed. * Made the unexpected `Peek` success a passing test Added notes about why it doesn't throw an error. * wip * wip * wip * wip * wip * Update README.md Co-authored-by: Brandon Williams <[email protected]> * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * fixes Co-authored-by: Stephen Celis <[email protected]> Co-authored-by: Stephen Celis <[email protected]> Co-authored-by: Brandon Williams <[email protected]> * wip * wip * Updates Optionally to throw any internal `wrapped.print(...)` error (#167) * Pointing `swift-custom-dump` to the "main" branch, since `0.4.0` hasn't been released publicly yet. * Made Optionally fail if the wrapped parser fails Added test cases for printing, and example of failing an invalid print. * Remove `.map` overload on Always * wip * wip * Make Rest.print fail on empty output. * roundtripping doc * wip * struct conversion docs * rename .struct to .memberwise * fixes * lots of docs * docs * wip * wip * Revert "wip" This reverts commit cde658d. * Printer -> ParserPrinter * wip * wip * wip * wip * wip * wip * wip * basic errors * wip * wip * fix * Simplify `Consumed` * wip * wip * wip * decumulator * wip * wip * wip * wip * wip * wip * clean up * wip * wip * wip * wip * wip * wip * fix * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * fix docc warnings * wip * wip * wip * wip * wip * wip * wip * fix Co-authored-by: Stephen Celis <[email protected]> Co-authored-by: David Peterson <[email protected]> Co-authored-by: Stephen Celis <[email protected]>
1 parent 23842d3 commit edb72e8

File tree

151 files changed

+21704
-1604
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+21704
-1604
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1330"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "_URLRouting"
18+
BuildableName = "_URLRouting"
19+
BlueprintName = "_URLRouting"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "_URLRoutingTests"
36+
BuildableName = "_URLRoutingTests"
37+
BlueprintName = "_URLRoutingTests"
38+
ReferencedContainer = "container:">
39+
</BuildableReference>
40+
</TestableReference>
41+
</Testables>
42+
</TestAction>
43+
<LaunchAction
44+
buildConfiguration = "Debug"
45+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
46+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
47+
launchStyle = "0"
48+
useCustomWorkingDirectory = "NO"
49+
ignoresPersistentStateOnLaunch = "NO"
50+
debugDocumentVersioning = "YES"
51+
debugServiceExtension = "internal"
52+
allowLocationSimulation = "YES">
53+
</LaunchAction>
54+
<ProfileAction
55+
buildConfiguration = "Release"
56+
shouldUseLaunchSchemeArgsEnv = "YES"
57+
savedToolIdentifier = ""
58+
useCustomWorkingDirectory = "NO"
59+
debugDocumentVersioning = "YES">
60+
<MacroExpansion>
61+
<BuildableReference
62+
BuildableIdentifier = "primary"
63+
BlueprintIdentifier = "_URLRouting"
64+
BuildableName = "_URLRouting"
65+
BlueprintName = "_URLRouting"
66+
ReferencedContainer = "container:">
67+
</BuildableReference>
68+
</MacroExpansion>
69+
</ProfileAction>
70+
<AnalyzeAction
71+
buildConfiguration = "Debug">
72+
</AnalyzeAction>
73+
<ArchiveAction
74+
buildConfiguration = "Release"
75+
revealArchiveInOrganizer = "YES">
76+
</ArchiveAction>
77+
</Scheme>

.swiftpm/xcode/xcshareddata/xcschemes/swift-parsing-Package.xcscheme

+32-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
ReferencedContainer = "container:">
2121
</BuildableReference>
2222
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "ParsingTests"
32+
BuildableName = "ParsingTests"
33+
BlueprintName = "ParsingTests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
2337
<BuildActionEntry
2438
buildForTesting = "YES"
2539
buildForRunning = "YES"
@@ -37,14 +51,14 @@
3751
<BuildActionEntry
3852
buildForTesting = "YES"
3953
buildForRunning = "YES"
40-
buildForProfiling = "NO"
41-
buildForArchiving = "NO"
54+
buildForProfiling = "YES"
55+
buildForArchiving = "YES"
4256
buildForAnalyzing = "YES">
4357
<BuildableReference
4458
BuildableIdentifier = "primary"
45-
BlueprintIdentifier = "ParsingTests"
46-
BuildableName = "ParsingTests"
47-
BlueprintName = "ParsingTests"
59+
BlueprintIdentifier = "variadics-generator"
60+
BuildableName = "variadics-generator"
61+
BlueprintName = "variadics-generator"
4862
ReferencedContainer = "container:">
4963
</BuildableReference>
5064
</BuildActionEntry>
@@ -56,9 +70,9 @@
5670
buildForAnalyzing = "YES">
5771
<BuildableReference
5872
BuildableIdentifier = "primary"
59-
BlueprintIdentifier = "variadics-generator"
60-
BuildableName = "variadics-generator"
61-
BlueprintName = "variadics-generator"
73+
BlueprintIdentifier = "_URLRouting"
74+
BuildableName = "_URLRouting"
75+
BlueprintName = "_URLRouting"
6276
ReferencedContainer = "container:">
6377
</BuildableReference>
6478
</BuildActionEntry>
@@ -80,6 +94,16 @@
8094
ReferencedContainer = "container:">
8195
</BuildableReference>
8296
</TestableReference>
97+
<TestableReference
98+
skipped = "NO">
99+
<BuildableReference
100+
BuildableIdentifier = "primary"
101+
BlueprintIdentifier = "_URLRoutingTests"
102+
BuildableName = "_URLRoutingTests"
103+
BlueprintName = "_URLRoutingTests"
104+
ReferencedContainer = "container:">
105+
</BuildableReference>
106+
</TestableReference>
83107
</Testables>
84108
</TestAction>
85109
<LaunchAction

.swiftpm/xcode/xcshareddata/xcschemes/swift-parsing-benchmark.xcscheme

+21-15
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,34 @@
2020
ReferencedContainer = "container:">
2121
</BuildableReference>
2222
</BuildActionEntry>
23-
<BuildActionEntry
24-
buildForTesting = "YES"
25-
buildForRunning = "NO"
26-
buildForProfiling = "NO"
27-
buildForArchiving = "NO"
28-
buildForAnalyzing = "NO">
29-
<BuildableReference
30-
BuildableIdentifier = "primary"
31-
BlueprintIdentifier = "ParsingTests"
32-
BuildableName = "ParsingTests"
33-
BlueprintName = "ParsingTests"
34-
ReferencedContainer = "container:">
35-
</BuildableReference>
36-
</BuildActionEntry>
3723
</BuildActionEntries>
3824
</BuildAction>
3925
<TestAction
40-
buildConfiguration = "Release"
26+
buildConfiguration = "Debug"
4127
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4228
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
4329
shouldUseLaunchSchemeArgsEnv = "YES">
4430
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "ParsingTests"
36+
BuildableName = "ParsingTests"
37+
BlueprintName = "ParsingTests"
38+
ReferencedContainer = "container:">
39+
</BuildableReference>
40+
</TestableReference>
41+
<TestableReference
42+
skipped = "NO">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "_URLRoutingTests"
46+
BuildableName = "_URLRoutingTests"
47+
BlueprintName = "_URLRoutingTests"
48+
ReferencedContainer = "container:">
49+
</BuildableReference>
50+
</TestableReference>
4551
</Testables>
4652
</TestAction>
4753
<LaunchAction

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ format:
3737
find . -type f -name '*.md' -print0 | xargs -0 perl -pi -e 's/ +$$//'
3838

3939
generate-variadics:
40-
swift run variadics-generator > Sources/Parsing/Builders/Variadics.swift
40+
swift run variadics-generator \
41+
--generate-zips \
42+
--generate-one-ofs \
43+
> Sources/Parsing/Builders/Variadics.swift
44+
45+
swift run variadics-generator \
46+
--generate-path-zips \
47+
> Sources/_URLRouting/Builders/Variadics.swift
4148

4249
.PHONY: benchmarks format generate-variadics test

Package.resolved

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
"revision": "a0564bf88df5f94eec81348a2f089494c6b28d80",
1919
"version": "0.1.1"
2020
}
21+
},
22+
{
23+
"package": "swift-case-paths",
24+
"repositoryURL": "https://github.com/pointfreeco/swift-case-paths",
25+
"state": {
26+
"branch": null,
27+
"revision": "241301b67d8551c26d8f09bd2c0e52cc49f18007",
28+
"version": "0.8.0"
29+
}
2130
}
2231
]
2332
},

Package.swift

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,47 @@ let package = Package(
88
.library(
99
name: "Parsing",
1010
targets: ["Parsing"]
11-
)
11+
),
12+
.library(
13+
name: "_URLRouting",
14+
targets: ["_URLRouting"]
15+
),
1216
],
1317
dependencies: [
1418
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.5.0"),
19+
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "0.8.0"),
1520
.package(name: "Benchmark", url: "https://github.com/google/swift-benchmark", from: "0.1.1"),
1621
],
1722
targets: [
1823
.target(
19-
name: "Parsing"
24+
name: "Parsing",
25+
dependencies: [.product(name: "CasePaths", package: "swift-case-paths")]
2026
),
2127
.testTarget(
2228
name: "ParsingTests",
23-
dependencies: ["Parsing"]
29+
dependencies: [
30+
"Parsing"
31+
]
2432
),
2533
.executableTarget(
2634
name: "swift-parsing-benchmark",
2735
dependencies: [
2836
"Parsing",
37+
"_URLRouting",
2938
.product(name: "Benchmark", package: "Benchmark"),
3039
]
3140
),
3241
.executableTarget(
3342
name: "variadics-generator",
43+
dependencies: [.product(name: "ArgumentParser", package: "swift-argument-parser")]
44+
),
45+
.target(
46+
name: "_URLRouting", dependencies: ["Parsing"]
47+
),
48+
.testTarget(
49+
name: "_URLRoutingTests",
3450
dependencies: [
35-
.product(name: "ArgumentParser", package: "swift-argument-parser")
51+
"_URLRouting"
3652
]
3753
),
3854
]

Parsing.playground/Contents.swift

+77-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,89 @@
11
import Parsing
22

3-
struct Color {
4-
let red, green, blue: UInt8
3+
/*
4+
This playground builds a parser for [day 13][AoC13] of the Advent of Code 2021 challenge.
5+
6+
[AoC13]: https://adventofcode.com/2021/day/13
7+
*/
8+
9+
/// Sample input to be parsed.
10+
let input = """
11+
6,10
12+
0,14
13+
9,10
14+
0,3
15+
10,4
16+
4,11
17+
6,0
18+
6,12
19+
4,1
20+
0,13
21+
10,12
22+
3,4
23+
3,0
24+
8,4
25+
1,10
26+
2,14
27+
8,10
28+
9,0
29+
30+
fold along y=7
31+
fold along x=5
32+
"""
33+
34+
// MARK: - Models
35+
36+
struct Dot {
37+
let x, y: Int
38+
}
39+
40+
enum Direction: String, CaseIterable {
41+
case x, y
542
}
643

7-
let hexPrimary = Prefix(2)
8-
.compactMap { UInt8($0, radix: 16) }
44+
struct Fold {
45+
let direction: Direction
46+
let position: Int
47+
}
948

10-
let hexColor = Parse(Color.init(red:green:blue:)) {
11-
"#"
12-
hexPrimary
13-
hexPrimary
14-
hexPrimary
49+
struct Instructions {
50+
let dots: [Dot]
51+
let folds: [Fold]
1552
}
1653

17-
do {
18-
var hex = "#000000"[...]
19-
print(hex.debugDescription, "->", try hexColor.parse(&hex), terminator: "\n ...\n\n")
54+
// MARK: - Parsers
55+
56+
let dot = ParsePrint(.memberwise(Dot.init)) {
57+
Digits()
58+
","
59+
Digits()
2060
}
2161

22-
do {
23-
var hex = "#FF0000"[...]
24-
print(hex.debugDescription, "->", try hexColor.parse(&hex), terminator: "\n ...\n\n")
62+
let fold = ParsePrint(.memberwise(Fold.init)) {
63+
"fold along "
64+
Direction.parser()
65+
"="
66+
Digits()
2567
}
2668

27-
do {
28-
var bad = "#BADHEX"[...]
29-
try hexColor.parse(&bad)
30-
} catch {
31-
print(error)
69+
let instructions = ParsePrint(.memberwise(Instructions.init)) {
70+
Many {
71+
dot
72+
} separator: {
73+
"\n"
74+
} terminator: {
75+
"\n\n"
76+
}
77+
Many {
78+
fold
79+
} separator: {
80+
"\n"
81+
}
3282
}
83+
84+
// MARK: Round-trip
85+
86+
let parsed = try instructions.parse(input)
87+
let printed = try instructions.print(parsed)
88+
89+
input == printed
+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='macos' buildActiveScheme='true'>
3-
<timeline fileName='timeline.xctimeline'/>
4-
</playground>
2+
<playground version='6.0' target-platform='macos' buildActiveScheme='true'/>

0 commit comments

Comments
 (0)