-
Notifications
You must be signed in to change notification settings - Fork 2
/
pascalparser.pas
224 lines (190 loc) · 5.96 KB
/
pascalparser.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
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
unit PascalParser;
{$MODE Delphi}
interface
uses
SysUtils, Classes, Recognizers, Parsers, TokenTypes;
function CreateRecognizers: TTokenRecognizers;
function CreateParser: TParser<TFmt>;
implementation
uses Types, Tokenizers, ParseResult, TokenParsers, CodeFmtParsers;
(* Recognizers *)
const
PasKeywords: array[0..98] of String =
('ABSOLUTE', 'ABSTRACT', 'AND', 'ARRAY', 'AS', 'ASM', 'ASSEMBLER',
'AUTOMATED', 'BEGIN', 'CASE', 'CDECL', 'CLASS', 'CONST', 'CONSTRUCTOR',
'DEFAULT', 'DESTRUCTOR', 'DISPID', 'DISPINTERFACE', 'DIV', 'DO',
'DOWNTO', 'DYNAMIC', 'ELSE', 'END', 'EXCEPT', 'EXPORT', 'EXPORTS',
'EXTERNAL', 'FAR', 'FILE', 'FINALIZATION', 'FINALLY', 'FOR', 'FORWARD',
'FUNCTION', 'GOTO', 'IF', 'IMPLEMENTATION', 'IN', 'INDEX', 'INHERITED',
'INITIALIZATION', 'INLINE', 'INTERFACE', 'IS', 'LABEL', 'LIBRARY',
'MESSAGE', 'MOD', 'NAME', 'NEAR', 'NIL', 'NODEFAULT', 'NOT', 'OBJECT',
'OF', 'OR', 'OUT', 'OVERRIDE', 'PACKED', 'PASCAL', 'PRIVATE', 'PROCEDURE',
'PROGRAM', 'PROPERTY', 'PROTECTED', 'PUBLIC', 'PUBLISHED', 'RAISE',
'READ', 'READONLY', 'RECORD', 'REGISTER', 'REPEAT', 'RESIDENT',
'RESOURCESTRING', 'SAFECALL', 'SET', 'SHL', 'SHR', 'STDCALL', 'STORED',
'STRING', 'STRINGRESOURCE', 'THEN', 'THREADVAR', 'TO', 'TRY', 'TYPE',
'UNIT', 'UNTIL', 'USES', 'VAR', 'VIRTUAL', 'WHILE', 'WITH', 'WRITE',
'WRITEONLY', 'XOR');
type
TTokenType = (
ttEol,
ttWhiteSpace,
ttDigits,
ttSingleQuote,
ttDollarSign,
ttDoubleSlash,
ttAnsiCommentBegin,
ttAnsiCommentEnd,
ttBraceOpen,
ttBraceClose,
ttHexNumber,
ttChar,
ttKeyword,
ttIdentifier,
ttUnknown
);
TTokenTypeSet = set of TTokenType;
const
AllTokenTypes: TTokenTypeSet = [ttEol..ttUnknown];
function IsPound(Ch: Char): Boolean;
begin
Result := Ch = '#';
end;
function IsDollarSign(Ch: Char): Boolean;
begin
Result := Ch = '$';
end;
function IsHexDigit(Ch: Char): Boolean;
begin
Result := Ch in ['0'..'9', 'a'..'f', 'A'..'F'];
end;
function CreateRecognizer(TokenType: TTokenType): TTokenRecognizer;
begin
case TokenType of
ttEol: Result := TNewLineRecognizer.Create;
ttWhiteSpace: Result := TPredicateRecognizer.Create(IsWhiteSpace);
ttDigits: Result := TPredicateRecognizer.Create(IsDigit);
ttSingleQuote: Result := TSingleCharRecognizer.Create(#39);
ttDollarSign: Result := TSingleCharRecognizer.Create('$');
ttDoubleSlash: Result := TStringRecognizer.Create('//');
ttAnsiCommentBegin: Result := TStringRecognizer.Create('(*');
ttAnsiCommentEnd: Result := TStringRecognizer.Create('*)');
ttBraceOpen: Result := TSingleCharRecognizer.Create('{');
ttBraceClose: Result := TSingleCharRecognizer.Create('}');
ttHexNumber: Result := TLeadingPredicateRecognizer.Create(IsDollarSign, IsHexDigit, 1);
ttChar: Result := TLeadingPredicateRecognizer.Create(IsPound, IsDigit);
ttKeyword: Result := TKeywordRecognizer.Create(PasKeyWords, csInsensitive);
ttIdentifier: Result := IdentifierRecognizer;
ttUnknown: Result := TAnyRecognizer.Create;
else raise Exception.Create('Unknown token type')
end;
end;
function CreateRecognizers: TTokenRecognizers;
var
TokenType: TTokenType;
begin
SetLength(Result, Ord(High(AllTokenTypes)) - Ord(Low(AllTokenTypes)) + 1);
for TokenType := Low(AllTokenTypes) to High(AllTokenTypes) do
Result[Ord(TokenType)] := CreateRecognizer(TokenType);
end;
(* Parsers *)
(* TokenTypeFilterParser *)
function FilterToken(TokenType: TTokenType): TParser<TToken>;
begin
Result := FilterTokenType(Ord(TokenType));
end;
function FilterTokens(TokenTypes: TTokenTypeSet): TParser<TToken>;
var
x: TByteDynArray;
TokenType: TTokenType;
begin
SetLength(x, 0);
for TokenType in TokenTypes do
begin
SetLength(x, Length(x) + 1);
x[Length(x) - 1] := Ord(TokenType);
end;
Result := FilterTokenTypes(x);
end;
(* Simple Parser maps tokens almost as-is from one enum to another *)
function SimpleParser(TokenType: TTokenType; HigherTokenType: THigherTokenType): TParser<TFmt>; overload;
begin
Result := TListToFmtMapper.Create(MapTokenToList(FilterToken(TokenType)), HigherTokenType);
end;
function SimpleParser: TParser<TFmt>; overload;
var
SourceTokens: array of TTokenType = [
ttEol,
ttWhiteSpace,
ttDigits,
ttHexNumber,
ttChar,
ttKeyword,
ttIdentifier,
ttUnknown
];
DestTokens: array of THigherTokenType = [
htCRLF,
htSpace,
htNumber,
htNumber,
htString,
htKeyword,
htIdentifier,
htUnknown
];
i: Integer;
begin
Result := nil;
for i := Low(SourceTokens) to High(SourceTokens) do
Result := OrElse<TFmt>(Result, SimpleParser(SourceTokens[i], DestTokens[i]));
end;
(* NoEol *)
function NoEol: TParser<TTokenLinkedList>;
begin
Result := ManyTokens(FilterTokens(AllTokenTypes - [ttEol]));
end;
// Slash comments
function SlashComments: TParser<TTokenLinkedList>;
begin
Result := Seq(FilterToken(ttDoubleSlash), NoEol);
end;
// String
function StringParser: TParser<TTokenLinkedList>;
begin
Result := Seq(
Seq(FilterToken(ttSingleQuote), ManyTokens(FilterTokens(AllTokenTypes - [ttEol, ttSingleQuote]))),
FilterToken(ttSingleQuote)
);
end;
// Ansi Comments
function AnsiCommentsParser: TParser<TTokenLinkedList>;
begin
Result := Seq(
Seq(
FilterToken(ttAnsiCommentBegin),
ManyTokens(FilterTokens(AllTokenTypes - [ttAnsiCommentEnd]))
),
FilterToken(ttAnsiCommentEnd)
);
end;
// Borland Comments
function BorlandCommentsParser: TParser<TTokenLinkedList>;
begin
Result := Seq(
Seq(
FilterToken(ttBraceOpen),
ManyTokens(FilterTokens(AllTokenTypes - [ttBraceClose]))
),
FilterToken(ttBraceClose)
);
end;
function CreateParser: TParser<TFmt>;
begin
Result := TListToFmtMapper.Create(SlashComments, htComment)
.OrElse(TListToFmtMapper.Create(StringParser, htString))
.OrElse(TListToFmtMapper.Create(AnsiCommentsParser, htComment))
.OrElse(TListToFmtMapper.Create(BorlandCommentsParser, htComment))
.OrElse(SimpleParser);
end;
end.