예제 #1
0
파일: fstack.c 프로젝트: selb/rgbds-linux
ULONG fstk_RunInclude(char *s)
{
	FILE *f;
	char tzFileName[_MAX_PATH + 1];

	//printf( "INCLUDE: %s\n", s );

	strcpy(tzFileName, s);
	fstk_FindFile(tzFileName);
	//printf( "INCLUDING: %s\n", tzFileName );

	if ((f = fopen(tzFileName, "rt")) != NULL) {
		pushcontext();
		nLineNo = 1;
		nCurrentStatus = STAT_isInclude;
		strcpy(tzCurrentFileName, tzFileName);
		pCurrentFile = f;
		CurrentFlexHandle = yy_create_buffer(pCurrentFile);
		yy_switch_to_buffer(CurrentFlexHandle);

		//      Dirty hack to give the INCLUDE directive a linefeed

		yyunput('\n');
		nLineNo -= 1;

		return (1);
	} else
		return (0);
}
예제 #2
0
/** Setup the input buffer state to scan directly from a user-specified character buffer.
 * @param base the character buffer
 * @param size the size in bytes of the character buffer
 * 
 * @return the newly allocated buffer state object. 
 */
YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
{
	YY_BUFFER_STATE b;
    
	if ( size < 2 ||
	     base[size-2] != YY_END_OF_BUFFER_CHAR ||
	     base[size-1] != YY_END_OF_BUFFER_CHAR )
		/* They forgot to leave room for the EOB's. */
		return 0;

	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
	if ( ! b )
		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );

	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
	b->yy_buf_pos = b->yy_ch_buf = base;
	b->yy_is_our_buffer = 0;
	b->yy_input_file = 0;
	b->yy_n_chars = b->yy_buf_size;
	b->yy_is_interactive = 0;
	b->yy_at_bol = 1;
	b->yy_fill_buffer = 0;
	b->yy_buffer_status = YY_BUFFER_NEW;

	yy_switch_to_buffer(b  );

	return b;
}
예제 #3
0
파일: Main.c 프로젝트: Winter-M/Trot
pt_node *buildAST(const char *expr) {
	extern int yyparse(pt_node **, yyscan_t);

	FILE *file;
	yyscan_t scanner;
	pt_node *node = NULL;
	YY_BUFFER_STATE state;

	if(yylex_init(&scanner)) {
		fprintf(stderr, "Failed to initialize parser.\n");
		exit(-1);
	}

	file = fopen(expr, "r");
	if(file == NULL) {
		perror("fopen");
		exit(-1);
	}

	state = yy_create_buffer(file, 1024, scanner);
	yy_switch_to_buffer(state, scanner);

	if(yyparse(&node, scanner)) {
		fprintf(stderr, "Error parsing input.\n");
		exit(-1);
	}

	yy_delete_buffer(state, scanner);
	yylex_destroy(scanner);

	return node;
}
예제 #4
0
int	yywrap() {
	
	if (!include_app) {	/* all input files and strings get a newline appended to them */
		include_app = 1;
		yy_scan_string("\n");
		return 0;
	} else {
		include_app = 0;
		if (inpfile) {	/* (this is just an informal input text for printing-out...) */
			FREE(inpfile);  inpfile = NULL;
		}
			/* delete the input buffer, and possibly close the input file */
		yy_delete_buffer(YY_CURRENT_BUFFER);
#if CLOSE_AFTER_FLEX>0
		if (include_stack_file[include_stack_ptr])
			fclose(include_stack_file[include_stack_ptr]);
#endif
			/* pop the previous saved state from the stack */
		if (--include_stack_ptr>=0) {
			yy_switch_to_buffer(include_stack[include_stack_ptr]);
			inpfile = include_stack_fn[include_stack_ptr];
			inpline = include_stack_ln[include_stack_ptr];
			yy_pop_state();
			return 0;
		} /*** else  yyterminate();	does not work ??? ***/
	}
	return 1;	/* (means the end of current input) */
}
예제 #5
0
파일: source.c 프로젝트: amirsalah/cs2007
int source_parse_file(source *src, const char *filename, const char *modname)
{
  YY_BUFFER_STATE bufstate;
  int r;

  if (NULL == (yyin = fopen(filename,"r"))) {
    perror(filename);
    return -1;
  }

  yyfilename = filename;
  yyfileno = add_parsedfile(src,filename);    ////yyfileno is the index in the src->parsedfiles
  yylloc.first_line = 1;

  bufstate = yy_create_buffer(yyin,YY_BUF_SIZE);
  yy_switch_to_buffer(bufstate);
  parse_src = src;
  parse_modname = modname ? modname : "";
  r = yyparse();
  yy_delete_buffer(bufstate);
#if HAVE_YYLEX_DESTROY
  yylex_destroy();
#endif

  if (yyin)
    fclose(yyin);

  return r;
}
예제 #6
0
int popFile(void)
{
		bufferStack * buffStack = curbs;
	bufferStack * previousBuffStack = 0;

	if(!buffStack)
	{
		return -1;
	}

	//Switch back to previous
	previousBuffStack = buffStack->previous;
	free(buffStack);

	if(!previousBuffStack)
	{
		return 0;
	}

	yy_switch_to_buffer(previousBuffStack->bufferState);
	curbs = previousBuffStack;
	yylineno = curbs->lineno;
	currentFileName = curbs->fileName;
	
	return 1;
}
예제 #7
0
파일: fstack.c 프로젝트: michalisb/rgbds
/*
 * Set up an include file for parsing
 */
