-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Language.Parser.Twee.pas
139 lines (111 loc) · 2.79 KB
/
GS.Language.Parser.Twee.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
//Twee3 parser. (twine.org)
unit GS.Language.Parser.Twee;
{$IFDEF FPC}
{$mode delphi}{$H+}
{$ENDIF FPC}
interface
uses Classes, SysUtils, ContNrs,
GS.Language.Tokenizer.Twee,
GS.Language.Syntax.Twee,
GS.Language.Semantic.Twee,
GS.Language.Compiler;
type
TGSCRootTwee = class(TGSCRoot)
public
Tokenizer : TTweeTokenizer; //simple tokenizer is enough.
SyntaxCheck : TTweeSyntaxCheck;
SemanticProcessor : TTweeSemanticProcessor;
Constructor Create; override;
Destructor Destroy; override;
end;
TTweeParserStat = record
FirstPassTokenizerSymbolCount : Uint32;
//TODO : Time metric
SecondPassTokenizerSymbolCount : Uint32;
//...
procedure reset;
end;
//Group : Tokenizer, syntax checker, and semantic checker.
TTweeParser = class
private
fRoot : TGSCRootTwee;
fstats : TTweeParserStat;
function GetLogs: TStringList;
function getTokenizer: TTweeTokenizer;
protected
public
constructor Create; virtual;
destructor Destroy; override;
function Parse(const code : string) : boolean;
property logs : TStringList read GetLogs;
property stats : TTweeParserStat read fstats;
property Tokernizer : TTweeTokenizer read getTokenizer;
property Root : TGSCRootTwee read fRoot;
end;
implementation
{ TTweeParser }
constructor TTweeParser.Create;
begin
inherited;
froot := TGSCRootTwee.Create;
fstats.reset;
end;
destructor TTweeParser.Destroy;
begin
FreeAndNil(fRoot);
inherited;
end;
function TTweeParser.GetLogs: TStringList;
begin
result := froot.logs;
end;
function TTweeParser.getTokenizer: TTweeTokenizer;
begin
result := fRoot.Tokenizer;
end;
function TTweeParser.Parse(const code: string): boolean;
begin
result := true;
FRoot.Tokenizer.Tokenize(code);
fstats.FirstPassTokenizerSymbolCount := fRoot.Tokenizer.tokenCount;
fstats.SecondPassTokenizerSymbolCount := fRoot.Tokenizer.TweeTokenCount;
if FRoot.SyntaxCheck.Check then
begin
if FRoot.SemanticProcessor.Process then
begin
FRoot.SemanticProcessor.log('Semantic check success.');
end
else
begin
result := false;
FRoot.SemanticProcessor.log('Semantic check unsuccess.');
end;
end
else
begin
result := false;
FRoot.log('Syntax check unsuccess.');
end;
end;
{ TGSCRootTwee }
constructor TGSCRootTwee.Create;
begin
inherited;
Tokenizer := TTweeTokenizer.Create;
SyntaxCheck := TTweeSyntaxCheck.Create(Self, Tokenizer);
SemanticProcessor := TTweeSemanticProcessor.Create(Self);
end;
destructor TGSCRootTwee.Destroy;
begin
FreeAndNil(Tokenizer);
FreeAndNil(SyntaxCheck);
FreeAndNil(SemanticProcessor);
inherited;
end;
{ TTweeParserStat }
procedure TTweeParserStat.reset;
begin
FirstPassTokenizerSymbolCount := 0;
SecondPassTokenizerSymbolCount := 0;
end;
end.