comp.lang.ada
 help / color / mirror / Atom feed
From: Warren <ve3wwg@gmail.com>
Subject: Dynamic Variant Record Creation
Date: Tue, 16 Mar 2010 17:11:04 +0000 (UTC)
Date: 2010-03-16T17:11:04+00:00	[thread overview]
Message-ID: <Xns9D3D861E55DA0WarrensBlatherings@188.40.43.213> (raw)

I am trying to solve a lexer Ada design problem, using a 
variant record (Token_Unit) definition:
        
    type Token_Type is (
        LEX_000,
        LEX_BINARY,
        LEX_SECTION,
        '!',
        '"',
        '#',
        '$', 
        ...etc..
        '^',
        '_',
        '`',
        LEX_SFUN3,
        LEX_SFUN2,
        LEX_SFUN1,
        LEX_SFUNN,
        LEX_FUN3,
        LEX_FUN2,
        LEX_FUN1,
        LEX_FUNN,
        ...etc...
        LEX_EOF
    );

    type Token_Unit(Token : Token_Type := LEX_EOF) is
        record
            case Token is
                when LEX_SFUN3 | LEX_SFUN2 | LEX_SFUN1 | LEX_SFUNN
                   | LEX_FUN3  | LEX_FUN2  | LEX_FUN1  | LEX_FUNN =>
                    Func :      Func_Type;
                when LEX_IDENT | LEX_SIDENT | LEX_LNUMBER | LEX_NUMBER
                   | LEX_HEX   | LEX_STRLIT | LEX_INLREM  | LEX_FLOAT
                   | LEX_REM =>
                    ID :        String_ID;                                                                                                                                                             
                when others =>
                    null;
            end case;
        end record;
                            

    procedure Get_Token(Object : in out Lexer; Token : out Token_Unit) is
                        
        procedure Emit_Token(T : Token_Type) is
            T : Token_Type := Token_Type'Val(Character'Pos(Ch));
        begin
            Token := ( Token => T );
        end;

        ...         
        
    end;

In the above, I need to create a Token_Unit from             
a single character (there is no "id" in this case). The values 
of operator characters are carefully coordinated in the Token_Type 
using a representation clause (just FYI).

But the issue is this:     

*.adb:339:33: value for discriminant "token" must be static
*.adb:339:33: "T" is not static constant or named number (RM 4.9(5))

I could put a huge case statement in, but I'd prefer not to.

Is there a better way to handle the dynamic creation of
a variant record? I have other scenarios involving a    
Func_Type and String_ID as well, which suffer from the same 
"must be static" problem.

I should also mention that I am trying to keep the Token_Unit
to 32-bits, for efficiency.  If it were possible to
divide up the Token_Type (16-bits) into 13-bits and use 
a different descriminant of 3 bits, that would
work also if the total size remained 32. The String_ID 
portion (or Func_Type) are both 16-bit.

Warren



             reply	other threads:[~2010-03-16 17:11 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-16 17:11 Warren [this message]
2010-03-16 18:31 ` Dynamic Variant Record Creation Georg Bauhaus
2010-03-16 18:57 ` Adam Beneschan
2010-03-16 20:01   ` Warren
2010-03-16 20:09     ` Jeffrey R. Carter
2010-03-16 20:24       ` Warren
2010-03-16 20:40         ` Robert A Duff
2010-03-16 20:44           ` Warren
2010-03-16 20:31     ` Robert A Duff
2010-03-16 20:59       ` Warren
2010-03-16 21:55         ` Jeffrey R. Carter
2010-03-17 14:40           ` Warren
2010-03-18 12:57           ` Warren
2010-03-16 21:58         ` Robert A Duff
2010-03-17 14:22           ` Charmed Snark
2010-03-17 14:49             ` Robert A Duff
2010-03-17 16:30               ` Warren
2010-03-16 21:15       ` Adam Beneschan
2010-03-16 23:24       ` Adam Beneschan
2010-03-16 20:15   ` Robert A Duff
2010-03-16 21:00     ` Warren
2010-03-16 23:39   ` Randy Brukardt
2010-03-16 23:43     ` Randy Brukardt
2010-03-17  0:15     ` Robert A Duff
2010-03-17 14:28       ` Warren
2010-03-18  0:02       ` Randy Brukardt
2010-03-17  4:20     ` Adam Beneschan
2010-03-18  0:13       ` Randy Brukardt
2010-03-18 13:00         ` Warren
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox