• Stars
    star
    291
  • Rank 142,563 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Recursive Descent Into Madness

Recursive Descent into Madness

Madness is a Swift µframework for parsing strings in simple context-free grammars. Combine parsers from simple Swift expressions and parse away:

let digit = %("0"..."9") <|> %("a"..."f") <|> %("A"..."F")
let hex = digit+ |> map { strtol(join("", $0), nil, 16) }
parse(%"0x" *> hex, "0xdeadbeef") // => 3,735,928,559

Your parsers can produce your own model objects directly, making Madness ideal for experimenting with grammars, for example in a playground.

screenshot of parsing HTML colours in an Xcode playground: let reddish = parse(colour, "#d52a41")!

See Madness.playground for some examples of parsing with Madness.

Use

  • Lexing

    Madness can be used to write lexers, lexeme parsers, and scannerless parsers. @bencochran has built a lexer and parser for the LLVM tutorial language, Kaleidoscope.

  • Any

     any

    parses any single character.

  • Strings

     %"hello"

    parses the string “hello”.

  • Ranges

     %("a"..."z")

    parses any lowercase letter from “a” to “z” inclusive.

  • Concatenation

     x <*> y <*> z

    parses x followed by y and produces parses as (X, Y).

  • Alternation

     x <|> y

    parses x, and if it fails, y, and produces parses as Either<X, Y>. If x and y are of the same type, then it produces parses as X.

     oneOf([x1, x2, x3])

    tries a sequence of parsers until the first success, producing parses as X.

     anyOf(["x", "y", "z"])

    tries to parse one each of a set of literals in sequence, collecting each successful parse into an array until none match.

     allOf(["x", "y", "z"])

    greedier than anyOf, parsing every match from a set of literals in sequence, including duplicates.

  • Repetition

     x*

    parses x 0 or more times, producing parses as [X].

     x+

    parses x one or more times.

     x * 3

    parses x exactly three times.

     x * (3..<6)

    parses x three to five times. Use Int.max for the upper bound to parse three or more times.

  • Mapping

     x |> map { $0 }
     { $0 } <^> x
     x --> { _, _, y in y }

    parses x and maps its parse trees using the passed function. Use mapping to build your model objects. --> passes the input and parsed range as well as the parsed data for e.g. error reporting or AST construction.

  • Ignoring

    Some text is just decoration. x *> y parses x and then y just like <*>, but drops the result of x. x <* y does the same, but drops the result of y.

API documentation is in the source.

This way Madness lies

∞ loop de loops

Madness employs simple—naïve, even—recursive descent parsing. Among other things, that means that it can’t parse any arbitrary grammar that you could construct with it. In particular, it can’t parse left-recursive grammars:

let number = %("0"..."9")
let addition = expression <*> %"+" <*> expression
let expression = addition <|> number

expression is left-recursive: its first term is addition, whose first term is expression. This will cause infinite loops every time expression is invoked; try to avoid it.

I love ambiguity more than @numist

Alternations try their left operand before their operand, and are short-circuiting. This means that they disambiguate (arbitrarily) to the left, which can be handy; but this can have unintended consequences. For example, this parser:

%"x" <|> %"xx"

will not parse “xx” completely.

Integration

  1. Add this repository as a submodule and check out its dependencies, and/or add it to your Cartfile if you’re using carthage to manage your dependencies.
  2. Drag Madness.xcodeproj into your project or workspace, and do the same with its dependencies (i.e. the other .xcodeproj files included in Madness.xcworkspace). NB: Madness.xcworkspace is for standalone development of Madness, while Madness.xcodeproj is for targets using Madness as a dependency.
  3. Link your target against Madness.framework and each of the dependency frameworks.
  4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Madness and its dependencies.)

More Repositories

1

RXCollections

Obsolete—use https://github.com/robrix/Reducers instead.
Objective-C
477
star
2

Prelude

Swift µframework of simple functional programming tools
Swift
409
star
3

Postmodern-Programming

236
star
4

Box

Swift µframework of the ubiquitous Box<T> & MutableBox<T> reference types, for recursive value types & misc. other purposes.
Swift
215
star
5

Either

Swift µframework of Either, which represents two alternatives.
Swift
150
star
6

Reducers

Clojure-inspired reducers for Cocoa & Cocoa Touch.
Objective-C
138
star
7

