forked from jdehaan2014/GearLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ureader.pas
108 lines (85 loc) · 2.44 KB
/
ureader.pas
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
unit uReader;
{ This unit contains the reader class that reads a file and provides characters
to the Lexer.
Copyright (C) 2018 J. de Haan [email protected]
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.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Generics.Collections;
const
//on Unix '^D' on windows ^Z (#26)
{$IFDEF UNIX}
FileEnding = ^D;
{$ENDIF}
{$IFDEF WINDOWS}
FileEnding = ^Z //(#26)
{$ENDIF}
type
TInputType = (itPrompt, itFile);
{ TReader }
TReader = class(TStringList)
private
FFileName: TFileName;
FFileIndex: Integer;
Index: LongInt;
function getPeekChar: char;
public
property FileName: TFileName read FFileName;
property FileIndex: Integer read FFileIndex;
property PeekChar: char read getPeekChar;
constructor Create(Source: String; InputType: TInputType);
function NextChar: char;
end;
TFileNameArray = specialize TList<TFileName>;
var
FileNameArray: TFileNameArray;
implementation
{ TReader }
function TReader.getPeekChar: char; //peek at next character, but don't process it
begin
try
Result := Text[Index];
except
Result := FileEnding;
end;
end;
constructor TReader.Create(Source: String; InputType: TInputType);
begin
inherited Create;
FFileName := '';
FFileIndex := -1;
Index := 1;
case InputType of
itPrompt: add(Source);
itFile:
begin
FFileName := Source;
FFileIndex := FileNameArray.Add(FFileName);
LoadFromFile(FFileName);
end;
end;
end;
function TReader.NextChar: char;
begin
try
Result := Text[Index];
Inc(Index);
except
Result := FileEnding;
end;
end;
initialization
FileNameArray := TFileNameArray.Create;
finalization
FileNameArray.Free;
end.