[erlang-questions] [ANN] ETP - An Erlang Term Parser for Java
Dave Parfitt
dparfitt@REDACTED
Mon Apr 29 22:59:10 CEST 2013
Hello -
I'd like to announce the release of ETP v0.3. ETP is a parser for Java
built with Antlr 4 that can parse a string representation of an Erlang
term. The parsed model can be manipulated in Java, and rendered back to a
string by calling toString().
The source can be found online at:
http://metadave.github.io/etp/
The Maven artifact is available from the Sonatype OSS repository.
<dependency>
<groupId>com.github.metadave</groupId>
<artifactId>etp</artifactId>
<version>0.3</version>
</dependency>
Here's a simple example:
ETPTuple tuple =
(ETPTuple)ETP.parse("{mylist, [1,2,3,4], \n" +
"my_string, \"Hello world\"}");
// All ETP objects subclass ETPTerm
// you can use "instanceof" to see what the result of the parse is
ETPAtom atom = (ETPAtom)tuple.getValue(0);
System.out.println("Atom = " + atom);
ETPList list = (ETPList)tuple.getValue(1);
// use getValue() to access the list of terms
for(ETPTerm v : list.getValue()) {
System.out.println(" List item " + v);
}
ETPAtom my_string_atom = (ETPAtom)tuple.getValue(2);
System.out.println(my_string_atom);
ETPString s = (ETPString)tuple.getValue(3);
System.out.println(s.getValue()); // getValue() gets the "raw" value
System.out.println(s.toString()); // toString() gets the string
representation
// you can set values on the ETP structure and call toString() to render
// a new Erlang term
atom.setValue("new_atom_value");
list.getValue().add(new ETPInteger(100));
System.out.println(tuple.toString());
which yields the following output:
Atom = mylist
List item 1
List item 2
List item 3
List item 4
my_string
Hello world
"Hello world"
{new_atom_value,[1,2,3,4,100],my_string,"Hello world"}
Cheers -
Dave
https://github.com/metadave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130429/ed0b57b3/attachment.htm>
More information about the erlang-questions
mailing list