Set

An implementation of Multiset and PredicateSet in Swift.
Swift
112
star
8

RXFutures

Race-free completion and cancellation of asynchronous work.
Objective-C
89
star
9

path

A lambda calculus to explore type-directed program synthesis.
Haskell
82
star
10

Grid

Rational window management
Objective-C
80
star
11

Hammer

Parsing and pattern matching in Objective-C
Objective-C
75
star
12

starlight

spaceships in space
Haskell
71
star
13

Traversal

Enumeration & iteration of collections in Swift.
Swift
58
star
14

Obstruct

Block introspection and interaction
C
47
star
15

Memo

Swift µframework of Memo, a lazily memoized value.
Swift
45
star
16

semilattices

join and meet semilattices, lower and upper bounds.
Haskell
43
star
17

Lagrangian

On the dark side of the sun.
Objective-C
42
star
18

RXPreprocessing

A variety of utilities for the C preprocessor.
C
38
star
19

Many-Types-Make-Light-Work

Don’t subclass.
35
star
20

Haxcessibility

Hackery via the AX (Accessibility) APIs
Objective-C
33
star
21

ui-effects

An experiment towards a UI programming model inspired by algebraic effects.
Haskell
30
star
22

RXConcreteProtocol

Mix-ins for Objective-C
Objective-C
25
star
23

List

Singly-linked lists in Swift.
Swift
25
star
24

Hammer.swift

When all you have is a parser, everything looks like a language.
Swift
24
star
25

freer-cofreer

freer monads and cofreer comonads.
Haskell
21
star
26

languages-all-the-way-down

outline & examples for my ZuriHac 2020 talk, Languages All the Way Down
Haskell
20
star
27

Project-Templates

Xcode project templates, especially for µframework development.
Swift
20
star
28

sequoia

classical sequent calculus, embedded in Haskell
Haskell
20
star
29

A-Swiftly-Tilting-Parser

Talk about the derivative of parser combinators in ObjC & Swift
19
star
30

Manifold

Another experiment in dependently typed languages, this time with some quantitative type theory smooshed in.
Haskell
18
star
31

silkscreen

Prettyprinting transformers for rainbow parens, precedence, etc.
Haskell
16
star
32

abstract-interpretation

Experiments in abstracting definitional interpreters
Haskell
16
star
33

Delay

µframework for lazy evaluation in Swift.
Swift
16
star
34

tilec

Sometimes when I feel sad I implement a dependently typed lambda calculus.
Haskell
15
star
35

Auspicion

Convenient LLVM compiler API for Objective-C.
Swift
15
star
36

objectbinder

Bind arbitrary objects’ properties without writing new IB3 plugins.
Objective-C
13
star
37

Pork

PDF → other
Swift
12
star
38

isometry

programmatic voxels
Haskell
12
star
39

RXAssertions

Smarter assertion macros for your OCUnit tests.
Objective-C
12
star
40

NSFastEnumeration-or-Consequences

NSFastEnumeration and lazy evaluation—or doom!
10
star
41

surface

Dependently-typed language w/ little to say for itself
Haskell
10
star
42

dotfiles

the very dottest of files 💻
Shell
9
star
43

dragon

Renders iterations of the dragon curve to SVG
Haskell
9
star
44

TaPL

Types and Programming Languages notes and exercises
Swift
9
star
45

Extraction

A tool to audio tracks from QuickTime files, e.g. for YouTube → iTunes use
Objective-C
9
star
46

credit-card-validation

Credit card number validation as functional–style Swift.
Swift
8
star
47

RXSynchronously

A very convenient way to wait for asynchronous work to complete.
Objective-C
8
star
48

semirings-modules

Semirings, with and without additive/multiplicative identities, and R-modules.
Haskell
7
star
49

Loyalist

Algebraic autolayout
Swift
7
star
50

RXVersionedUndoManager

An NSUndoManager subclass which stores commits to a git branch.
Objective-C
6
star
51

interval-functor

General purpose intervals of functors.
Haskell
6
star
52

xcode-light

A lightweight Xcode-integration bundle for TextMate. Clean and fresh inside and out!
Ruby
6
star
53

Navel-Gazing

Introspection at its worst.
Objective-C
6
star
54

RXVisitor

A convenient implementation of Visitor pattern for Objective-C collections.
Objective-C
6
star
55

