SBSPSS/Utils/Scripter/mylexer.cpp

295 lines
5.0 KiB
C++
Raw Normal View History

2000-12-11 23:25:12 +01:00
/*=========================================================================
mylexer.cpp
Author: PKG
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
/*----------------------------------------------------------------------
Includes
-------- */
2000-12-12 17:29:42 +01:00
#ifndef _LEXER_H
2000-12-11 23:25:12 +01:00
#include "lexer.h"
2000-12-12 17:29:42 +01:00
#endif
#ifndef _PARSER_H
2000-12-11 23:25:12 +01:00
#include "parser.h"
2000-12-12 17:29:42 +01:00
#endif
#ifndef __PFILE_H__
#include "pfile.h"
#endif
2000-12-11 23:25:12 +01:00
2000-12-12 21:51:59 +01:00
#ifndef __PREPRO_H__
#include "prepro.h"
#endif
2000-12-11 23:25:12 +01:00
/* Std Lib
------- */
#include <yacc.h>
2000-12-12 21:51:59 +01:00
#include <string.h>
2000-12-11 23:25:12 +01:00
/* Data
---- */
/*----------------------------------------------------------------------
Tyepdefs && Defines
------------------- */
/*----------------------------------------------------------------------
Structure defintions
-------------------- */
/*----------------------------------------------------------------------
Function Prototypes
------------------- */
/*----------------------------------------------------------------------
Vars
---- */
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int mylexer::openInputFile(char *_filename)
{
return openPFile(_filename);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int mylexer::closeInputFile()
{
return closePFile();
}
2000-12-12 17:29:42 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void mylexer::error()
{
2000-12-22 18:10:46 +01:00
CPFile *cp;
cp=CPFile::getCurrentFile();
if(cp)
{
cp->error(yyerr);
}
else
{
// Hmm.. this happens when you forget '*/' on the end of a comment
}
2000-12-12 21:51:59 +01:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int mylexer::getErrorCount()
{
CPFile *cpf;
cpf=CPFile::getCurrentFile();
if(cpf)
{
return cpf->getErrorCount();
}
else
{
return 0;
}
2000-12-12 17:29:42 +01:00
}
2000-12-11 23:25:12 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int mylexer::yygetchar()
{
2000-12-12 21:51:59 +01:00
CPFile *cpf;
2000-12-11 23:25:12 +01:00
int ret;
2000-12-12 21:51:59 +01:00
cpf=CPFile::getCurrentFile();
if(cpf)
{
ret=cpf->readChar();
}
else
{
ret=-1;
}
// Force compilation to stop after finding errors ( hmm.. )
cpf=CPFile::getCurrentFile();
if(cpf)
2000-12-11 23:25:12 +01:00
{
2000-12-12 21:51:59 +01:00
if(cpf->getErrorCount())
2000-12-11 23:25:12 +01:00
{
2000-12-12 21:51:59 +01:00
printf("Stopping compilation due to errors!\n");
ret=-1;
}
}
return ret;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void mylexer::unexpectedChar()
{
int result;
char name[256]="\0";
2001-01-05 20:25:07 +01:00
char *endOfMacro=name;
2000-12-12 21:51:59 +01:00
char *replacement;
int nextChar;
char c[2]="*"; // Err...
2001-01-05 20:25:07 +01:00
int extraChars=0;
// Search for a macro
2000-12-12 21:51:59 +01:00
strcat(name,yytext);
2001-01-05 20:25:07 +01:00
yyunput(*(name+strlen(name)-1));
name[strlen(name)-1]='\0';
while(1)
2000-12-12 21:51:59 +01:00
{
2001-01-05 20:25:07 +01:00
do
2000-12-12 21:51:59 +01:00
{
2001-01-05 20:25:07 +01:00
nextChar=yyinput();
if(nextChar!=-1)
2000-12-11 23:25:12 +01:00
{
2001-01-05 20:25:07 +01:00
if(strlen(name)>=255)
{
printf("OVERFLOW WHILE LOOKING UP MACRO\n");
error();
return;
}
c[0]=(char)nextChar;
strcat(name,c);
}
else
{
printf("END OF FILE WHILE LOOKING FOR MACRO\n");
2000-12-12 21:51:59 +01:00
error();
return;
2000-12-11 23:25:12 +01:00
}
2001-01-05 20:25:07 +01:00
lookupMacro(name,&result);
}
while(result==POSSIBLE_KNOWN_MACRO);
if(result==UNKNOWN_MACRO)
{
if(endOfMacro!=name)
{
char *cp;
cp=name+strlen(name)-1;
while(cp!=endOfMacro-1)
{
yyunput(*cp--);
name[strlen(name)-1]='\0';
}
}
break;
2000-12-11 23:25:12 +01:00
}
else
{
2001-01-05 20:25:07 +01:00
endOfMacro=name+strlen(name);
2000-12-11 23:25:12 +01:00
}
2000-12-12 21:51:59 +01:00
}
2000-12-11 23:25:12 +01:00
2001-01-05 20:25:07 +01:00
replacement=lookupMacro(name,&result);
2000-12-12 21:51:59 +01:00
if(result==KNOWN_MACRO)
{
// Err.. shove the string into the input buffer ( is this good? )
char *ptr;
ptr=replacement+strlen(replacement);
while(ptr!=replacement)
2000-12-11 23:25:12 +01:00
{
2000-12-12 21:51:59 +01:00
yyunput(*--ptr);
2000-12-11 23:25:12 +01:00
}
}
}
2000-12-22 18:10:46 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void mylexer::comment()
{
typedef enum CommentState { INCOMMENT,FOUNDASTERISK,OUTOFCOMMENT };
int nextChar;
CommentState commentState=INCOMMENT;
do
{
nextChar=yygetchar();
if(nextChar==-1)
{
printf("END OF FILE FOUND INSIDE COMMENT\n");
error();
return;
}
switch(commentState)
{
case INCOMMENT:
if(nextChar=='*')
{
commentState=FOUNDASTERISK;
}
break;
case FOUNDASTERISK:
if(nextChar=='/')
{
commentState=OUTOFCOMMENT;
}
else
{
commentState=INCOMMENT;
}
break;
default:
break;
}
}
while(commentState!=OUTOFCOMMENT);
}
2000-12-11 23:25:12 +01:00
/*===========================================================================
end */