Giter Club home page Giter Club logo

jiffy's Introduction

Jiffy?

Jiffy is a parser-combinator library written in Swift, heavily inspired by FParsec, which in turn originates from Parsec.

Included is a work-in-progress parser for the Doge Serialized Object Notation, the premier textual data representation of our time. See: DSON.swift.

Parser combinators?

Are fantastic, and you can read all about them here: "Dive into parser combinators." Whoever wrote that must be a pretty OK dude. Gist: you can quickly and easily write composable, readable parsers for complicated grammars. Here, for example, is a parser for floating-point numbers:

let pdigit = oneOf(Array("01234567"))
let pdigit1 = oneOf(Array("1234567"))
let pceil = string("0") <|> (concat <%> (s <%> pdigit1) <*> manyChar(pdigit1))
let pmantissa = concat <%> oneChar(".") <*> many1Char(pdigit)
let pexponent = (string("very") <|> string("VERY")) *> (concat <%> oneCharOf(Array("+-")) <*> many1Char(pdigit))
let pnumber = toNumber <%> opt(oneChar("-")) <*> pceil <*> opt(pmantissa) <*> opt(pexponent)

(If IEEE were to replace the e keyword with the DOGE very -- and I think we can all agree they should.)

If you allow for some leeway, I hope to convince you that the code is a trivial transcription of this diagram:

DSON floating-point number diagram

Swift?

Swift is a new language with a compiler that crashes and hangs a lot. Generics and typeclasses I mean protocols are ideal for something like parser combinators, don't you think? Having custom operators doesn't hurt either.

If you're coming from Haskell or F#, there's one slight change you should be aware of: <$> isn't a valid operator name in Swift, so I went with <%> instead. Mnemonic: they're right next to each other!

Yours,
Hao.

jiffy's People

Watchers

James Cloos avatar akira avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.