Changes to preprocessor. More parsing improvements

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@906 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-09-29 20:52:45 +00:00
commit df8f29fe1b
7 changed files with 52 additions and 210 deletions

View file

@ -394,43 +394,19 @@ yylex1(void) {
if (strcmp(yytext,"%module") == 0) return(MODULE);
if (strcmp(yytext,"%constant") == 0) return (CONSTANT);
if (strcmp(yytext,"%type") == 0) return (TYPE);
if (strcmp(yytext,"%init") == 0) return(INIT);
if (strcmp(yytext,"%wrapper") == 0) return(WRAPPER);
if (strcmp(yytext,"%runtime") == 0) return(RUNTIME);
if (strcmp(yytext,"%readonly") == 0) return(READONLY);
if (strcmp(yytext,"%readwrite") == 0) return(READWRITE);
if (strcmp(yytext,"%name") == 0) return(NAME);
if (strcmp(yytext,"%rename") == 0) return(RENAME);
if (strcmp(yytext,"%includefile") == 0) return(INCLUDE);
if (strcmp(yytext,"%externfile") == 0) return(WEXTERN);
if (strcmp(yytext,"%file") == 0) return(FILEDIRECTIVE);
if (strcmp(yytext,"%insert") == 0) return (INSERT);
if (strcmp(yytext,"%checkout") == 0) return(CHECKOUT);
if (strcmp(yytext,"%macro") == 0) return(MACRO);
if (strcmp(yytext,"%section") == 0) return(SECTION);
if (strcmp(yytext,"%subsection") == 0) return(SUBSECTION);
if (strcmp(yytext,"%subsubsection") == 0) return(SUBSUBSECTION);
if (strcmp(yytext,"%title") == 0) return(TITLE);
if (strcmp(yytext,"%style") == 0) return(STYLE);
if (strcmp(yytext,"%localstyle") == 0) return(LOCALSTYLE);
if (strcmp(yytext,"%typedef") == 0) return(TYPEDEF);
if (strcmp(yytext,"%alpha") == 0) return(ALPHA_MODE);
if (strcmp(yytext,"%raw") == 0) return(RAW_MODE);
if (strcmp(yytext,"%text") == 0) return(TEXT);
if (strcmp(yytext,"%native") == 0) return(NATIVE);
if (strcmp(yytext,"%disabledoc") == 0) return(DOC_DISABLE);
if (strcmp(yytext,"%enabledoc") == 0) return(DOC_ENABLE);
if (strcmp(yytext,"%pragma") == 0) return(PRAGMA);
if (strcmp(yytext,"%addmethods") == 0) return(ADDMETHODS);
if (strcmp(yytext,"%inline") == 0) return(INLINE);
if (strcmp(yytext,"%typemap") == 0) return(TYPEMAP);
if (strcmp(yytext,"%except") == 0) return(EXCEPT);
if (strcmp(yytext,"%importfile") == 0) return(IMPORT);
if (strcmp(yytext,"%echo") == 0) return(ECHO);
if (strcmp(yytext,"%new") == 0) return(NEW);
if (strcmp(yytext,"%apply") == 0) return(APPLY);
if (strcmp(yytext,"%clear") == 0) return(CLEAR);
if (strcmp(yytext,"%doconly") == 0) return(DOCONLY);
if (strcmp(yytext,"%map") == 0) return(MAP);
}
/* Have an unknown identifier, as a last step, we'll */

View file