language-haskell-ast

Generic interface over the AST provided by haskell-src-exts
Haskell
5
star
56

seq

a µµ̃ calculus with pretty-printing and evaluating interpreters
Haskell
5
star
57

RXTraits

Trait-ish decorators, as an alternative to RXConcreteProtocol.
Objective-C
5
star
58

text-gl

Text, rendered with OpenGL.
Haskell
5
star
59

derivative-parsing

Parsing with Derivatives, in Haskell, with GADTs.
Haskell
5
star
60

forthward

A toy concatenative language to explore K ( http://k-framework.org )
Ruby
5
star
61

effects-sequences

Extensible effects à la Oleg, but with fancier effect sets.
Haskell
5
star
62

Lagrangian.swift

Unit tests from the dark side of the sun.
Swift
4
star
63

robrix.github.io

Words by me
HTML
4
star
64

BlendColourPicker

A colour picker for Mac OS X 10.5+; blend two colours to get a third.
Objective-C
4
star
65

RXJSONCoder

An experimental implementation of JSON encoding/decoding in ObjC using NSCoder/NSCoding
Objective-C
4
star
66

elab

Dependently-typed language to experiment with An Algebraic Approach to Typechecking and Elaboration
Haskell
4
star
67

RXPaths

Serialize and deserialize paths to an SVG path command-compatible format
Objective-C
4
star
68

Y

The Y combinator, implemented in Ruby and Objective-C.
Objective-C
3
star
69

semiring-parsing

Parsing in semirings
Haskell
3
star
70

Relation

Finitary relations in Swift.
C++
3
star
71

meconation

For Micah
Objective-C
3
star
72

ns

Quick access to ADC docset documentation: `ns array`, `ui view`, `cf type`
3
star
73

Halftone

I wanted to make something pretty.
Objective-C
3
star
74

q

q is for query
Ruby
3
star
75

build-stack

Build stack packages from Atom via https://atom.io/packages/build
JavaScript
3
star
76

proficiency

Haskell profile visualization tools.
Haskell
2
star
77

spline

Generative SVG experiments
Haskell
2
star
78

Focus

An experimental language of little interest to anyone.
C
2
star
79

continuations

Continuations, CPS, co-functions, and combinators for working with them
Haskell
2
star
80

RXLazyEnumerations

Lazy enumerations of files (as characters) and generators.
Objective-C
2
star
81

Xcode-Templates

My Xcode Templates, just because. You will probably not care about this.
Objective-C
2
star
82

Quantificate

Measurements and conversions in ObjC
2
star
83

distribution

Probability distributions à la Deriving a Probability Density Calculator by Wazim Mohammed Ismail & Chung-chieh Shan http://homes.soic.indiana.edu/ccshan/rational/pearl.pdf
Haskell
2
star
84

parallel-incremental

Haskell
2
star
85

glitch

An increasingly charming view of your git repositories written with sinatra, grit, and love; just add browser/RSS reader.
Ruby
2
star
86

Drgn

A Cocoa app which draws the dragon curve. cf http://en.wikipedia.org/wiki/Dragon_curve
Objective-C
2
star
87

deterministic-parsing

An experiment in deterministic parsing of LL(1) grammars
Haskell
1
star
88

complexity-analysis

Sized type elaboration, eventually via abstract interpretation.
Haskell
1
star
89

crocus

a datalog
Haskell
1
star
90

memory

Outsourcing my memory
1
star
91

hello-world

Hello, world! ❤️
1
star
92

raytracer

A toy raytracer in Haskell
Haskell
1
star
93

higher-order

Higher-order (type-indexed) analogues of various typeclasses, and machinery over them.
Haskell
1
star
94

RXAllocator

A dumb bump pointer allocator designed dumbly to be as dumb as dumbness itself.
Objective-C
1
star
95

-

The ω function is a non-recursive, non-terminating function; this is an implementation of it in Objective-C.
Objective-C
1
star
96

Monochrome-motemplate

My *.motemplate files. Nothing to see here, move along folks.
1
star
97

Arborescence

arborescence, n. a quality of being treelike in growth or appearance.
C
1
star
98

cabal-action

GitHub Actions for cabal new-build
Dockerfile
1
star
99

lambdaft

Haskell
1
star
100

synth

Audio synthesis and sequencing
Haskell
1
star