From a200d6291d25b7f287320292edfc61b34216b7f4 Mon Sep 17 00:00:00 2001 From: kramm Date: Sat, 22 Feb 2003 18:18:01 +0000 Subject: [PATCH] includes. --- src/parser.lex | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/parser.lex b/src/parser.lex index 57c7056..7da8d5e 100644 --- a/src/parser.lex +++ b/src/parser.lex @@ -116,6 +116,38 @@ static void store(enum type_t type, int line, int column, char*text, int length) prefix = 0; } +#define MAX_INCLUDE_DEPTH 16 +YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; +int include_stack_ptr = 0; + +void handleInclude(char*text, int len) +{ + text+=9;len-=9; + while(len >=1 && (text[0] == ' ' || text[0] == '\t')) { + text++;len--; + } + while(len >= 1 && (text[len-1] == ' ' || text[len-1] == '\n')) { + len--; + } + if(len >= 2 && text[0] == '"' && text[len-1] == '"') { + text++; len-=2; + } + text[len] = 0; + if(include_stack_ptr >= MAX_INCLUDE_DEPTH) { + fprintf( stderr, "Includes nested too deeply" ); + exit( 1 ); + } + include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER; + yyin = fopen(text, "rb"); + if (!yyin) { + fprintf(stderr, "Couldn't open %s\n", text); + exit(1); + } + yy_switch_to_buffer( + yy_create_buffer( yyin, YY_BUF_SIZE ) ); + BEGIN(INITIAL); +} + #define c() {count(yytext, yyleng, YY_START);} #define s(type) {store(type, line, column, yytext, yyleng);} %} @@ -148,6 +180,7 @@ RVALUE \"{STRING}\"|([^ \n\r\t]+) { /* values which appear only on the right-hand side of assignments, like: x=50% */ [^ \n\t\r]* {s(IDENTIFIER);c();BEGIN(0);} } +\.include{S}.*\n {handleInclude(yytext, yyleng);} \.{NAME} {s(COMMAND);c();} {NAME}{S}*: {s(LABEL);c();} {NAME} {s(IDENTIFIER);c();} @@ -167,7 +200,17 @@ RVALUE \"{STRING}\"|([^ \n\r\t]+) exit(1); yyterminate(); } -<> {c();s(END);yyterminate();} +<> {c(); + if ( --include_stack_ptr < 0 ) { + s(END); + yyterminate(); + } else { + yy_delete_buffer( YY_CURRENT_BUFFER ); + yy_switch_to_buffer( + include_stack[include_stack_ptr] ); + } + } + %% int yywrap() -- 1.7.10.4