1 | unit Parser;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TErrorEvent = procedure (Text: string; Pos: TPoint) of object;
|
---|
10 |
|
---|
11 | TTokenKind = (tkKeyword, tkString, tkNumber, tkSpecialSymbol, tkEof, tkEol,
|
---|
12 | tkIdentifier);
|
---|
13 |
|
---|
14 | TToken = record
|
---|
15 | Kind: TTokenKind;
|
---|
16 | Value: string;
|
---|
17 | Pos: TPoint;
|
---|
18 | end;
|
---|
19 |
|
---|
20 | { TParserPos }
|
---|
21 |
|
---|
22 | TParserPos = record
|
---|
23 | Index: Integer;
|
---|
24 | Pos: TPoint;
|
---|
25 | procedure Reset;
|
---|
26 | procedure NextChar;
|
---|
27 | procedure NextLine;
|
---|
28 | end;
|
---|
29 |
|
---|
30 | { TParser }
|
---|
31 |
|
---|
32 | TParser = class
|
---|
33 | private
|
---|
34 | FOnError: TErrorEvent;
|
---|
35 | public
|
---|
36 | Pos: TParserPos;
|
---|
37 | Source: string;
|
---|
38 | function IsDigit(Value: Char): Boolean;
|
---|
39 | function IsSpecialSymbol(C: Char): Boolean;
|
---|
40 | function IsAlpha(C: Char): Boolean;
|
---|
41 | function IsAlphaNumeric(C: Char): Boolean;
|
---|
42 | function IsWhiteSpace(C: Char): Boolean;
|
---|
43 | function ReadNext: TToken;
|
---|
44 | function CheckNext(Kind: TTokenKind; Value: string = ''): Boolean;
|
---|
45 | function CheckNextKind(Kind: TTokenKind): Boolean;
|
---|
46 | function CheckNextAndRead(Kind: TTokenKind; Value: string = ''): Boolean;
|
---|
47 | function Expect(Kind: TTokenKind; Value: string = ''): Boolean;
|
---|
48 | procedure Error(Text: string; Pos: TPoint);
|
---|
49 | procedure Reset;
|
---|
50 | constructor Create;
|
---|
51 | property OnError: TErrorEvent read FOnError write FOnError;
|
---|
52 | end;
|
---|
53 |
|
---|
54 |
|
---|
55 | implementation
|
---|
56 |
|
---|
57 | resourcestring
|
---|
58 | SUnknownCharacter = 'Unknown character %s';
|
---|
59 | SExpectedButFound = 'Expected %s but %s found.';
|
---|
60 |
|
---|
61 | { TParserPos }
|
---|
62 |
|
---|
63 | procedure TParserPos.Reset;
|
---|
64 | begin
|
---|
65 | Index := 1;
|
---|
66 | Pos := Point(1, 1);
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TParserPos.NextChar;
|
---|
70 | begin
|
---|
71 | Inc(Index);
|
---|
72 | Inc(Pos.X);
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TParserPos.NextLine;
|
---|
76 | begin
|
---|
77 | Inc(Index);
|
---|
78 | Pos.X := 1;
|
---|
79 | Inc(Pos.Y);
|
---|
80 | end;
|
---|
81 |
|
---|
82 | { TParser }
|
---|
83 |
|
---|
84 | function TParser.IsDigit(Value: Char): Boolean;
|
---|
85 | begin
|
---|
86 | Result := Value in ['0'..'9'];
|
---|
87 | end;
|
---|
88 |
|
---|
89 | function TParser.IsSpecialSymbol(C: Char): Boolean;
|
---|
90 | begin
|
---|
91 | Result := (C = ':') or (C = ',') or (C = '(') or (C = ')') or (C = '+');
|
---|
92 | end;
|
---|
93 |
|
---|
94 | function TParser.IsAlpha(C: Char): Boolean;
|
---|
95 | begin
|
---|
96 | Result := (C in ['a'..'z']) or (C in ['A'..'Z']);
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function TParser.IsAlphaNumeric(C: Char): Boolean;
|
---|
100 | begin
|
---|
101 | Result := IsAlpha(C) or IsDigit(C) or (C = '_');
|
---|
102 | end;
|
---|
103 |
|
---|
104 | function TParser.IsWhiteSpace(C: Char): Boolean;
|
---|
105 | begin
|
---|
106 | Result := (C = ' ') or (C = #9);
|
---|
107 | end;
|
---|
108 |
|
---|
109 | function TParser.ReadNext: TToken;
|
---|
110 | type
|
---|
111 | TParserState = (psNone, psNumber, psString, psComment, psIdentifier);
|
---|
112 | var
|
---|
113 | C: Char;
|
---|
114 | State: TParserState;
|
---|
115 | begin
|
---|
116 | State := psNone;
|
---|
117 | Result.Value := '';
|
---|
118 | while Pos.Index < Length(Source) do begin
|
---|
119 | C := Source[Pos.Index];
|
---|
120 | if State = psNone then begin
|
---|
121 | if IsWhiteSpace(C) then begin
|
---|
122 | end else
|
---|
123 | if C = ';' then begin
|
---|
124 | State := psComment;
|
---|
125 | end else
|
---|
126 | if IsAlpha(C) then begin
|
---|
127 | Result.Pos := Pos.Pos;
|
---|
128 | Result.Kind := tkIdentifier;
|
---|
129 | Result.Value := C;
|
---|
130 | State := psIdentifier;
|
---|
131 | end else
|
---|
132 | if IsDigit(C) then begin
|
---|
133 | Result.Pos := Pos.Pos;
|
---|
134 | Result.Kind := tkNumber;
|
---|
135 | Result.Value := C;
|
---|
136 | State := psNumber;
|
---|
137 | end else
|
---|
138 | if C = '''' then begin
|
---|
139 | Result.Pos := Pos.Pos;
|
---|
140 | Result.Kind := tkString;
|
---|
141 | State := psString;
|
---|
142 | end else
|
---|
143 | if C = #13 then begin
|
---|
144 | end else
|
---|
145 | if C = #10 then begin
|
---|
146 | Result.Pos := Pos.Pos;
|
---|
147 | Result.Kind := tkEol;
|
---|
148 | Result.Value := '';
|
---|
149 | Pos.NextLine;
|
---|
150 | Break;
|
---|
151 | end else
|
---|
152 | if IsSpecialSymbol(C) then begin
|
---|
153 | Result.Pos := Pos.Pos;
|
---|
154 | Result.Kind := tkSpecialSymbol;
|
---|
155 | Result.Value := C;
|
---|
156 | Pos.NextChar;
|
---|
157 | Break;
|
---|
158 | end else
|
---|
159 | Error(Format(SUnknownCharacter, [C]), Pos.Pos);
|
---|
160 | end else
|
---|
161 | if State = psIdentifier then begin
|
---|
162 | if IsAlphaNumeric(C) then begin
|
---|
163 | Result.Value := Result.Value + C;
|
---|
164 | end else begin
|
---|
165 | Break;
|
---|
166 | end;
|
---|
167 | end else
|
---|
168 | if State = psComment then begin
|
---|
169 | if (C = #10) or (C = #13) then begin
|
---|
170 | State := psNone;
|
---|
171 | Continue;
|
---|
172 | end;
|
---|
173 | end else
|
---|
174 | if State = psNumber then begin
|
---|
175 | if IsDigit(C) then Result.Value := Result.Value + C
|
---|
176 | else begin
|
---|
177 | Break;
|
---|
178 | end;
|
---|
179 | end else
|
---|
180 | if State = psString then begin
|
---|
181 | if C = '''' then begin
|
---|
182 | Pos.NextChar;
|
---|
183 | Break;
|
---|
184 | end else begin
|
---|
185 | Result.Value := Result.Value + C;
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 | Pos.NextChar;
|
---|
189 | end;
|
---|
190 | if (State = psNone) and (Pos.Index >= Length(Source)) then begin
|
---|
191 | Result.Kind := tkEof;
|
---|
192 | Result.Value := '';
|
---|
193 | Result.Pos := Pos.Pos;
|
---|
194 | end;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | function TParser.CheckNext(Kind: TTokenKind; Value: string = ''): Boolean;
|
---|
198 | var
|
---|
199 | LastPos: TParserPos;
|
---|
200 | Token: TToken;
|
---|
201 | begin
|
---|
202 | LastPos := Pos;
|
---|
203 | Token := ReadNext;
|
---|
204 | Result := (Token.Kind = Kind) and (LowerCase(Token.Value) = LowerCase(Value));
|
---|
205 | Pos := LastPos;
|
---|
206 | end;
|
---|
207 |
|
---|
208 | function TParser.CheckNextKind(Kind: TTokenKind): Boolean;
|
---|
209 | var
|
---|
210 | LastPos: TParserPos;
|
---|
211 | Token: TToken;
|
---|
212 | begin
|
---|
213 | LastPos := Pos;
|
---|
214 | Token := ReadNext;
|
---|
215 | Result := Token.Kind = Kind;
|
---|
216 | Pos := LastPos;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | function TParser.CheckNextAndRead(Kind: TTokenKind; Value: string): Boolean;
|
---|
220 | var
|
---|
221 | LastPos: TParserPos;
|
---|
222 | Token: TToken;
|
---|
223 | begin
|
---|
224 | LastPos := Pos;
|
---|
225 | Token := ReadNext;
|
---|
226 | Result := (Token.Kind = Kind) and (LowerCase(Token.Value) = LowerCase(Value));
|
---|
227 | if not Result then Pos := LastPos;
|
---|
228 | end;
|
---|
229 |
|
---|
230 | function TParser.Expect(Kind: TTokenKind; Value: string = ''): Boolean;
|
---|
231 | var
|
---|
232 | Token: TToken;
|
---|
233 | begin
|
---|
234 | Result := True;
|
---|
235 | Token := ReadNext;
|
---|
236 | if (Token.Kind <> Kind) or (LowerCase(Token.Value) <> LowerCase(Value)) then begin
|
---|
237 | Result := False;
|
---|
238 | Error(Format(SExpectedButFound, [Value, Token.Value]), Token.Pos);
|
---|
239 | end;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | procedure TParser.Error(Text: string; Pos: TPoint);
|
---|
243 | begin
|
---|
244 | if Assigned(FOnError) then
|
---|
245 | FOnError(Text, Pos);
|
---|
246 | end;
|
---|
247 |
|
---|
248 | procedure TParser.Reset;
|
---|
249 | begin
|
---|
250 | Pos.Reset;
|
---|
251 | end;
|
---|
252 |
|
---|
253 | constructor TParser.Create;
|
---|
254 | begin
|
---|
255 | Reset;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | end.
|
---|
259 |
|
---|