summaryrefslogtreecommitdiff
path: root/packages/pastojs/src/pas2jsfs.pp
blob: 090a046217510dae335499edac97e7940609eba1 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 2018  Michael Van Canneyt

    Pascal to Javascript converter class.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************

  Abstract:
    FileSystem abstraction layer for compiler.
    Has only abstract classes with no actual implementation, so it does not actually
    interacts with the filesystem.
    See Pas2JSFileCache for an actual implementation.
}
unit Pas2JSFS;

{$mode objfpc}{$H+}
{$I pas2js_defines.inc}

interface

uses
  // No filesystem-dependent units here !
  Classes, SysUtils, PScanner, fpjson;

const // Messages
  nIncludeSearch = 201; sIncludeSearch = 'Include file search: %s';
  nUnitSearch = 202; sUnitSearch = 'Unitsearch: %s';
  nSearchingFileFound = 203; sSearchingFileFound = 'Searching file: %s... found';
  nSearchingFileNotFound = 204; sSearchingFileNotFound = 'Searching file: %s... not found';
  nDuplicateFileFound = 205; sDuplicateFileFound = 'Duplicate file found: "%s" and "%s"';
  nCustomJSFileNotFound = 206; sCustomJSFileNotFound = 'custom JS file not found: "%s"';
  nUsingPath = 104; sUsingPath = 'Using %s: "%s"';
  nFolderNotFound = 105; sFolderNotFound = '%s not found: %s';

Type
  // Forward definitions
  EPas2jsFS = Class(Exception);
  TPas2jsFile = class;
  TSourceLineReader = class;
  TPas2jsFSResolver = class;
  TPas2JSFS = Class;

  { TSourceLineReader }

  TSourceLineReader = class(TLineReader)
  private
    FIsEOF: boolean;
    FLineNumber: integer;
    FSource: string;
    FSrcPos: integer;
  Protected
    Procedure IncLineNumber; virtual;
    property Source: string read FSource;
    property SrcPos: integer read FSrcPos;
  public
    Constructor Create(Const aFileName, aSource: String); overload;
    function IsEOF: Boolean; override;
    function ReadLine: string; override;
    property LineNumber: integer read FLineNumber;
  end;

  TP2jsFSOption = (
    caoShowFullFilenames,
    caoShowTriedUsedFiles,
    caoSearchLikeFPC,
    caoStrictFileCase
    );
  TP2jsFSOptions = set of TP2jsFSOption;
  TKeyCompareType = (kcFilename,kcUnitName);

  { TPas2JSFS }

  TPas2JSFS = Class
  Private
    FOptions: TP2jsFSOptions;
    FReadLineCounter: SizeInt;
    FDefaultOutputPath: string;
    FUnitOutputPath: string;
    procedure SetOptionFromIndex(AIndex: Integer; AValue: boolean);
    procedure SetDefaultOutputPath(AValue: string);
    procedure SetUnitOutputPath(AValue: string);
  Protected
    // Not to be overridden
    procedure SetOption(Flag: TP2jsFSOption; Enable: boolean);
    Function OptionIsSet(Index: Integer):  Boolean;
  Protected
    // Protected Abstract. Must be overridden
    function FindSourceFileName(const aFilename: string): String; virtual; abstract;
  Public
    // Public Abstract. Must be overridden
    function FindIncludeFileName(const aFilename: string): String; virtual; abstract;
    function LoadFile(Filename: string; Binary: boolean = false): TPas2jsFile; virtual; abstract;
    Function FileExists(Const aFileName: String): Boolean; virtual; abstract;
    function FindUnitJSFileName(const aUnitFilename: string): String; virtual; abstract;
    function FindCustomJSFileName(const aFilename: string): String; virtual; abstract;
    function FindUnitFileName(const aUnitname, InFilename, ModuleDir: string; out IsForeign: boolean): String; virtual; abstract;
    procedure SaveToFile(ms: TFPJSStream; Filename: string); virtual; abstract;
    function PCUExists(var aFileName: string): Boolean; virtual;
    procedure GetPCUDirs(aList: TStrings; const aBaseDir: String); virtual;
  Public
    // Public, may be overridden
    Function SameFileName(Const File1,File2: String): Boolean; virtual;
    Function File1IsNewer(Const File1,File2: String): Boolean; virtual;
    function ExpandDirectory(const Filename: string): string; virtual;
    function ExpandFileName(const Filename: string): string; virtual;
    function ExpandExecutable(const Filename: string): string; virtual;
    Function FormatPath(Const aFileName: string): String; virtual;
    Function DirectoryExists(Const aDirectory: string): boolean; virtual;
    function TryCreateRelativePath(const Filename, BaseDirectory: String; UsePointDirectory: boolean; out RelPath: String): Boolean; virtual;
    procedure DeleteDuplicateFiles(List: TStrings); virtual;
    function IndexOfFile(FileList: TStrings; aFilename: string; Start: integer = 0): integer; virtual;// -1 if not found
    Procedure WriteFoldersAndSearchPaths; virtual;
    function CreateResolver: TPas2jsFSResolver; virtual;
    // On success, return '', On error, return error message.
    Function AddForeignUnitPath(Const aValue: String; FromCmdLine: Boolean): String; virtual;
    Function HandleOptionPaths(C: Char; aValue: String; FromCmdLine: Boolean): String; virtual;
  Public
    Constructor Create; virtual;
    Procedure Reset; virtual;
    Procedure IncReadLineCounter;
    property ReadLineCounter: SizeInt read FReadLineCounter write FReadLineCounter;
    property Options: TP2jsFSOptions read FOptions write FOptions;
    property ShowFullPaths: boolean Index 0 Read OptionIsSet Write SetOptionFromIndex;
    property ShowTriedUsedFiles: boolean Index 1 read OptionIsSet Write SetOptionFromIndex;
    property SearchLikeFPC: boolean index 2 read OptionIsSet Write SetOptionFromIndex;
    Property StrictFileCase: Boolean Index 3 Read OptionIsSet Write SetOptionFromIndex;
    property MainOutputPath: string read FDefaultOutputPath write SetDefaultOutputPath; // includes trailing pathdelim
    property UnitOutputPath: string read FUnitOutputPath write SetUnitOutputPath; // includes trailing pathdelim
  end;

  { TPas2jsFile }

  TPas2jsFile = class
  private
    FFilename: string;
    FFS: TPas2JSFS;
    FSource: string;
  Protected
    Procedure SetSource(aSource: String);
  public
    constructor Create(aFS: TPas2jsFS; const aFilename: string);
    function CreateLineReader(RaiseOnError: boolean): TSourceLineReader; virtual; abstract;
    function Load(RaiseOnError: boolean; Binary: boolean): boolean; virtual; abstract;
    property Source: string read FSource; // UTF-8 without BOM or Binary
    Property FS: TPas2JSFS Read FFS;
    property Filename: string read FFilename;
  end;

  { TPas2jsFSResolver }

  TPas2jsFSResolver = class({$IFDEF HASFILESYSTEM}TFileResolver{$ELSE}TBaseFileResolver{$ENDIF})
  private
    FFS: TPas2jsFS;
  public
    constructor Create(aFS: TPas2jsFS); reintroduce;
    // Redirect all calls to FS.
    function FindIncludeFileName(const aFilename: string): String; override;
    function FindIncludeFile(const aFilename: string): TLineReader; override;
    function FindSourceFile(const aFilename: string): TLineReader; override;
    property FS: TPas2jsFS read FFS;
  end;


