Skip to content
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

Parse over sliceables #67

Merged
merged 19 commits into from
Feb 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions Documentation/Collections.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='macosx' auto-termination-delay='5'>
<sections>
<code source-file-name='section-1.swift' style='setup'/>
<code source-file-name='section-2.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
4 changes: 4 additions & 0 deletions Documentation/Collections.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Box
import Either
import Madness
import Prelude
13 changes: 13 additions & 0 deletions Documentation/Collections.playground/section-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let input = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

typealias Fibonacci = Parser<Slice<Int>, [Int]>.Function

let fibonacci: (Int, Int) -> Fibonacci = fix { fibonacci in
{ x, y -> Fibonacci in
%(x + y) >>- { xy -> Fibonacci in
fibonacci(y, xy) --> { [ xy ] + $0 }
} | { ([], $0) }
}
}

fibonacci(0, 1)(Slice(input))
6 changes: 6 additions & 0 deletions Documentation/Collections.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
8 changes: 8 additions & 0 deletions Documentation/Colours.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='macosx' auto-termination-delay='4'>
<sections>
<code source-file-name='section-1.swift' style='setup'/>
<code source-file-name='section-2.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
5 changes: 5 additions & 0 deletions Documentation/Colours.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Box
import Cocoa
import Darwin
import Madness
import Prelude
24 changes: 24 additions & 0 deletions Documentation/Colours.playground/section-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let toComponent: String -> CGFloat = { CGFloat(strtol($0, nil, 16)) / 255 }

let hex = %("0"..."9") | %("a"..."f") | %("A"..."F")
let hex2 = (hex ++ hex --> { $0 + $1 })
let component1: Parser<String, CGFloat>.Function = hex --> { toComponent($0 + $0) }
let component2: Parser<String, CGFloat>.Function = hex2 --> toComponent
let three: Parser<String, [CGFloat]>.Function = component1 * 3
let six: Parser<String, [CGFloat]>.Function = component2 * 3

let colour: Parser<String, NSColor>.Function = ignore("#") ++ (six | three) --> {
NSColor(calibratedRed: $0[0], green: $0[1], blue: $0[2], alpha: 1)
}

if let reddish = parse(colour, "#d52a41") {
reddish
}

if let greenish = parse(colour, "#5a2") {
greenish
}

if let blueish = parse(colour, "#5e8ca1") {
blueish
}
6 changes: 6 additions & 0 deletions Documentation/Colours.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='macosx' auto-termination-delay='5'>
<sections>
<code source-file-name='section-1.swift' style='setup'/>
<code source-file-name='section-2.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
4 changes: 4 additions & 0 deletions Documentation/Lambda Calculus.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Box
import Either
import Madness
import Prelude
32 changes: 32 additions & 0 deletions Documentation/Lambda Calculus.playground/section-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
enum Term: Printable {
case Variable(String)
case Abstraction(String, Box<Term>)
case Application(Box<Term>, Box<Term>)

var description: String {
switch self {
case let Variable(symbol):
return symbol
case let Abstraction(symbol, body):
return "λ\(symbol).\(body.value.description)"
case let Application(x, y):
return "(\(x.value.description) \(y.value.description))"
}
}
}


let symbol = %("a"..."z")

let term: Parser<String, Term>.Function = fix { (term: Parser<String, Term>.Function) -> Parser<String, Term>.Function in
let variable: Parser<String, Term>.Function = symbol --> { Term.Variable($0) }
let abstraction: Parser<String, Term>.Function = ignore("λ") ++ symbol ++ ignore(".") ++ term --> { Term.Abstraction($0, Box($1)) }
let parenthesized: Parser<String, (Term, Term)>.Function = ignore("(") ++ term ++ ignore(" ") ++ term ++ ignore(")")
let application: Parser<String, Term>.Function = parenthesized --> { (function: Term, argument: Term) -> Term in
Term.Application(Box(function), Box(argument))
}
return variable | abstraction | application
}

parse(term, "λx.(x x)")?.description
parse(term, "(λx.(x x) λx.(x x))")?.description
6 changes: 6 additions & 0 deletions Documentation/Lambda Calculus.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
7 changes: 0 additions & 7 deletions Documentation/Madness.playground/contents.xcplayground

This file was deleted.

64 changes: 0 additions & 64 deletions Documentation/Madness.playground/section-1.swift

This file was deleted.

