From 258d544fac1d3a4e9bf5f2f87fe9c9a9e2bfa2a7 Mon Sep 17 00:00:00 2001 From: cxl Date: Fri, 4 Nov 2011 11:55:08 +0000 Subject: [PATCH] Core: CParser improved const-correctness (many methods are now const) git-svn-id: svn://ultimatepp.org/upp/trunk@4124 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Parser.h | 28 +++++------ uppsrc/Core/parser.cpp | 8 +-- uppsrc/Core/src.tpp/CParser$en-us.tpp | 71 ++++++++++++++------------- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/uppsrc/Core/Parser.h b/uppsrc/Core/Parser.h index 2b40fbe3a..ce95e1f43 100644 --- a/uppsrc/Core/Parser.h +++ b/uppsrc/Core/Parser.h @@ -15,7 +15,7 @@ protected: bool skipspaces; bool Spaces0(); - const char *IsId0(const char *s); + const char *IsId0(const char *s) const; bool Id0(const char *id); void DoSpaces() { if(skipspaces) Spaces(); } @@ -29,12 +29,12 @@ public: void SkipSpaces() { skipspaces = true; } bool Spaces() { return (byte)*term <= ' ' || *term == '/' ? Spaces0() : false; } - char PeekChar() { return *term; } + char PeekChar() const { return *term; } char GetChar(); - bool IsChar(char c) { return *term == c; } - bool IsChar2(char c1, char c2) { return term[0] == c1 && term[1] == c2; } - bool IsChar3(char c1, char c2, char c3) { return term[0] == c1 && term[1] == c2 && term[2] == c3; } + bool IsChar(char c) const { return *term == c; } + bool IsChar2(char c1, char c2) const { return term[0] == c1 && term[1] == c2; } + bool IsChar3(char c1, char c2, char c3) const { return term[0] == c1 && term[1] == c2 && term[2] == c3; } bool Char(char c); bool Char2(char c1, char c2); bool Char3(char c1, char c2, char c3); @@ -43,20 +43,20 @@ public: void PassChar3(char c1, char c2, char c3) throw(Error); bool Id(const char *s) { return term[0] == s[0] && (s[1] == 0 || term[1] == s[1]) && Id0(s); } void PassId(const char *s) throw(Error); - bool IsId() { return iscib(*term); } - bool IsId(const char *s) { return term[0] == s[0] && (s[1] == 0 || term[1] == s[1]) && IsId0(s); } + bool IsId() const { return iscib(*term); } + bool IsId(const char *s) const { return term[0] == s[0] && (s[1] == 0 || term[1] == s[1]) && IsId0(s); } String ReadId() throw(Error); String ReadIdt() throw(Error); - bool IsInt(); + bool IsInt() const; int ReadInt() throw(Error); int ReadInt(int min, int max) throw(Error); - bool IsNumber() { return IsDigit(*term); } - bool IsNumber(int base); + bool IsNumber() const { return IsDigit(*term); } + bool IsNumber(int base) const; uint32 ReadNumber(int base = 10) throw(Error); uint64 ReadNumber64(int base = 10) throw(Error); - bool IsDouble() { return IsInt(); } + bool IsDouble() const { return IsInt(); } double ReadDouble() throw(Error); - bool IsString() { return IsChar('\"'); }; + bool IsString() const { return IsChar('\"'); }; String ReadOneString(bool chkend = false) throw(Error); String ReadString(bool chkend = false) throw(Error); String ReadOneString(int delim, bool chkend = false) throw(Error); @@ -75,9 +75,9 @@ public: Pos(const char *ptr = NULL, int line = 1, String fn = Null) : ptr(ptr), line(line), fn(fn) {} }; - const char *GetPtr() { return (const char *)term; } + const char *GetPtr() const { return (const char *)term; } - Pos GetPos(); + Pos GetPos() const; void SetPos(const Pos& pos); bool IsEof() const { return *term == '\0'; } diff --git a/uppsrc/Core/parser.cpp b/uppsrc/Core/parser.cpp index 170a22c86..7f883bd41 100644 --- a/uppsrc/Core/parser.cpp +++ b/uppsrc/Core/parser.cpp @@ -48,7 +48,7 @@ bool CParser::Spaces0() { return true; } -const char *CParser::IsId0(const char *s) { +const char *CParser::IsId0(const char *s) const { const char *t = term + 1; s++; while(*s) { @@ -123,7 +123,7 @@ String CParser::ReadIdt() throw(Error) { return result; } -bool CParser::IsInt() { +bool CParser::IsInt() const { LTIMING("IsInt"); const char *t = term; if(*t == '-' || *t == '+') { @@ -162,7 +162,7 @@ int CParser::ReadInt(int min, int max) throw(Error) return n; } -bool CParser::IsNumber(int base) +bool CParser::IsNumber(int base) const { if(IsDigit(*term)) return true; @@ -354,7 +354,7 @@ void CParser::SkipTerm() DoSpaces(); } -CParser::Pos CParser::GetPos() +CParser::Pos CParser::GetPos() const { Pos p; p.line = line; diff --git a/uppsrc/Core/src.tpp/CParser$en-us.tpp b/uppsrc/Core/src.tpp/CParser$en-us.tpp index bb0cf57c9..5cef62705 100644 --- a/uppsrc/Core/src.tpp/CParser$en-us.tpp +++ b/uppsrc/Core/src.tpp/CParser$en-us.tpp @@ -76,7 +76,7 @@ etPos], then the skip is performed after any symbol.&] to skip, [* false] otherwise.&] [s3; &] [s4; &] -[s5;:CParser`:`:PeekChar`(`): [@(0.0.255) char]_[* PeekChar]()&] +[s5;:CParser`:`:PeekChar`(`)const: [@(0.0.255) char]_[* PeekChar]()_[@(0.0.255) const]&] [s2;%% Returns the current single character.&] [s3; &] [s4; &] @@ -86,19 +86,20 @@ returns the character at the position before advancing.&] [s7;%% [*/ Return value]-|Character at position before advancing it.&] [s3; &] [s4; &] -[s5;:CParser`:`:IsChar`(char`): [@(0.0.255) bool]_[* IsChar]([@(0.0.255) char]_[*@3 c])&] +[s5;:CParser`:`:IsChar`(char`)const: [@(0.0.255) bool]_[* IsChar]([@(0.0.255) char]_[*@3 c])_ +[@(0.0.255) const]&] [s2;%% Tests whether there is a specific character [%-*@3 c ]at the current position.&] -[s3; &] +[s3;%% &] [s4; &] -[s5;:CParser`:`:IsChar2`(char`,char`): [@(0.0.255) bool]_[* IsChar2]([@(0.0.255) char]_[*@3 c -1], [@(0.0.255) char]_[*@3 c2])&] +[s5;:CParser`:`:IsChar2`(char`,char`)const: [@(0.0.255) bool]_[* IsChar2]([@(0.0.255) char]_ +[*@3 c1], [@(0.0.255) char]_[*@3 c2])_[@(0.0.255) const]&] [s2;%% Tests whether there is a specific character pair ([%-*@3 c1], [%-*@3 c2]) at the current position.&] -[s3; &] +[s3;%% &] [s4; &] -[s5;:CParser`:`:IsChar3`(char`,char`,char`): [@(0.0.255) bool]_[* IsChar3]([@(0.0.255) char -]_[*@3 c1], [@(0.0.255) char]_[*@3 c2], [@(0.0.255) char]_[*@3 c3])&] +[s5;:CParser`:`:IsChar3`(char`,char`,char`)const: [@(0.0.255) bool]_[* IsChar3]([@(0.0.255) c +har]_[*@3 c1], [@(0.0.255) char]_[*@3 c2], [@(0.0.255) char]_[*@3 c3])_[@(0.0.255) const]&] [s2;%% Test for a specific character triplet ([%-*@3 c1], [%-*@3 c2], [%-*@3 c3]) at the current position.&] [s3; &] @@ -168,13 +169,13 @@ d] method with [%-*@3 s] as parameter. If it returns [* false], throws ].&] [s3; &] [s4; &] -[s5;:CParser`:`:IsId`(`)const: [@(0.0.255) bool]_[* IsId]()_const&] +[s5;:CParser`:`:IsId`(`)const: [@(0.0.255) bool]_[* IsId]()_[@(0.0.255) const]&] [s2;%% Tests whether there is any C`-like identifier at the current position.&] [s3; &] [s4; &] -[s5;:CParser`:`:IsId`(const char`*`): [@(0.0.255) bool]_[* IsId]([@(0.0.255) const]_[@(0.0.255) c -har]_`*[*@3 s])_const&] +[s5;:CParser`:`:IsId`(const char`*`)const: [@(0.0.255) bool]_[* IsId]([@(0.0.255) const]_[@(0.0.255) c +har]_`*[*@3 s])_[@(0.0.255) const]&] [s2;%% Tests whether there is C`-like identifier [%-*@3 s] at current position.&] [s3;%% &] @@ -195,11 +196,10 @@ or template based type.&] [s7;%% [*/ Return value]-|Identifier.&] [s3; &] [s4; &] -[s5;:CParser`:`:IsInt`(`): [@(0.0.255) bool]_[* IsInt]()&] +[s5;:CParser`:`:IsInt`(`)const: [@(0.0.255) bool]_[* IsInt]()_[@(0.0.255) const]&] [s2;%% Test for integer at current position `- there either must be digit, or `'`+`' or `'`-`' sign followed by any number of spaces and digit.&] -[s7;%% [*/ Return value]-|true if there is integer.&] [s3; &] [s4; &] [s5;:CParser`:`:ReadInt`(`)throw`(CParser`:`:Error`): [@(0.0.255) int]_[* ReadInt]()_[@(0.0.255) t @@ -216,20 +216,17 @@ false, throws an &] Parser`::Error], otherwise returns it.&] [s3;%% &] [s4; &] -[s5;:CParser`:`:IsNumber`(`): [@(0.0.255) bool]_[* IsNumber]()&] +[s5;:CParser`:`:IsNumber`(`)const: [@(0.0.255) bool]_[* IsNumber]()_[@(0.0.255) const]&] [s2;%% Tests for sign`-less number at current position `- there must be digit at current position.&] -[s7;%% [*/ Return value]-|true if there is number.&] [s3; &] [s4; &] -[s5;:CParser`:`:IsNumber`(int`): [@(0.0.255) bool]_[* IsNumber]([@(0.0.255) int]_[*@3 base])&] +[s5;:CParser`:`:IsNumber`(int`)const: [@(0.0.255) bool]_[* IsNumber]([@(0.0.255) int]_[*@3 ba +se])_[@(0.0.255) const]&] [s2;%% Tests for sign`-less number with given base `- there must be digit or letter `'A`' `- `'Z`' or `'a`' `- `'z`', where range -is limit by actual base (e.g. for base 12 letters `'a`' `'A`' -`'b`' `'B`' are allowed).&] -[s7;%% [%-*C@3 base]-|Numeric base.&] -[s7;%% [*/ Return value]-|true if there is number with given numeric -base.&] +is limit by actual [%-*@3 base ](e.g. for base 12 letters `'a`' +`'A`' `'b`' `'B`' are allowed).&] [s3; &] [s4; &] [s5;:CParser`:`:ReadNumber`(int`)throw`(CParser`:`:Error`): [_^uint32^ uint32]_[* ReadNum @@ -246,11 +243,10 @@ umber64]([@(0.0.255) int]_[*@3 base]_`=_[@3 10])_[@(0.0.255) throw](Error)&] [s2;%% Reads 64`-bit unsigned number with given numeric [%-*@3 base].&] [s3;%% &] [s4; &] -[s5;:CParser`:`:IsDouble`(`): [@(0.0.255) bool]_[* IsDouble]()&] +[s5;:CParser`:`:IsDouble`(`)const: [@(0.0.255) bool]_[* IsDouble]()_[@(0.0.255) const]&] [s2;%% Test for floating point number at current position `- there either must be digit, or `'`+`' or `'`-`' sign followed by any number of spaces and digit.&] -[s7;%% [*/ Return value]-|true if there is the floating point number.&] [s3; &] [s4; &] [s5;:CParser`:`:ReadDouble`(`)throw`(CParser`:`:Error`): [@(0.0.255) double]_[* ReadDoubl @@ -259,10 +255,9 @@ e]()_[@(0.0.255) throw](Error)&] [s7;%% [*/ Return value]-|Floating point number.&] [s3; &] [s4; &] -[s5;:CParser`:`:IsString`(`): [@(0.0.255) bool]_[* IsString]()&] +[s5;:CParser`:`:IsString`(`)const: [@(0.0.255) bool]_[* IsString]()_[@(0.0.255) const]&] [s2;%% Tests for C`-like string literal at the current position. Same as [* IsChar](`'`\`"`');&] -[s7;%% [*/ Return value]-|true when there is string literal.&] [s3; &] [s4; &] [s5;:CParser`:`:ReadOneString`(bool`)throw`(CParser`:`:Error`): [_^String^ String]_[* Rea @@ -322,16 +317,14 @@ literals are skipped as whole symbols, otherwise input position is advanced by 1 character.&] [s3; &] [s4; &] -[s5;:CParser`:`:GetPtr`(`): [@(0.0.255) const]_[@(0.0.255) char]_`*[* GetPtr]()&] +[s5;:CParser`:`:GetPtr`(`)const: [@(0.0.255) const]_[@(0.0.255) char]_`*[* GetPtr]()_[@(0.0.255) c +onst]&] [s2;%% Returns a pointer to the current position.&] -[s7;%% [*/ Return value]-|Pointer to current position.&] [s3; &] [s4; &] -[s5;:CParser`:`:GetPos`(`): [_^topic`:`/`/Core`/src`/CParser`$en`-us`#CParser`:`:Pos`:`:struct^ C -Parser`::Pos]_[* GetPos]()&] -[s2;%% Gets the current position,.&] -[s7;%% [*/ Return value]-|Current position. It contains the pointer -as well as the line number and the filename.&] +[s5;:CParser`:`:GetPos`(`)const: [_^CParser`:`:Pos^ Pos]_[* GetPos]()_[@(0.0.255) const]&] +[s2;%% Gets the current position. It contains the pointer as well +as the line number and the filename.&] [s3; &] [s4; &] [s5;:CParser`:`:SetPos`(const CParser`:`:Pos`&`): [@(0.0.255) void]_[* SetPos]([@(0.0.255) c @@ -349,7 +342,6 @@ input text (`'`\0`' character).&] [s4; &] [s5;:CParser`:`:operator bool`(`)const: [* operator_bool]()_[@(0.0.255) const]&] [s2; Returns [* true ]if end of file has not been reached, [* false ]otherwise.&] -[s7;%% &] [s3; &] [s4; &] [s5;:CParser`:`:GetLine`(`)const: [@(0.0.255) int]_[* GetLine]()_[@(0.0.255) const]&] @@ -361,6 +353,17 @@ input text (`'`\0`' character).&] t]&] [s2; Returns the actual filename.&] [s3;%% &] +[s3; &] +[s4; &] +[s5;:CParser`:`:Set`(const char`*`,const char`*`,int`): [@(0.0.255) void]_[* Set]([@(0.0.255) c +onst]_[@(0.0.255) char]_`*[*@3 ptr], [@(0.0.255) const]_[@(0.0.255) char]_`*[*@3 fn], +[@(0.0.255) int]_[*@3 line]_`=_[@3 1])&] +[s2;%% Sets the new input string (with filename and line).&] +[s3;%% &] +[s4; &] +[s5;:CParser`:`:Set`(const char`*`): [@(0.0.255) void]_[* Set]([@(0.0.255) const]_[@(0.0.255) c +har]_`*[*@3 ptr])&] +[s2;%% Sets the new input string.&] [s0;%% &] [s0;%% &] [s0;%% &] @@ -380,7 +383,7 @@ tring] with error description.&] [s3; &] [s5;:Exc`:`:Exc`(`): [* Exc]()&] [s2;%% Default constructor. Error message is empty.&] -[s3; &] +[s3;%% &] [s4; &] [s5;:Exc`:`:Exc`(const String`&`): [* Exc]([@(0.0.255) const]_[_^String^ String][@(0.0.255) `& ]_[*@3 desc])&]