void
fstk_RunInclude(char *tzFileName)
{
	FILE *f;

	f = fstk_FindFile(tzFileName);

	if (f == NULL) {
		err(1, "Unable to open included file '%s'",
		    tzFileName);
	}

	pushcontext();
	nLineNo = 1;
	nCurrentStatus = STAT_isInclude;
	strcpy(tzCurrentFileName, tzFileName);
	pCurrentFile = f;
	CurrentFlexHandle = yy_create_buffer(pCurrentFile);
	yy_switch_to_buffer(CurrentFlexHandle);

	//Dirty hack to give the INCLUDE directive a linefeed

	yyunput('\n');
	nLineNo -= 1;
}
예제 #8
0
파일: entmgr.c 프로젝트: TEIC/Carthage
char *PCCloseentity(void) {
    char * pc;
    PEntRec per;

#ifdef DEBUGGING
    FILESTATUS(stdout,"stdout");
    MsgKwS(msgTRACE,"Closing the current entity ...",NULL);
    MsgKwS(msgTRACE,"PCCloseentity() called ...",NULL);
    FILESTATUS(stdout,"stdout");
    FILESTATUS(stdin,"stdin");
    FILESTATUS(stderr,"stderr");
    FILESTATUS(yyin,"yyin");
#endif

    if (cEntCur > 0) {
         per = rgEntities[cEntCur];
         pc = per->pcEntname;
         if (per->fExtern)
              fclose(yyin);
         /* close yyin.  We don't need to reset it,
            that happens in yy_switch_to_buffer, below */
         yy_delete_buffer(YY_CURRENT_BUFFER);
         yy_switch_to_buffer(rgBuffers[--cEntCur]);
         /* reset pcEntpos and fRdexternal */
         if (cEntCur > 0) {
              per = rgEntities[cEntCur];
              if (per->fExtern) {
                   fRdexternal = 1;
                   pcEntpos = "";
              } else {
                   fRdexternal = 0;
                   pcEntpos = rgPCPos[cEntCur];
              }
         } else { /* back to stdin */
              fRdexternal = 1;
              pcEntpos = "";
         }
         cLinecount = rgILine[cEntCur];
         fnCurrent  = rgPCFn[cEntCur];


#ifdef DEBUGGING
    FILESTATUS(stdout,"stdout");
    FILESTATUS(stdin,"stdin");
    FILESTATUS(stderr,"stderr");
    FILESTATUS(yyin,"yyin");
#endif

         return pc;
    } else {
#ifdef DEBUGGING
    FILESTATUS(stdout,"stdout");
    FILESTATUS(stdin,"stdin");
    FILESTATUS(stderr,"stderr");
    FILESTATUS(yyin,"yyin");
#endif
         return NULL;
    }
}
예제 #9
0
void
lexer_set_buffer(RemarkFile *rf)
{
	yyin = rf->file;
	file = rf;
	yy_switch_to_buffer(yy_create_buffer(rf->file,YY_BUF_SIZE));
	rf->lex_buffer = YY_CURRENT_BUFFER;
}
예제 #10
0
void
lexer_restore_buffer(RemarkFile *rf)
{
	file = rf;
	yy_delete_buffer(YY_CURRENT_BUFFER);
	if (rf != NULL)
		yy_switch_to_buffer(rf->lex_buffer);
}
예제 #11
0
Ast* Interpreter::parse(const std::string& input) const
{
    YY_BUFFER_STATE buffer = yy_scan_string(input.c_str());
    yy_switch_to_buffer(buffer);
    yyparse();
    yylex_destroy();

    return m_ast;
}
예제 #12
0
TEST(BisonFlexTest, Test0009)
{
    FileHolder holder("./Resources/Example0009.titan");
    GTEST_ASSERT_NE((void*)0, holder.f);
    BufferStateHolder bp(yy_create_buffer(holder.f, YY_BUF_SIZE));
    yy_switch_to_buffer(bp.bp);
    int ans = yyparse();
    yy_flush_buffer(bp.bp);
    GTEST_ASSERT_EQ(0, ans);
}
예제 #13
0
void parseFile(FILE* in)
{
  YY_BUFFER_STATE* tmp =  new YY_BUFFER_STATE;
  *tmp = yy_create_buffer(in, YY_BUF_SIZE);
  yy_switch_to_buffer(*tmp);
  if(buffer)
    yy_delete_buffer(*buffer);
    delete buffer;
  buffer = tmp;
}
예제 #14
0
static VALUE
parse_string(VALUE self, VALUE code) {
  VALUE lineno = rb_funcall(self, rb_intern("lineno"), 0);
  char *str = StringValueCStr(code);
  YY_BUFFER_STATE buffstate = yy_scan_string(str);
  yy_switch_to_buffer(buffstate);
  yylineno = NUM2INT(lineno);
  yyparse(self);
  yy_delete_buffer(buffstate);
  return self;
}
예제 #15
0
파일: fstack.c 프로젝트: selb/rgbds-linux
void fstk_RunString(char *s)
{
	struct sSymbol *pSym;

	if ((pSym = sym_FindSymbol(s)) != NULL) {
		pushcontext();
		nCurrentStatus = STAT_isMacroArg;
		strcpy(tzCurrentFileName, s);
		CurrentFlexHandle =
		    yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro));
		yy_switch_to_buffer(CurrentFlexHandle);
	} else
		yyerror("No such string symbol");
}
예제 #16
0
/**********************************************
 * SEMANTIC ANALYSIS
 **********************************************/
