summaryrefslogtreecommitdiff
path: root/utils/unicode/gbpparser.lpr
blob: 1d47fa44795985d4b89080a5093919a8f8ab087a (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
{ Parser and code generator for the GraphemeBreakProperty.

  Copyright (C) 2021 Nikolay Nikolov <nickysn@users.sourceforge.net>

  This source 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 code 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.

  A copy of the GNU General Public License is available on the World Wide Web
  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  Boston, MA 02110-1335, USA.
}


program gbpparser;

{$mode objfpc}{$H+}

uses
  SysUtils, StrUtils;

type
  TGraphemeBreakProperty = (
    gbpOther,
    gbpPrepend,
    gbpCR,
    gbpLF,
    gbpControl,
    gbpExtend,
    gpbRegional_Indicator,
    gbpSpacingMark,
    gbpL,
    gbpV,
    gbpT,
    gbpLV,
    gbpLVT,
    gbpE_Base,
    gbpE_Modifier,
    gbpZWJ,
    gbpGlue_After_Zwj,
    gbpE_Base_GAZ);

  TRange = record
    RangeLo, RangeHi: UCS4Char;
  end;
  TRanges = array of TRange;

var
  GraphemeBreakProperties: array [UCS4Char] of TGraphemeBreakProperty;
  GBPStats: array [TGraphemeBreakProperty] of record
    Exists: Boolean;
    Handled: Boolean;
    MinValue: UCS4Char;
    MaxValue: UCS4Char;
    Count: LongInt;
    Ranges: TRanges;
  end;

function ParseGraphemeBreakProperty(S: string): TGraphemeBreakProperty;
begin
  S := Trim(S);
  case S of
    'Prepend':
      Result := gbpPrepend;
    'CR':
      Result := gbpCR;
    'LF':
      Result := gbpLF;
    'Control':
      Result := gbpControl;
    'Extend':
      Result := gbpExtend;
    'Regional_Indicator':
      Result := gpbRegional_Indicator;
    'SpacingMark':
      Result := gbpSpacingMark;
    'L':
      Result := gbpL;
    'V':
      Result := gbpV;
    'T':
      Result := gbpT;
    'LV':
      Result := gbpLV;
    'LVT':
      Result := gbpLVT;
    'E_Base':
      Result := gbpE_Base;
    'E_Modifier':
      Result := gbpE_Modifier;
    'ZWJ':
      Result := gbpZWJ;
    'Glue_After_Zwj':
      Result := gbpGlue_After_Zwj;
    'E_Base_GAZ':
      Result := gbpE_Base_GAZ;
    else
      raise EArgumentException('Unknown grapheme break property: ''' + S + '''');
  end;
end;

procedure ParseRange(S: string; out RangeLo, RangeHi: UCS4Char);
var
  dp: SizeInt;
begin
  S := Trim(S);
  dp := Pos('..', S);
  if dp > 0 then
  begin
    RangeLo := StrToInt('$' + LeftStr(S, dp - 1));
    RangeHi := StrToInt('$' + Copy(S, dp + 2, Length(S) - dp + 3));
  end
  else
  begin
    RangeLo := StrToInt('$' + S);
    RangeHi := RangeLo;
  end;
end;

procedure ParseGraphemeBreakProperties(const FileName: string);
var
  InF: TextFile;
  S: string;
  SplitS: TStringArray;
  LineNr: Integer = 0;
  gbp: TGraphemeBreakProperty;
  RangeLo, RangeHi, R: UCS4Char;
begin
  if not FileExists(FileName) then
  begin
    Writeln('File doesn''t exist: ', FileName);
    Halt(1);
  end;
  AssignFile(InF, FileName);
  Reset(InF);
  while not EoF(InF) do
  begin
    Inc(LineNr);
    Readln(InF, S);
    S := Trim(S);
    if Pos('#', S) > 0 then
      S := LeftStr(S, Pos('#', S) - 1);
    if S <> '' then
    begin
      SplitS := S.Split([';']);
      if Length(SplitS) <> 2 then
        raise Exception.Create('Invalid number of ; separators on line ' + IntToStr(LineNr));
      ParseRange(SplitS[0], RangeLo, RangeHi);
      gbp := ParseGraphemeBreakProperty(SplitS[1]);
      for R := RangeLo to RangeHi do
        GraphemeBreakProperties[R] := gbp;
    end;
  end;
  CloseFile(InF);
end;

procedure CalcStatsAndRanges;
var
  Ch: UCS4Char;
  gbp, prev_gbp: TGraphemeBreakProperty;
begin
  FillChar(GBPStats, SizeOf(GBPStats), 0);
  gbp := Low(TGraphemeBreakProperty);
  for Ch := Low(UCS4Char) to High(UCS4Char) do
  begin
    prev_gbp := gbp;
    gbp := GraphemeBreakProperties[Ch];
    with GBPStats[gbp] do
    begin
      if not Exists then
      begin
        Exists := True;
        MinValue := Ch;
        MaxValue := Ch;
        Count := 1;
        SetLength(Ranges, 1);
        Ranges[0].RangeLo := Ch;
        Ranges[0].RangeHi := Ch;
      end
      else
      begin
        MaxValue := Ch;
        Inc(Count);
        if prev_gbp <> gbp then
        begin
          SetLength(Ranges, Length(Ranges) + 1);
          with Ranges[High(Ranges)] do
          begin
            RangeLo := Ch;
            RangeHi := Ch;
          end;
        end
        else
          Ranges[High(Ranges)].RangeHi := Ch;
      end;
    end;
  end;
end;

procedure MaybeCoalesceRanges(RLo, RHi: UCS4Char);
var
  gbp: TGraphemeBreakProperty;
  RI: Integer;
begin
  for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
    if GBPStats[gbp].Exists and (not GBPStats[gbp].Handled) then
    begin
      for RI := 0 to High(GBPStats[gbp].Ranges) - 1 do
        if (GBPStats[gbp].Ranges[RI].RangeHi = (RLo - 1)) and
           (GBPStats[gbp].Ranges[RI + 1].RangeLo = (RHi + 1)) then
        begin
          GBPStats[gbp].Ranges[RI].RangeHi := GBPStats[gbp].Ranges[RI + 1].RangeHi;
          Delete(GBPStats[gbp].Ranges, RI + 1, 1);
          exit;
        end;
    end;
end;

function FindMinRangeCount: Integer;
var
  gbp: TGraphemeBreakProperty;
begin
  Result := High(Integer);
  for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
    if GBPStats[gbp].Exists and (not GBPStats[gbp].Handled) and (Length(GBPStats[gbp].Ranges) < Result) then
      Result := Length(GBPStats[gbp].Ranges);
end;

function ApplyLV_LVTCompression: Boolean;
const
  RangeLo = 44032;
  RangeHi = 55203;
var
  Ch: UCS4Char;
begin
  Result := False;
  if (GBPStats[gbpLV].MinValue <> RangeLo) or (GBPStats[gbpLV].MaxValue <> (RangeHi - 27)) or
     (GBPStats[gbpLVT].MinValue <> (RangeLo + 1)) or (GBPStats[gbpLVT].MaxValue <> RangeHi) then
    exit;
  for Ch := RangeLo to RangeHi do
  begin
    if ((Ch - RangeLo) mod 28) = 0 then
    begin
      if GraphemeBreakProperties[Ch] <> gbpLV then
        exit;
    end
    else
    begin
      if GraphemeBreakProperties[Ch] <> gbpLVT then
        exit;
    end;
  end;
  Result := True;
end;

procedure GenCode(const OutFileName: string);
const
  RangeCountThreshold = 30{400};
var
  gbp: TGraphemeBreakProperty;
  RI, NextRangeCount: Integer;
  OutFile: TextFile;
begin
  Writeln('Generating file: ', OutFileName);

  AssignFile(OutFile, OutFileName);
  Rewrite(OutFile);

  Writeln(OutFile, '{ do not edit, this file is autogenerated by the gbpparser tool }');

  { unused properties are already handled }
  for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
    if not GBPStats[gbp].Exists then
      GBPStats[gbp].Handled := True;

  { handle single codepoints first }
  for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
    if (not GBPStats[gbp].Handled) and (GBPStats[gbp].Count = 1) then
    begin
      if GBPStats[gbp].MinValue <> GBPStats[gbp].MaxValue then
        raise Exception.Create('Internal error');
      Writeln(OutFile, 'if Ch=', GBPStats[gbp].MinValue, 'then result:=',gbp,' else');
      GBPStats[gbp].Handled := True;
      MaybeCoalesceRanges(GBPStats[gbp].MinValue, GBPStats[gbp].MaxValue);
    end;

  { handle single range codepoints next }
  while FindMinRangeCount = 1 do
    for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
      if (not GBPStats[gbp].Handled) and (Length(GBPStats[gbp].Ranges) = 1) then
      begin
        Writeln(OutFile, 'if(Ch>=', GBPStats[gbp].MinValue, ')and(Ch<=', GBPStats[gbp].MaxValue, ')then result:=',gbp,' else');
        GBPStats[gbp].Handled := True;
        MaybeCoalesceRanges(GBPStats[gbp].MinValue, GBPStats[gbp].MaxValue);
      end;

  if ApplyLV_LVTCompression then
  begin
    Writeln(OutFile, 'if(Ch>=44032)and(Ch<=55203)then begin if((Ch-44032)mod 28)=0then result:=gbpLV else result:=gbpLVT end else');
    GBPStats[gbpLV].Handled := True;
    GBPStats[gbpLVT].Handled := True;
  end;

  repeat
    NextRangeCount := FindMinRangeCount;
    if NextRangeCount <= RangeCountThreshold then
      for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
      begin
        if not GBPStats[gbp].Handled and (Length(GBPStats[gbp].Ranges) <= NextRangeCount) then
        begin
          GBPStats[gbp].Handled := True;
          Write(OutFile, 'if');
          for RI := 0 to High(GBPStats[gbp].Ranges) do
          begin
            if RI <> 0 then
              Writeln(OutFile, 'or');
            with GBPStats[gbp].Ranges[RI] do
            begin
              if RangeLo = RangeHi then
                Write(OutFile, '(Ch=', RangeLo, ')')
              else
                Write(OutFile, '((Ch>=', RangeLo, ')and(Ch<=', RangeHi, '))');
              MaybeCoalesceRanges(RangeLo, RangeHi);
            end;
          end;
          Writeln(OutFile, 'then result:=',gbp,' else');
        end;
      end;
  until NextRangeCount > RangeCountThreshold;

  if NextRangeCount <> High(Integer) then
  begin
    //for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
    //  if not GBPStats[gbp].Handled then
    //    Writeln(gbp, ' ', GBPStats[gbp].MinValue, '..', GBPStats[gbp].MaxValue, ' ', GBPStats[gbp].Count, ' ', Length(GBPStats[gbp].Ranges), ' ', (GBPStats[gbp].MaxValue - GBPStats[gbp].MinValue + 7) div 8);
    Writeln(OutFile, 'case Ch of');
    for gbp := Succ(Low(TGraphemeBreakProperty)) to High(TGraphemeBreakProperty) do
    begin
      if not GBPStats[gbp].Handled then
      begin
        GBPStats[gbp].Handled := True;
        for RI := 0 to High(GBPStats[gbp].Ranges) do
        begin
          if RI <> 0 then
            Writeln(OutFile, ',');
          with GBPStats[gbp].Ranges[RI] do
          begin
            if RangeLo = RangeHi then
              Write(OutFile, RangeLo)
            else
              Write(OutFile, RangeLo, '..', RangeHi);
          end;
        end;
        Writeln(OutFile, ':result:=', gbp, ';');
      end;
    end;
    Writeln(OutFile, 'else result:=gbpOther end');
  end
  else
    Writeln(OutFile, 'result:=gbpOther');

  CloseFile(OutFile);
end;

begin
  FillChar(GraphemeBreakProperties, SizeOf(GraphemeBreakProperties), 0);
  ParseGraphemeBreakProperties('data/UCD/auxiliary/GraphemeBreakProperty.txt');
  CalcStatsAndRanges;
  GenCode('graphemebreakproperty_code.inc');
  Writeln('Done');
end.