summaryrefslogtreecommitdiff
path: root/auto/lib/OpenGL/Spec.pm
blob: 1311b394dd8af5c920a996abbff2d12c04efc11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package OpenGL::Spec;

# A very simple task further complicated by the fact that some people
# can't read, others use legacy Operating Systems, and others don't give
# a damn about using a halfway decent text editor.
#
# The code to parse the _template_ is so simple and straightforward...
# yet the code to parse the real spec files is this mess.

my %typemap = (
    bitfield    => "GLbitfield",
    boolean     => "GLboolean",
    # fsck up in EXT_vertex_array
    Boolean     => "GLboolean",
    byte        => "GLbyte",
    clampd      => "GLclampd",
    clampf      => "GLclampf",
    double      => "GLdouble",
    enum        => "GLenum",
    # Intel fsck up
    Glenum      => "GLenum",
    float       => "GLfloat",
    half        => "GLuint",
    int         => "GLint",
    short       => "GLshort",
    sizei       => "GLsizei",
    ubyte       => "GLubyte",
    uint        => "GLuint",
    ushort      => "GLushort",
    DMbuffer    => "void *",

    # ARB VBO introduces these
    sizeiptrARB => "GLsizeiptrARB",
    intptrARB   => "GLintptrARB",

    # ARB shader objects introduces these, charARB is at least 8 bits,
    # handleARB is at least 32 bits
    charARB     => "GLcharARB",
    handleARB   => "GLhandleARB",

    # GLX 1.3 defines new types which might not be available at compile time
    #GLXFBConfig   => "void*",
    #GLXFBConfigID => "XID",
    #GLXContextID  => "XID",
    #GLXWindow     => "XID",
    #GLXPbuffer    => "XID",

    # Weird stuff for some SGIX extension
    #GLXFBConfigSGIX   => "void*",
    #GLXFBConfigIDSGIX => "XID",
);

my %void_typemap = (
    void    => "GLvoid",
);

my $section_re  = qr{^[A-Z]};
my $function_re = qr{^(.+) ([a-z][a-z0-9_]*) \((.+)\)$}i;
my $token_re    = qr{^([A-Z0-9][A-Z0-9_]*):?\s+((?:0x)?[0-9A-F]+)(.*)$};
my $prefix_re   = qr{^(?:AGL | GLX | WGL)_}x;
my $eofnc_re    = qr{ \);?$ | ^$ }x;
my $function_re = qr{^(.+) ([a-z][a-z0-9_]*) \((.+)\)$}i;
my $prefix_re   = qr{^(?:gl | agl | wgl | glX)}x;
my $types_re    = __compile_wordlist_cap(keys %typemap);
my $voidtype_re = __compile_wordlist_cap(keys %void_typemap);

sub new($)
{
    my $class = shift;
    my $self = { section => {} };
    $self->{filename} = shift;
    local $/;
    open(my $fh, "<$self->{filename}") or die "Can't open $self->{filename}";
    my $content = <$fh>;
    my $section;
    my $s = $self->{section};

    $content =~ s{[ \t]+$}{}mg;
    # Join lines that end with a word-character and ones that *begin*
    # with one
    $content =~ s{(\w)\n(\w)}{$1 $2}sg;

    foreach (split /\n/, $content)
    {
        if (/$section_re/)
        {
            chomp;
            s/^Name String$/Name Strings/; # Fix common mistake
            $section = $_;
            $s->{$section} = "";
        }
        elsif (defined $section and exists $s->{$section})
        {
            s{^\s+}{}mg; # Remove leading whitespace
            $s->{$section} .= $_ . "\n";
        }
    }

    $s->{$_} =~ s{(?:^\n+|\n+$)}{}s foreach keys %$s;

    bless $self, $class;
}

sub sections()
{
    my $self = shift;
    keys %{$self->{section}};
}

sub name()
{
    my $self = shift;
    $self->{section}->{Name};
}

sub name_strings()
{
    my $self = shift;
    split("\n", $self->{section}->{"Name Strings"});
}

sub tokens()
{
    my $self = shift;
    my %tokens = ();
    foreach (split /\n/, $self->{section}->{"New Tokens"})
    {
        next unless /$token_re/;
        my ($name, $value) = ($1, $2);
        $name =~ s{^}{GL_} unless $name =~ /$prefix_re/;
        $tokens{$name} = $value;
    }

    return %tokens;
}

sub functions()
{
    my $self = shift;
    my %functions = ();
    my @fnc = ();

    foreach (split /\n/, $self->{section}->{"New Procedures and Functions"})
    {
        push @fnc, $_ unless ($_ eq "" or $_ eq "None");

        next unless /$eofnc_re/;

        if (__normalize_proto(@fnc) =~ /$function_re/)
        {
            my ($return, $name, $parms) = ($1, $2, $3);
            if (!__ignore_function($name, $extname))
            {
                $name =~ s/^/gl/ unless $name =~ /$prefix_re/;
                if ($name =~ /^gl/ && $name !~ /^glX/)
                {
                    $return =~ s/$types_re/$typemap{$1}/g;
                    $return =~ s/$voidtype_re/$void_typemap{$1}/g;
                    $parms  =~ s/$types_re/$typemap{$1}/g;
                    $parms  =~ s/$voidtype_re/$void_typemap{$1}/g;
                }
                $functions{$name} = {
                    rtype => $return,
                    parms => $parms,
                };
            }
        }
        @fnc = ();
    }

    return %functions;
}

sub __normalize_proto
{
    local $_ = join(" ", @_);
    s/\s+/ /g;                # multiple whitespace -> single space
    s/\s*\(\s*/ \(/;          # exactly one space before ( and none after
    s/\s*\)\s*/\)/;           # no after before or after )
    s/\s*\*([a-zA-Z])/\* $1/; # "* identifier" XXX: g missing?
    s/\*wgl/\* wgl/;          # "* wgl"        XXX: why doesn't the
    s/\*glX/\* glX/;          # "* glX"             previous re catch this?
    s/\.\.\./void/;           # ... -> void
    s/;$//;                   # remove ; at the end of the line
    return $_;
}

sub __ignore_function
{
    return 0;
}

sub __compile_regex
{
    my $regex = join('', @_);
    return qr/$regex/
}

sub __compile_wordlist_cap
{
    __compile_regex('\b(', join('|', @_), ')\b');
}