Const
  p2jsfcoCaption: array[TP2jsFSOption] of string = (
      // only used by experts, no need for resourcestrings
      'Show full filenames',
      'Show tried/used files',
      'Search files like FPC',
      'Strict file case'
      );
    // 'Combine all JavaScript into main file',
    EncodingBinary = 'Binary';

  DefaultPas2jsFSOptions = [];

implementation

// No filesystem-dependent units here !

{ TPas2JSFS }

procedure TPas2JSFS.SetOptionFromIndex(AIndex: Integer; AValue: boolean);
begin
  SetOption(TP2jsFSOption(aIndex),aValue);
end;

procedure TPas2JSFS.SetOption(Flag: TP2jsFSOption; Enable: boolean);
begin
  if Enable then
    Include(FOptions,Flag)
  else
    Exclude(FOptions,Flag);
end;

function TPas2JSFS.OptionIsSet(Index: Integer): Boolean;
begin
  Result:=TP2jsFSOption(Index) in FOptions;
end;

function TPas2JSFS.PCUExists(var aFileName: string): Boolean;
begin
  Result:=Self.FileExists(aFileName);
end;

procedure TPas2JSFS.GetPCUDirs(aList: TStrings; const aBaseDir: String);
begin
  if UnitOutputPath<>'' then
    aList.Add(UnitOutputPath);
  aList.Add(aBaseDir);
end;

function TPas2JSFS.SameFileName(const File1, File2: String): Boolean;
begin
  Result:=CompareText(File1,File2)=0;
end;

function TPas2JSFS.File1IsNewer(const File1, File2: String): Boolean;
begin
  Result:=False;
  if File1=File2 then ;
end;

function TPas2JSFS.ExpandDirectory(const Filename: string): string;
begin
  Result:=FileName;
end;

function TPas2JSFS.ExpandFileName(const Filename: string): string;
begin
  Result:=FileName;
end;

function TPas2JSFS.ExpandExecutable(const Filename: string): string;
begin
  Result:=FileName
end;

function TPas2JSFS.FormatPath(const aFileName: string): String;
begin
  Result:=aFileName;
end;

function TPas2JSFS.DirectoryExists(const aDirectory: string): boolean;
begin
  Result:=aDirectory='';
end;

function TPas2JSFS.TryCreateRelativePath(const Filename, BaseDirectory: String; UsePointDirectory: boolean; out RelPath: String
  ): Boolean;
