Skip to content

Commit

Permalink
Added filename to errors in parsers 2 and 4.
Browse files Browse the repository at this point in the history
Closes #77 Add filename to the error reporting in the preprocessors.
  • Loading branch information
Fubukimaru committed Jan 10, 2021
1 parent 8b401c7 commit c1918f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
35 changes: 24 additions & 11 deletions src/parser2.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define P2_BUFF_SIZE 0x4000

static FILE *p2_output;
static char *p2_text, *p2_buffer;
static char *p2_text, *p2_buffer, *p2_name, *p2_strtmp;
static int p2_number, p2_lines, p2_level, p2_nested=0;

int prompt_error2(int);
Expand All @@ -30,16 +30,27 @@ int prompt_error2(int);

%%

<INITIAL>"#"file[ \t]*\"[a-z_][a-z0-9_]*"."[a-z_][a-z0-9_]*\"\n {
// Save original text
safe_strcat(p2_text, yytext, P2_TEXT_SIZE, p2_name, p2_lines);
// Save filename
p2_strtmp = strtok(yytext, "\"");
p2_strtmp = strtok(NULL, "\"");
// # file statements
p2_name[0] = '\0';
safe_strcat(p2_name, p2_strtmp, PATH_MAX, p2_name, p2_lines);
}

<INITIAL>"#"line[ \t]*[0-9]+\n {
safe_strcat(p2_text, yytext, P2_TEXT_SIZE, fname_src, -1);
safe_strcat(p2_text, yytext, P2_TEXT_SIZE, p2_name, p2_lines);
p2_lines = atoi(&yytext[5]);
BEGIN(line);
}

<line>"."?rept[ \t]+ BEGIN(repnum);

<line>. {
safe_strcat(p2_text, yytext, P2_TEXT_SIZE, fname_src, -1);
safe_strcat(p2_text, yytext, P2_TEXT_SIZE, p2_name, p2_lines);
BEGIN(INITIAL);
}

Expand All @@ -51,15 +62,15 @@ int prompt_error2(int);
}

<rept>"."?rept[ \t]+[0-9]+[ \t]* {
p2_buffer = safe_strcat(p2_buffer, yytext, P2_BUFF_SIZE, fname_src, -1);
p2_buffer = safe_strcat(p2_buffer, yytext, P2_BUFF_SIZE, p2_name, p2_lines);
p2_nested++;
p2_level++;
}

<rept>"#"line[ \t]*[0-9]+\n[ \t]*.?endr[ \t]*\n {
if (p2_nested) {
p2_nested--;
p2_buffer = safe_strcat(p2_buffer, yytext, P2_BUFF_SIZE, fname_src, -1);
p2_buffer = safe_strcat(p2_buffer, yytext, P2_BUFF_SIZE, p2_name, p2_lines);
} else {
int i;

Expand All @@ -72,11 +83,11 @@ int prompt_error2(int);
}
}

<rept>. p2_buffer = safe_strcat(p2_text, yytext, P2_TEXT_SIZE, fname_src, -1);
<rept>. p2_buffer = safe_strcat(p2_text, yytext, P2_TEXT_SIZE, p2_name, p2_lines);

<rept>\n p2_buffer = safe_strcat(p2_buffer, yytext, P2_BUFF_SIZE, fname_src, -1);
<rept>\n p2_buffer = safe_strcat(p2_buffer, yytext, P2_BUFF_SIZE, p2_name, p2_lines);

<rept><<EOF>> error_message(2, fname_src, -1);
<rept><<EOF>> error_message(2, p2_name, p2_lines);

<repnum>. prompt_error2(1);

Expand All @@ -85,12 +96,12 @@ int prompt_error2(int);
p2_text[0] = 0;
}

<INITIAL>. safe_strcat(p2_text, yytext, P2_TEXT_SIZE, fname_src, -1);
<INITIAL>. safe_strcat(p2_text, yytext, P2_TEXT_SIZE, p2_name, p2_lines);

%%

