-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyutils.cpp
98 lines (82 loc) · 2.39 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
// $Id: lyutils.cpp,v 1.6 2019-04-18 13:35:11-07 - - $
// Wai Chun Leung
// wleung11
// Shineng Tang
// stang38
#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;
// FILE* tok_out;
astree* parser::root = nullptr;
const string* lexer::filename (int 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::include() {
size_t linenr;
static char filename[0x1000];
assert (sizeof filename > strlen (yytext));
int scan_rc = sscanf (yytext, "# %2zu \"%[^\"]\"",
&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);
}
// SEND TO OUTFILE
fprintf(tok_out, "# %2zd \"%s\"\n", linenr, filename);
lexer::lloc.linenr = linenr - 1;
lexer::newfilename (filename);
}
}
int lexer::token (int symbol) {
yylval = new astree (symbol, lexer::lloc, yytext);
// SEND TO OUTFILE
fprintf(tok_out, "%4zd %zd.%05zd %3d %-13s %s\n",
lexer::lloc.filenr, lexer::lloc.linenr, lexer::lloc.offset,
symbol, parser::get_tname(symbol), yytext);
return symbol;
}
int lexer::badtoken (int symbol) {
errllocprintf (lexer::lloc, "invalid token (%s)\n", yytext);
return lexer::token (symbol);
}
void yyerror (const char* message) {
assert (not lexer::filenames.empty());
errllocprintf (lexer::lloc, "%s\n", message);
}