Code Monkey home page Code Monkey logo

ada-toml's Introduction

ada-toml: TOML parser for Ada

ada-toml is a pure Ada library for parsing and creating TOML documents.

Status

The development of this library started recently and it currently lacks several important features, mainly: support for floating-point and date/time values.

Build and install

With an Ada 2012 compiler and GPRbuild, building the library is as simple as running:

gprbuild -Pada_toml.gpr -p

This will build in debug mode and produce a static library. In order to build in production mode, add -XBUILD_MODE=prod, and to build a dynamic library, add -XLIBRARY_TYPE=static.

Installation to $PREFIX is simply done using GPRinstall:

gprinstall -Pada_toml.gpr --prefix=$PREFIX

Quick tutorial

All basic types and subprograms are in the TOML package. All "nodes" in a TOML documents are materialized using the TOML.TOML_Value type. Since TOML values make up a tree, this type has reference semantics. This means that modifying a TOML node does not modify the corresponding TOML_Value value itself, but rather the TOML value that is referenced.

Parsing a TOML file is as easy as using the TOML.File_IO.Load_File function:

declare
   Result : constant TOML.Read_Result :=
      TOML.File_IO.Load_File ("config.toml");
begin
   if Result.Success then
      Ada.Text_IO.Put_Line ("config.toml loaded with success!");
   else
      Ada.Text_IO.Put_Line ("error while loading config.toml:");
      Ada.Text_IO.Put_Line
         (Ada.Strings.Unbounded.To_String (Result.Message));
   end if;
end;

Each TOML value has kind, defining which data it contains (a boolean, an integer, a string, a table, ...). To each kind, one or several primitives are associated to let one process the underlying data:

case Result.Kind is
   when TOML.TOML_Boolean =>
      Ada.Text_IO.Put_Line ("Boolean: " & Result.As_Boolean'Image);

   when TOML.TOML_Integer =>
      Ada.Text_IO.Put_Line ("Boolean: " & Result.As_Integer'Image);

   when TOML.TOML_String =>
      Ada.Text_IO.Put_Line ("Boolean: " & Result.As_String);

   when TOML.TOML_Array =>
      Ada.Text_IO.Put_Line ("Array of " & Result.Length & " elements");

   when others =>
      null;
end case;

There are also primitives to build TOML values:

declare
   Bool : constant TOML.TOML_Value := TOML.Create_Boolean (False);
   Int  : constant TOML.TOML_Value := TOML.Create_Integer (10);
   Str  : constant TOML.TOML_Value := TOML.Create_String ("Hello, world");

   Table : constant TOML.TOML_Value := TOML.Create_Table;
begin
   Table.Set ("bool_field", Bool);
   Table.Set ("int_field", Int);
   Table.Set ("str_field", Str);
end;

And finally one can turn a tree of TOML nodes back in text form:

Ada.Text_IO.Put_Line ("TOML document:");
Ada.Text_IO.Put_Line (Table.Dump_As_String);

ada-toml's People

Contributors

mosteo avatar pmderodat avatar

Stargazers

 avatar

Watchers

 avatar  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.