1 | unit UGrammer;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, fgl, UTokenizer;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TRuleType = (rtSequence, rtAlternative);
|
---|
12 | TRuleItemType = (itTerminal, itNonterminal);
|
---|
13 |
|
---|
14 | TGrammerRule = class;
|
---|
15 | TGrammer = class;
|
---|
16 |
|
---|
17 | THandleEvent = procedure (GrammerRule: TGrammerRule) of object;
|
---|
18 |
|
---|
19 | TGrammerItem = class
|
---|
20 | private
|
---|
21 | Parent: TGrammerRule;
|
---|
22 | public
|
---|
23 | TokenType: TTokenType;
|
---|
24 | Rule: TGrammerRule;
|
---|
25 | ItemType: TRuleItemType;
|
---|
26 | Optional: Boolean;
|
---|
27 | Repetition: Boolean;
|
---|
28 | Text: string;
|
---|
29 | end;
|
---|
30 |
|
---|
31 | TGrammerItemList = specialize TFPGObjectList<TGrammerItem>;
|
---|
32 |
|
---|
33 | { TGrammerRule }
|
---|
34 |
|
---|
35 | TGrammerRule = class
|
---|
36 | private
|
---|
37 | FOnHandle: THandleEvent;
|
---|
38 | Parent: TGrammer;
|
---|
39 | public
|
---|
40 | Name: string;
|
---|
41 | RuleType: TRuleType;
|
---|
42 | Items: TGrammerItemList;
|
---|
43 | procedure AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean);
|
---|
44 | procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean);
|
---|
45 | constructor Create;
|
---|
46 | destructor Destroy; override;
|
---|
47 | property OnHandle: THandleEvent read FOnHandle write FOnHandle;
|
---|
48 | end;
|
---|
49 |
|
---|
50 | TGrammerRuleList = specialize TFPGObjectList<TGrammerRule>;
|
---|
51 |
|
---|
52 | TGrammer = class
|
---|
53 | Rules: TGrammerRuleList;
|
---|
54 | TopRule: TGrammerRule;
|
---|
55 | constructor Create;
|
---|
56 | destructor Destroy; override;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | implementation
|
---|
60 |
|
---|
61 | procedure TGrammerRule.AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean
|
---|
62 | );
|
---|
63 | begin
|
---|
64 | with Items[Items.Add(TGrammerItem.Create)] do begin
|
---|
65 | ItemType := itTerminal;
|
---|
66 | TokenType := ATokenType;
|
---|
67 | Text := AText;
|
---|
68 | Optional := AOptional;
|
---|
69 | Repetition := ARepetition;
|
---|
70 | Parent := Self;
|
---|
71 | end;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TGrammerRule.AddRule(ARule: TGrammerRule; AOptional,
|
---|
75 | ARepetition: Boolean);
|
---|
76 | begin
|
---|
77 | with Items[Items.Add(TGrammerItem.Create)] do begin
|
---|
78 | ItemType := itNonterminal;
|
---|
79 | Rule := ARule;
|
---|
80 | Optional := AOptional;
|
---|
81 | Repetition := ARepetition;
|
---|
82 | Parent := Self;
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | constructor TGrammerRule.Create;
|
---|
87 | begin
|
---|
88 | Items := TGrammerItemList.Create;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | destructor TGrammerRule.Destroy;
|
---|
92 | begin
|
---|
93 | inherited Destroy;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | { TGrammer }
|
---|
97 |
|
---|
98 | constructor TGrammer.Create;
|
---|
99 | begin
|
---|
100 | Rules := TGrammerRuleList.Create;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | destructor TGrammer.Destroy;
|
---|
104 | begin
|
---|
105 | Rules.Destroy;
|
---|
106 | inherited Destroy;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | end.
|
---|
110 |
|
---|