@ -231,10 +231,9 @@ static int promote(int t1, int t2) {
%token <tok> TYPE_INT TYPE_UNSIGNED TYPE_SHORT TYPE_LONG TYPE_FLOAT TYPE_DOUBLE TYPE_CHAR TYPE_VOID TYPE_SIGNED TYPE_BOOL
/* SWIG directives */
%token <tok> ADDMETHODS ALPHA_MODE APPLY CHECKOUT CLEAR CONSTANT DOCONLY DOC_DISABLE DOC_ENABLE ECHO EXCEPT
%token <tok> ILLEGAL IMPORT INCLUDE INIT INLINE LOCALSTYLE MACRO MODULE NAME NATIVE NEW PRAGMA INSERT
%token <tok> RAW_MODE READONLY READWRITE RENAME RUNTIME SECTION STYLE SUBSECTION SUBSUBSECTION TEXT TITLE
%token <tok> TYPE TYPEMAP USERDIRECTIVE WEXTERN WRAPPER MAP
%token <tok> ADDMETHODS APPLY CLEAR CONSTANT ECHO EXCEPT
%token <tok> ILLEGAL FILEDIRECTIVE INLINE MACRO MODULE NAME NATIVE PRAGMA INSERT
%token <tok> TYPE TYPEMAP MAP
/* Operators */
%left <tok> LOR
@ -250,7 +249,7 @@ static int promote(int t1, int t2) {
%left <tok> DCOLON
%type <tok> idstring template_decl cpptype expr definetype def_args storage_spec pragma_arg ename
%type <node> file_include_type parm parms ptail idlist stylelist styletail stars
%type <node> parm parms ptail idlist stars
%type <node> array array2
%type <node> type strict_type opt_signed opt_unsigned
%type <decl> declaration
@ -259,7 +258,7 @@ static int promote(int t1, int t2) {
%type <tmname> tm_name
%type <tok> tm_method
%type <node> statement swig_directive c_declaration
%type <node> file_include code_block doc_directive except_directive pragma_directive modifier_directive native_directive typemap_directive map_directive
%type <node> file_include code_block except_directive pragma_directive native_directive typemap_directive map_directive
%type <node> variable_decl function_decl enum_decl typedef_decl stail edecl typedeflist map_element
%type <nodelist> enumlist interface
%type <node> inherit base_list
@ -335,11 +334,6 @@ swig_directive : MODULE idstring {
LParse_set_location($7.filename,$7.line-1);
$$ = $9.node;
}
| RENAME ID ID SEMI {
$$ = new_node("rename",$2.filename,$2.line);
Setattr($$,"oldname",$2.text);
Setattr($$,"newname",$3.text);
}
| CONSTANT ID definetype SEMI {
$$ = new_node("constant",$2.filename, $2.line);
Setattr($$,ATTR_NAME,$2.text);
@ -385,10 +379,8 @@ swig_directive : MODULE idstring {
| echo_directive { $$ = 0; }
| file_include { $$ = $1; }
| code_block { $$ = $1; }
| doc_directive { $$ = $1; }
| except_directive { $$ = $1; }
| pragma_directive { $$ = $1; }
| modifier_directive { $$ = $1; }
| native_directive { $$ = $1; }
| typemap_directive { $$ = $1; }
| map_directive { $$ = $1; }
@ -401,36 +393,19 @@ echo_directive: ECHO HBLOCK { Printf(stderr,"%s\n", $2.text); }
/* -- File inclusion directives -- */
file_include : file_include_type STRING LBRACE {
Setattr($1,ATTR_NAME,$2.text);
$$ = $1;
LParse_set_location($2.text,0);
file_include : FILEDIRECTIVE LPAREN STRING RPAREN STRING LBRACE {
$$ = new_node("file",$1.filename,$1.line);
Setattr($$,ATTR_NAME,$5.text);
Setattr($$,"type",$3.text);
LParse_set_location($5.text,0);
} interface RBRACE {
LParse_set_location($3.filename,$3.line + 1);
if ($5.node) {
Setattr($$,ATTR_CHILD,$5.node);
setparent($$,$5.node);
LParse_set_location($6.filename,$6.line + 1);
if ($8.node) {
Setattr($$,ATTR_CHILD,$8.node);
setparent($$,$8.node);
}
}
file_include_type : INCLUDE { $$ = new_node("includefile",$1.filename,$1.line); }
| WEXTERN { $$ = new_node("externfile",$1.filename,$1.line); }
| IMPORT { $$ = new_node("importfile", $1.filename,$1.line); }
;
/* -- Modifier directives -- */
modifier_directive : READONLY { $$ = new_node("readonly",$1.filename, $1.line); }
| READWRITE { $$ = new_node("readwrite",$1.filename,$1.line); }
| NAME LPAREN idstring RPAREN {
$$ = new_node("name",$3.filename,$3.line);
Setattr($$,ATTR_NAME,$3.text);
}
| NEW {
$$ = new_node("new",$1.filename,$1.line);
}
;
/* -- Code inclusion directives -- */
code_block : INSERT LPAREN idstring RPAREN STRING {
@ -458,82 +433,6 @@ code_block : INSERT LPAREN idstring RPAREN STRING {
Seek(pp,0,SEEK_SET);
LParse_push(pp);
}
/*
code_block : HBLOCK {
$$ = new_node("headerblock",$1.filename,$1.line);
Setattr($$,"code", $1.text);
}
| WRAPPER HBLOCK {
$$ = new_node("wrapperblock",$2.filename,$2.line);
Setattr($$,"code",$2.text);
}
| INIT HBLOCK {
$$ = new_node("initblock",$2.filename,$2.line);
Setattr($$,"code",$2.text);
}
| INLINE HBLOCK {
DOH *pp;
$$ = new_node("headerblock",$2.filename,$2.line);
Setattr($$,"code", $2.text);
Seek($2.text,0,SEEK_SET);
pp = Preprocessor_parse($2.text);
Seek(pp,0,SEEK_SET);
LParse_push(pp);
}
| RUNTIME HBLOCK {
$$ = new_node("runtimeblock",$2.filename,$2.line);
Setattr($$,"code",$2.text);
}
;
*/
/* -- Documentation directives -- */
doc_directive : DOC_ENABLE { $$ = 0; }
| DOC_DISABLE { $$ = 0; }
/* %title directive */
| TITLE STRING styletail { $$ = 0; }
/* %section directive */
| SECTION STRING styletail { $$ = 0; }
/* %subsection directive */
| SUBSECTION STRING styletail { $$ = 0; }
/* %subsubsection directive */
| SUBSUBSECTION STRING styletail { $$ = 0; }
/* %text directive */
| TEXT HBLOCK { $$ = 0; }
/* Disable code generation */
| DOCONLY { $$ = 0; }
/* %style directive. This applies to all current styles */
| STYLE stylelist { $$ = 0; }
/* %localstyle directive. This applies only to the current style */
| LOCALSTYLE stylelist { $$ = 0; }
/* Documentation style list */
stylelist : ID stylearg styletail { $$ = 0; }
;
styletail : styletail COMMA ID stylearg { $$ = 0; }
| empty { $$ = 0; }
;
stylearg : EQUAL NUM_INT { }
| EQUAL STRING { }
| empty { }
;
idstring : ID { $$ = $1; }
@ -546,7 +445,7 @@ idstring : ID { $$ = $1; }
except_directive: EXCEPT LPAREN ID RPAREN LBRACE {
DOH *t;
t = LParse_skip_balanced('{','}');
$$ = new_node("exceptiondirective",$1.filename,$1.line);
$$ = new_node("exception",$1.filename,$1.line);
Setattr($$,"lang",$3.text);
Setattr($$,"code",t);
}
@ -555,19 +454,19 @@ except_directive: EXCEPT LPAREN ID RPAREN LBRACE {
| EXCEPT LBRACE {
DOH *t;
t = LParse_skip_balanced('{','}');
$$ = new_node("exceptiondirective",$1.filename,$1.line);
$$ = new_node("exception",$1.filename,$1.line);
Setattr($$,"code",t);
}
/* Clear an exception */
| EXCEPT LPAREN ID RPAREN SEMI {
$$ = new_node("exceptiondirective",$1.filename,$1.line);
$$ = new_node("exception",$1.filename,$1.line);
Setattr($$,"lang",$3.text);
}
/* Generic clear */
| EXCEPT SEMI {
$$ = new_node("exceptiondirective",$1.filename,$1.line);
$$ = new_node("exception",$1.filename,$1.line);
}
;

View file

@ -30,11 +30,6 @@ tag : "module"
syntax : %module idstring
attributes : "name" - Module name
tag : "rename"
syntax : %rename id1 id2;
attributes : "oldname" - Old name (id1)
"newname" - New name (id2)
tag : "constant"
syntax : %constant name value;
attributes : "name" - Constant name
@ -53,22 +48,6 @@ tag : "importfile"
syntax : %import filename
attributes : None
tag : "readonly"
syntax : %readonly
attributes : None
tag : "readwrite"
syntax : %readwrite
attributes : None
tag : "name"
syntax : %name(idstring) decl
attributes : "name" - New name
tag : "new"
syntax : %new decl
attributes : None
tag : "insert"
syntax : %insert(section) "filename"
%insert(section) %{ code %}
@ -78,7 +57,6 @@ attributes : "section" - Code section
"filename" - File to include (if given)
"code" - Code to include (if given)
tag : "pragma"
syntax : %pragma name [ value ];
%pragma(lang) name [ value ];

View file

@ -699,6 +699,7 @@ static int
check_id(DOH *s)
{
int c, state = 0;
int hasvalue = 0;
Seek(s,0,SEEK_SET);
while ((c = Getc(s)) != EOF) {
switch(state) {
@ -706,12 +707,17 @@ check_id(DOH *s)
case 0:
if (isdigit(c)) state = 1;
else if (isidentifier(c)) return 1;
else if (c == '\"') skip_tochar(s,'\"',0);
else if (c == '\'') skip_tochar(s,'\'',0);
else if (c == '/') state = 3;
else if (c == '\"') {
skip_tochar(s,'\"',0);
hasvalue = 1;
} else if (c == '\'') {
skip_tochar(s,'\'',0);
hasvalue = 1;
} else if (c == '/') state = 3;
break;
case 1:
if (isspace(c)) state = 0;
hasvalue = 1;
break;
case 3:
if (c == '*') state = 10;
@ -733,6 +739,7 @@ check_id(DOH *s)
break;
}
}
if (!hasvalue) return 1;
return 0;
}
@ -930,7 +937,12 @@ Preprocessor_parse(DOH *s)
case 45:
if (c == '/') state = 46;
else if (c == '*') state = 47;
else {
else if (c == '\n') {
Putc('/',value);
Ungetc(c,s);
cpp_lines++;
state = 50;
} else {
Putc('/',value);
Putc(c,value);
state = 43;
@ -970,6 +982,7 @@ Preprocessor_parse(DOH *s)
silent_errors = 1;
v1 = Preprocessor_replace(v);
silent_errors = 0;
/* Printf(stdout,"checking '%s'\n", v1); */
if (!check_id(v1)) {
if (Len(comment) == 0)
Printf(ns,"%%constant %s %s;\n", Getattr(m,"name"), v1);
@ -1077,7 +1090,7 @@ Preprocessor_parse(DOH *s)
fn = get_filename(value);
s1 = cpp_include(fn);
if (s1) {
Printf(ns,"%%includefile \"%s\" {\n", Swig_last_file());
Printf(ns,"%%file(\"include\") \"%s\" {\n", Swig_last_file());
s2 = Preprocessor_parse(s1);
addline(ns,s2,allow);
Printf(ns,"\n}\n");
@ -1153,7 +1166,7 @@ Preprocessor_parse(DOH *s)
if (s1) {
add_chunk(ns,chunk,allow);
copy_location(s,chunk);
Printf(ns,"%sfile \"%s\" {\n", decl, Swig_last_file());
Printf(ns,"%%file(\"%s\") \"%s\" {\n", Char(decl)+1, Swig_last_file());
if ((Cmp(decl,"%import") == 0) || (Cmp(decl,"%extern") == 0)) {
Preprocessor_define("WRAPEXTERN 1", 0);
}

View file

@ -401,7 +401,7 @@ int SWIG_main(int argc, char *argv[], Language *l) {
if (lang_config) {
Printf(fs,"\n%%include \"%s\"\n", lang_config);
}
Printf(fs,"\n%%includefile \"%s\" {\n", Swig_last_file());
Printf(fs,"\n%%file(\"include\") \"%s\" {\n", Swig_last_file());
Append(fs, ds);
Append(fs,"\n}\n");
Delete(ds);

View file

@ -477,7 +477,7 @@ static void dump_nested(char *parent) {
%token <id> ID
%token <id> HBLOCK WRAPPER POUND RUNTIME HEADER
%token <id> STRING
%token <loc> INCLUDE IMPORT WEXTERN SWIGMACRO INSERT
%token <loc> INCLUDE SWIGMACRO INSERT
%token <id> NUM_INT NUM_FLOAT CHARCONST NUM_UNSIGNED NUM_LONG NUM_ULONG
%token <ivalue> TYPEDEF
%token <type> TYPE_INT TYPE_UNSIGNED TYPE_SHORT TYPE_LONG TYPE_FLOAT TYPE_DOUBLE TYPE_CHAR TYPE_VOID TYPE_SIGNED TYPE_BOOL TYPE_TYPEDEF TYPE_RAW
@ -546,47 +546,25 @@ command : command statement {
}
;
statement : INCLUDE STRING LBRACE {
statement : INCLUDE LPAREN STRING RPAREN STRING LBRACE {
$1.filename = Swig_copy_string(input_file);
$1.line = line_number;
input_file = Swig_copy_string($2);
input_file = Swig_copy_string($5);
$1.flag = WrapExtern;
if (strcmp($3,"extern") == 0) {
WrapExtern = 1;
}
if (strcmp($3,"import") == 0) {
WrapExtern = 1;
lang->import($5);
}
line_number = 0;
} command RBRACE {
input_file = $1.filename;
line_number = $1.line;
WrapExtern = $1.flag;
}
/* %extern directive */
| WEXTERN STRING LBRACE {
$1.flag = WrapExtern;
WrapExtern = 1;
$1.filename = Swig_copy_string(input_file);
$1.line = line_number;
input_file = Swig_copy_string($2);
line_number = 0;
} command RBRACE {
input_file = $1.filename;
line_number = $1.line;
WrapExtern = $1.flag;
}
/* %import directive. Like %extern but calls out to a language module */
| IMPORT STRING LBRACE {
$1.flag = WrapExtern;
WrapExtern = 1;
$1.filename = Swig_copy_string(input_file);
$1.line = line_number;
input_file = Swig_copy_string($2);
line_number = 0;
lang->import($2);
} command RBRACE {
input_file = $1.filename;
line_number = $1.line;
WrapExtern = $1.flag;
}
| SWIGMACRO ID COMMA STRING COMMA NUM_INT LBRACE {
$1.filename = Swig_copy_string(input_file);
$1.line = line_number;

View file

@ -1039,8 +1039,7 @@ extern "C" int yylex(void) {
if (strcmp(yytext,"%readwrite") == 0) return(READWRITE);
if (strcmp(yytext,"%name") == 0) return(NAME);
if (strcmp(yytext,"%rename") == 0) return(RENAME);
if (strcmp(yytext,"%includefile") == 0) return(INCLUDE);
if (strcmp(yytext,"%externfile") == 0) return(WEXTERN);
if (strcmp(yytext,"%file") == 0) return(INCLUDE);
if (strcmp(yytext,"%val") == 0) {
Printf(stderr,"%s:%d %%val directive deprecated (ignored).\n", input_file, line_number);
return (yylex());
@ -1083,7 +1082,6 @@ extern "C" int yylex(void) {
if (strcmp(yytext,"%inline") == 0) return(INLINE);
if (strcmp(yytext,"%typemap") == 0) return(TYPEMAP);
if (strcmp(yytext,"%except") == 0) return(EXCEPT);
if (strcmp(yytext,"%importfile") == 0) return(IMPORT);
if (strcmp(yytext,"%echo") == 0) return(ECHO);
if (strcmp(yytext,"%new") == 0) return(NEW);
if (strcmp(yytext,"%apply") == 0) return(APPLY);