summaryrefslogtreecommitdiff
path: root/utils/wasmbin/parseutils.pas
blob: 898f815b6b95aef9d56f566b8be1336898555825 (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
380
381
382
383
384
385
{ This file is part of wasmbin - a collection of WebAssembly binary utils.

  Copyright (C) 2019, 2020 Dmitry Boyarintsev <skalogryz.lists@gmail.com>
  Copyright (C) 2020 by the Free Pascal development team

  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.
}

unit parseutils;

{$ifdef fpc}{$mode delphi}{$h+}{$endif}

interface

uses
  Classes, SysUtils;

type
  TCharSet = set of Char;

const
  EoLnChars      = [#10,#13];
  SpaceChars     = [#32,#9];
  InvsChars      = [#0..#32];
  WhiteSpaceChars = SpaceChars;
  SpaceEolnChars = EoLnChars+SpaceChars;
  NumericChars   = ['0'..'9'];
  HexChars       = ['0'..'9','a'..'f','A'..'F'];
  SignChars      = ['+','-'];
  SignNumericChars = NumericChars + SignChars;
  AlphabetChars  = ['a'..'z','A'..'Z'];
  AlphaNumChars  = AlphabetChars+NumericChars;

function ScanWhileWithFirst(const s: AnsiString; var index: Integer; const first, body: TCharSet): AnsiString;
function ScanWhile(const s: AnsiString; var index: Integer; const ch: TCharSet): AnsiString;
function ScanTo(const s: AnsiString; var index: Integer; const ch: TCharSet): AnsiString;
function SkipToEoln(const s: AnsiString; var index: Integer): AnsiString;
function ScanToSubstr(const s: AnsiString; var index: Integer; const substr: string): AnsiString;

// returns #10, #13, #10#13 or #13#10, if s[index] is end-of-line sequence
// otherwise returns empty string
function EolnStr(const s: AnsiString; index: Integer): String;

function IsSubStr(const sbs, s: AnsiString; index: Integer): Boolean;

// todo: not used?
function SkipCommentBlock(const s: AnsiString; var index: Integer; const closecmt: AnsiString): AnsiString;

function SkipLine(const s: AnsiString; var index: Integer): AnsiString;

procedure OffsetToLinePos(const t: AnsiString; Offset: Integer; var P: TPoint);

procedure ParseCSSValues(const s: String; css: TStrings);
procedure GetCssAbsBoundsRect(Css: TStrings; var r: TRect);
function CssValInt(const s: String; Def: integer): Integer;

type
  TCNumberFormat = (nfError, nfInteger, nfHex, nfFloat, nfFloatHex);

// if buf contains "nan" or "inf" it's also recognized as float numbers
function ScanNumberC(const buf: string; var idx: Integer;
  var numberText: string): TCNumberFormat;

implementation

function CssValInt(const s: String; Def: integer): Integer;
var
  i : integer;
  n : String;
  err : Integer;
begin
  i:=1;
  n:=ScanWhile(s, i, ['+','-']+NumericChars);
  Val(n, Result, err);
  if err<>0 then Result:=Def;
end;

procedure GetCssAbsBoundsRect(Css: TStrings; var r: TRect);
begin
  r.Left:=CssValInt(Css.Values['LEFT'], 0);
  r.Top:=CssValInt(Css.Values['top'], 0);
  r.Right:=r.Left+CssValInt(Css.Values['width'], 0);
  r.Bottom:=r.Top+CssValInt(Css.Values['height'], 0);
end;

procedure ParseCSSValues(const s: String; css: TStrings);
var
  i : integer;
  n : String;
  v : String;
begin
  i:=1;
  if (s='') or not Assigned(css) then Exit;
  while (i<=length(s))  do begin
    ScanTo(s, i, AlphaNumChars);
    n:=ScanWhile(s, i, AlphaNumChars+['_']);
    ScanTo(s, i, [':']);
    inc(i);
    ScanWhile(s, i, SpaceEolnChars);
    v:=ScanTo(s, i, [';']);
    css.Values[n]:=v;
  end;
end;

function ScanWhile(const s: AnsiString; var index: Integer; const ch: TCharSet): AnsiString;
var
  i : Integer;
begin
  Result := '';
  if (index <= 0) or (index > length(s)) then Exit;
  for i := index to length(s) do
    if not (s[i] in ch) then begin
      if i = index then Result := ''
      else Result := Copy(s, index, i - index);
      index := i;
      Exit;
    end;
  Result := Copy(s, index, length(s) - index + 1);
  index := length(s) + 1;
end;

function ScanWhileWithFirst(const s: AnsiString; var index: Integer; const first, body: TCharSet): AnsiString;
var
  i : Integer;
begin
  Result := '';
  if (index <= 0) or (index > length(s)) then Exit;

  i:=index;
  if not (s[i] in first) then Exit;

  inc(i);
  while (i<=length(s)) and (s[i] in body) do inc(i);
  Result := Copy(s, index, i-index);
  index:=i;
end;

function ScanTo(const s: AnsiString; var index: Integer; const ch: TCharSet): AnsiString;
var
  i : Integer;
begin
  Result := '';
  if (index <= 0) or (index > length(s)) then Exit;
  for i := index to length(s) do
    if (s[i] in ch) then begin
      if i = index then Result := ''
      else Result := Copy(s, index, i - index);
      index := i;
      Exit;
    end;
  Result := Copy(s, index, length(s) - index + 1);
  index := length(s) + 1;
end;

function EolnStr(const s: AnsiString; index: Integer): String;
begin
  if (index<=0) or (index>length(s)) or (not (s[index] in EoLnChars)) then
    Result:=''
  else begin
    if (index<length(s)) and (s[index+1] in EolnChars) and (s[index]<>s[index+1]) then
      Result:=Copy(s, index, 2)
    else
      Result:=s[index];
  end;
end;

function SkipToEoln(const s: AnsiString; var index: Integer): AnsiString;
begin
  Result := ScanTo(s, index, EoLnChars);
end;

function IsSubStr(const sbs, s: AnsiString; index: Integer): Boolean;
var
  i : Integer;
  j : Integer;
begin
  Result := false;
  if (sbs = '') or (length(sbs) > length(s) - index) then Exit;
  j := index;
  for i := 1 to length(sbs) do begin
    if sbs[i] <> s[j] then Exit;
    inc(j);
  end;
  Result := true;
end;

function SkipCommentBlock(const s: AnsiString; var index: Integer; const closecmt: AnsiString): AnsiString;
begin
  Result := '';
  if closecmt = '' then begin
    index := length(s) + 1;
    Exit;
  end;
  while index <= length(s) do begin
    Result := Result + ScanTo(s, index, [closecmt[1]]+EoLnChars);
    //if (index<=length(s)) and (s in EoLnChars(

    if IsSubStr(closecmt, s, index) then begin
      inc(index, length(closecmt));
      Exit;
    end else begin
      Result := Result + s[index];
      inc(index);
    end;
  end;
end;

function SkipLine(const s: AnsiString; var index: Integer): AnsiString;
begin
  Result:=ScanTo(s, index, EoLnChars);
  if (index<length(s)) and (s[index+1] in EoLnChars) and (s[index]<>s[index+1]) then
    inc(index);
  inc(index);
end;

procedure OffsetToLinePos(const t: AnsiString; Offset: Integer; var P: TPoint);
var
  i,  le  : Integer;
begin
  i := 1;
  le := 0;
  P.X := 0;
  P.Y := 0;
  while i < Offset do begin
    Inc(P.Y);
    le := i;
    SkipLine(t, i);
  end;
  P.X := Offset - le + 1;
end;

function isSubStrMatch(const s: AnsiString; index: integer; const substr: string): Boolean;
var
  i : integer;
  j : integer;
begin
  j:=index;
  Result:=false;
  for i:=1 to length(substr) do begin
    if s[j]<>substr[i] then Exit;
    inc(j);
  end;
  Result:=true;
end;

function ScanToSubstr(const s: AnsiString; var index: Integer; const substr: string): AnsiString;
var
  i: integer;
begin
  if substr='' then begin
    Result:='';
    Exit;
  end;
  i:=index;
  while (index<=length(s)) do begin
    ScanTo(s, index, [substr[1]]);
    if isSubStrMatch(s, index, substr) then begin
      inc(index, length(substr));
      Break;
    end else
      inc(index);
  end;
  Result:=Copy(s, i, index-i);
end;

function ScanHexNumber(const buf: string; var idx: Integer; var numberText: string): TCNumberFormat;
var
  xp : char;
  s : string;
begin
  Result := nfError;
  if (idx=length(buf)) or (buf[idx]<>'0') or (buf[idx+1]<>'x') then Exit;
  inc(idx, 2);
  numberText := ScanWhile(buf, idx, HexChars);
  if numberText = '' then Exit;

  numberText := '0x'+numberText;
  if ((idx<=length(buf)) and (buf[idx] in ['.','p','P'])) then begin
    if buf[idx]='.' then begin
      s := ScanWhileWithFirst(buf, idx, ['.']+HexChars, HexChars);
      if s = '' then Exit; // should not be empty
      numberText := numberText + s;
    end;

    if buf[idx] in ['p','P'] then begin
      // hexal exponenta is numeric, not hexidemical
      xp := buf[idx];
      inc(idx);
      s := ScanWhileWithFirst(buf, idx, SignNumericChars, NumericChars);
      if s = '' then Exit;
      numberText := numberText + xp+s;
    end;
    Result := nfFloathex
  end else
    Result := nfHex;
end;

function ScanNumeric(const buf: string; var idx: integer; var numberText: string): TCNumberFormat;
var
  mnt : string;
  exp : string;
  xp  : char;
begin
  Result := nfError;
  numberText:=ScanWhile(buf, idx, NumericChars);

  if ((idx<=length(buf)) and (buf[idx] in ['.','e','E'])) then begin

    //  mantissa (or fractional part) can be empty
    mnt := ScanWhileWithFirst(buf, idx, ['.']+NumericChars, NumericChars);

    if (buf[idx] in ['e','E']) then begin
      xp:=buf[idx];
      inc(idx);
      exp := ScanWhileWithFirst(buf, idx, SignNumericChars, NumericChars);
      // exponent cannot be empty, if "e" is present
      if exp='' then Exit;
      exp := xp+exp;
    end else
      exp := '';

    numberText:=NumberText+mnt+exp;
    Result := nfFloat;
  end else if numberText<>'' then
    Result := nfInteger;
end;


function ScanNumberC(const buf: string; var idx: Integer; var numberText: string): TCNumberFormat;
var
  ch  : char;
  sub : string;
begin
  Result := nfError;

  if buf[idx] in SignChars then begin
    ch:=buf[idx];
    inc(idx);
  end else
    ch := #0;

  if (idx+2<=length(buf)) and ((buf[idx]='i') and (buf[idx+1]='n') and (buf[idx+2]='f')) then begin
    numberText:='inf';
    inc(idx, 3);
    Result := nfFloat;
  end else if (idx+2<=length(buf)) and ((buf[idx]='n') and (buf[idx+1]='a') and (buf[idx+2]='n')) then begin
    numberText:='nan';
    inc(idx, 3);
    if (idx < length(buf)) and (buf[idx]=':') then begin
      inc(idx);
      sub := '';
      if (ScanNumberC(buf, idx, sub) in [nfHex, nfInteger]) then
        numberText:=numberText+':'+sub
      else
        Exit; // error
    end;
    Result := nfFloat;
  end else if (idx<length(buf)) and (buf[idx]='0') and (buf[idx+1]='x') then begin
    Result := ScanHexNumber(buf, idx, numberText)
  end else
    Result := ScanNumeric(buf, idx, numberText);

  if Result = nfError then Exit;

  if (ch<>#0) then begin
    if (numberText = '') then Exit;
    numberText:=ch+numberText;
  end;
end;

end.