-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyutils.cc
106 lines (86 loc) · 2.61 KB
/
lyutils.cc
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
99
100
101
102
103
104
105
#include <vector>
#include <string>
using namespace std;
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lyutils.h"
#include "auxlib.h"
astree* yyparse_astree = NULL;
int scan_linenr = 1;
int scan_offset = 0;
bool scan_echo = false;
vector<string> included_filenames;
const string* scanner_filename (int filenr) {
return &included_filenames.at(filenr);
}
void scanner_newfilename (const char* filename) {
included_filenames.push_back (filename);
}
void scanner_newline (void) {
++scan_linenr;
scan_offset = 0;
}
void scanner_setecho (bool echoflag) {
scan_echo = echoflag;
}
void scanner_useraction (void) {
if (scan_echo) {
if (scan_offset == 0) printf (";%5d: ", scan_linenr);
printf ("%s", yytext);
}
scan_offset += yyleng;
}
void yyerror (const char* message) {
assert (not included_filenames.empty());
errprintf ("%:%s: %d: %s\n",
included_filenames.back().c_str(), scan_linenr, message);
}
void scanner_badchar (unsigned char bad) {
char char_rep[16];
sprintf (char_rep, isgraph ((int) bad) ? "%c" : "\\%03o", bad);
errprintf ("%:%s: %d: invalid source character (%s)\n",
included_filenames.back().c_str(), scan_linenr, char_rep);
}
void scanner_badtoken (char* lexeme) {
errprintf ("%:%s: %d: invalid token (%s)\n",
included_filenames.back().c_str(), scan_linenr, lexeme);
}
int yylval_token (int symbol) {
int offset = scan_offset - yyleng;
yylval = new_astree (symbol, included_filenames.size() - 1,
scan_linenr, offset, yytext);
yyprint(tok_file, symbol, yylval);
return symbol;
}
void error_destructor (astree* tree) {
if (tree == yyparse_astree) return;
DEBUGSTMT ('a', dump_astree (stderr, tree); );
free_ast (tree);
}
astree* new_parseroot (void) {
yyparse_astree = new_astree(TOK_ROOT, "program");
return yyparse_astree;
}
astree* new_parsenode (int tok_type, const char* type_str) {
astree* parsenode = new_astree(tok_type, type_str);
return parsenode;
}
void scanner_include (void) {
scanner_newline();
char filename[strlen (yytext) + 1];
int linenr;
int scan_rc = sscanf (yytext, "# %d \"%[^\"]\"", &linenr, filename);
if (scan_rc != 2) {
errprintf ("%: %d: [%s]: invalid directive, ignored\n",
scan_rc, yytext);
} else {
fprintf (tok_file, "# %d \"%s\"\n", linenr, filename);
scanner_newfilename (filename);
scan_linenr = linenr - 1;
DEBUGF ('m', "filename=%s, scan_linenr=%d\n",
included_filenames.back().c_str(), scan_linenr);
}
}