12 changes: 0 additions & 12 deletions Documentation/Madness.playground/timeline.xctimeline

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum Node: Printable {

// MARK: - Parsing rules

typealias NodeParser = Parser<()>.Function -> Parser<Node>.Function
typealias NodeParser = Parser<String, ()>.Function -> Parser<String, Node>.Function

let element: NodeParser = fix { element in
{ prefix in
Expand All @@ -56,7 +56,7 @@ let element: NodeParser = fix { element in
}
}

let ok: Parser<()>.Function = { ((), $0) }
let ok: Parser<String, ()>.Function = { ((), $0) }
let parsed = parse(element(ok), "> # hello\n> \n> hello\n> there\n> \n> \n")
if let translated = parsed?.0 {
translated.description
Expand Down
22 changes: 15 additions & 7 deletions Madness.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
D490927A1A98F11A00275C79 /* SliceableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49092791A98F11A00275C79 /* SliceableTests.swift */; };
D490927B1A98F11A00275C79 /* SliceableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49092791A98F11A00275C79 /* SliceableTests.swift */; };
D4BC5E021A98C8B4008C6851 /* Madness.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4BC5DF71A98C8B4008C6851 /* Madness.framework */; };
D4BC5E101A98C978008C6851 /* Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47CD1A37E48800D23A6F /* Parser.swift */; };
D4BC5E111A98C978008C6851 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47CF1A37E51500D23A6F /* String.swift */; };
D4BC5E111A98C978008C6851 /* Sliceable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47CF1A37E51500D23A6F /* Sliceable.swift */; };
D4BC5E121A98C97C008C6851 /* ParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47C31A37E47C00D23A6F /* ParserTests.swift */; };
D4BC5E131A98C97C008C6851 /* FlatMapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C8B02D1A69B1A900943303 /* FlatMapTests.swift */; };
D4BC5E151A98C9A6008C6851 /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4FC47D31A37EFCC00D23A6F /* Box.framework */; };
Expand All @@ -32,14 +34,15 @@
D4C2EDBA1A98D82200054FAA /* RepetitionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C2EDB81A98D82200054FAA /* RepetitionTests.swift */; };
D4C2EDBC1A98D8F800054FAA /* FlatMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C2EDBB1A98D8F800054FAA /* FlatMap.swift */; };
D4C2EDBD1A98D8F800054FAA /* FlatMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C2EDBB1A98D8F800054FAA /* FlatMap.swift */; };
D4C2EDFC1A98DEE800054FAA /* Madness.h in Headers */ = {isa = PBXBuildFile; fileRef = D4FC47B61A37E47C00D23A6F /* Madness.h */; settings = {ATTRIBUTES = (Public, ); }; };
D4C8B02E1A69B1A900943303 /* FlatMapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C8B02D1A69B1A900943303 /* FlatMapTests.swift */; };
D4D5B0ED1A98E03400BE42A2 /* Assertions.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4D5B0EC1A98E03400BE42A2 /* Assertions.framework */; };
D4D5B0EE1A98E2B600BE42A2 /* Assertions.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4D5B0EC1A98E03400BE42A2 /* Assertions.framework */; };
D4FC47B71A37E47C00D23A6F /* Madness.h in Headers */ = {isa = PBXBuildFile; fileRef = D4FC47B61A37E47C00D23A6F /* Madness.h */; settings = {ATTRIBUTES = (Public, ); }; };
D4FC47BD1A37E47C00D23A6F /* Madness.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4FC47B11A37E47C00D23A6F /* Madness.framework */; };
D4FC47C41A37E47C00D23A6F /* ParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47C31A37E47C00D23A6F /* ParserTests.swift */; };
D4FC47CE1A37E48800D23A6F /* Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47CD1A37E48800D23A6F /* Parser.swift */; };
D4FC47D01A37E51500D23A6F /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47CF1A37E51500D23A6F /* String.swift */; };
D4FC47D01A37E51500D23A6F /* Sliceable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FC47CF1A37E51500D23A6F /* Sliceable.swift */; };
D4FC47D21A37EF9800D23A6F /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4FC47D11A37EF9800D23A6F /* Either.framework */; };
D4FC47D51A37EFCC00D23A6F /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4FC47D31A37EFCC00D23A6F /* Box.framework */; };
D4FC47D61A37EFCC00D23A6F /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4FC47D41A37EFCC00D23A6F /* Prelude.framework */; };
Expand All @@ -66,6 +69,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
D49092791A98F11A00275C79 /* SliceableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliceableTests.swift; sourceTree = "<group>"; };
D4BC5DF71A98C8B4008C6851 /* Madness.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Madness.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4BC5E011A98C8B4008C6851 /* Madness-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Madness-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
D4C2EDA61A98D38E00054FAA /* Concatenation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Concatenation.swift; sourceTree = "<group>"; };
Expand All @@ -84,7 +88,7 @@
D4FC47C21A37E47C00D23A6F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D4FC47C31A37E47C00D23A6F /* ParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParserTests.swift; sourceTree = "<group>"; };
D4FC47CD1A37E48800D23A6F /* Parser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Parser.swift; sourceTree = "<group>"; };
D4FC47CF1A37E51500D23A6F /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
D4FC47CF1A37E51500D23A6F /* Sliceable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sliceable.swift; sourceTree = "<group>"; };
D4FC47D11A37EF9800D23A6F /* Either.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Either.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4FC47D31A37EFCC00D23A6F /* Box.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Box.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4FC47D41A37EFCC00D23A6F /* Prelude.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Prelude.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -167,7 +171,7 @@
D4C2EDBB1A98D8F800054FAA /* FlatMap.swift */,
D4C2EDA61A98D38E00054FAA /* Concatenation.swift */,
D4C2EDB31A98D63600054FAA /* Repetition.swift */,
D4FC47CF1A37E51500D23A6F /* String.swift */,
D4FC47CF1A37E51500D23A6F /* Sliceable.swift */,
D4FC47B41A37E47C00D23A6F /* Supporting Files */,
);
path = Madness;
Expand All @@ -189,9 +193,10 @@
children = (
D4FC47C31A37E47C00D23A6F /* ParserTests.swift */,
D4C2EDB01A98D5DB00054FAA /* AlternationTests.swift */,
D4C8B02D1A69B1A900943303 /* FlatMapTests.swift */,
D4C2EDA81A98D49500054FAA /* ConcatenationTests.swift */,
D4C8B02D1A69B1A900943303 /* FlatMapTests.swift */,
D4C2EDB81A98D82200054FAA /* RepetitionTests.swift */,
D49092791A98F11A00275C79 /* SliceableTests.swift */,
D4FC47C11A37E47C00D23A6F /* Supporting Files */,
);
path = MadnessTests;
Expand All @@ -213,6 +218,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
D4C2EDFC1A98DEE800054FAA /* Madness.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -378,7 +384,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D4BC5E111A98C978008C6851 /* String.swift in Sources */,
D4BC5E111A98C978008C6851 /* Sliceable.swift in Sources */,
D4C2EDAF1A98D53400054FAA /* Alternation.swift in Sources */,
D4BC5E101A98C978008C6851 /* Parser.swift in Sources */,
D4C2EDBD1A98D8F800054FAA /* FlatMap.swift in Sources */,
Expand All @@ -393,6 +399,7 @@
files = (
D4C2EDAB1A98D4D600054FAA /* ConcatenationTests.swift in Sources */,
D4C2EDBA1A98D82200054FAA /* RepetitionTests.swift in Sources */,
D490927B1A98F11A00275C79 /* SliceableTests.swift in Sources */,
D4BC5E131A98C97C008C6851 /* FlatMapTests.swift in Sources */,
D4BC5E121A98C97C008C6851 /* ParserTests.swift in Sources */,
D4C2EDB21A98D5DB00054FAA /* AlternationTests.swift in Sources */,
Expand All @@ -403,7 +410,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D4FC47D01A37E51500D23A6F /* String.swift in Sources */,
D4FC47D01A37E51500D23A6F /* Sliceable.swift in Sources */,
D4C2EDAE1A98D52B00054FAA /* Alternation.swift in Sources */,
D4FC47CE1A37E48800D23A6F /* Parser.swift in Sources */,
D4C2EDBC1A98D8F800054FAA /* FlatMap.swift in Sources */,
Expand All @@ -418,6 +425,7 @@
files = (
D4C2EDAA1A98D4D400054FAA /* ConcatenationTests.swift in Sources */,
D4C8B02E1A69B1A900943303 /* FlatMapTests.swift in Sources */,
D490927A1A98F11A00275C79 /* SliceableTests.swift in Sources */,
D4C2EDB91A98D82200054FAA /* RepetitionTests.swift in Sources */,
D4FC47C41A37E47C00D23A6F /* ParserTests.swift in Sources */,
D4C2EDB11A98D5DB00054FAA /* AlternationTests.swift in Sources */,
Expand Down
8 changes: 7 additions & 1 deletion Madness.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading