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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,50e705cdf2767cc6 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Parser interface design Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <4d9c8c19$0$6769$9b4e6d93@newsspool3.arcor-online.net> Date: Thu, 7 Apr 2011 22:52:27 +0200 Message-ID: <33xzyg535kh2$.d718j0i28nn8$.dlg@40tude.net> NNTP-Posting-Date: 07 Apr 2011 22:52:27 CEST NNTP-Posting-Host: a687772e.newsspool4.arcor-online.net X-Trace: DXC=ZALbf\:OLXcOKO]LCQ@0g`4IUK On Thu, 7 Apr 2011 19:44:22 +0000 (UTC), Natasha Kerensikova wrote: > The direct transposition of my C implementation would be something like > > type Renderer_Callbacks is record > Emphasis: access function (Contents: String) return String; > Normal_Text: access function (Contents: String) return String; > Paragraph: access function (Contents: String) return String; > end record; > > function Parser (Renderer: Renderer_Callbacks; Input: String) > return String; An OO approach: type Abstract_Context is abstract new Ada.Finalization.Limited_Controlled with private; procedure On_Emphasis ( Context : in out Abstract_Context; Token : String ) is null; ... type Abstract_Source is abstract ...; function Get_Next_Line (Source : in out Abstract_Source) return String is abstract; ... type Parser is ... -- Abstract if many parsing methods to coexist procedure Parse ( Object : in out Parser; Source : in out Abstract_Source'Class; Context : in out Abstract_Context'Class ); or using mix-in: type Parser ( Source : not null access Abstract_Source'Class; Context : not null access Abstract_Context'Class ) is ...; procedure Parse (Object : in out Parser); A renderer may directly implement Abstract_Context, but usually one makes it mix-in in order to have an ability for many different renderers to have a hierarchy of their own independently on any parsing issues. This is what I suppose Georg's example of Print procedure should illustrate. [ Abstract_Context may render, generate AST, interperet. Abstract_Source may encapsulate a text file, text stream, string, XML-ish garbage etc. ] -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de