Git Product home page Git Product logo

struct-parse's Introduction

struct-parse

Error-reporting parser for unityped data structures (e.g. JSON). Aimed to be convenient for parsing resource files, configs and other pieces written by human.

Some examples of parsing and error-reporting:

-- We'll use example.toml from Tom's Preston-Werner https://github.com/toml-lang/toml
ghci> :t parseFromFile
parseFromFile :: FilePath -> Data.TomlObject.TomlParser a -> IO (Either String a)

-- So let's read it, run parser and print results (or errors)
ghci> let example p = either putStrLn print =<< parseFromFile "example.toml" p

-- What is the interface of parser?
ghci> :browse Data.TomlObject
...
bool :: StructParser AnnotatedTomlObject Bool
int :: StructParser AnnotatedTomlObject GHC.Int.Int64
double :: StructParser AnnotatedTomlObject Double
string :: StructParser AnnotatedTomlObject Data.Text.Internal.Text
index :: Int -> StructParser AnnotatedTomlObject AnnotatedTomlObject
elems :: StructParser AnnotatedTomlObject [AnnotatedTomlObject]
key :: String -> StructParser AnnotatedTomlObject AnnotatedTomlObject
fields :: StructParser AnnotatedTomlObject (HashMap Text AnnotatedTomlObject)
context :: String -> StructParser a a
this :: StructParser a a
traversing :: (Traversable t) => StructParser a b -> StructParser (t a) (t b)
...

-- Something simple
ghci> example $ key "title" >>> string
"TOML Example"

-- All int ports of databases
ghci> example $ key "database" >>> key "ports" >>> elems >>> traversing int
[8001,8001,8002]

-- Bool? Nah, error.
ghci> example $ key "database" >>> key "ports" >>> elems >>> traversing bool
Failure:
in .database(Object): in .ports(Array): all of
{ at [0](Integer): expected Bool, got Integer
, at [1](Integer): expected Bool, got Integer
, at [2](Integer): expected Bool, got Integer
}

-- Server names and their IPs
ghci> example $ key "servers" >>> fields >>> traversing (key "ip" >>> string)
fromList [("alpha","10.0.0.1"),("beta","10.0.0.2")]

-- More complex query?
ghci> :{
ghci| example $ (,)
ghci|   <$> (key "owner" >>> key "name" >>> string)
ghci|   <*> (key "products" >>> elems >>> traversing (key "name" >>> string))
ghci| :}
("Tom Preston-Werner",["Hammer","Nail"])

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.