-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SiViC_ASM_Parser.pas
144 lines (122 loc) · 4.15 KB
/
SiViC_ASM_Parser.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
unit SiViC_ASM_Parser;
{$INCLUDE '.\SiViC_defs.inc'}
interface
uses
SiViC_ASM_Parser_Instr;
type
TSVCParserResultType = (prtNone,prtSys,prtConst,prtVar,prtLabel,prtData,prtInstr);
// event called on result
TSVCParserResultEvent = procedure(Sender: TObject; ResultType: TSVCParserResultType; Result: Pointer) of object;
TSVCParserStage = (psInitial,psMain_Sys,psMain_Const,psMain_Var,
psMain_Label,psMain_Data,psMain_Instr,psFinal);
TSVCParser = class(TSVCParser_Instr)
private
fParsingStage: TSVCParserStage;
fOnResult: TSVCParserResultEvent;
protected
procedure ParsingChangeRequest(RequestID: Integer); override;
procedure InitializeParsing; override;
procedure Parse_Stage_Initial; virtual;
procedure Parse_Stage_Final; virtual;
procedure PassResult; override;
public
Function Parse(const Line: String): Boolean; override;
published
property OnResult: TSVCParserResultEvent read fOnResult write fOnResult;
end;
implementation
uses
SysUtils,
SiViC_ASM_Lists,
SiViC_ASM_Lexer,
SiViC_ASM_Parser_Base,
SiViC_ASM_Parser_Label,
SiViC_ASM_Parser_Data;
procedure TSVCParser.ParsingChangeRequest(RequestID: Integer);
begin
case RequestID of
SVC_ASM_PARSER_PCR_ID_TODATA: fParsingStage := psMain_Data;
SVC_ASM_PARSER_PCR_ID_TOINSTRUCTION: fParsingStage := psMain_Instr;
end;
end;
//------------------------------------------------------------------------------
procedure TSVCParser.InitializeParsing;
begin
inherited;
fParsingStage := psInitial;
end;
//------------------------------------------------------------------------------
procedure TSVCParser.Parse_Stage_Initial;
begin
If fLexer[fTokenIndex].TokenType = lttIdentifier then
begin
If AnsiSameText(fLexer[fTokenIndex].Str,SVC_ASM_LISTS_RESERVEDWORDS[0]) then
fParsingStage := psMain_Sys
else If AnsiSameText(fLexer[fTokenIndex].Str,SVC_ASM_LISTS_RESERVEDWORDS[1]) then
fParsingStage := psMain_Const
else If AnsiSameText(fLexer[fTokenIndex].Str,SVC_ASM_LISTS_RESERVEDWORDS[2]) then
fParsingStage := psMain_Var
else If IsDataIntro(fLexer[fTokenIndex].Str) then
begin
fParsingStage := psMain_Data;
Dec(fTokenIndex);
end
else
begin // label or instruction
fParsingStage := psMain_Label;
Dec(fTokenIndex);
end;
end
else AddErrorMessage('Identifier expected but "%s" found',[fLexer[fTokenIndex].Str]);
end;
//------------------------------------------------------------------------------
procedure TSVCParser.Parse_Stage_Final;
begin
case fParsingStage of
psMain_Sys: Parse_Stage_Sys_Final;
psMain_Const: Parse_Stage_Const_Final;
psMain_Var: Parse_Stage_Var_Final;
psMain_Label: Parse_Stage_Label_Final;
psMain_Data: Parse_Stage_Data_Final;
psMain_Instr: Parse_Stage_Instr_Final;
end;
fParsingStage := psInitial;
end;
//------------------------------------------------------------------------------
procedure TSVCParser.PassResult;
begin
If Assigned(fOnResult) then
case fParsingStage of
psMain_Sys: fOnResult(Self,prtSys,Addr(fParsingResult_Sys));
psMain_Const: fOnResult(Self,prtConst,Addr(fParsingResult_Const));
psMain_Var: fOnResult(Self,prtVar,Addr(fParsingResult_Var));
psMain_Label: fOnResult(Self,prtLabel,Addr(fParsingResult_Label));
psMain_Data: fOnResult(Self,prtData,Addr(fParsingResult_Data));
psMain_Instr: fOnResult(Self,prtInstr,Addr(fParsingResult_Instr));
end;
end;
//------------------------------------------------------------------------------
Function TSVCParser.Parse(const Line: String): Boolean;
begin
Result := inherited Parse(Line);
try
while (fTokenIndex < fLexer.Count) do
begin
case fParsingStage of
psInitial: Parse_Stage_Initial;
psMain_Sys: Parse_Stage_Sys;
psMain_Const: Parse_Stage_Const;
psMain_Var: Parse_Stage_Var;
psMain_Label: Parse_Stage_Label;
psMain_Data: Parse_Stage_Data;
psMain_Instr: Parse_Stage_Instr;
end;
Inc(fTokenIndex);
end;
Parse_Stage_Final;
except
on ESVCParsingError do Result := False
else raise;
end;
end;
end.