begin
  Result:=True;
  RelPath:=FileName;
  if (BaseDirectory='') or UsePointDirectory then ;
end;

procedure TPas2JSFS.DeleteDuplicateFiles(List: TStrings);
var
  i, j: Integer;
begin
  for i:=0 to List.Count-2 do
    for j:=List.Count-1 downto i+1 do
      if SameFileName(List[i],List[j]) then
        List.Delete(j);
end;

function TPas2JSFS.IndexOfFile(FileList: TStrings; aFilename: string;
  Start: integer): integer;
var
  i: Integer;
begin
  if FileList<>nil then
    for i:=Start to FileList.Count-1 do
      if SameFileName(FileList[i],aFilename) then exit(i);
  Result:=-1;
end;

procedure TPas2JSFS.WriteFoldersAndSearchPaths;
begin
  // Do nothing
end;

function TPas2JSFS.CreateResolver: TPas2jsFSResolver;
begin
  Result:=TPas2jsFSResolver.Create(Self);
end;

function TPas2JSFS.AddForeignUnitPath(const aValue: String; FromCmdLine: Boolean): String;
begin
  Result:='';
  if (aValue='') or FromCmdLine then ;
end;

function TPas2JSFS.HandleOptionPaths(C: Char; aValue: String; FromCmdLine: Boolean): String;
begin
  Result:='Invalid parameter: -F'+C+aValue;
  if FromCmdLine then ;
end;

constructor TPas2JSFS.Create;
begin
  FOptions:=DefaultPas2jsFSOptions;
end;

procedure TPas2JSFS.Reset;
begin
  FReadLineCounter:=0;
  FUnitOutputPath:='';
  FDefaultOutputPath:='';
end;

procedure TPas2JSFS.IncReadLineCounter;
begin
  Inc(FReadLineCounter);
end;

procedure TPas2JSFS.SetDefaultOutputPath(AValue: string);
begin
  AValue:=ExpandDirectory(AValue);
  if FDefaultOutputPath=AValue then Exit;
  FDefaultOutputPath:=AValue;
end;

procedure TPas2JSFS.SetUnitOutputPath(AValue: string);

begin
  AValue:=ExpandDirectory(AValue);
  if FUnitOutputPath=AValue then Exit;
  FUnitOutputPath:=AValue;
end;


{ TPas2jsFile }

procedure TPas2jsFile.SetSource(aSource: String);
begin
  FSource:=ASource;
end;

constructor TPas2jsFile.Create(aFS: TPas2jsFS; const aFilename: string);
begin
  FFS:=aFS;
  FFileName:=aFileName;
end;

procedure TSourceLineReader.IncLineNumber;
begin
  inc(FLineNumber);
end;

Constructor TSourceLineReader.Create(Const aFileName, aSource: String);
begin
  Inherited Create(aFileName);
  FSource:=aSource;
  FSrcPos:=1;
  FIsEOF:=FSource='';
end;

function TSourceLineReader.IsEOF: Boolean;
begin
  Result:=FIsEOF;
end;

function TSourceLineReader.ReadLine: string;
var
  S: string;
  p, SrcLen: integer;

  procedure GetLine;
  var
    l: SizeInt;
  begin
    l:=p-FSrcPos;
    Result:=copy(S,FSrcPos,l);
    FSrcPos:=p;
    IncLineNumber;
    //writeln('GetLine "',Result,'"');
  end;

begin
  if FIsEOF then exit('');
  S:=Source;
  SrcLen:=length(S);
  p:=FSrcPos;
  while p<=SrcLen do
    case S[p] of
    #10,#13:
      begin
        GetLine;
        inc(p);
        if (p<=SrcLen) and (S[p] in [#10,#13]) and (S[p]<>S[p-1]) then
          inc(p);
        if p>SrcLen then
          FIsEOF:=true;
        FSrcPos:=p;
        exit;
      end;
    else
      inc(p);
    end;
  FIsEOF:=true;
  GetLine;
end;


function TPas2jsFSResolver.FindIncludeFile(const aFilename: string): TLineReader;
var
  Filename: String;
begin
  Result:=nil;
  Filename:=FS.FindIncludeFileName(aFilename);
  if Filename='' then exit;
  try
    Result:=FindSourceFile(Filename);
  except
    // error is shown in the scanner, which has the context information
  end;
end;

constructor TPas2jsFSResolver.Create(aFS: TPas2jsFS);
begin
  FFS:=aFS;
end;

function TPas2jsFSResolver.FindIncludeFileName(const aFilename: string): String;

begin
  Result:=FS.FindIncludeFileName(aFilename);
end;


function TPas2jsFSResolver.FindSourceFile(const aFilename: string): TLineReader;

var
  CurFilename: String;

begin
  CurFilename:=FS.FindSourceFileName(aFileName);
  Result:=FS.LoadFile(CurFilename).CreateLineReader(false);
end;



end.