-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyacc_tools.c
97 lines (88 loc) · 1.87 KB
/
yacc_tools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "yacc_tools.h"
void yacc_error(const char *filename, char const *s, const char *t, FILE *yyin, int yylineno, int yycolumn, int yyleng, char *yytext)
{
const int expr_size = 255;
char expr[expr_size];
rewind(yyin);
for (int l = 1; l <= yylineno; l++)
{
fgets(expr, expr_size, yyin);
char *lb = strchr(expr, '\n');
if (lb)
*lb = 0;
}
fclose(yyin);
char *e = strstr(expr, yytext);
int error_begin;
if (e != NULL)
{
error_begin = (int)(e - expr) + 1;
}
else
{
error_begin = yycolumn;
}
printf("Error parsing %s file:\n", t);
printf("%s:%i:%i: %s\n", filename, yylineno, error_begin, s);
printf("%-3i | %s\n", yylineno, expr);
const int extra_spc = 6;
const int x = error_begin + yyleng;
for (int i = -extra_spc; i < x; i++)
{
if (i == 0)
{
continue;
}
else if (i < error_begin)
{
printf(" ");
}
else if (i == error_begin)
{
printf("^");
}
else
{
printf("~");
}
}
printf("\n");
printf("Char dumps (yytext): ");
char h;
while (*yytext)
{
h = *yytext++;
printf("%i", h);
if (h >= ' ' && h <= '~')
{
printf("(%c) ", h);
}
else
{
printf(" ");
}
}
printf("\n");
}
bool yy_doesFileExist(const char *filename, FILE *yyin)
{
if (!yyin)
{
fclose(yyin);
printf("%s does not exist.\n", filename);
return false;
}
return true;
}
void sanitize_path(char *path)
{
char *filename = strrchr(path, '/');
if (filename)
{
memmove(path, filename + 1, strlen(filename));
}
}