8000 GitHub - hlian/jiffy: Parser combinators, for Swift · GitHub
[go: up one dir, main page]

Skip to content
{"props":{"color_mode":"dark","logged_in":false,"marketing_page":false,"home_path":"/","login_path":"/login?return_to=https%3A%2F%2FGitHub.com%2Fhlian%2Fjiffy","signup_path":"/signup?ref_cta=Sign+up\u0026ref_loc=header+logged+out\u0026ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E\u0026source=header-repo\u0026source_repo=hlian%2Fjiffy","signup_enabled":true,"is_signup_controller":false,"show_search_and_nav":true,"hide_search":false,"private_mode_enabled":false,"should_use_dotcom_links":true,"auth_hydro_click":"{\"event_type\":\"authentication.click\",\"payload\":{\"location_in_page\":\"site header menu\",\"repository_id\":null,\"auth_type\":\"SIGN_UP\",\"originating_url\":\"https://GitHub.com/hlian/jiffy\",\"user_id\":null}}","auth_hydro_click_hmac":"164ee0d6a115fc853323d05fdff68657e267d713662db908a1cad0dbda6f1188"}}
hlianPublic

About

Parser combinators, for Swift

Resources

Stars

65 stars

Watchers

1 watching

Forks

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Parser combinators, for Swift

Resources

Stars

65 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0