semantic_analysis::error_id execute_test(std::string program){
	semantic_analysis v;

	YY_BUFFER_STATE program_buffer = yy_scan_string(program.c_str());
	yy_switch_to_buffer(program_buffer);

	yyparse();

	ast->accept(v);

	yy_delete_buffer(program_buffer);

	return v.get_last_error();
}
예제 #17
0
int	yyincl(char *fn, int fil) {
	char		*s;

	DEBUG(CURDLEV+1,"Going to include (ptr=%d) %s \'%s\'.\n",include_stack_ptr,fil?"file":"string",fn);
	if (include_stack_ptr>=MAX_INCLUDE_DEPTH || include_stack_ptr<-MAX_INCLUDE_DEPTH) {
		PROGERROR("Includes are nested too deeply! %d",include_stack_ptr);
		exit(-1);
	}
	if (include_stack_ptr<0) {
		include_stack_ptr = -1;	 include_app = 0;
		inpfile = NULL;  include_stack_file[0] = NULL;
	}
			/* opens the given include file for reading */
	if (fil) {
		if (include_stack_ptr>0)	/* (tries also the subdir of the previous include) */
			s = fdir_extract(include_stack_fn[include_stack_ptr-1]);
		else  s = NULL;
		yyin = fopen_path(fn,"r",frame_path_read,s,frame_extension);
		//********* add stdin as an input "-" (interactive input) ........
		if (!yyin) {
			LERROR("Cannot open include file \'%s\'!",fn);
			return -1;
		}
		if (!FRNAME(curframe)) {
			frame_setname(curframe,s=fname_extract(fn));
			FREE(s);	/* (this s is a private copy of the string) */
		}
	}
	if (include_stack_ptr>=0) {	/* stores the current buffer, and opens a new buffer */
		include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
	}
	if (fil) {
		yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
		if (include_stack_ptr+1>=0)  include_stack_file[include_stack_ptr+1] = yyin;
	} else {
		yy_scan_string(fn);
		if (include_stack_ptr+1>=0)  include_stack_file[include_stack_ptr+1] = NULL;
	}
	if (include_stack_ptr>=0) {	/* stores the whole current state on the stack */
		yy_push_state(INITIAL);
		include_stack_fn[include_stack_ptr] = inpfile;
		include_stack_ln[include_stack_ptr] = inpline;
	}
	inpfile = MSTRDUP(fn);	/* (freed in yywrap()) */
	inpline = 1;
	include_stack_ptr++;
	inpspecial = !fil;	/* whether special-lines should apply to this include - see SPECMATRIXLINE */
	return 0;
}
예제 #18
0
파일: fstack.c 프로젝트: selb/rgbds-linux
void fstk_RunRept(ULONG count)
{
	if (count) {
		pushcontext();
		sym_UseCurrentMacroArgs();
		sym_SetMacroArgID(nMacroCount++);
		sym_UseNewMacroArgs();
		nCurrentREPTBlockCount = count;
		nCurrentStatus = STAT_isREPTBlock;
		nCurrentREPTBlockSize = ulNewMacroSize;
		pCurrentREPTBlock = tzNewMacro;
		CurrentFlexHandle =
		    yy_scan_bytes(pCurrentREPTBlock, nCurrentREPTBlockSize);
		yy_switch_to_buffer(CurrentFlexHandle);
	}
}
예제 #19
0
static VALUE
parse_file(VALUE self) {
  VALUE filename = rb_funcall(self, rb_intern("filename"), 0);
  VALUE lineno = rb_funcall(self, rb_intern("lineno"), 0);
  char *str = StringValueCStr(filename);
  FILE *f = fopen(str, "r");
  if(!f) {
    rb_funcall(self, rb_intern("file_error"), 2, rb_str_new2("Could not open file"), rb_str_new2(str));
    return Qnil;
  }
  YY_BUFFER_STATE buffstate = yy_create_buffer(f, YY_BUF_SIZE);
  yy_switch_to_buffer(buffstate);
  yylineno = NUM2INT(lineno);
  yyparse(self);
  yy_delete_buffer(buffstate);
  fclose(f);
  return self;
}
예제 #20
0
파일: fstack.c 프로젝트: selb/rgbds-linux
void fstk_RunMacroArg(SLONG s)
{
	char *sym;

	if (s == '@')
		s = -1;
	else
		s -= '0';

	if ((sym = sym_FindMacroArg(s)) != NULL) {
		pushcontext();
		nCurrentStatus = STAT_isMacroArg;
		sprintf(tzCurrentFileName, "%c", (UBYTE) s);
		CurrentFlexHandle = yy_scan_bytes(sym, strlen(sym));
		yy_switch_to_buffer(CurrentFlexHandle);
	} else
		fatalerror("No such macroargument");
}
예제 #21
0
void test_parser_2(){
	std::string full_test_program =
			"class Program {int x;}\n"
			"class main {void main() {}}\0\0"; // Double NULL termination (needed for flex)

	YY_BUFFER_STATE program_buffer = yy_scan_string(full_test_program.c_str());
	yy_switch_to_buffer(program_buffer);

	yyparse();

	std::cout << "7) Declaration of multiple classes: ";

	assert(ast->classes.size() == 2);

	std::cout << "OK" << std::endl;

	yy_delete_buffer(program_buffer);

	ast.reset();
}
예제 #22
0
파일: fstack.c 프로젝트: bentley/rgbds
/*
 * Set up a macroargument for parsing
 */
