File: girtokens.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (190 lines) | stat: -rw-r--r-- 5,209 bytes parent folder | download | duplicates (3)
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
{
girtokens.pas
Copyright (C) 2011  Andrew Haines andrewd207@aol.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
}
unit girTokens;

{$mode objfpc}{$H+}

interface

uses
  Classes;

type
  TGirToken = (gtInvalid, gtEmpty, gtAlias, gtConstant, gtRecord, gtBitField, gtEnumeration,
               gtCallback, gtUnion, gtFunction, gtReturnValue, gtType,
               gtParameters, gtParameter, gtInstanceParameter, gtMember, gtField, gtMethod, gtArray,
               gtDoc, gtDocDeprecated, gtConstructor, gtRepository, gtInclude, gtNameSpace, gtPackage,
               gtCInclude, gtClass, gtProperty, gtVirtualMethod, gtInterface,
               gtGlibSignal, gtImplements, gtPrerequisite,gtVarArgs, gtObject, gtClassStruct, gtGType,
               // Direction for parameters.  in is default = no pointer. out and inout means one pointer level.
               // If subnode is array then increase pointer level.
               gtIn, gtOut, gtInOut, gtSourcePosition, gtFunctionMacro, gtDocSection, gtAttribute
               );

  TGirVersion = object
    Major: Integer;
    Minor: Integer;
    function AsString: String; // '$major.$minor'
    function AsMajor: TGirVersion; // return as $major.0 i.e 3.0 instead of 3.8

  end;


var
  GirTokenName: array[TGirToken] of String = (
    'Invalid Name',
    '{empty}',
    'alias',
    'constant',
    'record',
    'bitfield',
    'enumeration',
    'callback',
    'union',
    'function',
    'return-value',
    'type',
    'parameters',
    'parameter',
    'instance-parameter',
    'member',
    'field',
    'method',
    'array',
    'doc',
    'doc-deprecated',
    'constructor',
    'repository',
    'include',
    'namespace',
    'package',
    'c:include',
    'class',
    'property',
    'virtual-method',
    'interface',
    'glib:signal',
    'implements',
    'prerequisite',
    'varargs',
    'object',
    'classstruct',
    'gtype',
    'in',
    'out',
    'inout',
    'source-position',
    'function-macro',
    'docsection',
    'attribute'

  );

  function GirTokenNameToToken(AName: String): TGirToken;
  function girVersion(AVersion: String; ADefaultMajor: Integer = -1; ADefaultMinor: Integer = -1): TGirVersion;
  function girVersion(AMajor, AMinor: Integer): TGirVersion;

  operator >= (AVersion, BVersion: TGirVersion): Boolean;
  operator <= (AVersion, BVersion: TGirVersion): Boolean;
  operator > (AVersion, BVersion: TGirVersion): Boolean;

implementation
uses
  sysutils;

function GirTokenNameToToken(AName: String): TGirToken;
begin
  if AName = '' then
    Exit(gtEmpty);
  try
  for Result in TGirToken do
    if GirTokenName[Result][1] <> AName[1] then
      continue
    else if GirTokenName[Result] = AName then
      Exit;
  Result := gtInvalid;

  except
    WriteLn('GirToken Exception: ',AName);
  end;
end;

function girVersion(AVersion: String; ADefaultMajor: Integer; ADefaultMinor: Integer): TGirVersion;
var
  SplitPoint: Integer;
  Minor: String;
begin
  if (AVersion = '') and (ADefaultMajor <> -1) and (ADefaultMinor <> -1) then
  begin
    Result := girVersion(ADefaultMajor, ADefaultMinor);
    Exit;
  end;
  SplitPoint := Pos('.', AVersion);

  if SplitPoint < 1 then
    raise Exception.Create(Format('Invalid version string format: "%s" (length %d)', [AVersion, Length(AVersion)]));

  Result.Major:=StrToInt(Copy(AVersion,1, SplitPoint-1));
  Minor := Copy(AVersion,SplitPoint+1, MaxInt);
  SplitPoint := Pos('.', Minor);
  // we are not interested in the third version chunk
  if SplitPoint > 0 then
    Minor := Copy(Minor,1, SplitPoint-1);
  Result.Minor:=StrToInt(Minor);
end;

function girVersion(AMajor, AMinor: Integer): TGirVersion;
begin
  REsult.Major := AMajor;
  Result.Minor := AMinor;
end;

operator >= (AVersion, BVersion: TGirVersion): Boolean;
begin
  Result :=     (AVersion.Major > BVersion.Major)
            or ((AVersion.Major = BVersion.Major) and (AVersion.Minor >= BVersion.Minor));
end;

operator<=(AVersion, BVersion: TGirVersion): Boolean;
begin
  Result :=     (AVersion.Major < BVersion.Major)
            or ((AVersion.Major = BVersion.Major) and (AVersion.Minor <= BVersion.Minor));
end;

operator > (AVersion, BVersion: TGirVersion): Boolean;
begin
  Result :=     (AVersion.Major > BVersion.Major)
            or ((AVersion.Major = BVersion.Major) and (AVersion.Minor > BVersion.Minor));
end;

{ TGirVersion }

function TGirVersion.AsString: String;
begin
  Result := IntToStr(Major)+'.'+IntToStr(Minor);
end;

function TGirVersion.AsMajor: TGirVersion;
begin
  Result.Major:=Major;
  REsult.Minor:=0;
end;

end.