-
Notifications
You must be signed in to change notification settings - Fork 1
/
lyutils.cpp
98 lines (84 loc) · 2.55 KB
/
lyutils.cpp
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
98
// $Id: lyutils.cpp,v 1.3 2016-10-06 16:42:35-07 - - $
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "auxlib.h"
#include "lyutils.h"
bool lexer::interactive = true;
location lexer::lloc = {0, 1, 0};
size_t lexer::last_yyleng = 0;
vector<string> lexer::filenames;
astree *parser::root = nullptr;
FILE *lexer::tokenFile = nullptr;
const string *lexer::filename(size_t filenr) {
return &lexer::filenames.at(filenr);
}
void lexer::newfilename(const string &filename) {
lexer::lloc.filenr = lexer::filenames.size();
lexer::filenames.push_back(filename);
}
void lexer::advance() {
if (not interactive) {
if (lexer::lloc.offset == 0) {
printf(";%2zd.%3zd: ",
lexer::lloc.filenr, lexer::lloc.linenr);
}
printf("%s", yytext);
}
lexer::lloc.offset += last_yyleng;
last_yyleng = yyleng;
}
void lexer::newline() {
++lexer::lloc.linenr;
lexer::lloc.offset = 0;
}
void lexer::badchar(unsigned char bad) {
char buffer[16];
snprintf(buffer, sizeof buffer,
isgraph(bad) ? "%c" : "\\%03o", bad);
errllocprintf(lexer::lloc, "invalid source character (%s)\n",
buffer);
}
void lexer::badtoken(char *lexeme) {
errllocprintf(lexer::lloc, "invalid token (%s)\n", lexeme);
}
void lexer::include() {
size_t linenr;
static char filename[0x1000];
assert (sizeof filename > strlen(yytext));
int scan_rc = sscanf(yytext, R"(# %zu "%[^"]")", &linenr, filename);
if (scan_rc != 2) {
errprintf("%s: invalid directive, ignored\n", yytext);
} else {
if (yy_flex_debug) {
fprintf(stderr, "--included # %zd \"%s\"\n",
linenr, filename);
}
printFormattedDirective(linenr, filename);
lexer::lloc.linenr = linenr - 1;
lexer::newfilename(filename);
}
}
void yyerror(const char *message) {
assert (not lexer::filenames.empty());
errllocprintf(lexer::lloc, "%s\n", message);
}
void printFormattedDirective(size_t linenr, const string &filename) {
DEBUGF('v', "We are in printFormattedDirective!\n");
fprintf(lexer::tokenFile, "# %zd \"%s\"\n",
linenr, filename.c_str());
}
void printFormattedToken(const astree &tree) {
DEBUGF('v', "We are in printFormattedToken!\n");
fprintf(lexer::tokenFile, "%4ld %3ld.%03ld %4d %-12s %s\n",
tree.lloc.filenr, tree.lloc.linenr, tree.lloc.offset,
tree.symbol, parser::get_tname(tree.symbol),
tree.lexinfo->c_str());
}
int yylval_token(int symbol) {
yylval = new astree(symbol, lexer::lloc, yytext);
printFormattedToken(*yylval);
return symbol;
}