void fstk_RunMacroArg(int32_t s)
{
	char *sym;

	if (s == '@')
		s = -1;
	else
		s -= '0';

	sym = sym_FindMacroArg(s);

	if (sym == NULL)
		fatalerror("No such macroargument");

	pushcontext();
	nCurrentStatus = STAT_isMacroArg;
	snprintf(tzCurrentFileName, _MAX_PATH + 1, "%c", (uint8_t)s);
	CurrentFlexHandle = yy_scan_bytes(sym, strlen(sym));
	yy_switch_to_buffer(CurrentFlexHandle);
}
예제 #23
0
파일: TimeParser.c 프로젝트: chadcf/Moment
time_t parseDateTimeString(const char *str)
{
    yy_switch_to_buffer(yy_scan_string(str));
    yyparse();
    time_t tempTime = mktime(str_time);
    str_time= localtime(&tempTime);
    
    
    setFinalTime(&set_time, finalSpecAmount, fromChangeAmount);
    
    tempTime = mktime(str_time);
    str_time= localtime(&tempTime);
    if(fromModifier.specValue[1])
    {
        setDayOfWeek(fromModifier.specValue[1], fromModifier.specAmount);
    }
    
    return mktime(str_time);
    
}
예제 #24
0
파일: fstack.c 프로젝트: selb/rgbds-linux
ULONG fstk_RunMacro(char *s)
{
	struct sSymbol *sym;

	if ((sym = sym_FindMacro(s)) != NULL) {
		pushcontext();
		sym_SetMacroArgID(nMacroCount++);
		nLineNo = -1;
		sym_UseNewMacroArgs();
		nCurrentStatus = STAT_isMacro;
		strcpy(tzCurrentFileName, s);
		pCurrentMacro = sym;
		CurrentFlexHandle =
		    yy_scan_bytes(pCurrentMacro->pMacro,
				  pCurrentMacro->ulMacroSize);
		yy_switch_to_buffer(CurrentFlexHandle);
		return (1);
	} else
		return (0);
}
예제 #25
0
파일: fstack.c 프로젝트: selb/rgbds-linux
ULONG fstk_Init(char *s)
{
	char tzFileName[_MAX_PATH + 1];

	sym_AddString("__FILE__", s);

	strcpy(tzFileName, s);
	fstk_FindFile(tzFileName);

	pFileStack = NULL;
	if ((pCurrentFile = fopen(tzFileName, "rt")) != NULL) {
		nMacroCount = 0;
		nCurrentStatus = STAT_isInclude;
		strcpy(tzCurrentFileName, tzFileName);
		CurrentFlexHandle = yy_create_buffer(pCurrentFile);
		yy_switch_to_buffer(CurrentFlexHandle);
		nLineNo = 1;
		return (1);
	} else
		return (0);
}
예제 #26
0
파일: fstack.c 프로젝트: michalisb/rgbds
/*
 * Initialize the filestack routines
 */
