* Provide an abstraction of YY_INPUT so that we have a single definition which we can use in multiple lexers which can read from both in-memory buffers as well as PhysicsFS files

* Utilise this new version of YY_INPUT in script_parser and script_lexer


git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5008 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-05-10 00:35:24 +00:00
parent 2bc35a5714
commit 701f65469c
8 changed files with 119 additions and 22 deletions

View File

@ -13,11 +13,12 @@ CLEANFILES = resource_lexer.lex.c strres_lexer.lex.c resource_parser.tab.c strre
noinst_LIBRARIES = libframework.a
noinst_HEADERS = configfile.h cursors.h debug.h frame.h frameint.h \
frameresource.h input.h listmacs.h math-help.h printf_ext.h resly.h \
strlfuncs.h strnlen1.h strres.h strresly.h treap.h treapint.h trig.h \
types.h tagfile.h i18n.h file.h physfs_ext.h
frameresource.h input.h lexer_input.h listmacs.h math-help.h \
printf_ext.h resly.h strlfuncs.h strnlen1.h strres.h strresly.h treap.h \
treapint.h trig.h types.h tagfile.h i18n.h file.h physfs_ext.h
libframework_a_SOURCES = SDL_framerate.c configfile.c cursors.c cursors16.c \
cursors32.c debug.c frame.c frameresource.c input.c printf_ext.c \
resource_lexer.lex.c resource_parser.tab.c strnlen1.c strres.c \
strres_lexer.lex.c strres_parser.tab.c treap.c trig.c tagfile.c i18n.c
cursors32.c debug.c frame.c frameresource.c input.c lexer_input.c \
printf_ext.c resource_lexer.lex.c resource_parser.tab.c strnlen1.c \
strres.c strres_lexer.lex.c strres_parser.tab.c treap.c trig.c \
tagfile.c i18n.c

View File

@ -10,6 +10,7 @@ SRC=configfile.c \
frameresource.c \
input.c \
i18n.c \
lexer_input.c \
printf_ext.c \
resource_parser.tab.c \
resource_lexer.lex.c \

View File

@ -201,6 +201,10 @@
RelativePath=".\input.c"
>
</File>
<File
RelativePath=".\lexer_input.c"
>
</File>
<File
RelativePath=".\printf_ext.c"
>
@ -309,6 +313,10 @@
RelativePath=".\input.h"
>
</File>
<File
RelativePath=".\lexer_input.h"
>
</File>
<File
RelativePath=".\listmacs.h"
>

View File

@ -0,0 +1,42 @@
#include "frame.h"
#include "lexer_input.h"
int lexer_input(lexerinput_t* input, char* buf, size_t max_size, int nullvalue)
{
switch (input->type)
{
case LEXINPUT_PHYSFS:
if (PHYSFS_eof(input->input.physfsfile))
{
buf[0] = EOF;
return nullvalue;
}
else
{
int result = PHYSFS_read(input->input.physfsfile, buf, 1, max_size);
if (result == -1)
{
buf[0] = EOF;
return nullvalue;
}
return result;
}
break;
case LEXINPUT_BUFFER:
if (input->input.buffer.begin != input->input.buffer.end)
{
buf[0] = *input->input.buffer.begin++;
return 1;
}
else
{
buf[0] = EOF;
return nullvalue;
}
break;
}
ASSERT(!"Invalid input type!", "Invalid input type used for lexer (numeric value: %u)", (unsigned int)input->type);
return nullvalue;
}

View File

@ -0,0 +1,39 @@
#ifndef __INCLUDED_LIB_FRAMEWORK_LEXER_INPUT_H__
#define __INCLUDED_LIB_FRAMEWORK_LEXER_INPUT_H__
#include <physfs.h>
typedef struct
{
union
{
PHYSFS_file* physfsfile;
struct
{
const char* begin;
const char* end;
} buffer;
} input;
enum
{
LEXINPUT_PHYSFS,
LEXINPUT_BUFFER,
} type;
} lexerinput_t;
#ifdef YY_EXTRA_TYPE
# undef YY_EXTRA_TYPE
#endif
#define YY_EXTRA_TYPE lexerinput_t *
extern int lexer_input(lexerinput_t* input, char* buf, size_t max_size, int nullvalue);
#define YY_INPUT(buf, result, max_size) \
do \
{ \
result = lexer_input(yyextra, buf, max_size, YY_NULL); \
} while(0)
#endif // __INCLUDED_LIB_FRAMEWORK_LEXER_INPUT_H__

View File

@ -47,21 +47,7 @@ static UDWORD currText=0;
// Note if we are in a comment
static BOOL inComment = false;
/* Handle to the input file */
#define YY_EXTRA_TYPE PHYSFS_file *
#define YY_INPUT(buf,result,max_size) \
if (PHYSFS_eof(yyextra)) \
{ \
buf[0] = EOF; result = YY_NULL; \
} \
else { \
result = PHYSFS_read(yyextra, buf, 1, max_size); \
if (result == -1) \
{ \
buf[0] = EOF; result = YY_NULL; \
} \
}
#include "lib/framework/lexer_input.h"
#ifndef yyextra
# define yyextra yyget_extra()

View File

@ -32,6 +32,8 @@
#include "lib/sound/audio.h"
#include "scriptvals.h"
#include "lib/framework/lexer_input.h"
#include "scriptvals_parser.tab.h"
#include "scriptvals_lexer.lex.h"
#include "src/scripttabs.h"
#include "src/objects.h"
@ -830,7 +832,12 @@ BOOL scrvLookUpArray(const char *pIdent, UDWORD *pIndex)
// Load a script value file
BOOL scrvLoad(PHYSFS_file* fileHandle)
{
scrv_set_extra(fileHandle);
lexerinput_t input;
input.type = LEXINPUT_PHYSFS;
input.input.physfsfile = fileHandle;
scrv_set_extra(&input);
if (scrv_parse() != 0)
{

View File

@ -354,6 +354,19 @@
<Option target="DBGWindows" />
<Option target="NDBGWindows" />
</Unit>
<Unit filename="lib/framework/lexer_input.c">
<Option compilerVar="CC" />
<Option target="DBGUnix" />
<Option target="NDBGUnix" />
<Option target="DBGWindows" />
<Option target="NDBGWindows" />
</Unit>
<Unit filename="lib/framework/lexer_input.h">
<Option target="DBGUnix" />
<Option target="NDBGUnix" />
<Option target="DBGWindows" />
<Option target="NDBGWindows" />
</Unit>
<Unit filename="lib/framework/listmacs.h">
<Option target="DBGUnix" />
<Option target="NDBGUnix" />