GithubHelp home page GithubHelp logo

psfblair / erldn Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marianoguerra/erldn

0.0 1.0 0.0 136 KB

edn format parser for the erlang platform

License: MIT License

Makefile 28.40% Elixir 2.71% Erlang 68.89%

erldn's Introduction

erldn - an edn parser for the erlang platform

erldn is a parser for the edn specification.

implemented using leex and yecc, tested with eunit.

this is a low level parser, it gives you an erlang data structure where you have to decide how will you actually represent things like maps, sets, vectors since each person may have a different need, no imposition here.

Build

./rebar compile

Test

./rebar eunit

Use

1> erldn:parse_str("{}").
{ok,{map,[]}}

2> erldn:parse_str("1").
{ok,1}

3> erldn:parse_str("true").
{ok,true}

4> erldn:parse_str("nil").
{ok,nil}

5> erldn:parse_str("[1 true nil]").
{ok,{vector,[1,true,nil]}}

6> erldn:parse_str("(1 true nil :foo)").
{ok,[1,true,nil,foo]}

7> erldn:parse_str("(1 true nil :foo ns/foo)").
{ok,[1,true,nil,foo,{symbol,'ns/foo'}]}

8> erldn:parse_str("#{1 true nil :foo ns/foo}").
{ok,{set,[1,true,nil,foo,{symbol,'ns/foo'}]}}

9> erldn:parse_str("#myapp/Person {:first \"Fred\" :last \"Mertz\"}").
{ok,{tag,'myapp/Person',
         {map,[{first,"Fred"},{last,"Mertz"}]}}}
         10> erldn:parse_str("#{1 true #_ nil :foo ns/foo}").
         {ok,{set,[1,true,{ignore,nil},foo,{symbol,'ns/foo'}]}}
         11> erldn:parse_str("#{1 true #_ 42 :foo ns/foo}").
         {ok,{set,[1,true,{ignore,42},foo,{symbol,'ns/foo'}]}}

 % to_string

 10> {ok, Result} = erldn:parse_str("{:a 42}").
 {ok,{map,[{a,42}]}}

 11> io:format("~s~n", [erldn:to_string(Result)]).
 {:a 42}
 ok

 % to_erlang

 12> erldn:to_erlang(element(2, erldn:parse_str("[1, nil, :nil, \"asd\"]"))).
 [1,nil,nil,<<"asd">>]

API

parse_str/1

parses a string with edn into an erlang data structure maintaining all the details from the original edn

to_string/1

converts the result from parse_str/1 into an edn string representation

to_erlang/1

converts the result from parse_str/1 into an erlang-friendly version of itself; see "To Erlang Mappings" below.

to_erlang/2

like to_erlang/1 but accepts a tuplelist as a second argument with a tag as the first argument and a function (fun (Tag, Value, OtherHandlers) -> .. end) as the second of each pair to handle tagged values.

check the unit tests for usage examples.

Type Mappings

edn erlang
integer integer
float float
boolean boolean
nil nil (atom)
char tagged integer -> {char, <integer>}}
string binary string (utf-8)
list list
vector tagged list -> {vector, [...]}
map tagged property list -> {map, [{key1, val1}, ...]}
set tagged list -> {set, [...]} (not made unique on parsing)
symbol tagged atom -> {symbol, <symbol>}
tagged elements tagged tuple with tag and value -> {tag, Symbol, Value}

To Erlang Mappings

The to_erlang function transforms the incoming data structure into a more erlang-friendly data structure, but this can't be converted back to a string without transforming it again. The mappings by default are:

edn erlang
integer integer
float float
boolean boolean
char string
nil (atom) nil (atom)
nil (symbol) nil (atom)
binary string binary string
list list
vector list
map dict (dict module)
set set (sets module)
symbol stay the same
tagged elements call registered handler for that tag, fail if not found

TODO

nothing that I can think of!

Notes

  • since keywords are mapped to atoms and nil is mapped to the nil atom, if the nil keyword is encountered it will be mapped to {keyword, nil}.

Author

marianoguerra

License

MIT + optional beer for the author if you meet me

erldn's People

Contributors

edubkendo avatar marianoguerra avatar psfblair avatar

Watchers

 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.