void
fstk_Init(char *s)
{
	char tzFileName[_MAX_PATH + 1];

	sym_AddString("__FILE__", s);

	strcpy(tzFileName, s);
	pFileStack = NULL;
	pCurrentFile = fopen(tzFileName, "rb");
	if (pCurrentFile == NULL) {
		err(1, "Unable to open file '%s'", tzFileName);
	}

	nMacroCount = 0;
	nCurrentStatus = STAT_isInclude;
	strcpy(tzCurrentFileName, tzFileName);
	CurrentFlexHandle = yy_create_buffer(pCurrentFile);
	yy_switch_to_buffer(CurrentFlexHandle);
	nLineNo = 1;
}
예제 #27
0
/* Compile a file */
void compile_file(compiler_type *comp_void, char *file_name, bool include_baselib) {
  compiler_core_type *compiler = (compiler_core_type *)comp_void;
  ins_stream_type *baselib = 0; /* TODO: should be gc root */
  FILE *in = 0;
  char path[PATH_MAX];

  /* Actually parse the input stream. */
  yylex_init_extra(compiler, &(compiler->scanner));

  in = fopen(file_name, "r");
  if (!in) {
    (void)fprintf(stderr, "Error %i while attempting to open '%s'\n",
      errno, file_name);
      assert(0);
  }

  //yyset_in(in, compiler->scanner);
  yy_switch_to_buffer(
    yy_create_buffer(in, YY_BUF_SIZE, compiler->scanner), compiler->scanner);

  push_include_path(compiler, file_name);

  /* TODO: Need a better way to handle GC than leaking */
  gc_protect(compiler->gc);

  /* Inject include for base library */
  if (include_baselib) {
    strcpy(path, compiler->home);
    strcat(path, "/lib/baselib.scm");

    STREAM_NEW(baselib, string, path);
    setup_include(compiler, baselib); 
  }
  
  parse_internal(compiler, compiler->scanner);
  
  gc_unprotect(compiler->gc);

  yylex_destroy(compiler->scanner);
}
예제 #28
0
파일: fstack.c 프로젝트: bentley/rgbds
/*
 * Set up an include file for parsing
 */
