Index: README.md ================================================================== --- README.md +++ README.md @@ -31,10 +31,11 @@ - `Generic_Evaluate`: Generic boolean expression evaluation framework - `Strings`: Boolean expressions on standard strings - `Dynamic_Interpreters`: S-expression interpreter with mutable commands and callbacks - `Encodings`: translators to and from official S-expression encodings + - `Enumeration_IO`: tools to help I/O of enumerations in S-expressions - `File_Readers`: objects reading a file to an atom or a S-expression - `File_Writers`: file-backed S-expression printer - `Generic_Caches`: memory container for S-expressions - `Interpeter_Loop`: inner loop of S-expression interpreters, typically used in static interpreters ADDED src/natools-s_expressions-enumeration_io.adb Index: src/natools-s_expressions-enumeration_io.adb ================================================================== --- src/natools-s_expressions-enumeration_io.adb +++ src/natools-s_expressions-enumeration_io.adb @@ -0,0 +1,66 @@ +------------------------------------------------------------------------------ +-- Copyright (c) 2015, Natacha Porté -- +-- -- +-- Permission to use, copy, modify, and distribute this software for any -- +-- purpose with or without fee is hereby granted, provided that the above -- +-- copyright notice and this permission notice appear in all copies. -- +-- -- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +------------------------------------------------------------------------------ + +with Ada.Characters.Handling; +with Ada.Strings.Fixed; + +package body Natools.S_Expressions.Enumeration_IO is + + function To_Atom (Enumeration_Image : in String) return Atom is + Result : Atom := S_Expressions.To_Atom (Ada.Strings.Fixed.Translate + (Enumeration_Image, + Ada.Characters.Handling.To_Lower'Access)); + begin + for I in Result'Range loop + if Result (I) = Character'Pos ('_') then + Result (I) := Character'Pos ('-'); + end if; + end loop; + + return Result; + end To_Atom; + + + function To_Image (Data : in Atom) return String is + Result : String := Ada.Strings.Fixed.Translate + (To_String (Data), + Ada.Characters.Handling.To_Upper'Access); + begin + for I in Result'Range loop + if Result (I) = '-' then + Result (I) := '_'; + end if; + end loop; + + return Result; + end To_Image; + + + package body Typed_IO is + + function Image (T : Enum) return Atom is + begin + return To_Atom (Enum'Image (T)); + end Image; + + function Value (Data : Atom) return Enum is + begin + return Enum'Value (To_Image (Data)); + end Value; + + end Typed_IO; + +end Natools.S_Expressions.Enumeration_IO; ADDED src/natools-s_expressions-enumeration_io.ads Index: src/natools-s_expressions-enumeration_io.ads ================================================================== --- src/natools-s_expressions-enumeration_io.ads +++ src/natools-s_expressions-enumeration_io.ads @@ -0,0 +1,46 @@ +------------------------------------------------------------------------------ +-- Copyright (c) 2015, Natacha Porté -- +-- -- +-- Permission to use, copy, modify, and distribute this software for any -- +-- purpose with or without fee is hereby granted, provided that the above -- +-- copyright notice and this permission notice appear in all copies. -- +-- -- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +------------------------------------------------------------------------------ + +------------------------------------------------------------------------------ +-- Natools.S_Expressions.Enumeration_IO provides subprograms to help with -- +-- I/O of enumerated type values. It involves mostly the conversion between -- +-- Ada-style names (underscore-separated uppercase words) and S-expression -- +-- style (hyphen-separated lowercase words). -- +------------------------------------------------------------------------------ + +package Natools.S_Expressions.Enumeration_IO is + pragma Preelaborate; + + function To_Atom (Enumeration_Image : in String) return Atom; + -- Convert the output of a 'Image function into a S-expression atom + + function To_Image (Data : in Atom) return String; + -- Convert an atom into a string similar to 'Image output + + + generic + type Enum is (<>); + package Typed_IO is + + function Image (T : Enum) return Atom; + -- Convert an enumeration value into an atom + + function Value (Data : Atom) return Enum; + -- Convert an atom into an enumeration value + + end Typed_IO; + +end Natools.S_Expressions.Enumeration_IO;