1 (* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 {$INCLUDE a_modes.inc}
18 {.$DEFINE XPARSER_DEBUG}
24 SysUtils
, Classes
{$IFDEF USE_MEMPOOL}, mempool
{$ENDIF};
27 // ////////////////////////////////////////////////////////////////////////// //
31 TParserException
= class(Exception
)
33 tokLine
, tokCol
: Integer;
36 constructor Create (pr
: TTextParser
; const amsg
: AnsiString);
37 constructor CreateFmt (pr
: TTextParser
; const afmt
: AnsiString; const args
: array of const);
40 TTextParser
= class{$IFDEF USE_MEMPOOL}(TPoolObject
){$ENDIF}
47 //TTFloat = 3; // not yet
49 TTDelim
= 5; // one-char delimiters
54 TTGreatEqu
= 14; // >=
56 TTEqu
= 16; // == or <>
65 SignedNumbers
, // allow signed numbers; otherwise sign will be TTDelim
66 DollarIsId
, // allow dollar in identifiers; otherwise dollar will be TTDelim
67 DotIsId
, // allow dot in identifiers; otherwise dot will be TTDelim
68 PascalComments
// allow `{}` pascal comments
70 TOptions
= set of TOption
;
74 TAnsiCharSet
= set of AnsiChar;
78 mCurChar
, mNextChar
: AnsiChar;
82 mTokLine
, mTokCol
: Integer; // token start
84 mTokStr
: AnsiString; // string or identifier
85 mTokChar
: AnsiChar; // for delimiters
89 procedure warmup (); // called in constructor to warm up the system
90 procedure loadNextChar (); virtual; abstract; // loads next char into mNextChar; #0 means 'eof'
93 constructor Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
94 destructor Destroy (); override;
96 procedure error (const amsg
: AnsiString); noreturn
;
97 procedure errorfmt (const afmt
: AnsiString; const args
: array of const); noreturn
;
99 function isEOF (): Boolean; inline;
101 function skipChar (): Boolean; // returns `false` on eof
103 function skipBlanks (): Boolean; // ...and comments; returns `false` on eof
105 function skipToken (): Boolean; // returns `false` on eof
106 {$IFDEF XPARSER_DEBUG}
107 function skipToken1 (): Boolean;
110 function isIdOrStr (): Boolean; inline;
112 function expectId (): AnsiString;
113 procedure expectId (const aid
: AnsiString; caseSens
: Boolean=true);
114 function eatId (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
115 function eatIdOrStr (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
116 function eatIdOrStrCI (const aid
: AnsiString): Boolean; inline;
118 function expectStr (allowEmpty
: Boolean=false): AnsiString;
119 function expectInt (): Integer;
121 function expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
123 procedure expectTT (ttype
: Integer);
124 function eatTT (ttype
: Integer): Boolean;
126 procedure expectDelim (const ch
: AnsiChar);
127 function expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
128 function eatDelim (const ch
: AnsiChar): Boolean;
130 function isDelim (const ch
: AnsiChar): Boolean; inline;
133 property options
: TOptions read mOptions write mOptions
;
136 property col
: Integer read mCol
;
137 property line
: Integer read mLine
;
139 property curChar
: AnsiChar read mCurChar
;
140 property nextChar
: AnsiChar read mNextChar
;
143 property tokCol
: Integer read mTokCol
;
144 property tokLine
: Integer read mTokLine
;
146 property tokType
: Integer read mTokType
; // see TTXXX constants
147 property tokStr
: AnsiString read mTokStr
; // string or identifier
148 property tokChar
: AnsiChar read mTokChar
; // for delimiters
149 property tokInt
: Integer read mTokInt
;
153 // ////////////////////////////////////////////////////////////////////////// //
155 TFileTextParser
= class(TTextParser
)
157 const BufSize
= 16384;
161 mStreamOwned
: Boolean;
167 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
170 constructor Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
171 constructor Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
172 destructor Destroy (); override;
175 TStrTextParser
= class(TTextParser
)
181 procedure loadNextChar (); override; // loads next char into mNextChar; #0 means 'eof'
184 constructor Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
185 destructor Destroy (); override;
189 // ////////////////////////////////////////////////////////////////////////// //
196 procedure putBuf (constref buf
; len
: SizeUInt
); virtual; abstract;
199 constructor Create ();
201 procedure flush (); virtual;
203 procedure put (const s
: AnsiString); overload
;
204 procedure put (v
: Byte); overload
;
205 procedure put (v
: Integer); overload
;
206 procedure put (const fmt
: AnsiString; args
: array of const); overload
;
207 procedure putIndent ();
209 procedure unindent ();
212 property curIndent
: Integer read mIndent
;
216 // ////////////////////////////////////////////////////////////////////////// //
218 TFileTextWriter
= class(TTextWriter
)
220 const BufSize
= 16384;
224 mStreamOwned
: Boolean;
229 procedure putBuf (constref buf
; len
: SizeUInt
); override;
232 constructor Create (const fname
: AnsiString);
233 constructor Create (ast
: TStream
; astOwned
: Boolean=true); // will own the stream by default
234 destructor Destroy (); override;
236 procedure flush (); override;
239 TStrTextWriter
= class(TTextWriter
)
244 procedure putBuf (constref buf
; len
: SizeUInt
); override;
247 constructor Create ();
248 destructor Destroy (); override;
250 property str
: AnsiString read mStr
;
260 // ////////////////////////////////////////////////////////////////////////// //
261 constructor TParserException
.Create (pr
: TTextParser
; const amsg
: AnsiString);
263 if (pr
<> nil) then begin tokLine
:= pr
.tokLine
; tokCol
:= pr
.tokCol
; end;
264 inherited Create(amsg
);
267 constructor TParserException
.CreateFmt (pr
: TTextParser
; const afmt
: AnsiString; const args
: array of const);
269 if (pr
<> nil) then begin tokLine
:= pr
.tokLine
; tokCol
:= pr
.tokCol
; end;
270 inherited Create(formatstrf(afmt
, args
));
274 // ////////////////////////////////////////////////////////////////////////// //
275 constructor TTextParser
.Create (aopts
: TOptions
=[TOption
.SignedNumbers
]);
291 destructor TTextParser
.Destroy ();
297 procedure TTextParser
.error (const amsg
: AnsiString); noreturn
;
299 raise TParserException
.Create(self
, amsg
);
303 procedure TTextParser
.errorfmt (const afmt
: AnsiString; const args
: array of const); noreturn
;
305 raise TParserException
.CreateFmt(self
, afmt
, args
);
309 function TTextParser
.isEOF (): Boolean; inline; begin result
:= (mCurChar
= #0); end;
312 procedure TTextParser
.warmup ();
316 mCurChar
:= mNextChar
;
317 if (mNextChar
<> #0) then loadNextChar();
321 function TTextParser
.skipChar (): Boolean;
323 if (mCurChar
= #0) then begin result
:= false; exit
; end;
324 if (mCurChar
= #10) then begin mCol
:= 1; Inc(mLine
); end else Inc(mCol
);
325 mCurChar
:= mNextChar
;
326 if (mCurChar
= #0) then begin result
:= false; exit
; end;
329 if (mCurChar
= #13) then
331 if (mNextChar
= #10) then loadNextChar();
338 function TTextParser
.skipBlanks (): Boolean;
344 if (curChar
= '/') then
346 // single-line comment
347 if (nextChar
= '/') then
349 while not isEOF
and (curChar
<> #10) do skipChar();
350 skipChar(); // skip EOL
354 if (nextChar
= '*') then
356 // skip comment start
361 if (curChar
= '*') and (nextChar
= '/') then
372 // nesting multline comment
373 if (nextChar
= '+') then
375 // skip comment start
381 if (curChar
= '+') and (nextChar
= '/') then
387 if (level
= 0) then break
;
390 if (curChar
= '/') and (nextChar
= '+') then
392 // skip comment start
403 else if (curChar
= '(') and (nextChar
= '*') then
405 // pascal comment; skip comment start
410 if (curChar
= '*') and (nextChar
= ')') then
421 else if (curChar
= '{') and (TOption
.PascalComments
in mOptions
) then
423 // pascal comment; skip comment start
427 if (curChar
= '}') then
437 if (curChar
> ' ') then break
;
438 skipChar(); // skip blank
444 {$IFDEF XPARSER_DEBUG}
445 function TTextParser
.skipToken (): Boolean;
447 writeln('getting token...');
448 result
:= skipToken1();
449 writeln(' got token: ', mTokType
, ' <', mTokStr
, '> : <', mTokChar
, '>');
452 function TTextParser
.skipToken1 (): Boolean;
454 function TTextParser
.skipToken (): Boolean;
456 procedure parseInt ();
458 neg
: Boolean = false;
462 if (TOption
.SignedNumbers
in mOptions
) then
464 if (curChar
= '+') or (curChar
= '-') then
466 neg
:= (curChar
= '-');
468 if (curChar
< '0') or (curChar
> '9') then
471 if (neg
) then mTokChar
:= '-' else mTokChar
:= '+';
476 if (curChar
= '0') then
492 if (base
< 0) then base
:= 10;
493 if (digitInBase(curChar
, base
) < 0) then raise Exception
.Create('invalid number');
495 mTokInt
:= 0; // just in case
498 n
:= digitInBase(curChar
, base
);
499 if (n
< 0) then break
;
501 if (n
< 0) or (n
< mTokInt
) then raise Exception
.Create('integer overflow');
505 // check for valid number end
508 if (curChar
= '.') then raise Exception
.Create('floating numbers aren''t supported yet');
509 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then
511 raise Exception
.Create('invalid number');
514 if neg
then mTokInt
:= -mTokInt
;
517 procedure parseString ();
523 mTokStr
:= ''; // just in case
525 skipChar(); // skip starting quote
529 if (qch
= '"') and (curChar
= '\') then
531 if (nextChar
= #0) then raise Exception
.Create('unterminated string escape');
533 // skip backslash and escape type
542 'x', 'X': // hex escape
544 n
:= digitInBase(curChar
, 16);
545 if (n
< 0) then raise Exception
.Create('invalid hexstr escape');
547 if (digitInBase(curChar
, 16) > 0) then
549 n
:= n
*16+digitInBase(curChar
, 16);
552 mTokStr
+= AnsiChar(n
);
558 // duplicate single quote (pascal style)
559 if (qch
= '''') and (curChar
= '''') and (nextChar
= '''') then
567 if (curChar
= qch
) then
569 skipChar(); // skip ending quote
577 procedure parseId ();
580 mTokStr
:= ''; // just in case
581 while (curChar
= '_') or ((curChar
>= '0') and (curChar
<= '9')) or
582 ((curChar
>= 'A') and (curChar
<= 'Z')) or
583 ((curChar
>= 'a') and (curChar
<= 'z')) or
585 ((TOption
.DollarIsId
in mOptions
) and (curChar
= '$')) or
586 ((TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.')) do
599 if not skipBlanks() then
613 if (TOption
.SignedNumbers
in mOptions
) and ((curChar
= '+') or (curChar
= '-')) then begin parseInt(); exit
; end;
614 if (curChar
>= '0') and (curChar
<= '9') then begin parseInt(); exit
; end;
617 if (curChar
= '"') or (curChar
= '''') then begin parseString(); exit
; end;
620 if (curChar
= '_') or ((curChar
>= 'A') and (curChar
<= 'Z')) or ((curChar
>= 'a') and (curChar
<= 'z')) or (curChar
>= #128) then begin parseId(); exit
; end;
621 if (TOption
.DollarIsId
in mOptions
) and (curChar
= '$') then begin parseId(); exit
; end;
622 if (TOption
.DotIsId
in mOptions
) and (curChar
= '.') and (nextChar
<> '.') then begin parseId(); exit
; end;
628 if (curChar
= '=') then
631 '<': begin mTokType
:= TTLessEqu
; mTokStr
:= '<='; skipChar(); exit
; end;
632 '>': begin mTokType
:= TTGreatEqu
; mTokStr
:= '>='; skipChar(); exit
; end;
633 '!': begin mTokType
:= TTNotEqu
; mTokStr
:= '!='; skipChar(); exit
; end;
634 '=': begin mTokType
:= TTEqu
; mTokStr
:= '=='; skipChar(); exit
; end;
635 ':': begin mTokType
:= TTAss
; mTokStr
:= ':='; skipChar(); exit
; end;
638 else if (mTokChar
= curChar
) then
641 '<': begin mTokType
:= TTShl
; mTokStr
:= '<<'; skipChar(); exit
; end;
642 '>': begin mTokType
:= TTShr
; mTokStr
:= '>>'; skipChar(); exit
; end;
643 '&': begin mTokType
:= TTLogAnd
; mTokStr
:= '&&'; skipChar(); exit
; end;
644 '|': begin mTokType
:= TTLogOr
; mTokStr
:= '||'; skipChar(); exit
; end;
650 '<': if (curChar
= '>') then begin mTokType
:= TTNotEqu
; mTokStr
:= '<>'; skipChar(); exit
; end;
651 '.': if (curChar
= '.') then begin mTokType
:= TTDotDot
; mTokStr
:= '..'; skipChar(); exit
; end;
657 function TTextParser
.isIdOrStr (): Boolean; inline;
659 result
:= (mTokType
= TTId
) or (mTokType
= TTStr
);
663 function TTextParser
.expectId (): AnsiString;
665 if (mTokType
<> TTId
) then raise Exception
.Create('identifier expected');
671 procedure TTextParser
.expectId (const aid
: AnsiString; caseSens
: Boolean=true);
675 if (mTokType
<> TTId
) or (mTokStr
<> aid
) then raise Exception
.Create('identifier '''+aid
+''' expected');
679 if (mTokType
<> TTId
) or (not strEquCI1251(mTokStr
, aid
)) then raise Exception
.Create('identifier '''+aid
+''' expected');
685 function TTextParser
.eatId (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
689 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
693 result
:= (mTokType
= TTId
) and strEquCI1251(mTokStr
, aid
);
695 if result
then skipToken();
699 function TTextParser
.eatIdOrStr (const aid
: AnsiString; caseSens
: Boolean=true): Boolean;
703 result
:= (mTokType
= TTId
) and (mTokStr
= aid
);
704 if not result
then result
:= (mTokType
= TTStr
) and (mTokStr
= aid
);
708 result
:= (mTokType
= TTId
) and strEquCI1251(mTokStr
, aid
);
709 if not result
then result
:= (mTokType
= TTStr
) and strEquCI1251(mTokStr
, aid
);
711 if result
then skipToken();
715 function TTextParser
.eatIdOrStrCI (const aid
: AnsiString): Boolean; inline;
717 result
:= eatIdOrStr(aid
, false);
721 function TTextParser
.expectStr (allowEmpty
: Boolean=false): AnsiString;
723 if (mTokType
<> TTStr
) then raise Exception
.Create('string expected');
724 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
730 function TTextParser
.expectStrOrId (allowEmpty
: Boolean=false): AnsiString;
734 if (not allowEmpty
) and (Length(mTokStr
) = 0) then raise Exception
.Create('non-empty string expected');
738 raise Exception
.Create('string or identifier expected');
745 function TTextParser
.expectInt (): Integer;
747 if (mTokType
<> TTInt
) then raise Exception
.Create('string expected');
753 procedure TTextParser
.expectTT (ttype
: Integer);
755 if (mTokType
<> ttype
) then raise Exception
.Create('unexpected token');
760 function TTextParser
.eatTT (ttype
: Integer): Boolean;
762 result
:= (mTokType
= ttype
);
763 if result
then skipToken();
767 procedure TTextParser
.expectDelim (const ch
: AnsiChar);
769 if (mTokType
<> TTDelim
) or (mTokChar
<> ch
) then raise Exception
.CreateFmt('delimiter ''%s'' expected', [ch
]);
774 function TTextParser
.expectDelims (const ch
: TAnsiCharSet
): AnsiChar;
776 if (mTokType
<> TTDelim
) then raise Exception
.Create('delimiter expected');
777 if not (mTokChar
in ch
) then raise Exception
.Create('delimiter expected');
783 function TTextParser
.eatDelim (const ch
: AnsiChar): Boolean;
785 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
786 if result
then skipToken();
790 function TTextParser
.isDelim (const ch
: AnsiChar): Boolean; inline;
792 result
:= (mTokType
= TTDelim
) and (mTokChar
= ch
);
796 // ////////////////////////////////////////////////////////////////////////// //
797 constructor TFileTextParser
.Create (const fname
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
800 mFile
:= openDiskFileRO(fname
);
801 mStreamOwned
:= true;
802 GetMem(mBuffer
, BufSize
);
804 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
805 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
806 inherited Create(aopts
);
810 constructor TFileTextParser
.Create (st
: TStream
; astOwned
: Boolean=true; aopts
: TOptions
=[TOption
.SignedNumbers
]);
812 if (st
= nil) then raise Exception
.Create('cannot create parser for nil stream');
814 mStreamOwned
:= astOwned
;
815 GetMem(mBuffer
, BufSize
);
817 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
818 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
819 inherited Create(aopts
);
823 destructor TFileTextParser
.Destroy ();
825 if (mBuffer
<> nil) then FreeMem(mBuffer
);
829 if mStreamOwned
then mFile
.Free();
835 procedure TFileTextParser
.loadNextChar ();
837 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
838 if (mBufPos
>= mBufLen
) then
840 mBufLen
:= mFile
.Read(mBuffer
^, BufSize
);
841 if (mBufLen
< 0) then raise Exception
.Create('TFileTextParser: read error');
842 if (mBufLen
= 0) then begin mNextChar
:= #0; exit
; end;
845 assert(mBufPos
< mBufLen
);
846 mNextChar
:= mBuffer
[mBufPos
];
848 if (mNextChar
= #0) then mNextChar
:= ' ';
852 // ////////////////////////////////////////////////////////////////////////// //
853 constructor TStrTextParser
.Create (const astr
: AnsiString; aopts
: TOptions
=[TOption
.SignedNumbers
]);
857 inherited Create(aopts
);
861 destructor TStrTextParser
.Destroy ();
868 procedure TStrTextParser
.loadNextChar ();
871 if (mPos
> Length(mStr
)) then exit
;
872 mNextChar
:= mStr
[mPos
]; Inc(mPos
);
873 if (mNextChar
= #0) then mNextChar
:= ' ';
877 // ////////////////////////////////////////////////////////////////////////// //
878 constructor TTextWriter
.Create (); begin mIndent
:= 0; end;
879 procedure TTextWriter
.flush (); begin end;
880 procedure TTextWriter
.put (const s
: AnsiString); overload
; begin if (Length(s
) > 0) then putBuf((@(s
[1]))^, Length(s
)); end;
881 procedure TTextWriter
.put (v
: Byte); overload
; begin put('%d', [v
]); end;
882 procedure TTextWriter
.put (v
: Integer); overload
; begin put('%d', [v
]); end;
883 procedure TTextWriter
.put (const fmt
: AnsiString; args
: array of const); overload
; begin put(formatstrf(fmt
, args
)); end;
884 procedure TTextWriter
.putIndent (); var f
: Integer; begin for f
:= 1 to mIndent
do put(' '); end;
885 procedure TTextWriter
.indent (); begin Inc(mIndent
, 2); end;
886 procedure TTextWriter
.unindent (); begin Dec(mIndent
, 2); end;
889 // ////////////////////////////////////////////////////////////////////////// //
890 constructor TFileTextWriter
.Create (const fname
: AnsiString);
892 mFile
:= createDiskFile(fname
);
893 mStreamOwned
:= true;
895 GetMem(mBuffer
, BufSize
);
896 assert(mBuffer
<> nil);
901 constructor TFileTextWriter
.Create (ast
: TStream
; astOwned
: Boolean=true);
903 if (ast
= nil) then raise Exception
.Create('cannot write to nil stream');
905 mStreamOwned
:= astOwned
;
907 GetMem(mBuffer
, BufSize
);
908 assert(mBuffer
<> nil);
912 destructor TFileTextWriter
.Destroy ();
915 if (mBuffer
<> nil) then FreeMem(mBuffer
);
918 if (mStreamOwned
) then mFile
.Free();
924 procedure TFileTextWriter
.flush ();
926 if (mFile
<> nil) and (mBufUsed
> 0) then
928 mFile
.WriteBuffer(mBuffer
^, mBufUsed
);
934 procedure TFileTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
939 if (len
= 0) then exit
;
943 left
:= BufSize
-mBufUsed
;
947 left
:= BufSize
-mBufUsed
;
950 if (left
> len
) then left
:= Integer(len
);
951 Move(pc
^, (mBuffer
+mBufUsed
)^, left
);
959 // ////////////////////////////////////////////////////////////////////////// //
960 constructor TStrTextWriter
.Create ();
966 destructor TStrTextWriter
.Destroy ();
973 procedure TStrTextWriter
.putBuf (constref buf
; len
: SizeUInt
);
979 SetLength(st
, Integer(len
));
980 Move(buf
, PChar(st
)^, Integer(len
));