void fstk_RunInclude(char *tzFileName)
{
	char *incPathUsed = "";
	FILE *f = fstk_FindFile(tzFileName, &incPathUsed);

	if (f == NULL)
		err(1, "Unable to open included file '%s'", tzFileName);

	pushcontext();
	nLineNo = 1;
	nCurrentStatus = STAT_isInclude;
	snprintf(tzCurrentFileName, sizeof(tzCurrentFileName), "%s%s",
		 incPathUsed, tzFileName);
	pCurrentFile = f;
	CurrentFlexHandle = yy_create_buffer(pCurrentFile);
	yy_switch_to_buffer(CurrentFlexHandle);

	/* Dirty hack to give the INCLUDE directive a linefeed */

	yyunput('\n');
	nLineNo -= 1;
}
예제 #29
0
파일: source.c 프로젝트: amirsalah/cs2007
int source_parse_string(source *src, const char *str, const char *filename, const char *modname)
{
  YY_BUFFER_STATE bufstate;
  int r;
  yyfilename = filename;
  yyfileno = add_parsedfile(src,filename);
  yylloc.first_line = 1;

  bufstate = yy_scan_string(str);
  yy_switch_to_buffer(bufstate);

  parse_src = src;
  parse_modname = modname ? modname : "";
  r = yyparse();

  yy_delete_buffer(bufstate);
#if HAVE_YYLEX_DESTROY
  yylex_destroy();
#endif

  return r;
}
예제 #30
0
/* nested input files */
int newFile(char * _fileName)
{
	FILE * file = fopen(_fileName, "r");
	bufferStack * buffStack = 0;

	if(!file)
	{
		perror(_fileName);
		return -1;
	}

	buffStack = malloc(sizeof(bufferStack));
	if(!buffStack)
	{
		perror("malloc");
		exit(1);
	}

	// remember state
	if(curbs)
	{
		curbs->lineno = yylineno;
	}

	buffStack->previous = curbs;
	buffStack->currentFile = file;
	buffStack->fileName = _fileName;

	// set up current entry
	buffStack->bufferState = yy_create_buffer(file, YY_BUF_SIZE);
	yy_switch_to_buffer(buffStack->bufferState);
	curbs = buffStack;
	yylineno = 1;
	currentFileName = _fileName;

	return 1;
}