Skip to content

Commit

Permalink
Parser-printers (#194)
Browse files Browse the repository at this point in the history
* 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 <135203+mbrandonw@users.noreply.github.com>

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fixes

Co-authored-by: Stephen Celis <stephen@stephencelis.com>
Co-authored-by: Stephen Celis <stephen.celis@gmail.com>
Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com>

* 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 <stephen@stephencelis.com>
Co-authored-by: David Peterson <david@randombits.org>
Co-authored-by: Stephen Celis <stephen.celis@gmail.com>
  • Loading branch information
4 people authored Apr 11, 2022
1 parent 23842d3 commit edb72e8
Show file tree
Hide file tree
Showing 151 changed files with 21,704 additions and 1,604 deletions.
77 changes: 77 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/_URLRouting.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1330"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "_URLRouting"
BuildableName = "_URLRouting"
BlueprintName = "_URLRouting"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "_URLRoutingTests"
BuildableName = "_URLRoutingTests"
BlueprintName = "_URLRoutingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "_URLRouting"
BuildableName = "_URLRouting"
BlueprintName = "_URLRouting"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ParsingTests"
BuildableName = "ParsingTests"
BlueprintName = "ParsingTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
Expand All @@ -37,14 +51,14 @@
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ParsingTests"
BuildableName = "ParsingTests"
BlueprintName = "ParsingTests"
BlueprintIdentifier = "variadics-generator"
BuildableName = "variadics-generator"
BlueprintName = "variadics-generator"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -56,9 +70,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "variadics-generator"
BuildableName = "variadics-generator"
BlueprintName = "variadics-generator"
BlueprintIdentifier = "_URLRouting"
BuildableName = "_URLRouting"
BlueprintName = "_URLRouting"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -80,6 +94,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "_URLRoutingTests"
BuildableName = "_URLRoutingTests"
BlueprintName = "_URLRoutingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,34 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ParsingTests"
BuildableName = "ParsingTests"
BlueprintName = "ParsingTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Release"
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ParsingTests"
BuildableName = "ParsingTests"
BlueprintName = "ParsingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "_URLRoutingTests"
BuildableName = "_URLRoutingTests"
BlueprintName = "_URLRoutingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ format:
find . -type f -name '*.md' -print0 | xargs -0 perl -pi -e 's/ +$$//'

generate-variadics:
swift run variadics-generator > Sources/Parsing/Builders/Variadics.swift
swift run variadics-generator \
--generate-zips \
--generate-one-ofs \
> Sources/Parsing/Builders/Variadics.swift

swift run variadics-generator \
--generate-path-zips \
> Sources/_URLRouting/Builders/Variadics.swift

.PHONY: benchmarks format generate-variadics test
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"revision": "a0564bf88df5f94eec81348a2f089494c6b28d80",
"version": "0.1.1"
}
},
{
"package": "swift-case-paths",
"repositoryURL": "https://github.com/pointfreeco/swift-case-paths",
"state": {
"branch": null,
"revision": "241301b67d8551c26d8f09bd2c0e52cc49f18007",
"version": "0.8.0"
}
}
]
},
Expand Down
24 changes: 20 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,47 @@ let package = Package(
.library(
name: "Parsing",
targets: ["Parsing"]
)
),
.library(
name: "_URLRouting",
targets: ["_URLRouting"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.5.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "0.8.0"),
.package(name: "Benchmark", url: "https://github.com/google/swift-benchmark", from: "0.1.1"),
],
targets: [
.target(
name: "Parsing"
name: "Parsing",
dependencies: [.product(name: "CasePaths", package: "swift-case-paths")]
),
.testTarget(
name: "ParsingTests",
dependencies: ["Parsing"]
dependencies: [
"Parsing"
]
),
.executableTarget(
name: "swift-parsing-benchmark",
dependencies: [
"Parsing",
"_URLRouting",
.product(name: "Benchmark", package: "Benchmark"),
]
),
.executableTarget(
name: "variadics-generator",
dependencies: [.product(name: "ArgumentParser", package: "swift-argument-parser")]
),
.target(
name: "_URLRouting", dependencies: ["Parsing"]
),
.testTarget(
name: "_URLRoutingTests",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser")
"_URLRouting"
]
),
]
Expand Down
97 changes: 77 additions & 20 deletions Parsing.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,89 @@
import Parsing

struct Color {
let red, green, blue: UInt8
/*
This playground builds a parser for [day 13][AoC13] of the Advent of Code 2021 challenge.
[AoC13]: https://adventofcode.com/2021/day/13
*/

/// Sample input to be parsed.
let input = """
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5
"""

// MARK: - Models

struct Dot {
let x, y: Int
}

enum Direction: String, CaseIterable {
case x, y
}

let hexPrimary = Prefix(2)
.compactMap { UInt8($0, radix: 16) }
struct Fold {
let direction: Direction
let position: Int
}

let hexColor = Parse(Color.init(red:green:blue:)) {
"#"
hexPrimary
hexPrimary
hexPrimary
struct Instructions {
let dots: [Dot]
let folds: [Fold]
}

do {
var hex = "#000000"[...]
print(hex.debugDescription, "->", try hexColor.parse(&hex), terminator: "\n ...\n\n")
// MARK: - Parsers

let dot = ParsePrint(.memberwise(Dot.init)) {
Digits()
","
Digits()
}

do {
var hex = "#FF0000"[...]
print(hex.debugDescription, "->", try hexColor.parse(&hex), terminator: "\n ...\n\n")
let fold = ParsePrint(.memberwise(Fold.init)) {
"fold along "
Direction.parser()
"="
Digits()
}

do {
var bad = "#BADHEX"[...]
try hexColor.parse(&bad)
} catch {
print(error)
let instructions = ParsePrint(.memberwise(Instructions.init)) {
Many {
dot
} separator: {
"\n"
} terminator: {
"\n\n"
}
Many {
fold
} separator: {
"\n"
}
}

// MARK: Round-trip

let parsed = try instructions.parse(input)
let printed = try instructions.print(parsed)

input == printed
4 changes: 1 addition & 3 deletions Parsing.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>
<playground version='6.0' target-platform='macos' buildActiveScheme='true'/>
Loading

0 comments on commit edb72e8

Please sign in to comment.