Browse Source

DTC: Incorporate some review suggestions.

- Change include syntax to:  /include/ "filename"
    - Move private functions directly into dtc-lexer.l
    - Define YYID for some older parser templates

Also fix a #include ordering problem around YYLTPE.

Signed-off-by; Jon Loeliger <jdl@freescale.com>
Acked-by: Haiying Wang <Haiying.Wang@freescale.com>
main
Jon Loeliger 18 years ago
parent
commit
ce34ae3b23
  1. 111
      dtc-lexer.l
  2. 7
      srcpos.h
  3. 138
      srcposstack.h

111
dtc-lexer.l

@ -33,8 +33,8 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) @@ -33,8 +33,8 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])

%{
#include "dtc.h"
#include "srcpos.h"
#include "dtc-parser.tab.h"
#include "srcposstack.h"


/*#define LEXDEBUG 1*/
@ -51,9 +51,8 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) @@ -51,9 +51,8 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])

%%

#[ \t]*include BEGIN(INCLUDE);
"/include/" BEGIN(INCLUDE);

<INCLUDE>[ \t]* /* whitespace before file name */
<INCLUDE>\"[^"\n]*\" {
yytext[strlen(yytext) - 1] = 0;
if (!push_input_file(yytext + 1)) {
@ -233,3 +232,109 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) @@ -233,3 +232,109 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
}

%%


/*
* Stack of nested include file contexts.
*/

struct incl_file {
int filenum;
FILE *file;
YY_BUFFER_STATE yy_prev_buf;
int yy_prev_lineno;
struct incl_file *prev;
};

struct incl_file *incl_file_stack;


/*
* Detect infinite include recursion.
*/
#define MAX_INCLUDE_DEPTH (100)

static int incl_depth = 0;


int push_input_file(const char *filename)
{
FILE *f;
struct incl_file *incl_file;

if (!filename) {
yyerror("No include file name given.");
return 0;
}

if (incl_depth++ >= MAX_INCLUDE_DEPTH) {
yyerror("Includes nested too deeply");
return 0;
}

f = dtc_open_file(filename);

incl_file = malloc(sizeof(struct incl_file));
if (!incl_file) {
yyerror("Can not allocate include file space.");
return 0;
}

/*
* Save current context.
*/
incl_file->yy_prev_buf = YY_CURRENT_BUFFER;
incl_file->yy_prev_lineno = yylineno;
incl_file->filenum = srcpos_filenum;
incl_file->file = yyin;
incl_file->prev = incl_file_stack;

incl_file_stack = incl_file;

/*
* Establish new context.
*/
srcpos_filenum = lookup_file_name(filename, 0);
yylineno = 1;
yyin = f;
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));

return 1;
}


int pop_input_file(void)
{
struct incl_file *incl_file;

if (incl_file_stack == 0)
return 0;

fclose(yyin);

/*
* Pop.
*/
--incl_depth;
incl_file = incl_file_stack;
incl_file_stack = incl_file->prev;

/*
* Recover old context.
*/
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(incl_file->yy_prev_buf);
yylineno = incl_file->yy_prev_lineno;
srcpos_filenum = incl_file->filenum;
yyin = incl_file->file;

/*
* Free old state.
*/
free(incl_file);

if (YY_CURRENT_BUFFER == 0)
return 0;

return 1;
}

7
srcpos.h

@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
* array of all opened filenames.
*/

#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
#if ! defined(YYLTYPE) && ! defined(YYLTYPE_IS_DECLARED)
typedef struct YYLTYPE {
int first_line;
int first_column;
@ -35,6 +35,10 @@ typedef struct YYLTYPE { @@ -35,6 +35,10 @@ typedef struct YYLTYPE {
#define YYLTYPE_IS_TRIVIAL 1
#endif

/* Cater to old parser templates. */
#ifndef YYID
#define YYID(n) (n)
#endif

#define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
@ -68,4 +72,3 @@ extern FILE *dtc_open_file(const char *fname); @@ -68,4 +72,3 @@ extern FILE *dtc_open_file(const char *fname);
extern int lookup_file_name(const char *fname, int add_it);
extern const char *srcpos_filename_for_num(int filenum);
const char *srcpos_get_filename(void);


138
srcposstack.h

@ -1,138 +0,0 @@ @@ -1,138 +0,0 @@
/*
* Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/

#include "srcpos.h"


/*
* This code should only be included into the lexical analysis.
* It references global context symbols that are only present
* in the generated lex.yy,c file.
*/

#ifdef FLEX_SCANNER


/*
* Stack of nested include file contexts.
*/

struct incl_file {
int filenum;
FILE *file;
YY_BUFFER_STATE yy_prev_buf;
int yy_prev_lineno;
struct incl_file *prev;
};

struct incl_file *incl_file_stack;


/*
* Detect infinite include recursion.
*/
#define MAX_INCLUDE_DEPTH (100)

static int incl_depth = 0;



int push_input_file(const char *filename)
{
FILE *f;
struct incl_file *incl_file;

if (!filename) {
yyerror("No include file name given.");
return 0;
}

if (incl_depth++ >= MAX_INCLUDE_DEPTH) {
yyerror("Includes nested too deeply");
return 0;
}

f = dtc_open_file(filename);

incl_file = malloc(sizeof(struct incl_file));
if (!incl_file) {
yyerror("Can not allocate include file space.");
return 0;
}

/*
* Save current context.
*/
incl_file->yy_prev_buf = YY_CURRENT_BUFFER;
incl_file->yy_prev_lineno = yylineno;
incl_file->filenum = srcpos_filenum;
incl_file->file = yyin;
incl_file->prev = incl_file_stack;

incl_file_stack = incl_file;

/*
* Establish new context.
*/
srcpos_filenum = lookup_file_name(filename, 0);
yylineno = 1;
yyin = f;
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));

return 1;
}


int pop_input_file(void)
{
struct incl_file *incl_file;

if (incl_file_stack == 0)
return 0;

fclose(yyin);

/*
* Pop.
*/
--incl_depth;
incl_file = incl_file_stack;
incl_file_stack = incl_file->prev;

/*
* Recover old context.
*/
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(incl_file->yy_prev_buf);
yylineno = incl_file->yy_prev_lineno;
srcpos_filenum = incl_file->filenum;
yyin = incl_file->file;

/*
* Free old state.
*/
free(incl_file);

if (YY_CURRENT_BUFFER == 0)
return 0;

return 1;
}

#endif /* FLEX_SCANNER */
Loading…
Cancel
Save