Scanning: 1) rationalize end-of-input everywhere, 2) prevent infinite loop at end-of-input, 3) use positive chars.
Fixes issue #25. (char 255 aliased to -1 and missing tests for end of input). 1) All layers of input scanning now share a single EndOfInput value. This avoids translation of it across layers of encapsulation. 2) Some places looking for end of line were not stopping on EndOfInput. 3) Use of "char" for the input made char values > 127 be negative numbers. This allowed for aliasing of 255 to -1, etc. This is fixed by using unsigned char.
This commit is contained in:
parent
91b977e172
commit
c777fc2c4c
15 changed files with 134 additions and 112 deletions
|
|
@ -134,8 +134,6 @@ public:
|
|||
virtual int getch() = 0;
|
||||
virtual void ungetch() = 0;
|
||||
|
||||
static const int endOfInput = -2;
|
||||
|
||||
protected:
|
||||
bool done;
|
||||
TPpContext* pp;
|
||||
|
|
@ -210,21 +208,18 @@ protected:
|
|||
|
||||
// Get the next token from *stack* of input sources, popping input sources
|
||||
// that are out of tokens, down until an input sources is found that has a token.
|
||||
// Return EOF when there are no more tokens to be found by doing this.
|
||||
// Return EndOfInput when there are no more tokens to be found by doing this.
|
||||
int scanToken(TPpToken* ppToken)
|
||||
{
|
||||
int token = EOF;
|
||||
int token = EndOfInput;
|
||||
|
||||
while (! inputStack.empty()) {
|
||||
token = inputStack.back()->scan(ppToken);
|
||||
if (token != tInput::endOfInput)
|
||||
if (token != EndOfInput)
|
||||
break;
|
||||
popInput();
|
||||
}
|
||||
|
||||
if (token == tInput::endOfInput)
|
||||
return EOF;
|
||||
|
||||
return token;
|
||||
}
|
||||
int getChar() { return inputStack.back()->getch(); }
|
||||
|
|
@ -248,7 +243,7 @@ protected:
|
|||
}
|
||||
|
||||
virtual int scan(TPpToken*);
|
||||
virtual int getch() { assert(0); return endOfInput; }
|
||||
virtual int getch() { assert(0); return EndOfInput; }
|
||||
virtual void ungetch() { assert(0); }
|
||||
MacroSymbol *mac;
|
||||
TVector<TokenStream*> args;
|
||||
|
|
@ -260,12 +255,12 @@ protected:
|
|||
virtual int scan(TPpToken*)
|
||||
{
|
||||
if (done)
|
||||
return endOfInput;
|
||||
return EndOfInput;
|
||||
done = true;
|
||||
|
||||
return marker;
|
||||
}
|
||||
virtual int getch() { assert(0); return endOfInput; }
|
||||
virtual int getch() { assert(0); return EndOfInput; }
|
||||
virtual void ungetch() { assert(0); }
|
||||
static const int marker = -3;
|
||||
};
|
||||
|
|
@ -274,7 +269,7 @@ protected:
|
|||
public:
|
||||
tZeroInput(TPpContext* pp) : tInput(pp) { }
|
||||
virtual int scan(TPpToken*);
|
||||
virtual int getch() { assert(0); return endOfInput; }
|
||||
virtual int getch() { assert(0); return EndOfInput; }
|
||||
virtual void ungetch() { assert(0); }
|
||||
};
|
||||
|
||||
|
|
@ -328,7 +323,7 @@ protected:
|
|||
public:
|
||||
tTokenInput(TPpContext* pp, TokenStream* t) : tInput(pp), tokens(t) { }
|
||||
virtual int scan(TPpToken *);
|
||||
virtual int getch() { assert(0); return endOfInput; }
|
||||
virtual int getch() { assert(0); return EndOfInput; }
|
||||
virtual void ungetch() { assert(0); }
|
||||
protected:
|
||||
TokenStream *tokens;
|
||||
|
|
@ -338,7 +333,7 @@ protected:
|
|||
public:
|
||||
tUngotTokenInput(TPpContext* pp, int t, TPpToken* p) : tInput(pp), token(t), lval(*p) { }
|
||||
virtual int scan(TPpToken *);
|
||||
virtual int getch() { assert(0); return endOfInput; }
|
||||
virtual int getch() { assert(0); return EndOfInput; }
|
||||
virtual void ungetch() { assert(0); }
|
||||
protected:
|
||||
int token;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue