Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

edn format parser for the erlang platform

License

NotificationsYou must be signed in to change notification settings

marianoguerra/erldn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

erldn is a parser for theedn specification.

implemented using leex and yecc, tested with eunit.

this is a low level parser, it gives you an erlang data structure where youhave to decide how will you actually represent things like maps, sets, vectorssince 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 allthe details from the original edn
to_string/1
converts the result fromparse_str/1 into an edn string representation
to_erlang/1
converts the result fromparse_str/1 into an erlang-friendly version ofitself; see "To Erlang Mappings" below.
to_erlang/2
liketo_erlang/1 but accepts a tuplelist as a second argument with atag 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

ednerlang
integerinteger
floatfloat
booleanboolean
nilnil (atom)
chartagged integer -> {char, <integer>}}
stringbinary string (utf-8)
listlist
vectortagged list -> {vector, [...]}
maptagged property list -> {map, [{key1, val1}, ...]}
settagged list -> {set, [...]} (not made unique on parsing)
symboltagged atom -> {symbol, <symbol>}
tagged elementstagged tuple with tag and value -> {tag, Symbol, Value}

To Erlang Mappings

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

ednerlang
integerinteger
floatfloat
booleanboolean
charstring
nil (atom)nil (atom)
nil (symbol)nil (atom)
binary stringbinary string
listlist
vectorlist
mapdict (dict module)
setset (sets module)
symbolstay the same
tagged elementscall 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, ifthe nil keyword is encountered it will be mapped to {keyword, nil}.

Author

marianoguerra

License

MIT + optional beer for the author if you meet me

Notes to myself

make cleanmakeMIX_EXS=package.exs mix hex.publish

About

edn format parser for the erlang platform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp