TreeML parser

It is hardly necessary, but here is my own little parser. It parses tab-indented files into trees of nodes. It supports lists, integers, floats, booleans, strings, tokens and explicit nulls.

The Gist contains a sample file and the parser.

I do actually use it. Dropping in this class is so much easier than adding a parser library. I know it looks a lot like YAML, but reading the YAML feature list gives me a migraine.

Using the parser I can do things like this:


FileReader fr = new FileReader(new File(filename));
Parser.Node node = new org.treeml.Parser().parse(fr);
System.out.println("Careers loaded: " + node.children.size());
for (Parser.Node child : node.children) {
    String careerId = child.getValueAt("id");
    String careerName = child.getValueAt("careerName");
    String careerDescription = child.getValueAt("careerDescription");
    Long minimumLevel = child.getValueAt("minimumLevel");
    Long status = child.getValueAt("status");
    Long reputation = child.getValueAt("reputation");
    Long alignment = child.getValueAt("alignment");
    List<Parser.Node> possessions = child.getNodes("possession");
    List<Parser.Node> skills = child.getNodes("skill");
...

Nice. I’m a fan of edn for this kind of thing but it’s not an “ultralight” like this and support outside of clojure is less than stellar.

I updated the Gist with a bug fix (forgot to make RootNode a static class) and added a method to serialize to TreeML format. The fixes were needed for my PolyPerfect 5.1 tool (in WIP).

My knowledge of Java isn’t immense. Would there be an increase in performance using something like this VS. JSON + Jackson? Currently I am building a structure for static data (world details, characters, etc) using Jackson to read the files and my own interpreter to handle the rest. The TreeML format seems easier on the eyes so I’m very curious.

Regardless, nice work you put into this. 8)

I make no guarantees regarding performance :slight_smile:

I use this because it is easy on the eye, easy to type and easy to integrate, and I’m just eccentric. But I would never recommend it over a proper parser!

If you like TreeML, then you would like YAML, look it up.

I was doodling a schema language for treeml today.


career : repeats, tokenid
               description : repeats, optional, string
               minimumLevel : single, integer, positive
               items : single, empty
                              tokenid : repeats, optional, integer, positive
               wealth : single, token, enum, impoverished, poor, ok, prosperous, wealthy, opulent
               status : single, integer, positive
               alignment : single, integer
               minimumSkills : single, empty
                              tokenid : repeats, optional, integer, positive
               maxSkills : single, empty
                              tokenid : repeats, optional, integer, positive
               behaviors: single, token, set

//various identifiers that can given to a property
 
single|repeats, optional?, string|token|tokenid|tokenidref|integer|decimal|boolean|empty, (positive|negative|id)?, (list|set)?, enum, emums...

The schema language would be pure treeml, but it would require token, tokenid and tokenidref to be reserved words.

I may use this. Can you make it eval basic math operation(+, -, *, / and %)?