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}
17 {.$DEFINE XPARSER_DEBUG}
23 SysUtils
, Classes
{$IFDEF USE_MEMPOOL}, mempool
{$ENDIF};
26 // ////////////////////////////////////////////////////////////////////////// //
30 TParserException
= class(Exception
)
32 tokLine
, tokCol
: Integer;
35 constructor Create (pr
: TTextParser
; const amsg
: AnsiString);
36 constructor CreateFmt (pr
: TTextParser
; const afmt
: AnsiString; const args
: array of const);
39 TTextParser
= class{$IFDEF USE_MEMPOOL}(TPoolObject
){$ENDIF}
46 //TTFloat = 3; // not yet
48 TTDelim
= 5; // one-char delimiters
53 TTGreatEqu
= 14; // >=
55 TTEqu
= 16; // == or <>
64 SignedNumbers
, // allow signed numbers; otherwise sign will be TTDelim
65 DollarIsId
, // allow dollar in identifiers; otherwise dollar will be TTDelim
66 DotIsId
, // allow dot in identifiers; otherwise dot will be TTDelim
67 PascalComments
// allow `{}` pascal comments
69 TOptions
= set of TOption
;
73 TAnsiCharSet
= set of AnsiChar;
77 mCurChar
, mNextChar
: AnsiChar;
81 mTokLine
, mTokCol
: Integer; // token start
83 mTokStr
: AnsiString; // string or identifier
84 mTokChar
: AnsiChar; // for delimiters
88 procedure warmup (); // called in constructor to warm up the system
89 procedure loadNextChar (); virtual; abstract; // loads next char into mNextChar; #0 means 'eof'
92 constructor Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
93 destructor Destroy (); override;
95 procedure error (const amsg
: AnsiString); noreturn
;
96 procedure errorfmt (const afmt
: AnsiString; const args
: array of const); noreturn
;
98 function isEOF (): Boolean; inline;
100 function skipChar (): Boolean; // returns `false` on eof
102 function skipBlanks (): Boolean; // ...and comments; returns `false` on eof
104 function skipToken (): Boolean; // returns `false` on eof
105 {$IFDEF XPARSER_DEBUG}
106 function skipToken1 (): Boolean;
109 function expectId (): AnsiString;
110 procedure expectId (const aid
: AnsiString; caseSens
: Boolean=true);
111 function eatId (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
112 function eatIdOrStr (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
114 function expectStr (allowEmpty
: Boolean=false): AnsiString;
115 function expectInt (): Integer;
117 function expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
119 procedure expectTT (ttype
: Integer);
120 function eatTT (ttype
: Integer): Boolean;
122 procedure expectDelim (const ch
: AnsiChar);
123 function expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
124 function eatDelim (const ch
: AnsiChar): Boolean;
126 function isDelim (const ch
: AnsiChar): Boolean; inline;
129 property options
: TOptions read mOptions write mOptions
;
132 property col
: Integer read mCol
;
133 property line
: Integer read mLine
;
135 property curChar
: AnsiChar read mCurChar
;
136 property nextChar
: AnsiChar read mNextChar
;
139 property tokCol
: Integer read mTokCol
;
140 property tokLine
: Integer read mTokLine
;
142 property tokType
: Integer read mTokType
; // see TTXXX constants
143 property tokStr
: AnsiString read mTokStr
; // string or identifier
144 property tokChar
: AnsiChar read mTokChar
; // for delimiters
145 property tokInt
: Integer read mTokInt
;
149 // ////////////////////////////////////////////////////////////////////////// //
151 TFileTextParser
= class(TTextParser
)
153 const BufSize
= 16384;
157 mStreamOwned
: Boolean;
163 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
166 constructor Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
167 constructor Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
168 destructor Destroy (); override;
171 TStrTextParser
= class(TTextParser
)
177 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
180 constructor Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
181 destructor Destroy (); override;
185 // ////////////////////////////////////////////////////////////////////////// //
192 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
195 constructor Create ();
197 procedure flush (); virtual;
199 procedure put (const s
: AnsiString); overload
;
200 procedure put (v
: Byte); overload
;
201 procedure put (v
: Integer); overload
;
202 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
203 procedure putIndent ();
205 procedure unindent ();
208 property curIndent
: Integer read mIndent
;
212 // ////////////////////////////////////////////////////////////////////////// //
214 TFileTextWriter
= class(TTextWriter
)
216 const BufSize
= 16384;
220 mStreamOwned
: Boolean;
225 procedure putBuf (constref buf
; len
: SizeUInt
); override;
228 constructor Create (const fname
: AnsiString);
229 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
230 destructor Destroy (); override;
232 procedure flush (); override;
235 TStrTextWriter
= class(TTextWriter
)
240 procedure putBuf (constref buf
; len
: SizeUInt
); override;
243 constructor Create ();
244 destructor Destroy (); override;
246 property str
: AnsiString read mStr
;
256 // ////////////////////////////////////////////////////////////////////////// //
257 constructor TParserException
.Create (pr
: TTextParser
; const amsg
: AnsiString);
259 if (pr
<> nil) then begin tokLine
:= pr
.tokLine
; tokCol
:= pr
.tokCol
; end;
260 inherited Create(amsg
);
263 constructor TParserException
.CreateFmt (pr
: TTextParser
; const afmt
: AnsiString; const args
: array of const);
265 if (pr
<> nil) then begin tokLine
:= pr
.tokLine
; tokCol
:= pr
.tokCol
; end;
266 inherited Create(formatstrf(afmt
, args
));
270 // ////////////////////////////////////////////////////////////////////////// //
271 constructor TTextParser
.Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
287 destructor TTextParser
.Destroy ();
293 procedure TTextParser
.error (const amsg
: AnsiString); noreturn
;
295 raise TParserException
.Create(self
, amsg
);
299 procedure TTextParser
.errorfmt (const afmt
: AnsiString; const args
: array of const); noreturn
;
301 raise TParserException
.CreateFmt(self
, afmt
, args
);
305 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
308 procedure TTextParser
.warmup ();
312 mCurChar
:= mNextChar
;
313 if (mNextChar
<> #0) then loadNextChar();
317 function TTextParser
.skipChar (): Boolean;
319 if (mCurChar
= #0) then begin result
:= false; exit
; end;
320 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
321 mCurChar
:= mNextChar
;
322 if (mCurChar
= #0) then begin result
:= false; exit
; end;
325 if (mCurChar
= #13) then
327 if (mNextChar
= #10) then loadNextChar();
334 function TTextParser
.skipBlanks (): Boolean;
340 if (curChar
= '/') then
342 // single-line comment
343 if (nextChar
= '/') then
345 while not isEOF
and (curChar
<> #10) do skipChar();
346 skipChar(); // skip EOL
350 if (nextChar
= '*') then
352 // skip comment start
357 if (curChar
= '*') and (nextChar
= '/') then
368 // nesting multline comment
369 if (nextChar
= '+') then
371 // skip comment start
377 if (curChar
= '+') and (nextChar
= '/') then
383 if (level
= 0) then break
;
386 if (curChar
= '/') and (nextChar
= '+') then
388 // skip comment start
399 else if (curChar
= '(') and (nextChar
= '*') then
401 // pascal comment; skip comment start
406 if (curChar
= '*') and (nextChar
= ')') then
417 else if (curChar
= '{') and (TOption
.PascalComments
in mOptions
) then
419 // pascal comment; skip comment start
423 if (curChar
= '}') then
433 if (curChar
> ' ') then break
;
434 skipChar(); // skip blank
440 {$IFDEF XPARSER_DEBUG}
441 function TTextParser
.skipToken (): Boolean;
443 writeln('getting token...');
444 result
:= skipToken1();
445 writeln(' got token: ', mTokType
, ' <', mTokStr
, '> : <', mTokChar
, '>');
448 function TTextParser
.skipToken1 (): Boolean;
450 function TTextParser
.skipToken (): Boolean;
452 procedure parseInt ();
454 neg
: Boolean = false;
458 if (TOption
.SignedNumbers
in mOptions
) then
460 if (curChar
= '+') or (curChar
= '-') then
462 neg
:= (curChar
= '-');
464 if (curChar
< '0') or (curChar
> '9') then
467 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
472 if (curChar
= '0') then
488 if (base
< 0) then base
:= 10;
489 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
491 mTokInt
:= 0; // just in case
494 n
:= digitInBase(curChar
, base
);
495 if (n
< 0) then break
;
497 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
501 // check for valid number end
504 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
505 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
507 raise Exception
.Create('invalid number');
510 if neg
then mTokInt
:= -mTokInt
;
513 procedure parseString ();
519 mTokStr
:= ''; // just in case
521 skipChar(); // skip starting quote
525 if (qch
= '"') and (curChar
= '\') then
527 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
529 // skip backslash and escape type
538 'x', 'X': // hex escape
540 n
:= digitInBase(curChar
, 16);
541 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
543 if (digitInBase(curChar
, 16) > 0) then
545 n
:= n
*16+digitInBase(curChar
, 16);
548 mTokStr
+= AnsiChar(n
);
554 // duplicate single quote (pascal style)
555 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
563 if (curChar
= qch
) then
565 skipChar(); // skip ending quote
573 procedure parseId ();
576 mTokStr
:= ''; // just in case
577 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
578 ((curChar
>= 'A') and (curChar
<= 'Z')) or
579 ((curChar
>= 'a') and (curChar
<= 'z')) or
581 ((TOption
.DollarIsId
in mOptions
) and (curChar
= '$')) or
582 ((TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.')) do
595 if not skipBlanks() then
609 if (TOption
.SignedNumbers
in mOptions
) and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
610 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
613 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
616 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
617 if (TOption
.DollarIsId
in mOptions
) and (curChar
= '$') then begin parseId(); exit
; end;
618 if (TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.') then begin parseId(); exit
; end;
624 if (curChar
= '=') then
627 '<': begin mTokType
:= TTLessEqu
; mTokStr
:= '<='; skipChar(); exit
; end;
628 '>': begin mTokType
:= TTGreatEqu
; mTokStr
:= '>='; skipChar(); exit
; end;
629 '!': begin mTokType
:= TTNotEqu
; mTokStr
:= '!='; skipChar(); exit
; end;
630 '=': begin mTokType
:= TTEqu
; mTokStr
:= '=='; skipChar(); exit
; end;
631 ':': begin mTokType
:= TTAss
; mTokStr
:= ':='; skipChar(); exit
; end;
634 else if (mTokChar
= curChar
) then
637 '<': begin mTokType
:= TTShl
; mTokStr
:= '<<'; skipChar(); exit
; end;
638 '>': begin mTokType
:= TTShr
; mTokStr
:= '>>'; skipChar(); exit
; end;
639 '&': begin mTokType
:= TTLogAnd
; mTokStr
:= '&&'; skipChar(); exit
; end;
640 '|': begin mTokType
:= TTLogOr
; mTokStr
:= '||'; skipChar(); exit
; end;
646 '<': if (curChar
= '>') then begin mTokType
:= TTNotEqu
; mTokStr
:= '<>'; skipChar(); exit
; end;
647 '.': if (curChar
= '.') then begin mTokType
:= TTDotDot
; mTokStr
:= '..'; skipChar(); exit
; end;
653 function TTextParser
.expectId (): AnsiString;
655 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
661 procedure TTextParser
.expectId (const aid
: AnsiString; caseSens
: Boolean=true);
665 if (mTokType
<> TTId
) or (mTokStr
<> aid
) then raise Exception
.Create('identifier '''+aid
+''' expected');
669 if (mTokType
<> TTId
) or (not strEquCI1251(mTokStr
, aid
)) then raise Exception
.Create('identifier '''+aid
+''' expected');
675 function TTextParser
.eatId (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
679 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
683 result
:= (mTokType
= TTId
) and strEquCI1251(mTokStr
, aid
);
685 if result
then skipToken();
689 function TTextParser
.eatIdOrStr (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
693 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
694 if not result
then result
:= (mTokType
= TTStr
) and (mTokStr
= aid
);
698 result
:= (mTokType
= TTId
) and strEquCI1251(mTokStr
, aid
);
699 if not result
then result
:= (mTokType
= TTStr
) and strEquCI1251(mTokStr
, aid
);
701 if result
then skipToken();
705 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
707 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
708 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
714 function TTextParser
.expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
718 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
722 raise Exception
.Create('string or identifier expected');
729 function TTextParser
.expectInt (): Integer;
731 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
737 procedure TTextParser
.expectTT (ttype
: Integer);
739 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
744 function TTextParser
.eatTT (ttype
: Integer): Boolean;
746 result
:= (mTokType
= ttype
);
747 if result
then skipToken();
751 procedure TTextParser
.expectDelim (const ch
: AnsiChar);
753 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then raise Exception
.CreateFmt('delimiter ''%s'' expected', [ch
]);
758 function TTextParser
.expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
760 if (mTokType
<> TTDelim
) then raise Exception
.Create('delimiter expected');
761 if not (mTokChar
in ch
) then raise Exception
.Create('delimiter expected');
767 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
769 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
770 if result
then skipToken();
774 function TTextParser
.isDelim (const ch
: AnsiChar): Boolean; inline;
776 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
780 // ////////////////////////////////////////////////////////////////////////// //
781 constructor TFileTextParser
.Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
784 mFile
:= openDiskFileRO(fname
);
785 mStreamOwned
:= true;
786 GetMem(mBuffer
, BufSize
);
788 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
789 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
790 inherited Create(aopts
);
794 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
796 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
798 mStreamOwned
:= astOwned
;
799 GetMem(mBuffer
, BufSize
);
801 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
802 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
803 inherited Create(aopts
);
807 destructor TFileTextParser
.Destroy ();
809 if (mBuffer
<> nil) then FreeMem(mBuffer
);
813 if mStreamOwned
then mFile
.Free();
819 procedure TFileTextParser
.loadNextChar ();
821 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
822 if (mBufPos
>= mBufLen
) then
824 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
825 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
826 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
829 assert(mBufPos
< mBufLen
);
830 mNextChar
:= mBuffer
[mBufPos
];
832 if (mNextChar
= #0) then mNextChar
:= ' ';
836 // ////////////////////////////////////////////////////////////////////////// //
837 constructor TStrTextParser
.Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
841 inherited Create(aopts
);
845 destructor TStrTextParser
.Destroy ();
852 procedure TStrTextParser
.loadNextChar ();
855 if (mPos
> Length(mStr
)) then exit
;
856 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
857 if (mNextChar
= #0) then mNextChar
:= ' ';
861 // ////////////////////////////////////////////////////////////////////////// //
862 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
863 procedure TTextWriter
.flush (); begin end;
864 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
865 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
866 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
867 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
868 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
869 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
870 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
873 // ////////////////////////////////////////////////////////////////////////// //
874 constructor TFileTextWriter
.Create (const fname
: AnsiString);
876 mFile
:= createDiskFile(fname
);
877 mStreamOwned
:= true;
879 GetMem(mBuffer
, BufSize
);
880 assert(mBuffer
<> nil);
885 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
887 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
889 mStreamOwned
:= astOwned
;
891 GetMem(mBuffer
, BufSize
);
892 assert(mBuffer
<> nil);
896 destructor TFileTextWriter
.Destroy ();
899 if (mBuffer
<> nil) then FreeMem(mBuffer
);
902 if (mStreamOwned
) then mFile
.Free();
908 procedure TFileTextWriter
.flush ();
910 if (mFile
<> nil) and (mBufUsed
> 0) then
912 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
918 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
923 if (len
= 0) then exit
;
927 left
:= BufSize
-mBufUsed
;
931 left
:= BufSize
-mBufUsed
;
934 if (left
> len
) then left
:= Integer(len
);
935 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);
943 // ////////////////////////////////////////////////////////////////////////// //
944 constructor TStrTextWriter
.Create ();
950 destructor TStrTextWriter
.Destroy ();
957 procedure TStrTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
963 SetLength(st
, Integer(len
));
964 Move(buf
, PChar(st
)^, Integer(len
));