From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,WEIRD_PORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,24d7acf9b853aac8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news4.google.com!feeder.news-service.com!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: S-expression I/O in Ada Date: Tue, 10 Aug 2010 02:11:41 +0200 Organization: A noiseless patient Spider Message-ID: <87ocdbl41u.fsf@ludovic-brenta.org> References: <547afa6b-731e-475f-a7f2-eaefefb25861@k8g2000prh.googlegroups.com> <87aap6wcdx.fsf@ludovic-brenta.org> <87vd7jliyi.fsf@ludovic-brenta.org> <699464f5-7f04-4ced-bc09-6ffc42c5322a@w30g2000yqw.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Tue, 10 Aug 2010 00:11:43 +0000 (UTC) Injection-Info: mx03.eternal-september.org; posting-host="oszCB9at6VruFFPJAZbiKg"; logging-data="28860"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183jQ2TpOzc1iGo0UTBjT/T" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:bQp/O/YyH2yHgC2y4TcaU5nIbSs= sha1:/fgPQDPqejsP+jBgIfYzCf6IjTY= Xref: g2news1.google.com comp.lang.ada:13027 Date: 2010-08-10T02:11:41+02:00 List-Id: Natacha Kerensikova writes: > On Aug 9, 8:49 pm, Ludovic Brenta wrote: >> I pursued that idea a little further and actually wrote an embryonic >> S-Expression library.  I'm not entirely satisfied with it because it >> uses copy semantics instead of reference semantics and so is probably >> inefficient.  But it does demonstrate how to read and write >> S-Expressions on a stream (e.g. a file).  Also, it shows how to >> hex-encode and hex-decode blobs, which I've modelled as Storage_Arrays. >> >> You can browse the sources here: >> >> http://green.ada-france.org:8081/branch/changes/org.ludovic-brenta.s_... >> >> Enjoy.  (the license is the GPLv3 or later). > > Interesting, though it seems your code won't like strings containing a > double-quote. Right; that's a minor detail and easy to correct by e.g. preceding embedded double-quotes with a backslash. > However, the principal conclusion I draw from reading this is that I'm > still far from having the Ada level required to write anything like > that (since I can't even understand everything while reading it). I think I could simplify the library with some additional thought. What you see is but a first iteration :) > Still, from what I understood, I'm not fond of the idea of having to > write type-to-atom conversion procedure while they already exist. > > It occurred to me that most types already know how to serialize > themselves, through the Stream subsystem. Strings, Unbounded_strings, > Integers, Float, etc, can all be read from and written into a stream. > So the code to convert them into file-suitable Stream_Element_Array > exist. Won't be a pity not to reuse it? > > Pursuing this idea leads to a library quite different from what I had > in mind when I started this thread. Let's call it Sexp_Stream, which > would be derived from Root_Stream_Type. Then Read and Write procedure > are called (from what I understand) by type'Read and type'Write after > said type has turned itself into a Stream_Element_Array. That would > cover atom I/O, and some procedures must be added to cover the list I/ > O part. > > I think I can imagine how the writing part would happen: > after having initialized a Sexp_Stream object with various > information, including an underlying stream where the S-expression > would be actually written, the application would use the Write > procedure to append an object as an atom to the current node, and the > newly appended atom would become the current node. List procedure > would be called something like List_Open, to open a new list and > append following nodes into it, and List_Close, to close the current > list and come back to the previous list. > > Using my tcp-connect example, it would look like this (please excuse > Ada syntax error and focus on the idea): > > Sexp_Stream.Open(SxStream, underlying_stream, ...); > Sexp_Stream.Open_List(SxStream); > String'Write(SxStream, "tcp-connect"); > Sexp_Stream.Open_List(SxStream); > String'Write(SxStream, "host"); > String'Write(SxStream, Hostname); > Sexp_Stream.Close_List(SxStream); > Sexp_Strean.Open_List(SxStream); > String'Write(SxStream, "port"); > Integer'Write(SxStream, PortNumber); > Sexp_Stream.Close_List(SxStream); > Sexp_Stream.Close_List(SxStream); > > Now I have to admit I don't know how to specify the reading part of > such a Sexp_Stream. I guess I would need the notion of a current node, > with a function telling the application whether the current node is a > list or an atom, type'Read converting the current atom into said type > (and raising an exception when the current atom is a list), and some > procedures to get to the next node, to the frist node following the > current list (i.e. up one level), and when the current node is a list > to go the first node of the list (i.e. one level deeper). > > However such a library wouldn't cover all my S-expression needs, > because I sometimes need to keep S-expressions into memory, so there > would be another package for in-memory S-expressions, which would have > to interact nicely with Sexp_Stream. That's exactly what my library is for: it lets you build S-Expressions in memory and serialize them to a stream (adding any parentheses or quoting necessary along the way) and, conversely, read from a stream and build in-memory S-Expressions. At this level, the value of any atom is an unbounded_string; the semantics are left to the application. The To_Atom and From_Atom functions are there to help you build a high-level layer on top of the S-Expression layer. The "test.adb" program contains such a higher layer: it defines a (simple) record type containing an integer and a boolean (enumerated type) and encodes it into an S-Expression containing strings, and back. I'll add a generic to encode arbitrary hashed_maps into S-Expressions of the form ((key value) (key value) ...) later on, if you're interested. > So, how does it sound? Is it totally crazy? Is it totally not-Ada? Is > there something right in there? I don't think "Ada" or "not Ada" should be the question; the proper question should be "is it reusable and maintainable?" > For Jeffry Carter (and anybody interested in helping me understand the > Ada way): here is how it looks like when I haven't thought much about > it. Notice that all this is completely from the point of view of the > application using the package, with very few implementation choices. > If I knew Ada, wouldn't these explanations be pretty much the contents > of the specification file, with about everything from the body being > still to invent? How Ada-ish is that thought process? > > > Thanks in advance for your reviews of my ideas, > Natacha -- Ludovic Brenta.