/* GOB C Preprocessor * Copyright (C) 1999 the Free Software Foundation. * * Author: George Lebl * * 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 "config.h" #include #include "parse.h" static int parenth_depth = 0; static int before_comment = INITIAL; static int class_after_c = FALSE; static int header_c = FALSE; static GString *cbuf = NULL; int ccode_line = 1; int line_no = 1; static void clear_cbuf(void) { if(!cbuf) { cbuf = g_string_new(""); } else { cbuf = g_string_assign(cbuf,""); } } static void add_to_cbuf(char *s) { if(!cbuf) { cbuf = g_string_new(s); } else { cbuf = g_string_append(cbuf,s); } } %} %x COMMENT %x C_CODE %x C_CODE_STRING %x CLASS_CODE %x CLASS_CODE_I %% <*>\n { line_no++; REJECT; } <*>MOTHERFUCKER { fprintf(stderr,"You are a bad bad person!\n"); REJECT; } \/\/.*$ { ; /*comment, ignore*/ } \/\/.*$ { ; /*comment, ignore*/ } \/\/.*$ { ; /*comment, ignore*/ } \/\/.*$ { ; /*comment, ignore*/ } \/\* {BEGIN(COMMENT); before_comment = INITIAL; } \/\* {BEGIN(COMMENT); before_comment = C_CODE; } \/\* {BEGIN(COMMENT); before_comment = CLASS_CODE; } \/\* {BEGIN(COMMENT); before_comment = CLASS_CODE_I; } \*\/ {BEGIN(before_comment);} . { ; /* comment, ignore */ } \n { ; /* comment, ignore */ } ^\%h\{ { BEGIN(C_CODE); parenth_depth = 1; class_after_c = FALSE; header_c = TRUE; clear_cbuf(); ccode_line = line_no; } ^\%\{ { BEGIN(C_CODE); parenth_depth = 1; class_after_c = FALSE; header_c = FALSE; clear_cbuf(); ccode_line = line_no; } ^\%\} { BEGIN(INITIAL); yylval.cbuf = cbuf; cbuf = NULL; if(header_c) return HCODE; else return CCODE; } \'\{\' { add_to_cbuf(yytext); } \'\\\{\' { add_to_cbuf(yytext); } \'\}\' { add_to_cbuf(yytext); } \'\\\}\' { add_to_cbuf(yytext); } \\. { add_to_cbuf(yytext); } \" { BEGIN(C_CODE_STRING); add_to_cbuf(yytext); } \\. { add_to_cbuf(yytext); } \" { BEGIN(C_CODE); add_to_cbuf(yytext); } . { add_to_cbuf(yytext); } \{ { parenth_depth++; add_to_cbuf(yytext); } \} { parenth_depth--; if(parenth_depth<0) { REJECT; } else if(parenth_depth==0 && class_after_c) { BEGIN(CLASS_CODE_I); yylval.cbuf = cbuf; cbuf = NULL; return CCODE; } add_to_cbuf(yytext); } . { add_to_cbuf(yytext); } \n { add_to_cbuf(yytext); } class { BEGIN(CLASS_CODE); return CLASS; } from {return FROM;} void {return VOID;} struct {return STRUCT;} union {return UNION;} enum {return ENUM;} signed {return SIGNED;} unsigned {return UNSIGNED;} long {return LONG;} short {return SHORT;} int {return INT;} float {return FLOAT;} double {return DOUBLE;} char {return CHAR;} const {return CONST;} \.\.\. {return THREEDOTS;} public {yylval.line = line_no; return PUBLIC;} private {yylval.line = line_no; return PRIVATE;} argument {yylval.line = line_no; return ARGUMENT;} virtual {yylval.line = line_no; return VIRTUAL;} signal {yylval.line = line_no; return SIGNAL;} override {yylval.line = line_no; return OVERRIDE;} onerror {return ONERROR;} 0|[1-9][0-9]*|0x[0-9a-fA-F]+|0[0-7]+|[0-9]*\.[0-9]+|\.[0-9][0-9]* { yylval.id = g_strdup(yytext); return NUMBER; } :?[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z0-9_]*)+ { yylval.id = g_strdup(yytext); return TYPETOKEN; } [A-Za-z_][A-Za-z0-9_]* { yylval.id = g_strdup(yytext); return TOKEN; } \{ { BEGIN(CLASS_CODE_I); return '{'; } \{ { BEGIN(C_CODE); parenth_depth=1; class_after_c = TRUE; ccode_line = line_no; yylval.line = line_no; return '{'; } \} { BEGIN(INITIAL); return '}'; } [\n\t ] ; /*ignore*/ <*>. { yylval.line = line_no; return yytext[0]; }