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 ();
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 function expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
80 procedure expectTT (ttype
: Integer);
81 function eatTT (ttype
: Integer): Boolean;
83 function expectDelim (const ch
: AnsiChar): AnsiChar;
84 function eatDelim (const ch
: AnsiChar): Boolean;
87 property col
: Integer read mCol
;
88 property line
: Integer read mLine
;
90 property curChar
: AnsiChar read mCurChar
;
91 property nextChar
: AnsiChar read mNextChar
;
94 property tokCol
: Integer read mTokCol
;
95 property tokLine
: Integer read mTokLine
;
97 property tokType
: Integer read mTokType
; // see TTXXX constants
98 property tokStr
: AnsiString read mTokStr
; // string or identifier
99 property tokChar
: AnsiChar read mTokChar
; // for delimiters
100 property tokInt
: Integer read mTokInt
;
104 // ////////////////////////////////////////////////////////////////////////// //
106 TFileTextParser
= class(TTextParser
)
108 const BufSize
= 16384;
112 mStreamOwned
: Boolean;
118 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
121 constructor Create (const fname
: AnsiString);
122 constructor Create (st
: TStream
; astOwned
: Boolean=true); // will take ownership on st by default
123 destructor Destroy (); override;
126 TStrTextParser
= class(TTextParser
)
132 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
135 constructor Create (const astr
: AnsiString);
136 destructor Destroy (); override;
140 // ////////////////////////////////////////////////////////////////////////// //
147 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
150 constructor Create ();
152 procedure flush (); virtual;
154 procedure put (const s
: AnsiString); overload
;
155 procedure put (v
: Byte); overload
;
156 procedure put (v
: Integer); overload
;
157 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
158 procedure putIndent ();
160 procedure unindent ();
163 property curIndent
: Integer read mIndent
;
167 // ////////////////////////////////////////////////////////////////////////// //
169 TFileTextWriter
= class(TTextWriter
)
171 const BufSize
= 16384;
175 mStreamOwned
: Boolean;
180 procedure putBuf (constref buf
; len
: SizeUInt
); override;
183 constructor Create (const fname
: AnsiString);
184 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
185 destructor Destroy (); override;
187 procedure flush (); override;
190 TStrTextWriter
= class(TTextWriter
)
195 procedure putBuf (constref buf
; len
: SizeUInt
); override;
198 constructor Create ();
199 destructor Destroy (); override;
201 property str
: AnsiString read mStr
;
211 // ////////////////////////////////////////////////////////////////////////// //
212 function StrEqu (const a
, b
: AnsiString): Boolean; inline; begin result
:= (a
= b
); end;
215 // ////////////////////////////////////////////////////////////////////////// //
216 constructor TTextParser
.Create ();
226 mAllowSignedNumbers
:= true;
227 warmup(); // change `mAllowSignedNumbers` there, if necessary
232 destructor TTextParser
.Destroy ();
238 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
241 procedure TTextParser
.warmup ();
245 mCurChar
:= mNextChar
;
246 if (mNextChar
<> #0) then loadNextChar();
250 function TTextParser
.skipChar (): Boolean;
252 if (mCurChar
= #0) then begin result
:= false; exit
; end;
253 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
254 mCurChar
:= mNextChar
;
255 if (mCurChar
= #0) then begin result
:= false; exit
; end;
258 if (mCurChar
= #13) then
260 if (mNextChar
= #10) then loadNextChar();
267 function TTextParser
.skipBlanks (): Boolean;
273 if (curChar
= '/') then
275 // single-line comment
276 if (nextChar
= '/') then
278 while not isEOF
and (curChar
<> #10) do skipChar();
279 skipChar(); // skip EOL
283 if (nextChar
= '*') then
285 // skip comment start
290 if (curChar
= '*') and (nextChar
= '/') then
301 // nesting multline comment
302 if (nextChar
= '+') then
304 // skip comment start
310 if (curChar
= '+') and (nextChar
= '/') then
316 if (level
= 0) then break
;
319 if (curChar
= '/') and (nextChar
= '+') then
321 // skip comment start
332 if (curChar
> ' ') then break
;
333 skipChar(); // skip blank
339 function TTextParser
.skipToken (): Boolean;
341 procedure parseInt ();
343 neg
: Boolean = false;
347 if mAllowSignedNumbers
then
349 if (curChar
= '+') or (curChar
= '-') then
351 neg
:= (curChar
= '-');
353 if (curChar
< '0') or (curChar
> '9') then
356 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
361 if (curChar
= '0') then
377 if (base
< 0) then base
:= 10;
378 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
380 mTokInt
:= 0; // just in case
383 n
:= digitInBase(curChar
, base
);
384 if (n
< 0) then break
;
386 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
390 // check for valid number end
393 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
394 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
396 raise Exception
.Create('invalid number');
399 if neg
then mTokInt
:= -mTokInt
;
402 procedure parseString ();
408 mTokStr
:= ''; // just in case
410 skipChar(); // skip starting quote
414 if (qch
= '"') and (curChar
= '\') then
416 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
418 // skip backslash and escape type
427 'x', 'X': // hex escape
429 n
:= digitInBase(curChar
, 16);
430 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
432 if (digitInBase(curChar
, 16) > 0) then
434 n
:= n
*16+digitInBase(curChar
, 16);
437 mTokStr
+= AnsiChar(n
);
443 // duplicate single quote (pascal style)
444 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
452 if (curChar
= qch
) then
454 skipChar(); // skip ending quote
462 procedure parseId ();
465 mTokStr
:= ''; // just in case
466 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
467 ((curChar
>= 'A') and (curChar
<= 'Z')) or
468 ((curChar
>= 'a') and (curChar
<= 'z')) or
482 if not skipBlanks() then
496 if mAllowSignedNumbers
and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
497 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
500 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
503 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
507 ',': mTokType
:= TTComma
;
508 ':': mTokType
:= TTColon
;
509 ';': mTokType
:= TTSemi
;
510 '{': mTokType
:= TTBegin
;
511 '}': mTokType
:= TTEnd
;
512 else mTokType
:= TTDelim
;
519 function TTextParser
.expectId (): AnsiString;
521 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
527 procedure TTextParser
.expectId (const aid
: AnsiString);
529 if (mTokType
<> TTId
) or (not StrEqu(mTokStr
, aid
)) then raise Exception
.Create('identifier '''+aid
+''' expected');
534 function TTextParser
.eatId (const aid
: AnsiString): Boolean;
537 if (mTokType
<> TTId
) or (not StrEqu(mTokStr
, aid
)) then exit
;
543 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
545 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
546 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
552 function TTextParser
.expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
556 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
560 raise Exception
.Create('string or identifier expected');
567 function TTextParser
.expectInt (): Integer;
569 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
575 procedure TTextParser
.expectTT (ttype
: Integer);
577 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
582 function TTextParser
.eatTT (ttype
: Integer): Boolean;
584 result
:= (mTokType
= ttype
);
585 if result
then skipToken();
589 function TTextParser
.expectDelim (const ch
: AnsiChar): AnsiChar;
591 if (mTokType
<> TTDelim
) then raise Exception
.Create(Format('delimiter ''%s'' expected', [ch
]));
597 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
600 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then exit
;
606 // ////////////////////////////////////////////////////////////////////////// //
607 constructor TFileTextParser
.Create (const fname
: AnsiString);
610 mFile
:= openDiskFileRO(fname
);
611 mStreamOwned
:= true;
612 GetMem(mBuffer
, BufSize
);
614 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
615 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
620 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true);
622 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
624 mStreamOwned
:= astOwned
;
625 GetMem(mBuffer
, BufSize
);
627 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
628 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
633 destructor TFileTextParser
.Destroy ();
635 if (mBuffer
<> nil) then FreeMem(mBuffer
);
639 if mStreamOwned
then mFile
.Free();
645 procedure TFileTextParser
.loadNextChar ();
647 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
648 if (mBufPos
>= mBufLen
) then
650 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
651 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
652 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
655 assert(mBufPos
< mBufLen
);
656 mNextChar
:= mBuffer
[mBufPos
];
658 if (mNextChar
= #0) then mNextChar
:= ' ';
662 // ////////////////////////////////////////////////////////////////////////// //
663 constructor TStrTextParser
.Create (const astr
: AnsiString);
671 destructor TStrTextParser
.Destroy ();
678 procedure TStrTextParser
.loadNextChar ();
681 if (mPos
> Length(mStr
)) then exit
;
682 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
683 if (mNextChar
= #0) then mNextChar
:= ' ';
687 // ////////////////////////////////////////////////////////////////////////// //
688 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
689 procedure TTextWriter
.flush (); begin end;
690 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
691 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
692 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
693 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
694 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
695 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
696 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
699 // ////////////////////////////////////////////////////////////////////////// //
700 constructor TFileTextWriter
.Create (const fname
: AnsiString);
702 mFile
:= createDiskFile(fname
);
703 mStreamOwned
:= true;
705 GetMem(mBuffer
, BufSize
);
706 assert(mBuffer
<> nil);
711 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
713 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
715 mStreamOwned
:= astOwned
;
717 GetMem(mBuffer
, BufSize
);
718 assert(mBuffer
<> nil);
722 destructor TFileTextWriter
.Destroy ();
725 if (mBuffer
<> nil) then FreeMem(mBuffer
);
728 if (mStreamOwned
) then mFile
.Free();
734 procedure TFileTextWriter
.flush ();
736 if (mFile
<> nil) and (mBufUsed
> 0) then
738 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
744 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
749 if (len
= 0) then exit
;
753 left
:= BufSize
-mBufUsed
;
757 left
:= BufSize
-mBufUsed
;
760 if (left
> len
) then left
:= Integer(len
);
761 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);
769 // ////////////////////////////////////////////////////////////////////////// //
770 constructor TStrTextWriter
.Create ();
776 destructor TStrTextWriter
.Destroy ();
783 procedure TStrTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
789 SetLength(st
, Integer(len
));
790 Move(buf
, PChar(st
)^, Integer(len
));