X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2Fxparser.pas;h=eb56686ee1daa9c4c9aa83184e4cb8c5226fefad;hb=cde380b2a554f452f7c2e2c6694bc29ce597ece9;hp=3f1d1db5b462a6ed9fc69fa7ff8284029e2270b6;hpb=2b04301f4303668096c37c21e06af70930f55b7f;p=d2df-sdl.git diff --git a/src/shared/xparser.pas b/src/shared/xparser.pas index 3f1d1db..eb56686 100644 --- a/src/shared/xparser.pas +++ b/src/shared/xparser.pas @@ -19,12 +19,12 @@ unit xparser; interface uses - Classes; + Classes, mempool; // ////////////////////////////////////////////////////////////////////////// // type - TTextParser = class + TTextParser = class(TPoolObject) public const TTNone = -1; @@ -39,6 +39,13 @@ type TTBegin = 8; // left curly TTEnd = 9; // right curly TTDelim = 10; // other delimiters + // + TTLogAnd = 11; // && + TTLogOr = 12; // || + TTLessEqu = 13; // <= + TTGreatEqu = 14; // >= + TTNotEqu = 15; // != + TTEqu = 16; // == private mLine, mCol: Integer; @@ -67,6 +74,7 @@ type function skipBlanks (): Boolean; // ...and comments; returns `false` on eof function skipToken (): Boolean; // returns `false` on eof + //function skipToken1 (): Boolean; function expectId (): AnsiString; procedure expectId (const aid: AnsiString); @@ -75,12 +83,17 @@ type function expectStr (allowEmpty: Boolean=false): AnsiString; function expectInt (): Integer; + function expectStrOrId (allowEmpty: Boolean=false): AnsiString; + procedure expectTT (ttype: Integer); function eatTT (ttype: Integer): Boolean; function expectDelim (const ch: AnsiChar): AnsiChar; function eatDelim (const ch: AnsiChar): Boolean; + public + property allowSignedNumbers: Boolean read mAllowSignedNumbers write mAllowSignedNumbers; + public property col: Integer read mCol; property line: Integer read mLine; @@ -156,6 +169,9 @@ type procedure putIndent (); procedure indent (); procedure unindent (); + + public + property curIndent: Integer read mIndent; end; @@ -182,6 +198,20 @@ type procedure flush (); override; end; + TStrTextWriter = class(TTextWriter) + private + mStr: AnsiString; + + protected + procedure putBuf (constref buf; len: SizeUInt); override; + + public + constructor Create (); + destructor Destroy (); override; + + property str: AnsiString read mStr; + end; + implementation @@ -317,6 +347,16 @@ begin end; +{ +function TTextParser.skipToken (): Boolean; +begin + writeln('getting token...'); + result := skipToken1(); + writeln(' got token: ', mTokType, ' <', mTokStr, '> : <', mTokChar, '>'); +end; +} + + function TTextParser.skipToken (): Boolean; procedure parseInt (); @@ -484,16 +524,27 @@ begin if (curChar = '_') or ((curChar >= 'A') and (curChar <= 'Z')) or ((curChar >= 'a') and (curChar <= 'z')) or (curChar >= #128) then begin parseId(); exit; end; // known delimiters? - case curChar of + mTokChar := curChar; + mTokType := TTDelim; + skipChar(); + if (curChar = '=') then + begin + case mTokChar of + '<': begin mTokType := TTLessEqu; mTokStr := '<='; skipChar(); exit; end; + '>': begin mTokType := TTGreatEqu; mTokStr := '>='; skipChar(); exit; end; + '!': begin mTokType := TTNotEqu; mTokStr := '!='; skipChar(); exit; end; + '=': begin mTokType := TTEqu; mTokStr := '=='; skipChar(); exit; end; + end; + end; + case mTokChar of ',': mTokType := TTComma; ':': mTokType := TTColon; ';': mTokType := TTSemi; '{': mTokType := TTBegin; '}': mTokType := TTEnd; - else mTokType := TTDelim; + '&': if (curChar = '&') then begin mTokType := TTLogAnd; mTokStr := '&&'; skipChar(); exit; end; + '|': if (curChar = '|') then begin mTokType := TTLogOr; mTokStr := '||'; skipChar(); exit; end; end; - mTokChar := curChar; - skipChar(); end; @@ -530,6 +581,21 @@ begin end; +function TTextParser.expectStrOrId (allowEmpty: Boolean=false): AnsiString; +begin + case mTokType of + TTStr: + if (not allowEmpty) and (Length(mTokStr) = 0) then raise Exception.Create('non-empty string expected'); + TTId: + begin end; + else + raise Exception.Create('string or identifier expected'); + end; + result := mTokStr; + skipToken(); +end; + + function TTextParser.expectInt (): Integer; begin if (mTokType <> TTInt) then raise Exception.Create('string expected'); @@ -732,4 +798,32 @@ begin end; +// ////////////////////////////////////////////////////////////////////////// // +constructor TStrTextWriter.Create (); +begin + mStr := ''; +end; + + +destructor TStrTextWriter.Destroy (); +begin + mStr := ''; + inherited; +end; + + +procedure TStrTextWriter.putBuf (constref buf; len: SizeUInt); +var + st: AnsiString = ''; +begin + if (len > 0) then + begin + SetLength(st, Integer(len)); + Move(buf, PChar(st)^, Integer(len)); + mStr += st; + st := ''; + end; +end; + + end.