int prompt_error2(int c) {
fprintf(stderr, ", line %d: ", p2_lines);
fprintf(stderr, "%s, line %d: ", p2_name, p2_lines);
switch (c) {
case 1:
fprintf(stderr, "number expected in REPT\n");
Expand All @@ -111,9 +122,11 @@ int preprocessor2() {
int loop = 0;

filename = malloc(PATH_MAX);
p2_name = malloc(PATH_MAX);
p2_text = malloc(P2_TEXT_SIZE);
p2_buffer = malloc(P2_BUFF_SIZE);
p2_text[0] = 0;
p2_text[0] = '\0';
p2_name[0] = '\0';
printf("Expanding REPT\n");

do {
Expand Down
32 changes: 23 additions & 9 deletions src/parser4.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
macro_type macro_list[MAX_MACROS];

static FILE *p4_output;
static char *p4_text, *p4_tmpstr;
static char *p4_text, *p4_tmpstr, *p4_name;
static int p4_lines;

int last_macro=0;
Expand All @@ -35,8 +35,20 @@ void register_macro_parameters();

%%

<INITIAL>"#"file[ \t]*\"[a-z_][a-z0-9_]*"."[a-z_][a-z0-9_]*\"\n {
// Save original text
safe_strcat(p4_text, yytext, P4_TEXT_SIZE, p4_name, p4_lines);
// Save filename
p4_tmpstr = strtok(yytext, "\"");
p4_tmpstr = strtok(NULL, "\"");
// # file statements
p4_name[0] = '\0';
safe_strcat(p4_name, p4_tmpstr, PATH_MAX, p4_name, p4_lines);
}


<INITIAL>"#"line[ \t]*[0-9]+\n {
safe_strcat(p4_text, yytext, P4_TEXT_SIZE, fname_src, -1);
safe_strcat(p4_text, yytext, P4_TEXT_SIZE, p4_name, p4_lines);
p4_lines = atoi(&yytext[5]);
BEGIN(line);
}
Expand Down Expand Up @@ -186,11 +198,11 @@ void register_macro_parameters();

macro_src_tmp = macro_src;
macro_src = add_lines_to_macro(macro_src);
safe_strcat(p4_text, macro_src, P4_TEXT_SIZE, fname_src, -1);
safe_strcat(p4_text, macro_src, P4_TEXT_SIZE, p4_name, p4_lines);
// Free previous code
free(macro_src);
} else {
safe_strcat(p4_text, yytext, P4_TEXT_SIZE, fname_src, -1);
safe_strcat(p4_text, yytext, P4_TEXT_SIZE, p4_name, p4_lines);
}
free(yytext_copy);
BEGIN(INITIAL);
Expand All @@ -211,7 +223,7 @@ void register_macro_parameters();

<macro_code>.+\n {
safe_strcat(macro_list[last_macro].code, yytext, MACRO_CODE_SIZE,
fname_src, p4_lines);
p4_name, p4_lines);
}

<macro_code>\n {
Expand All @@ -221,15 +233,15 @@ void register_macro_parameters();


<macro_code><<EOF>> {
error_message(1, fname_src, p4_lines);
error_message(1, p4_name, p4_lines);
}

<INITIAL>\n {
fprintf(p4_output, "%s%s", p4_text, yytext);
p4_text[0] = 0;
}

<INITIAL>. safe_strcat(p4_text, yytext, P4_TEXT_SIZE, fname_src, -1);
<INITIAL>. safe_strcat(p4_text, yytext, P4_TEXT_SIZE, p4_name, p4_lines);

%%

Expand Down Expand Up @@ -288,7 +300,7 @@ void register_macro_parameters() {


int prompt_error4(int c) {
fprintf(stderr, ", line %d: ", p4_lines);
fprintf(stderr, "%s, line %d: ", p4_name, p4_lines);
switch (c) {
case 1:
fprintf(stderr, "MACRO without ENDM or ENDMACRO\n");
Expand Down Expand Up @@ -321,7 +333,9 @@ int preprocessor4() {
printf("Expanding Macros\n");

p4_text = malloc(P4_TEXT_SIZE);
p4_text[0] = 0;
p4_text[0] = '\0';
p4_name = malloc(PATH_MAX);
p4_name[0] = '\0';

if ((input = fopen(tmp0, "r")) == NULL) {
fprintf(stderr, "Fatal: cannot process file %s", tmp0);
Expand Down

0 comments on commit c1918f3

Please sign in to comment.