1 | unit UParser;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils;
|
---|
9 |
|
---|
10 | const
|
---|
11 | Instructions: array[0..40] of string = ('JB', 'JNB', 'NOP', 'LCALL', 'SJMP',
|
---|
12 | 'PUSH', 'POP', 'MOV', 'ADD', 'SUB', 'MUL', 'DIV', 'RET', 'RETI', 'MOVC',
|
---|
13 | 'CLR', 'SETB', 'CALL', 'INC', 'DEC', 'ORL', 'ANL', 'SUBB', 'XCH', 'CPL',
|
---|
14 | 'JC', 'JNC', 'JZ', 'JNZ', 'DJNZ', 'CJNE', 'DB', 'DBIT', 'DS', 'PUBLIC',
|
---|
15 | 'EXTRN', 'CSEG', 'RSEG', 'JMP', 'DW', 'SWAP');
|
---|
16 | type
|
---|
17 | TErrorEvent = procedure (Message: string) of object;
|
---|
18 |
|
---|
19 |
|
---|
20 | { TParser }
|
---|
21 |
|
---|
22 | TParser = class
|
---|
23 | private
|
---|
24 | FOnError: TErrorEvent;
|
---|
25 | public
|
---|
26 | procedure Error(Text: string); virtual;
|
---|
27 | function Expect(Text: string): Boolean; virtual;
|
---|
28 | function GetNext(ChangePos: Boolean = False): string; virtual;
|
---|
29 | function ReadNext: string; virtual;
|
---|
30 | property OnError: TErrorEvent read FOnError write FOnError;
|
---|
31 | end;
|
---|
32 |
|
---|
33 | { TParserASM51 }
|
---|
34 |
|
---|
35 | TParserASM51 = class(TParser)
|
---|
36 | public
|
---|
37 | Source: string;
|
---|
38 | Position: Integer;
|
---|
39 | function Expect(Text: string): Boolean; override;
|
---|
40 | function GetNext(ChangePos: Boolean = False): string; override;
|
---|
41 | function ReadNext: string; override;
|
---|
42 | function ReadChar: Char;
|
---|
43 | function NextChar: Char;
|
---|
44 | function ReadEol: string;
|
---|
45 | function IsAlphabetic(Character: char): boolean;
|
---|
46 | function IsAlphanumeric(Character: char): boolean;
|
---|
47 | function IsHex(Character: char): boolean;
|
---|
48 | function IsNumeric(Character: char): boolean;
|
---|
49 | function IsIdentificator(Text: string): boolean;
|
---|
50 | function IsKeyword(Text: string): Boolean;
|
---|
51 | function IsNumber(Text: string): Boolean;
|
---|
52 | function IsHexNumber(Text: string): Boolean;
|
---|
53 | function IsString(Text: string): Boolean;
|
---|
54 | function IsWhiteSpace(Character: char): boolean;
|
---|
55 | function Eof: Boolean;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | TParserMacro = class
|
---|
59 |
|
---|
60 | end;
|
---|
61 |
|
---|
62 |
|
---|
63 | implementation
|
---|
64 |
|
---|
65 | procedure TParser.Error(Text: string);
|
---|
66 | begin
|
---|
67 | if Assigned(FOnError) then FOnError(Text);
|
---|
68 | end;
|
---|
69 |
|
---|
70 | function TParser.Expect(Text: string): Boolean;
|
---|
71 | begin
|
---|
72 |
|
---|
73 | end;
|
---|
74 |
|
---|
75 | function TParser.GetNext(ChangePos: Boolean): string;
|
---|
76 | begin
|
---|
77 |
|
---|
78 | end;
|
---|
79 |
|
---|
80 | function TParser.ReadNext: string;
|
---|
81 | begin
|
---|
82 |
|
---|
83 | end;
|
---|
84 |
|
---|
85 | { TParserASM51 }
|
---|
86 |
|
---|
87 | function TParserASM51.Expect(Text: string): Boolean;
|
---|
88 | var
|
---|
89 | NextToken: string;
|
---|
90 | begin
|
---|
91 | NextToken := ReadNext;
|
---|
92 | Result := NextToken = Text;
|
---|
93 | if not Result then Error('Expected "' + Text + '" but "' + NextToken + '" found');
|
---|
94 | end;
|
---|
95 |
|
---|
96 | function TParserASM51.GetNext(ChangePos: Boolean = False): string;
|
---|
97 | var
|
---|
98 | OldPosition: Integer;
|
---|
99 | begin
|
---|
100 | OldPosition := Position;
|
---|
101 | while IsWhiteSpace(NextChar) do ReadChar; // Skip white spaces
|
---|
102 |
|
---|
103 | if IsAlphabetic(NextChar) or (NextChar = '_') then begin
|
---|
104 | Result := ReadChar;
|
---|
105 | while IsAlphanumeric(NextChar) or (NextChar = '_') do begin
|
---|
106 | Result := Result + ReadChar;
|
---|
107 | end;
|
---|
108 | end else
|
---|
109 | if IsHex(NextChar) then begin
|
---|
110 | Result := ReadChar;
|
---|
111 | while IsHex(NextChar) do begin
|
---|
112 | Result := Result + ReadChar;
|
---|
113 | end;
|
---|
114 | if NextChar = 'h' then Result := Result + ReadChar;
|
---|
115 | // else
|
---|
116 | end else Result := ReadChar;
|
---|
117 | if not ChangePos then Position := OldPosition;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | function TParserASM51.ReadNext: string;
|
---|
121 | begin
|
---|
122 | Result := GetNext(True);
|
---|
123 | end;
|
---|
124 |
|
---|
125 | function TParserASM51.ReadChar: Char;
|
---|
126 | begin
|
---|
127 | Result := NextChar;
|
---|
128 | Inc(Position);
|
---|
129 | end;
|
---|
130 |
|
---|
131 | function TParserASM51.NextChar: Char;
|
---|
132 | begin
|
---|
133 | Result := Source[Position];
|
---|
134 | end;
|
---|
135 |
|
---|
136 | function TParserASM51.ReadEol: string;
|
---|
137 | begin
|
---|
138 | Result := '';
|
---|
139 | while (NextChar <> #13) and (NextChar <> #10) and not Eof do begin
|
---|
140 | Result := Result + ReadChar;
|
---|
141 | end;
|
---|
142 | if NextChar = #13 then ReadChar;
|
---|
143 | if NextChar = #10 then ReadChar;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | function TParserASM51.IsAlphabetic(Character: char): boolean;
|
---|
147 | begin
|
---|
148 | Result := (Character in ['a'..'z']) or (Character in ['A'..'Z']);
|
---|
149 | end;
|
---|
150 |
|
---|
151 | function TParserASM51.IsAlphanumeric(Character: char): boolean;
|
---|
152 | begin
|
---|
153 | Result := IsAlphabetic(Character) or IsNumeric(Character);
|
---|
154 | end;
|
---|
155 |
|
---|
156 | function TParserASM51.IsNumeric(Character: char): boolean;
|
---|
157 | begin
|
---|
158 | Result := Character in ['0'..'9'];
|
---|
159 | end;
|
---|
160 |
|
---|
161 | function TParserASM51.IsHex(Character: char): boolean;
|
---|
162 | begin
|
---|
163 | Result := IsNumeric(Character) or (Character in ['A'..'F']) or
|
---|
164 | (Character in ['a'..'f']);
|
---|
165 | end;
|
---|
166 |
|
---|
167 | function TParserASM51.IsString(Text: string): Boolean;
|
---|
168 | begin
|
---|
169 | raise Exception.Create('Not implemented');
|
---|
170 | end;
|
---|
171 |
|
---|
172 | function TParserASM51.IsIdentificator(Text: string): boolean;
|
---|
173 | var
|
---|
174 | I: integer;
|
---|
175 | begin
|
---|
176 | Result := True;
|
---|
177 | if Length(Text) = 0 then
|
---|
178 | Result := False;
|
---|
179 | //if IsKeyWord(Text) then
|
---|
180 | // Result := False;
|
---|
181 | if Length(Text) > 0 then
|
---|
182 | if not (Text[1] in ['a'..'z', 'A'..'Z', '_']) then
|
---|
183 | Result := False;
|
---|
184 | for I := 2 to Length(Text) do
|
---|
185 | if not (Text[i] in ['a'..'z', 'A'..'Z', '0'..'9', '_']) then
|
---|
186 | Result := False;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | function TParserASM51.IsWhiteSpace(Character: char): boolean;
|
---|
190 | begin
|
---|
191 | Result := (Character <= ' ');
|
---|
192 | end;
|
---|
193 |
|
---|
194 | function TParserASM51.Eof: Boolean;
|
---|
195 | begin
|
---|
196 | Result := (Position > Length(Source)) or (Trim(Copy(Source, Position, Length(Source))) = '');
|
---|
197 | end;
|
---|
198 |
|
---|
199 | function TParserASM51.IsKeyword(Text: string): Boolean;
|
---|
200 | var
|
---|
201 | I: integer;
|
---|
202 | begin
|
---|
203 | Result := False;
|
---|
204 | for I := 0 to High(Instructions) do
|
---|
205 | if Instructions[I] = Text then
|
---|
206 | Result := True;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | function TParserASM51.IsNumber(Text: string): Boolean;
|
---|
210 | var
|
---|
211 | Num: Integer;
|
---|
212 | begin
|
---|
213 | Result := TryStrToInt(Text, Num);
|
---|
214 | end;
|
---|
215 |
|
---|
216 | function TParserASM51.IsHexNumber(Text: string): Boolean;
|
---|
217 | var
|
---|
218 | Num: Integer;
|
---|
219 | begin
|
---|
220 | Result := TryStrToInt('$' + Text, Num);
|
---|
221 | end;
|
---|
222 |
|
---|
223 |
|
---|
224 | end.
|
---|
225 |
|
---|