A friend gave me some code to add to my project but I fail to compile it, probably because of the definition of byte(or BYTE, I don't know) as a variable. Here is the code that won't compile, and below are the errors from the compiler.
parser.cpp
Code:
//-----------------------------------------------------------------------------
// Parser
//-----------------------------------------------------------------------------
#include "parser.h"
char Parser::token[MAXTOKEN]; // buffer for tokens
int Parser::scriptline;
const byte *Parser::buffer;
const byte *Parser::buf_start;
const byte *Parser::buf_end;
void Parser::StartParseBuffer(const byte *buff, const int size)
{
buf_start = buffer = buff;
buf_end = buff + size;
scriptline = 1;
}
void Parser::StartParseString(const char *string)
{
buf_start = buffer = (byte *) string;
buf_end = (byte *) string + strlen(string);
scriptline = 1;
}
// if crossline is false, GetToken returns false when a line break is found
// GetToken(true) returns 0 only if end of file
// GetToken(false) returns 0 if line break or end of file
bool Parser::GetToken(bool crossline)
{
char *token_p;
if (!buffer) return 0;
if (buffer == buf_end) return 0;
*token = 0; // init to zero
// skip space
skipspace:
while (*buffer <= 32 || *buffer == '\n')
{
// no se si \n, \t y \r son menores que 32, pero supongo que sí
if (*buffer == '\n') ++scriptline;
if (buffer >= buf_end)
{
if (!crossline)
{
//gConsole->InsertLine("Line %i is incomplete", scriptline);
}
return 0;
}
if (!crossline)
{
if (*buffer == '\n')
{
--scriptline;
return 0;
}
}
++buffer;
}
if (buffer >= buf_end) return 0;
// # // comments
if (*buffer == '#' ||
(buffer[0] == '/' && buffer[1] == '/'))
{
while (*buffer++ != '\n')
{
if (buffer >= buf_end) return 0;
}
++scriptline;
if (!crossline) return 0;
goto skipspace;
}
// /* */ comments
if (buffer[0] == '/' && buffer[1] == '*')
{
buffer += 2;
while (buffer[0] != '*' &&
buffer[1] != '/')
{
++buffer;
if (buffer >= buf_end)
{
//print error here
return 0;
}
if (*buffer == '\n') ++scriptline;
}
buffer += 2;
goto skipspace;
}
// copy token
token_p = token;
if (*buffer == '"')
{
// quoted token
++buffer;
while (*buffer != '"')
{
*token_p++ = *buffer++;
if (buffer == buf_end) break;
if (token_p == &token[MAXTOKEN])
{
//Print Error here.
}
}
++buffer;
}
else
{
// regular token
if (*buffer == '{' ||
*buffer == '}')
{
*token_p++ = *buffer++;
*token_p = 0;
return 1;
}
while (*buffer > 32 &&
*buffer != '\n' &&
*buffer !='{' &&
*buffer !='}')
{
*token_p++ = *buffer++;
if (buffer == buf_end) break;
if (token_p == &token[MAXTOKEN])
{
//Print error here.
}
}
}
*token_p = 0;
return 1;
}
int Parser::GetOffset(void)
{
return (int) (buffer - buf_start);
}
void Parser::GoTo(const int offset)
{
buffer = buf_start + offset;
}
parser.h
Code:
//-----------------------------------------------------------------------------
// Parser
//-----------------------------------------------------------------------------
#ifndef __PARSER_H__
#define __PARSER_H__
//#include "files.h"
#define MAXTOKEN 1024
/**
* Parser class.
* The parser class provides methods to parse files. This is specially used
* for shaders parsing.
*/
class Parser
{
public:
static void StartParseBuffer(const byte *buffer, const int size);
static void StartParseString(const char *string);
static bool GetToken(bool crossline);
static int GetOffset(void);
static void GoTo(const int offset);
static char token[MAXTOKEN]; /**< buffer for tokens */
static int scriptline;
protected:
static const byte *buffer;
static const byte *buf_start;
static const byte *buf_end;
};
#endif /* __PARSER_H__ */
ERRORS
Code:
Compiling...
parser.cpp
Parser\parser.h(20): error: identifier "byte" is undefined
static void StartParseBuffer(const byte *buffer, const int size);
^
Parser\parser.h(32): error: identifier "byte" is undefined
static const byte *buffer;
^
Parser\parser.h(33): error: identifier "byte" is undefined
static const byte *buf_start;
^
Parser\parser.h(34): error: identifier "byte" is undefined
static const byte *buf_end;
^
Parser\parser.cpp(9): error: identifier "byte" is undefined
const byte *Parser::buffer;
^
Parser\parser.cpp(10): error: identifier "byte" is undefined
const byte *Parser::buf_start;
^
Parser\parser.cpp(11): error: identifier "byte" is undefined
const byte *Parser::buf_end;
^
Parser\parser.cpp(14): error: identifier "byte" is undefined
void Parser::StartParseBuffer(const byte *buff, const int size)
^
Parser\parser.cpp(23): error: identifier "byte" is undefined
buf_start = buffer = (byte *) string;
^
Parser\parser.cpp(23): error: expected an expression
buf_start = buffer = (byte *) string;
^
Parser\parser.cpp(23): error: expected a ";"
buf_start = buffer = (byte *) string;
^
Parser\parser.cpp(24): error: expected an expression
buf_end = (byte *) string + strlen(string);
^
Parser\parser.cpp(24): error: expected a ";"
buf_end = (byte *) string + strlen(string);
^
compilation aborted for Parser\parser.cpp (code 2)
PS: If you find any other mistake or anything that needs to be changed(including a way to get rid of the goto) please let me know.