Commit fcb4ed05 by John Kessenich

Preprocessor: Rationalize, simplify, and correct the stack of input sources and…

Preprocessor: Rationalize, simplify, and correct the stack of input sources and their tokenization. This included - consistently dealing with EOF and its effect on error recovery (bug 11444, #1) - turning a simulated OO hierarchy of function pointers and typecasting into a real C++ class hierarchy - correctly handling '\' everywhere, in all classes of tokens, as a layer before preprocessing itself - conditionally handling '\n' in macro expansion input, depending on whether it is in a macro expression or not - delete some unused "memory cleanup" code git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@24626 e7fa87d3-cd2b-0410-9028-fcbf551c1848
parent 08d18247
......@@ -5,14 +5,22 @@ ERROR: 0:66: '#define' : Macro redefined; different substitutions: BIG
ERROR: 0:81: 'preprocessor evaluation' : bad expression
ERROR: 0:81: '#if' : unexpected tokens following directive
ERROR: 0:82: '#error' : good macro
ERROR: 0:89: 'macro expansion' : unexpected '#' foobar
ERROR: 0:90: 'preprocessor evaluation' : bad expression
ERROR: 0:94: 'macro expansion' : unexpected '#' foobar
ERROR: 0:95: 'preprocessor evaluation' : bad expression
ERROR: 0:87: 'macro expansion' : end of line in macro substitution: foobar
ERROR: 0:88: 'preprocessor evaluation' : can't evaluate expression
ERROR: 0:88: 'preprocessor evaluation' : bad expression
ERROR: 0:88: '#if' : unexpected tokens following directive
ERROR: 0:92: 'macro expansion' : end of line in macro substitution: foobar
ERROR: 0:93: 'preprocessor evaluation' : can't evaluate expression
ERROR: 0:93: 'preprocessor evaluation' : bad expression
ERROR: 0:93: '#if' : unexpected tokens following directive
ERROR: 0:99: 'macro expansion' : end of line in macro substitution: foobar
ERROR: 0:100: 'preprocessor evaluation' : can't evaluate expression
ERROR: 0:100: 'preprocessor evaluation' : bad expression
ERROR: 0:100: '#if' : unexpected tokens following directive
ERROR: 0:102: 'macro expansion' : unexpected '#' foobar
ERROR: 0:102: 'preprocessor evaluation' : undefined macro in expression not allowed in es profile endif
ERROR: 0:101: 'macro expansion' : end of line in macro substitution: foobar
ERROR: 0:102: 'preprocessor evaluation' : can't evaluate expression
ERROR: 0:102: 'preprocessor evaluation' : bad expression
ERROR: 0:102: '#if' : unexpected tokens following directive
ERROR: 0:108: 'preprocessor evaluation' : undefined macro in expression not allowed in es profile UNDEF
ERROR: 0:111: '#error' : good 0
ERROR: 0:115: '#error' : good 1
......@@ -26,7 +34,7 @@ ERROR: 0:153: 'preprocessor evaluation' : undefined macro in expression not allo
ERROR: 0:156: 'preprocessor evaluation' : undefined macro in expression not allowed in es profile UNDEF2
ERROR: 0:159: 'preprocessor evaluation' : undefined macro in expression not allowed in es profile UNDEF2
ERROR: 0:10002: '' : missing #endif
ERROR: 27 compilation errors. No code generated.
ERROR: 35 compilation errors. No code generated.
ERROR: node is still EOpNull!
......
cppIndent.vert
ERROR: 0:61: 'macro expansion' : Too few args in Macro FUNC
ERROR: 0:61: '' : syntax error
ERROR: 2 compilation errors. No code generated.
0:? Sequence
ERROR: node is still EOpNull!
0:5 Sequence
0:5 move second child to first child (float)
0:5 'sum' (float)
......
......@@ -3,7 +3,9 @@ ERROR: 0:144: '#elif' : #elif after #else
ERROR: 0:152: '#else' : #else after #else
ERROR: 0:161: '#elif' : #elif after #else
ERROR: 0:169: '#else' : #else after #else
ERROR: 4 compilation errors. No code generated.
ERROR: 0:177: 'macro expansion' : EOF in macro FUNC
ERROR: 0:178: '' : syntax error
ERROR: 6 compilation errors. No code generated.
ERROR: node is still EOpNull!
......@@ -58,6 +60,11 @@ ERROR: node is still EOpNull!
0:133 'selected3' (int)
0:133 Constant:
0:133 3 (const int)
0:175 Function Definition: foo985( (void)
0:175 Function Parameters:
0:175 Sequence
0:175 Constant:
0:175 6 (const int)
0:? Linker Objects
0:? 'sum' (float)
0:? 'selected4' (int)
......
......@@ -6,15 +6,15 @@ ERROR: 0:85: '#error' : good3
ERROR: 0:89: '#error' : good4
ERROR: 0:93: '#error' : good5
ERROR: 0:97: '#error' : good6
ERROR: 0:101: 'preprocessor evaluation' : expected ')'
ERROR: 0:100: 'preprocessor evaluation' : expected ')'
ERROR: 0:101: '#error' : bad1
ERROR: 0:104: '#if' : unexpected tokens following directive
ERROR: 0:105: '#error' : bad2
ERROR: 0:109: 'preprocessor evaluation' : expected ')'
ERROR: 0:108: 'preprocessor evaluation' : expected ')'
ERROR: 0:109: '#error' : bad3
ERROR: 0:112: '#if' : unexpected tokens following directive
ERROR: 0:113: '#error' : bad4
ERROR: 0:117: 'preprocessor evaluation' : expected ')'
ERROR: 0:116: 'preprocessor evaluation' : expected ')'
ERROR: 0:117: '#error' : bad5
ERROR: 0:120: '#if' : unexpected tokens following directive
ERROR: 0:121: '#error' : bad6
......@@ -74,8 +74,16 @@ ERROR: 12:20021: '#error' : line should be 20021
ERROR: 12:20046: '#define' : Macro redefined; different substitutions: SPACE_IN_MIDDLE
ERROR: 12:20052: '#error' : good evaluation 1
ERROR: 12:20056: '#error' : good evaluation 2
ERROR: 12:9001: 'preprocessor evaluation' : expected ')'
ERROR: 12:9003: '#if' : unexpected tokens following directive
ERROR: 12:9015: 'macro expansion' : expected '(' following FOOOM
ERROR: 12:9015: 'FOOOM' : undeclared identifier
ERROR: 12:9015: '=' : cannot convert from 'float' to 'int'
ERROR: 12:9016: 'macro expansion' : expected '(' following FOOOM
ERROR: 12:9017: 'preprocessor evaluation' : can't evaluate expression
ERROR: 12:9017: 'preprocessor evaluation' : bad expression
ERROR: 12:10003: '' : missing #endif
ERROR: 75 compilation errors. No code generated.
ERROR: 83 compilation errors. No code generated.
ERROR: node is still EOpNull!
......@@ -165,6 +173,10 @@ ERROR: node is still EOpNull!
12:20034 6.000000
12:20034 6.000000
12:20034 6.000000
12:9012 Sequence
12:9012 move second child to first child (int)
12:9012 'R1' (int)
12:9012 'RECURSE' (int)
0:? Linker Objects
0:? 'sum' (float)
0:? 'linenumber' (int)
......@@ -174,6 +186,9 @@ ERROR: node is still EOpNull!
0:? 'a' (int)
0:? 'n' (int)
0:? 'f' (double)
0:? 'RECURSE' (int)
0:? 'R1' (int)
0:? 'aoeua' (int)
0:? 'gl_VertexID' (gl_VertexId int)
0:? 'gl_InstanceID' (gl_InstanceId int)
......
......@@ -12,7 +12,15 @@ ERROR: 0:48: '\' : illegal use of escape character
ERROR: 0:49: '$' : unexpected token
ERROR: 0:50: '@' : unexpected token
ERROR: 0:55: '#error' : good continuation
ERROR: 12 compilation errors. No code generated.
WARNING: 0:62: 'line continuation' : used at end of comment; the following line is still part of the comment
ERROR: 0:111: 'macro expansion' : end of line in macro substitution: FOOM
ERROR: 0:112: 'preprocessor evaluation' : can't evaluate expression
ERROR: 0:112: '#if' : unexpected tokens following directive
ERROR: 0:117: 'macro expansion' : end of line in macro substitution: FOOM
ERROR: 0:118: 'preprocessor evaluation' : can't evaluate expression
ERROR: 0:118: '#if' : unexpected tokens following directive
ERROR: 0:147: '' : syntax error
ERROR: 19 compilation errors. No code generated.
ERROR: node is still EOpNull!
......@@ -72,6 +80,84 @@ ERROR: node is still EOpNull!
0:50 'q4' (highp int)
0:50 Constant:
0:50 1 (const int)
0:65 Sequence
0:65 move second child to first child (highp int)
0:65 'abdece' (highp int)
0:65 Constant:
0:65 10 (const int)
0:66 Sequence
0:66 move second child to first child (highp int)
0:66 'aoeuntaoehu' (highp int)
0:66 'abdece' (highp int)
0:74 Sequence
0:74 move second child to first child (highp float)
0:74 'funkyf' (highp float)
0:75 Constant:
0:75 12300000000000000.000000
0:85 Sequence
0:84 move second child to first child (highp int)
0:84 'funkyh' (highp int)
0:86 Constant:
0:86 244 (const int)
0:91 Sequence
0:91 move second child to first child (highp int)
0:91 'funkyo' (highp int)
0:92 Constant:
0:92 34 (const int)
0:96 Sequence
0:96 move second child to first child (highp int)
0:96 'c' (highp int)
0:97 Constant:
0:97 11 (const int)
0:98 Sequence
0:98 move second child to first child (highp int)
0:98 'd' (highp int)
0:98 Constant:
0:98 12 (const int)
0:107 Sequence
0:107 move second child to first child (highp int)
0:107 'bar103' (highp int)
0:107 Constant:
0:107 17 (const int)
0:113 Sequence
0:113 move second child to first child (highp int)
0:113 'bar104' (highp int)
0:113 Constant:
0:113 19 (const int)
0:119 Sequence
0:119 move second child to first child (highp int)
0:119 'bar105' (highp int)
0:119 Constant:
0:119 19 (const int)
0:122 Sequence
0:122 move second child to first child (highp int)
0:122 'bar106' (highp int)
0:122 Constant:
0:122 12 (const int)
0:123 Sequence
0:123 move second child to first child (highp int)
0:123 'bar107' (highp int)
0:128 Constant:
0:128 5 (const int)
0:131 Function Definition: foo203209409( (void)
0:131 Function Parameters:
0:134 Sequence
0:134 add second child into first child (highp int)
0:133 'bar107' (highp int)
0:134 Constant:
0:134 37 (const int)
0:135 multiply second child into first child (highp int)
0:135 'bar107' (highp int)
0:136 Constant:
0:136 38 (const int)
0:137 divide second child into first child (highp int)
0:137 'bar107' (highp int)
0:138 Constant:
0:138 39 (const int)
0:139 add (highp int)
0:139 'bar107' (highp int)
0:140 Constant:
0:140 41 (const int)
0:? Linker Objects
0:? 'foo' (highp float)
0:? 'goodDecl' (highp int)
......@@ -83,6 +169,18 @@ ERROR: node is still EOpNull!
0:? 'q2' (highp int)
0:? 'q3' (highp int)
0:? 'q4' (highp int)
0:? 'abdece' (highp int)
0:? 'aoeuntaoehu' (highp int)
0:? 'funkyf' (highp float)
0:? 'funkyh' (highp int)
0:? 'funkyo' (highp int)
0:? 'c' (highp int)
0:? 'd' (highp int)
0:? 'bar103' (highp int)
0:? 'bar104' (highp int)
0:? 'bar105' (highp int)
0:? 'bar106' (highp int)
0:? 'bar107' (highp int)
0:? 'gl_VertexID' (gl_VertexId highp int)
0:? 'gl_InstanceID' (gl_InstanceId highp int)
......
......@@ -55,3 +55,7 @@ sum += 900000000.0;
// sum should be 980600301.0
gl_Position = vec4(sum);
}
#define FUNC(a,b) a+b
// needs to be last test in file due to syntax error
void foo986(){ FUNC( (((2)))), 4); } // ERROR, too many )
......@@ -170,3 +170,8 @@ int;
int;
#endif
#endif
#define FUNC(a,b) a+b
void foo985(){ FUNC( (((2))), ((3),4)); }
// needs to be last test in file
void foo987(){ FUNC(((); } // ERROR, EOF in argument
......@@ -306,6 +306,26 @@ void foo234()
#error good evaluation 2
#endif
// ERRORS...
#line 9000
#if defined(OUNH
#endif
#if defined OUNH)
#endif
// recursion (okay)
#define RECURSE RECURSE
int RECURSE;
#define R2 R1
#define R1 R2
#undef RECURSE
int R1 = RECURSE;
#define FOOOM(a,b) a + b
int aoeua = FOOOM;
#if FOOOM
#endif
#line 10000
#if 1
#else
......
......@@ -54,4 +54,95 @@ D;
# \
error good continuation
#define AA a \ b
#define AA1 a \ b
#define AA2 a \\ b
#define AA3 a \\\ b
#define AA4 a \\\\ b
// anoetuh nonaetu \\\\\\
still in comment
int abdece = 10;
int aoeuntaoehu = abd\
\
\
\
\
\
ece;
float funkyf = \
.\
1\
2\
3\
e\
+\
1\
7\
;\
int funkyh\
=\
0\
x\
f\
4\
;
int funkyo =\
0\
4\
2\
;
int c = \
11;
int d = 1\
2;
#define FOOM(a,b) a + b
#if FO\
OM(2\
,\
3)
int bar103 = 17;
#endif
// ERROR
#if FOOM(2,
3)
int bar104 = 19;
#endif
// ERROR
#if FOOM(
2,3)
int bar105 = 19;
#endif
int bar106 = FOOM(5,7);
int bar107 = FOOM // okay
(
2
,
3
)
;
void foo203209409()
{
bar107 \
+= 37;
bar107 *\
= 38;
bar107 /=\
39;
bar107 +\
41;
}
void foo230920394()
{
// syntax error
bar107 +\
= 42;
}
......@@ -9,5 +9,5 @@
// source have to figure out how to create revision.h just to get a build
// going. However, if it is not updated, it can be a version behind.
#define GLSLANG_REVISION "24569"
#define GLSLANG_DATE "2013/12/18 11:47:12"
#define GLSLANG_REVISION "24594"
#define GLSLANG_DATE "2013/12/20 11:36:27"
......@@ -84,7 +84,7 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang {
TPpContext::TPpContext(TParseContext& pc) :
preamble(0), strings(0), parseContext(pc)
preamble(0), strings(0), parseContext(pc), inComment(false)
{
InitAtomTable();
InitScanner(this);
......@@ -143,17 +143,18 @@ TPpContext::~TPpContext()
delete it->second->mac.body;
mem_FreePool(pool);
delete [] preamble;
// free up the inputStack
while (! inputStack.empty())
popInput();
}
void TPpContext::setInput(TInputScanner& input, bool versionWillBeError)
{
StringInputSrc* in = new StringInputSrc;
in->input = &input;
in->scan = sourceScan;
in->getch = (int (*)(TPpContext*, InputSrc *, TPpToken *))sourceGetCh;
in->ungetch = (void (*)(TPpContext*, InputSrc *, int, TPpToken *))sourceUngetCh;
in->prev = currentInput;
currentInput = in;
assert(inputStack.size() == 0);
pushInput(new tStringInput(this, input));
errorOnVersion = versionWillBeError;
versionSeen = false;
}
......
......@@ -121,19 +121,37 @@ public:
virtual ~TPpContext();
void setPreamble(const char* preamble, size_t length);
void setInput(TInputScanner& input, bool versionWillBeError);
const char* tokenize(TPpToken* ppToken);
// TODO: preprocessor simplification: this should be a base class, not a set of function pointers
struct InputSrc {
InputSrc() : prev(0), scan(0), getch(0), ungetch(0) { }
struct InputSrc *prev;
int (*scan)(TPpContext*, struct InputSrc *, TPpToken *);
int (*getch)(TPpContext*, struct InputSrc *, TPpToken *);
void (*ungetch)(TPpContext*, struct InputSrc *, int, TPpToken *);
class tInput {
public:
tInput(TPpContext* p) : done(false), pp(p) { }
virtual ~tInput() { }
virtual int scan(TPpToken*) = 0;
virtual int getch() = 0;
virtual void ungetch() = 0;
static const int endOfInput = -2;
protected:
bool done;
TPpContext* pp;
};
void setInput(TInputScanner& input, bool versionWillBeError);
void TPpContext::pushInput(tInput* in)
{
inputStack.push_back(in);
}
void TPpContext::popInput()
{
delete inputStack.back();
inputStack.pop_back();
}
struct TokenStream {
TokenStream() : current(0) { }
TVector<unsigned char> data;
......@@ -145,7 +163,6 @@ public:
uintptr_t free, end;
size_t chunksize;
uintptr_t alignmask;
struct cleanup *cleanup;
};
//
......@@ -184,10 +201,31 @@ protected:
int currentString; // which string we're currently parsing (-1 for preamble)
// Scanner data:
int mostRecentToken; // Most recent token seen by the scanner
int previous_token;
TParseContext& parseContext;
// 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.
int scanToken(TPpToken* ppToken)
{
int token = EOF;
while (! inputStack.empty()) {
token = inputStack.back()->scan(ppToken);
if (token != tInput::endOfInput)
break;
popInput();
}
if (token == tInput::endOfInput)
return EOF;
return token;
}
int getChar() { return inputStack.back()->getch(); }
void ungetChar() { inputStack.back()->ungetch(); }
static const int maxMacroArgs = 64;
static const int maxIfNesting = 64;
......@@ -196,18 +234,47 @@ protected:
int elsetracker; // #if-#else and #endif constructs...Counter.
const char *ErrMsg;
struct MacroInputSrc : public InputSrc {
MacroInputSrc() : mac(0) { }
virtual ~MacroInputSrc()
class tMacroInput : public tInput {
public:
tMacroInput(TPpContext* pp) : tInput(pp) { }
virtual ~tMacroInput()
{
for (size_t i = 0; i < args.size(); ++i)
delete args[i];
}
virtual int scan(TPpToken*);
virtual int getch() { assert(0); return endOfInput; }
virtual void ungetch() { assert(0); }
MacroSymbol *mac;
TVector<TokenStream*> args;
};
InputSrc *currentInput;
class tMarkerInput : public tInput {
public:
tMarkerInput(TPpContext* pp) : tInput(pp) { }
virtual int scan(TPpToken*)
{
if (done)
return endOfInput;
done = true;
return marker;
}
virtual int getch() { assert(0); return endOfInput; }
virtual void ungetch() { assert(0); }
static const int marker = -3;
};
class tZeroInput : public tInput {
public:
tZeroInput(TPpContext* pp) : tInput(pp) { }
virtual int scan(TPpToken*);
virtual int getch() { assert(0); return endOfInput; }
virtual void ungetch() { assert(0); }
};
std::vector<tInput*> inputStack;
bool errorOnVersion;
bool versionSeen;
......@@ -256,12 +323,8 @@ protected:
int CPPversion(TPpToken * ppToken);
int CPPextension(TPpToken * ppToken);
int readCPPline(TPpToken * ppToken);
void PushEofSrc();
void PopEofSrc();
TokenStream* PrescanMacroArg(TokenStream *a, TPpToken * ppToken);
static int macro_scan(TPpContext* pp, InputSrc *inInput, TPpToken * ppToken);
static int zero_scan(TPpContext* pp, InputSrc *inInput, TPpToken * ppToken);
int MacroExpand(int atom, TPpToken* ppToken, bool expandUndef);
TokenStream* PrescanMacroArg(TokenStream *a, TPpToken * ppToken, bool newLineOkay);
int MacroExpand(int atom, TPpToken* ppToken, bool expandUndef, bool newLineOkay);
//
// from PpSymbols.cpp
......@@ -278,32 +341,49 @@ protected:
void RecordToken(TokenStream* pTok, int token, TPpToken* ppToken);
void RewindTokenStream(TokenStream *pTok);
int ReadToken(TokenStream* pTok, TPpToken* ppToken);
int ReadFromTokenStream(TokenStream *ts, int name, int (*final)(TPpContext *));
void pushTokenStreamInput(TokenStream *ts, int name);
void UngetToken(int token, TPpToken* ppToken);
struct TokenInputSrc : public InputSrc {
class tTokenInput : public tInput {
public:
tTokenInput(TPpContext* pp, TokenStream* t) : tInput(pp), tokens(t) { }
virtual int scan(TPpToken *);
virtual int getch() { assert(0); return endOfInput; }
virtual void ungetch() { assert(0); }
protected:
TokenStream *tokens;
int (*final)(TPpContext *);
};
static int scan_token(TPpContext*, TokenInputSrc *in, TPpToken * ppToken);
struct UngotToken : public InputSrc {
class tUngotTokenInput : public tInput {
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 void ungetch() { assert(0); }
protected:
int token;
TPpToken lval;
};
static int reget_token(TPpContext *, UngotToken *t, TPpToken * ppToken);
//
// From PpScanner.cpp
//
struct StringInputSrc : public InputSrc {
class tStringInput : public tInput {
public:
tStringInput(TPpContext* pp, TInputScanner& i) : tInput(pp), input(&i) { }
virtual int scan(TPpToken *);
virtual int getch();
virtual void ungetch();
protected:
TInputScanner* input;
};
int InitScanner(TPpContext *cpp);
static int sourceGetCh(TPpContext*, StringInputSrc *in);
static void sourceUngetCh(TPpContext*, StringInputSrc *in, int ch, TPpToken *type);
int ScanFromString(char *s);
bool check_EOF(int token);
void missingEndifCheck();
int lFloatConst(char *str, int len, int ch, TPpToken * ppToken);
static int sourceScan(TPpContext*, InputSrc *in, TPpToken * ppToken);
bool inComment;
//
// From PpAtom.cpp
......
......@@ -75,7 +75,7 @@ NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
//
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
......@@ -93,13 +93,6 @@ struct chunk {
struct chunk *next;
};
struct cleanup {
struct cleanup *next;
void (*fn)(void *, void *);
void *arg1;
void *arg2;
};
TPpContext::MemoryPool* TPpContext::mem_CreatePool(size_t chunksize, unsigned int align)
{
MemoryPool *pool;
......@@ -108,32 +101,28 @@ TPpContext::MemoryPool* TPpContext::mem_CreatePool(size_t chunksize, unsigned in
align = ALIGN;
if (chunksize == 0)
chunksize = CHUNKSIZE;
if (align & (align-1))
if (align & (align - 1))
return 0;
if (chunksize < sizeof(MemoryPool))
return 0;
if (chunksize & (align-1))
if (chunksize & (align - 1))
return 0;
if (!(pool = (MemoryPool*)malloc(chunksize)))
return 0;
pool->next = 0;
pool->chunksize = chunksize;
pool->alignmask = (uintptr_t)(align)-1;
pool->alignmask = (uintptr_t)(align) - 1;
pool->free = ((uintptr_t)(pool + 1) + pool->alignmask) & ~pool->alignmask;
pool->end = (uintptr_t)pool + chunksize;
pool->cleanup = 0;
return pool;
}
void TPpContext::mem_FreePool(MemoryPool *pool)
{
struct cleanup *cleanup;
struct chunk *p, *next;
struct chunk *p, *next;
for (cleanup = pool->cleanup; cleanup; cleanup = cleanup->next) {
cleanup->fn(cleanup->arg1, cleanup->arg2);
}
for (p = (struct chunk *)pool; p; p = next) {
next = p->next;
free(p);
......@@ -168,19 +157,4 @@ void* TPpContext::mem_Alloc(MemoryPool *pool, size_t size)
return rv;
}
int TPpContext::mem_AddCleanup(MemoryPool *pool, void (*fn)(void *, void*), void* arg1, void* arg2)
{
struct cleanup *cleanup;
pool->free = (pool->free + sizeof(void *) - 1) & ~(sizeof(void *)-1);
cleanup = (struct cleanup *)(mem_Alloc(pool, sizeof(struct cleanup)));
if (!cleanup) return -1;
cleanup->next = pool->cleanup;
cleanup->fn = fn;
cleanup->arg1 = arg1;
cleanup->arg2 = arg2;
pool->cleanup = cleanup;
return 0;
}
} // end namespace glslang
......@@ -109,7 +109,7 @@ int TPpContext::lReadByte(TokenStream *pTok)
if (pTok->current < pTok->data.size())
return pTok->data[pTok->current++];
else
return -1;
return tInput::endOfInput;
}
/*
......@@ -124,6 +124,7 @@ void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken)
lAddByte(pTok, (unsigned char)((token & 0x7f) + 0x80));
else
lAddByte(pTok, (unsigned char)(token & 0x7f));
switch (token) {
case CPP_IDENTIFIER:
case CPP_STRCONSTANT:
......@@ -154,11 +155,6 @@ void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken)
void TPpContext::RewindTokenStream(TokenStream *pTok)
{
pTok->current = 0;
//if (pTok->head) {
// pTok->current = pTok->head;
// pTok->current->current = 0;
//}
}
/*
......@@ -172,105 +168,82 @@ int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken)
ltoken = lReadByte(pTok);
ppToken->loc = parseContext.getCurrentLoc();
if (ltoken >= 0) {
if (ltoken > 127)
ltoken += 128;
if (ltoken > 127)
ltoken += 128;
switch (ltoken) {
case CPP_STRCONSTANT:
case CPP_IDENTIFIER:
case CPP_FLOATCONSTANT:
case CPP_DOUBLECONSTANT:
case CPP_INTCONSTANT:
case CPP_UINTCONSTANT:
len = 0;
ch = lReadByte(pTok);
while (ch != 0) {
if (len < TPpToken::maxTokenLength) {
tokenText[len] = ch;
len++;
ch = lReadByte(pTok);
} else {
parseContext.error(ppToken->loc, "token too long", "", "");
break;
}
}
tokenText[len] = 0;
switch (ltoken) {
case CPP_STRCONSTANT:
case CPP_IDENTIFIER:
case CPP_STRCONSTANT:
ppToken->atom = LookUpAddString(tokenText);
break;
case CPP_FLOATCONSTANT:
case CPP_DOUBLECONSTANT:
strcpy(ppToken->name, tokenText);
ppToken->dval = atof(ppToken->name);
break;
case CPP_INTCONSTANT:
case CPP_UINTCONSTANT:
len = 0;
ch = lReadByte(pTok);
while (ch != 0) {
if (len < TPpToken::maxTokenLength) {
tokenText[len] = ch;
len++;
ch = lReadByte(pTok);
} else {
parseContext.error(ppToken->loc, "token too long", "", "");
break;
}
}
tokenText[len] = 0;
switch (ltoken) {
case CPP_IDENTIFIER:
case CPP_STRCONSTANT:
ppToken->atom = LookUpAddString(tokenText);
break;
case CPP_FLOATCONSTANT:
case CPP_DOUBLECONSTANT:
strcpy(ppToken->name, tokenText);
ppToken->dval = atof(ppToken->name);
break;
case CPP_INTCONSTANT:
case CPP_UINTCONSTANT:
strcpy(ppToken->name, tokenText);
if (len > 0 && tokenText[0] == '0') {
if (len > 1 && tokenText[1] == 'x' || tokenText[1] == 'X')
ppToken->ival = strtol(ppToken->name, 0, 16);
else
ppToken->ival = strtol(ppToken->name, 0, 8);
} else
ppToken->ival = atoi(ppToken->name);
break;
}
strcpy(ppToken->name, tokenText);
if (len > 0 && tokenText[0] == '0') {
if (len > 1 && tokenText[1] == 'x' || tokenText[1] == 'X')
ppToken->ival = strtol(ppToken->name, 0, 16);
else
ppToken->ival = strtol(ppToken->name, 0, 8);
} else
ppToken->ival = atoi(ppToken->name);
break;
}
return ltoken;
}
return EOF;
return ltoken;
}
int TPpContext::scan_token(TPpContext* pp, TokenInputSrc *in, TPpToken * ppToken)
int TPpContext::tTokenInput::scan(TPpToken* ppToken)
{
int token = pp->ReadToken(in->tokens, ppToken);
int (*final)(TPpContext *);
if (token > 0)
return token;
pp->currentInput = in->prev;
final = in->final;
delete in;
if (final && !final(pp))
return -1;
return pp->currentInput->scan(pp, pp->currentInput, ppToken);
return pp->ReadToken(tokens, ppToken);
}
int TPpContext::ReadFromTokenStream(TokenStream *ts, int name, int (*final)(TPpContext *))
void TPpContext::pushTokenStreamInput(TokenStream* ts, int name)
{
TokenInputSrc* in = new TokenInputSrc;
in->prev = currentInput;
in->scan = (int (*)(TPpContext*, InputSrc*, TPpToken*))scan_token;
in->tokens = ts;
in->final = final;
pushInput(new tTokenInput(this, ts));
RewindTokenStream(ts);
currentInput = in;
return 1;
}
int TPpContext::reget_token(TPpContext* pp, UngotToken *t, TPpToken * ppToken)
int TPpContext::tUngotTokenInput::scan(TPpToken* ppToken)
{
int token = t->token;
*ppToken = t->lval;
pp->currentInput = t->prev;
delete t;
if (done)
return endOfInput;
int ret = token;
*ppToken = lval;
done = true;
return token;
return ret;
}
void TPpContext::UngetToken(int token, TPpToken* ppToken)
{
UngotToken *t = new UngotToken;
t->token = token;
t->lval = *ppToken;
t->scan = (int(*)(TPpContext*, struct InputSrc *, TPpToken *))reget_token;
t->prev = currentInput;
currentInput = t;
pushInput(new tUngotTokenInput(this, token, ppToken));
}
} // end namespace glslang
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment