From: "Vincent Smeets" <No@Spam.org>
Subject: Overloading one instance of a dispatching function
Date: Thu, 26 Sep 2002 19:52:42 +0200
Date: 2002-09-26T19:52:42+02:00 [thread overview]
Message-ID: <amvhk6$4qp$04$1@news.t-online.com> (raw)
Hallo,
I am trying to make an implementation of the ABNF syntax defined in RFC2234.
It defines rules for parsing mail messages. Here are some examples:
BIT = "0" / "1" ; Character "0" or character "1"
CRLF = CR LF ; Rule CR followed by rule LF
X = BIT / CRLF ; Rule BIT or rule CRLF
I have added my source code so far. A rule is implemented as an tagged type,
a character rule as a Character_Set and an alternation of rules by a record
with class wide pointers.
The "or"-operation for two rules works OK, but I want to optimize the case
that the "or"-opration is used for two Character_Set's. This case can
produce a new Character_Set with the characters from both Character_Set's. I
have tryed to solve this problem by adding a function to the specification:
function "or" (Left, Right : in Character_Set) return Character_Set;
This gives a problem at the place where I am using the "or"-operator. The
compiler can't resolve which "or"-operator has to be used because both
"or"-operators match by type. I would suspect (and hoped) that an exact type
(like Character_Set) had an higher precedance than a class wide type (like
Rule'Class), but it isn't. :-(
How can I solve this problem? I would like to optimize the "or" for two
Character_Set's but don't want to require that the user has to classify
which "or" he will use.
Thanks,
Vincent
PS: This is no homework. See news:ajrisv$lc9$03$1@news.t-online.com
----------------------------------------------------------------------------
with Ada.Finalization;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;
package ABNF is
type Rule is abstract tagged private;
type Alternative is abstract new Rule with private;
function "or" (Left, Right : in Rule'Class) return Alternative'Class;
type Character_Set is new Alternative with private;
function "or" (Left, Right : in Character_Set) return Character_Set;
function To_Rule (Item : in Character) return Character_Set;
type Concatination is new Rule with private;
function "and" (Left, Right : in Rule'Class) return Concatination;
private
type Rule is abstract new Ada.Finalization.Controlled with null record;
type Rule_Access is access Rule'Class;
type Alternative is abstract new Rule with null record;
type Alternative_Rule is new Alternative with
record
Left, Right : Rule_Access;
end record;
-- procedure Adjust (Item : in out Alternative_Rule);
-- procedure Finalize (Item : in out Alternative_Rule);
type Character_Set is new Alternative with
record
Set : Ada.Strings.Maps.Character_Set;
end record;
type Concatination is new Rule with
record
Left, Right : Rule_Access;
end record;
-- procedure Adjust (Item : in out Concatination);
-- procedure Finalize (Item : in out Concatination);
end ABNF;
----------------------------------------------------------------------------
with ABNF;
pragma Elaborate_All (ABNF);
procedure Forward_PGP is
use ABNF;
A : constant ABNF.Rule'Class :=
ABNF.To_Rule ('a') or
ABNF.To_Rule ('A');
AB : constant ABNF.Rule'Class :=
A and
ABNF.To_Rule ('b');
AC : constant ABNF.Rule'Class :=
A and
ABNF.To_Rule ('C');
AB_AC : constant ABNF.Rule'Class :=
AB or AC;
begin
null;
end Forward_PGP;
next reply other threads:[~2002-09-26 17:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-09-26 17:52 Vincent Smeets [this message]
2002-09-27 21:26 ` Overloading one instance of a dispatching function Dmitry A.Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox