-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SiViC_ASM_Parser_Base.pas
282 lines (232 loc) · 9.44 KB
/
SiViC_ASM_Parser_Base.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
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
unit SiViC_ASM_Parser_Base;
{$INCLUDE '.\SiViC_defs.inc'}
interface
uses
SysUtils,
SiViC_Common,
SiViC_ASM_Lists,
SiViC_ASM_Lexer;
const
SVC_ASM_PARSER_CHARS_IDENTIFIERSTART = ['a'..'z','A'..'Z','_','@'];
SVC_ASM_PARSER_CHAR_EQUALS = '=';
SVC_ASM_PARSER_CHAR_LBRACKET = '(';
SVC_ASM_PARSER_CHAR_RBRACKET = ')';
SVC_ASM_PARSER_CHAR_LABELSTART = '@';
type
TSVCParserMessageType = (pmtComment,pmtHint,pmtWarning,pmtError,pmtOther);
TSVCParserMessage = record
MessageType: TSVCParserMessageType;
Text: String;
Position: Integer;
end;
TSVCParserMessages = record
Arr: array of TSVCParserMessage;
Count: Integer;
end;
TSVCParserModifier = (pmodNone,pmodByte,pmodWord,pmodLong,pmodQuad,pmodPtr);
TSVCParserModifiers = set of TSVCParserModifier;
type
TSVCParserModifiersItem = record
Str: String;
Modifier: TSVCParserModifier;
end;
const
SVC_ASM_PARSER_MODIFIERS: array[0..16] of TSVCParserModifiersItem = (
(Str: 'byte'; Modifier: pmodByte),(Str: 'short'; Modifier: pmodByte),
(Str: 'uint8'; Modifier: pmodByte),(Str: 'int8'; Modifier: pmodByte),
(Str: 'word'; Modifier: pmodWord),(Str: 'small'; Modifier: pmodWord),
(Str: 'uint16'; Modifier: pmodWord),(Str: 'int16'; Modifier: pmodWord),
(Str: 'dword'; Modifier: pmodLong),(Str: 'long'; Modifier: pmodLong),
(Str: 'uint32'; Modifier: pmodLong),(Str: 'int32'; Modifier: pmodLong),
(Str: 'qword'; Modifier: pmodQuad),(Str: 'quad'; Modifier: pmodQuad),
(Str: 'uint64'; Modifier: pmodQuad),(Str: 'int64'; Modifier: pmodQuad),
(Str: 'ptr'; Modifier: pmodPtr));
type
ESVCParsingError = class(Exception);
TSVCParser_Base = class(TObject)
private
fOwnsLists: Boolean;
fMessages: TSVCParserMessages;
Function GetMessage(Index: Integer): TSVCParserMessage;
protected
fLists: TSVCListManager;
fLexer: TSVCLexer;
// parsing engine variables
fTokenIndex: Integer;
Function AddParsingMessage(MessageType: TSVCParserMessageType; const Text: String; Values: array of const; Position: Integer = -1): Integer; overload; virtual;
Function AddParsingMessage(MessageType: TSVCParserMessageType; const Text: String; Position: Integer = -1): Integer; overload; virtual;
Function AddErrorMessage(const Text: String; Values: array of const; Position: Integer = -1): Integer; overload; virtual;
Function AddErrorMessage(const Text: String; Position: Integer = -1): Integer; overload; virtual;
Function AddWarningMessage(const Text: String; Values: array of const; Position: Integer = -1): Integer; overload; virtual;
Function AddWarningMessage(const Text: String; Position: Integer = -1): Integer; overload; virtual;
procedure CheckConstRangeAndIssueWarning(Value: TSVCSQuad; ValueSize: TSVCValueSize); virtual;
procedure ParsingChangeRequest(RequestID: Integer); virtual; abstract;
procedure InitializeParsing; virtual;
procedure PassResult; virtual; abstract;
public
class Function ResolveModifier(const Str: String): TSVCParserModifier; virtual;
class Function IsValidIdentifier(const Identifier: String): Boolean; virtual;
class Function IsValidLabel(const Identifier: String): Boolean; virtual;
class Function CheckRange(Value: TSVCSQuad; ValueSize: TSVCValueSize): Boolean; virtual;
constructor Create(Lists: TSVCListManager = nil);
destructor Destroy; override;
procedure Initialize; virtual;
Function Parse(const Line: String): Boolean; virtual;
procedure Clear; virtual;
property Messages[Index: Integer]: TSVCParserMessage read GetMessage; default;
published
property Count: Integer read fMessages.Count;
end;
implementation
Function TSVCParser_Base.GetMessage(Index: Integer): TSVCParserMessage;
begin
If (Index >= Low(fMessages.Arr)) and (Index < fMessages.Count) then
Result := fMessages.Arr[Index]
else
raise Exception.CreateFmt('TSVCParser.GetMessage: Index (%d) out of bounds.',[Index]);
end;
//==============================================================================
Function TSVCParser_Base.AddParsingMessage(MessageType: TSVCParserMessageType; const Text: String; Values: array of const; Position: Integer = -1): Integer;
begin
If fMessages.Count >= Length(fMessages.Arr) then
SetLength(fMessages.Arr,Length(fMessages.Arr) + 8);
fMessages.Arr[fMessages.Count].MessageType := MessageType;
fMessages.Arr[fMessages.Count].Text := Format(Text,Values);
If Position < 0 then
begin
If fTokenIndex < fLexer.Count then
fMessages.Arr[fMessages.Count].Position := fLexer[fTokenIndex].Start
else
fMessages.Arr[fMessages.Count].Position := 0;
end
else fMessages.Arr[fMessages.Count].Position := Position;
Result := fMessages.Count;
Inc(fMessages.Count);
If MessageType = pmtError then
raise ESVCParsingError.Create('Parsing error');
end;
//------------------------------------------------------------------------------
Function TSVCParser_Base.AddParsingMessage(MessageType: TSVCParserMessageType; const Text: String; Position: Integer = -1): Integer;
begin
Result := AddParsingMessage(MessageType,text,[],Position);
end;
//------------------------------------------------------------------------------
Function TSVCParser_Base.AddErrorMessage(const Text: String; Values: array of const; Position: Integer = -1): Integer;
begin
Result := AddParsingMessage(pmtError,Text,Values,Position);
end;
//------------------------------------------------------------------------------
Function TSVCParser_Base.AddErrorMessage(const Text: String; Position: Integer = -1): Integer;
begin
Result := AddParsingMessage(pmtError,Text,[],Position);
end;
//------------------------------------------------------------------------------
Function TSVCParser_Base.AddWarningMessage(const Text: String; Values: array of const; Position: Integer = -1): Integer;
begin
Result := AddParsingMessage(pmtWarning,Text,Values,Position);
end;
//------------------------------------------------------------------------------
Function TSVCParser_Base.AddWarningMessage(const Text: String; Position: Integer = -1): Integer;
begin
Result := AddParsingMessage(pmtWarning,Text,[],Position);
end;
//------------------------------------------------------------------------------
procedure TSVCParser_Base.CheckConstRangeAndIssueWarning(Value: TSVCSQuad; ValueSize: TSVCValueSize);
begin
If not CheckRange(Value,ValueSize) then
AddWarningMessage('Constant out of allowed range');
end;
//------------------------------------------------------------------------------
procedure TSVCParser_Base.InitializeParsing;
begin
fTokenIndex := 0;
end;
//==============================================================================
class Function TSVCParser_Base.ResolveModifier(const Str: String): TSVCParserModifier;
var
i: Integer;
begin
Result := pmodNone;
For i := Low(SVC_ASM_PARSER_MODIFIERS) to High(SVC_ASM_PARSER_MODIFIERS) do
If AnsiSameText(Str,SVC_ASM_PARSER_MODIFIERS[i].Str) then
begin
Result := SVC_ASM_PARSER_MODIFIERS[i].Modifier;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
class Function TSVCParser_Base.IsValidIdentifier(const Identifier: String): Boolean;
begin
If Length(Identifier) > 0 then
begin
If Length(Identifier) = 1 then
Result := not SVC_CharInSet(Identifier[1],SVC_ASM_LEXER_CHARS_INVAL_1_IDENT)
else
Result := SVC_CharInSet(Identifier[1],SVC_ASM_PARSER_CHARS_IDENTIFIERSTART);
end
else Result := False;
end;
//------------------------------------------------------------------------------
class Function TSVCParser_Base.IsValidLabel(const Identifier: String): Boolean;
begin
If Length(Identifier) > 1 then
Result := Identifier[1] = SVC_ASM_PARSER_CHAR_LABELSTART
else
Result := False;
end;
//------------------------------------------------------------------------------
class Function TSVCParser_Base.CheckRange(Value: TSVCSQuad; ValueSize: TSVCValueSize): Boolean;
begin
case ValueSize of
vsByte: Result := ((Value <= High(TSVCUByte)) and (Value >= Low(TSVCSByte)));
vsWord: Result := ((Value <= High(TSVCUWord)) and (Value >= Low(TSVCSWord)));
vsLong: Result := ((Value <= High(TSVCULong)) and (Value >= Low(TSVCSLong)));
vsQuad: Result := True;
else
raise Exception.CreateFmt('TSVCParser_Base.CheckRange: Unknown value size (%d).',[Ord(ValueSize)]);
end;
end;
//------------------------------------------------------------------------------
constructor TSVCParser_Base.Create(Lists: TSVCListManager = nil);
begin
inherited Create;
fOwnsLists := not Assigned(Lists);
fLexer := TSVCLexer.Create;
fLexer.IncludeComments := False;
If not Assigned(Lists) then
begin
fLists := TSVCListManager.Create;
fLists.LoadFromResource(SVC_ASM_LISTS_DEFAULTRESOURCENAME);
end
else fLists := Lists;
Initialize;
end;
//------------------------------------------------------------------------------
destructor TSVCParser_Base.Destroy;
begin
If fOwnsLists then
fLists.Free;
fLexer.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TSVCParser_Base.Initialize;
begin
fLexer.Initialize;
fTokenIndex := 0;
Clear;
end;
//------------------------------------------------------------------------------
Function TSVCParser_Base.Parse(const Line: String): Boolean;
begin
Result := True;
Clear;
fLexer.Tokenize(Line);
InitializeParsing;
end;
//------------------------------------------------------------------------------
procedure TSVCParser_Base.Clear;
begin
fMessages.Count := 0;
end;
end.