1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
25 // ////////////////////////////////////////////////////////////////////////// //
34 //TTFloat = 3; // not yet
39 TTBegin
= 8; // left curly
40 TTEnd
= 9; // right curly
41 TTDelim
= 10; // other delimiters
45 mCurChar
, mNextChar
: AnsiChar;
47 mAllowSignedNumbers
: Boolean; // internal control
49 mTokLine
, mTokCol
: Integer; // token start
51 mTokStr
: AnsiString; // string or identifier
52 mTokChar
: AnsiChar; // for delimiters
56 procedure warmup (); virtual; // called in constructor to warm up the system
57 procedure loadNextChar (); virtual; abstract; // loads next char into mNextChar; #0 means 'eof'
60 constructor Create (loadToken
: Boolean=true);
61 destructor Destroy (); override;
63 function isEOF (): Boolean; inline;
65 function skipChar (): Boolean; // returns `false` on eof
67 function skipBlanks (): Boolean; // ...and comments; returns `false` on eof
69 function skipToken (): Boolean; // returns `false` on eof
71 function expectId (): AnsiString;
72 procedure expectId (const aid
: AnsiString);
73 function eatId (const aid
: AnsiString): Boolean;
75 function expectStr (allowEmpty
: Boolean=false): AnsiString;
76 function expectInt (): Integer;
78 procedure expectTT (ttype
: Integer);
79 function eatTT (ttype
: Integer): Boolean;
81 function expectDelim (const ch
: AnsiChar): AnsiChar;
82 function eatDelim (const ch
: AnsiChar): Boolean;
85 property col
: Integer read mCol
;
86 property line
: Integer read mLine
;
88 property curChar
: AnsiChar read mCurChar
;
89 property nextChar
: AnsiChar read mNextChar
;
92 property tokCol
: Integer read mTokCol
;
93 property tokLine
: Integer read mTokLine
;
95 property tokType
: Integer read mTokType
; // see TTXXX constants
96 property tokStr
: AnsiString read mTokStr
; // string or identifier
97 property tokChar
: AnsiChar read mTokChar
; // for delimiters
98 property tokInt
: Integer read mTokInt
;
102 // ////////////////////////////////////////////////////////////////////////// //
104 TFileTextParser
= class(TTextParser
)
109 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
112 constructor Create (const fname
: AnsiString; loadToken
: Boolean=true);
113 destructor Destroy (); override;
116 TStrTextParser
= class(TTextParser
)
122 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
125 constructor Create (const astr
: AnsiString; loadToken
: Boolean=true);
126 destructor Destroy (); override;
130 // ////////////////////////////////////////////////////////////////////////// //
137 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
140 constructor Create ();
142 procedure put (const s
: AnsiString); overload
;
143 procedure put (v
: Byte); overload
;
144 procedure put (v
: Integer); overload
;
145 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
146 procedure putIndent ();
148 procedure unindent ();
152 // ////////////////////////////////////////////////////////////////////////// //
154 TFileTextWriter
= class(TTextWriter
)
159 procedure putBuf (constref buf
; len
: SizeUInt
); override;
162 constructor Create (const fname
: AnsiString);
163 destructor Destroy (); override;
174 wc2shitmap
: array[0..65535] of AnsiChar;
175 wc2shitmapInited
: Boolean = false;
178 // ////////////////////////////////////////////////////////////////////////// //
179 procedure initShitMap ();
181 cp1251
: array[0..127] of Word = (
182 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
183 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
184 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
185 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
186 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
187 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
188 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
189 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
194 for f
:= 0 to High(wc2shitmap
) do wc2shitmap
[f
] := '?';
195 for f
:= 0 to 127 do wc2shitmap
[f
] := AnsiChar(f
);
196 for f
:= 0 to 127 do wc2shitmap
[cp1251
[f
]] := AnsiChar(f
+128);
197 wc2shitmapInited
:= true;
201 // ////////////////////////////////////////////////////////////////////////// //
202 // TODO: make a hash or something
203 function wcharTo1251 (wc
: WideChar): AnsiChar; inline;
205 if not wc2shitmapInited
then initShitMap();
206 if (LongWord(wc
) > 65535) then result
:= '?' else result
:= wc2shitmap
[LongWord(wc
)];
210 // ////////////////////////////////////////////////////////////////////////// //
211 constructor TTextParser
.Create (loadToken
: Boolean=true);
221 mAllowSignedNumbers
:= true;
222 warmup(); // change `mAllowSignedNumbers` there, if necessary
223 if loadToken
then skipToken();
227 destructor TTextParser
.Destroy ();
233 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
236 procedure TTextParser
.warmup ();
240 mCurChar
:= mNextChar
;
241 if (mNextChar
<> #0) then loadNextChar();
245 function TTextParser
.skipChar (): Boolean;
247 if (mCurChar
= #0) then begin result
:= false; exit
; end;
248 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
249 mCurChar
:= mNextChar
;
250 if (mCurChar
= #0) then begin result
:= false; exit
; end;
253 if (mCurChar
= #13) then
255 if (mNextChar
= #10) then loadNextChar();
262 function TTextParser
.skipBlanks (): Boolean;
268 if (curChar
= '/') then
270 // single-line comment
271 if (nextChar
= '/') then
273 while not isEOF
and (curChar
<> #10) do skipChar();
274 skipChar(); // skip EOL
278 if (nextChar
= '*') then
280 // skip comment start
285 if (curChar
= '*') and (nextChar
= '/') then
296 // nesting multline comment
297 if (nextChar
= '+') then
299 // skip comment start
305 if (curChar
= '+') and (nextChar
= '/') then
311 if (level
= 0) then break
;
314 if (curChar
= '/') and (nextChar
= '+') then
316 // skip comment start
327 if (curChar
> ' ') then break
;
328 skipChar(); // skip blank
334 function TTextParser
.skipToken (): Boolean;
336 procedure parseInt ();
338 neg
: Boolean = false;
342 if mAllowSignedNumbers
then
344 if (curChar
= '+') or (curChar
= '-') then
346 neg
:= (curChar
= '-');
348 if (curChar
< '0') or (curChar
> '9') then
351 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
356 if (curChar
= '0') then
372 if (base
< 0) then base
:= 10;
373 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
375 mTokInt
:= 0; // just in case
378 n
:= digitInBase(curChar
, base
);
379 if (n
< 0) then break
;
381 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
385 // check for valid number end
388 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
389 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
391 raise Exception
.Create('invalid number');
394 if neg
then mTokInt
:= -mTokInt
;
397 procedure parseString ();
403 mTokStr
:= ''; // just in case
405 skipChar(); // skip starting quote
409 if (qch
= '"') and (curChar
= '\') then
411 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
413 // skip backslash and escape type
422 'x', 'X': // hex escape
424 n
:= digitInBase(curChar
, 16);
425 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
427 if (digitInBase(curChar
, 16) > 0) then
429 n
:= n
*16+digitInBase(curChar
, 16);
432 mTokStr
+= AnsiChar(n
);
438 // duplicate single quote (pascal style)
439 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
447 if (curChar
= qch
) then
449 skipChar(); // skip ending quote
457 procedure parseId ();
460 mTokStr
:= ''; // just in case
461 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
462 ((curChar
>= 'A') and (curChar
<= 'Z')) or
463 ((curChar
>= 'a') and (curChar
<= 'z')) or
477 if not skipBlanks() then
491 if mAllowSignedNumbers
and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
492 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
495 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
498 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
502 ',': mTokType
:= TTComma
;
503 ':': mTokType
:= TTColon
;
504 ';': mTokType
:= TTSemi
;
505 '{': mTokType
:= TTBegin
;
506 '}': mTokType
:= TTEnd
;
507 else mTokType
:= TTDelim
;
514 function TTextParser
.expectId (): AnsiString;
516 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
522 procedure TTextParser
.expectId (const aid
: AnsiString);
524 if (mTokType
<> TTId
) or (CompareText(mTokStr
, aid
) <> 0) then raise Exception
.Create('identifier '''+aid
+''' expected');
529 function TTextParser
.eatId (const aid
: AnsiString): Boolean;
532 if (mTokType
<> TTId
) or (CompareText(mTokStr
, aid
) <> 0) then exit
;
538 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
540 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
541 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
547 function TTextParser
.expectInt (): Integer;
549 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
555 procedure TTextParser
.expectTT (ttype
: Integer);
557 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
562 function TTextParser
.eatTT (ttype
: Integer): Boolean;
564 result
:= (mTokType
= ttype
);
565 if result
then skipToken();
569 function TTextParser
.expectDelim (const ch
: AnsiChar): AnsiChar;
571 if (mTokType
<> TTDelim
) then raise Exception
.Create(Format('delimiter ''%s'' expected', [ch
]));
577 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
580 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then exit
;
586 // ////////////////////////////////////////////////////////////////////////// //
587 constructor TFileTextParser
.Create (const fname
: AnsiString; loadToken
: Boolean=true);
589 mFile
:= openDiskFileRO(fname
);
590 inherited Create(loadToken
);
594 destructor TFileTextParser
.Destroy ();
601 procedure TFileTextParser
.loadNextChar ();
605 rd
:= mFile
.Read(mNextChar
, 1);
606 if (rd
= 0) then begin mNextChar
:= #0; exit
; end;
607 if (mNextChar
= #0) then mNextChar
:= ' ';
611 // ////////////////////////////////////////////////////////////////////////// //
612 constructor TStrTextParser
.Create (const astr
: AnsiString; loadToken
: Boolean=true);
616 inherited Create(loadToken
);
620 destructor TStrTextParser
.Destroy ();
627 procedure TStrTextParser
.loadNextChar ();
630 if (mPos
> Length(mStr
)) then exit
;
631 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
632 if (mNextChar
= #0) then mNextChar
:= ' ';
636 // ////////////////////////////////////////////////////////////////////////// //
637 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
638 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
639 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
640 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
641 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
642 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
643 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
644 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
647 // ////////////////////////////////////////////////////////////////////////// //
648 constructor TFileTextWriter
.Create (const fname
: AnsiString);
650 mFile
:= createDiskFile(fname
);
655 destructor TFileTextWriter
.Destroy ();
662 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
669 mFile
.WriteBuffer(pc
^, len
);