mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-15 22:28:18 +00:00
Move err() to the lexer, implement file inclusion capabilities directly in tblgen
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7436 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -8,7 +8,6 @@
|
|||||||
%option nostdinit
|
%option nostdinit
|
||||||
%option never-interactive
|
%option never-interactive
|
||||||
%option batch
|
%option batch
|
||||||
%option noyywrap
|
|
||||||
%option nodefault
|
%option nodefault
|
||||||
%option 8bit
|
%option 8bit
|
||||||
%option outfile="Lexer.cpp"
|
%option outfile="Lexer.cpp"
|
||||||
@@ -32,6 +31,32 @@ static int ParseInt(const char *Str) {
|
|||||||
|
|
||||||
static int CommentDepth = 0;
|
static int CommentDepth = 0;
|
||||||
|
|
||||||
|
struct IncludeRec {
|
||||||
|
std::string Filename;
|
||||||
|
FILE *File;
|
||||||
|
unsigned LineNo;
|
||||||
|
YY_BUFFER_STATE Buffer;
|
||||||
|
|
||||||
|
IncludeRec(const std::string &FN, FILE *F)
|
||||||
|
: Filename(FN), File(F), LineNo(0){
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::vector<IncludeRec> IncludeStack;
|
||||||
|
|
||||||
|
|
||||||
|
std::ostream &err() {
|
||||||
|
if (IncludeStack.empty())
|
||||||
|
return std::cerr << "At end of input: ";
|
||||||
|
|
||||||
|
for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
|
||||||
|
std::cerr << "IncFrom " << IncludeStack[i].Filename << ":"
|
||||||
|
<< IncludeStack[i].LineNo << ":\n";
|
||||||
|
return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
|
||||||
|
<< Filelineno << ": ";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int Fileparse();
|
int Fileparse();
|
||||||
|
|
||||||
void ParseFile(const std::string &Filename) {
|
void ParseFile(const std::string &Filename) {
|
||||||
@@ -43,17 +68,68 @@ void ParseFile(const std::string &Filename) {
|
|||||||
std::cerr << "Could not open input file '" + Filename + "'!\n";
|
std::cerr << "Could not open input file '" + Filename + "'!\n";
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
IncludeStack.push_back(IncludeRec(Filename, F));
|
||||||
|
} else {
|
||||||
|
IncludeStack.push_back(IncludeRec("<stdin>", stdin));
|
||||||
}
|
}
|
||||||
|
|
||||||
Filein = F;
|
Filein = F;
|
||||||
Filelineno = 1;
|
Filelineno = 1;
|
||||||
Fileparse();
|
Fileparse();
|
||||||
|
|
||||||
if (F != stdin)
|
|
||||||
fclose(F);
|
|
||||||
Filein = stdin;
|
Filein = stdin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleInclude - This function is called when an include directive is
|
||||||
|
// encountered in the input stream...
|
||||||
|
static void HandleInclude(const char *Buffer) {
|
||||||
|
unsigned Length = yyleng;
|
||||||
|
assert(Buffer[Length-1] == '"');
|
||||||
|
Buffer += strlen("include ");
|
||||||
|
Length -= strlen("include ");
|
||||||
|
while (*Buffer != '"') {
|
||||||
|
++Buffer;
|
||||||
|
--Length;
|
||||||
|
}
|
||||||
|
assert(Length >= 2 && "Double quotes not found?");
|
||||||
|
std::string Filename(Buffer+1, Buffer+Length-1);
|
||||||
|
//std::cerr << "Filename = '" << Filename << "'\n";
|
||||||
|
|
||||||
|
// Save the line number and lex buffer of the includer...
|
||||||
|
IncludeStack.back().LineNo = Filelineno;
|
||||||
|
IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
|
||||||
|
|
||||||
|
// Open the new input file...
|
||||||
|
yyin = fopen(Filename.c_str(), "r");
|
||||||
|
if (yyin == 0) {
|
||||||
|
err() << "Could not find include file '" << Filename << "'!\n";
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the file to our include stack...
|
||||||
|
IncludeStack.push_back(IncludeRec(Filename, yyin));
|
||||||
|
Filelineno = 1; // Reset line numbering...
|
||||||
|
//yyrestart(yyin); // Start lexing the new file...
|
||||||
|
|
||||||
|
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// yywrap - This is called when the lexer runs out of input in one of the files.
|
||||||
|
// Switch back to an includer if an includee has run out of input.
|
||||||
|
//
|
||||||
|
extern "C"
|
||||||
|
int yywrap() {
|
||||||
|
if (IncludeStack.back().File != stdin)
|
||||||
|
fclose(IncludeStack.back().File);
|
||||||
|
IncludeStack.pop_back();
|
||||||
|
if (IncludeStack.empty()) return 1; // Top-level file is done.
|
||||||
|
|
||||||
|
// Otherwise, we need to switch back to a file which included the current one.
|
||||||
|
Filelineno = IncludeStack.back().LineNo; // Restore current line number
|
||||||
|
yy_switch_to_buffer(IncludeStack.back().Buffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
Comment \/\/.*
|
Comment \/\/.*
|
||||||
@@ -61,11 +137,15 @@ Comment \/\/.*
|
|||||||
Identifier [a-zA-Z_][0-9a-zA-Z_]*
|
Identifier [a-zA-Z_][0-9a-zA-Z_]*
|
||||||
Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
|
Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
|
||||||
StringVal \"[^"]*\"
|
StringVal \"[^"]*\"
|
||||||
|
IncludeStr include[ \t\n]+\"[^"]*\"
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
{Comment} { /* Ignore comments */ }
|
{Comment} { /* Ignore comments */ }
|
||||||
|
|
||||||
|
{IncludeStr} { HandleInclude(yytext); }
|
||||||
|
|
||||||
|
|
||||||
int { return INT; }
|
int { return INT; }
|
||||||
bit { return BIT; }
|
bit { return BIT; }
|
||||||
bits { return BITS; }
|
bits { return BITS; }
|
||||||
@@ -87,7 +167,6 @@ in { return IN; }
|
|||||||
{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
|
{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
|
||||||
|
|
||||||
[ \t\n]+ { /* Ignore whitespace */ }
|
[ \t\n]+ { /* Ignore whitespace */ }
|
||||||
. { return Filetext[0]; }
|
|
||||||
|
|
||||||
|
|
||||||
"/*" { BEGIN(comment); CommentDepth++; }
|
"/*" { BEGIN(comment); CommentDepth++; }
|
||||||
@@ -96,6 +175,8 @@ in { return IN; }
|
|||||||
<comment>"/*" { ++CommentDepth; }
|
<comment>"/*" { ++CommentDepth; }
|
||||||
<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
|
<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
|
||||||
<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
|
<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
|
||||||
<comment><<EOF>> { fprintf(stderr, "Unterminated comment!\n"); abort(); }
|
<comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
|
||||||
|
|
||||||
|
. { return Filetext[0]; }
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@@ -21,9 +21,7 @@ typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
|
|||||||
static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
|
static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
|
||||||
Init*> > SetStack;
|
Init*> > SetStack;
|
||||||
|
|
||||||
static std::ostream &err() {
|
extern std::ostream &err();
|
||||||
return std::cerr << "Parsing Line #" << Filelineno << ": ";
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addValue(const RecordVal &RV) {
|
static void addValue(const RecordVal &RV) {
|
||||||
if (CurRec->getValue(RV.getName())) {
|
if (CurRec->getValue(RV.getName())) {
|
||||||
|
@@ -8,7 +8,6 @@
|
|||||||
%option nostdinit
|
%option nostdinit
|
||||||
%option never-interactive
|
%option never-interactive
|
||||||
%option batch
|
%option batch
|
||||||
%option noyywrap
|
|
||||||
%option nodefault
|
%option nodefault
|
||||||
%option 8bit
|
%option 8bit
|
||||||
%option outfile="Lexer.cpp"
|
%option outfile="Lexer.cpp"
|
||||||
@@ -32,6 +31,32 @@ static int ParseInt(const char *Str) {
|
|||||||
|
|
||||||
static int CommentDepth = 0;
|
static int CommentDepth = 0;
|
||||||
|
|
||||||
|
struct IncludeRec {
|
||||||
|
std::string Filename;
|
||||||
|
FILE *File;
|
||||||
|
unsigned LineNo;
|
||||||
|
YY_BUFFER_STATE Buffer;
|
||||||
|
|
||||||
|
IncludeRec(const std::string &FN, FILE *F)
|
||||||
|
: Filename(FN), File(F), LineNo(0){
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::vector<IncludeRec> IncludeStack;
|
||||||
|
|
||||||
|
|
||||||
|
std::ostream &err() {
|
||||||
|
if (IncludeStack.empty())
|
||||||
|
return std::cerr << "At end of input: ";
|
||||||
|
|
||||||
|
for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
|
||||||
|
std::cerr << "IncFrom " << IncludeStack[i].Filename << ":"
|
||||||
|
<< IncludeStack[i].LineNo << ":\n";
|
||||||
|
return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
|
||||||
|
<< Filelineno << ": ";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int Fileparse();
|
int Fileparse();
|
||||||
|
|
||||||
void ParseFile(const std::string &Filename) {
|
void ParseFile(const std::string &Filename) {
|
||||||
@@ -43,17 +68,68 @@ void ParseFile(const std::string &Filename) {
|
|||||||
std::cerr << "Could not open input file '" + Filename + "'!\n";
|
std::cerr << "Could not open input file '" + Filename + "'!\n";
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
IncludeStack.push_back(IncludeRec(Filename, F));
|
||||||
|
} else {
|
||||||
|
IncludeStack.push_back(IncludeRec("<stdin>", stdin));
|
||||||
}
|
}
|
||||||
|
|
||||||
Filein = F;
|
Filein = F;
|
||||||
Filelineno = 1;
|
Filelineno = 1;
|
||||||
Fileparse();
|
Fileparse();
|
||||||
|
|
||||||
if (F != stdin)
|
|
||||||
fclose(F);
|
|
||||||
Filein = stdin;
|
Filein = stdin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleInclude - This function is called when an include directive is
|
||||||
|
// encountered in the input stream...
|
||||||
|
static void HandleInclude(const char *Buffer) {
|
||||||
|
unsigned Length = yyleng;
|
||||||
|
assert(Buffer[Length-1] == '"');
|
||||||
|
Buffer += strlen("include ");
|
||||||
|
Length -= strlen("include ");
|
||||||
|
while (*Buffer != '"') {
|
||||||
|
++Buffer;
|
||||||
|
--Length;
|
||||||
|
}
|
||||||
|
assert(Length >= 2 && "Double quotes not found?");
|
||||||
|
std::string Filename(Buffer+1, Buffer+Length-1);
|
||||||
|
//std::cerr << "Filename = '" << Filename << "'\n";
|
||||||
|
|
||||||
|
// Save the line number and lex buffer of the includer...
|
||||||
|
IncludeStack.back().LineNo = Filelineno;
|
||||||
|
IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
|
||||||
|
|
||||||
|
// Open the new input file...
|
||||||
|
yyin = fopen(Filename.c_str(), "r");
|
||||||
|
if (yyin == 0) {
|
||||||
|
err() << "Could not find include file '" << Filename << "'!\n";
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the file to our include stack...
|
||||||
|
IncludeStack.push_back(IncludeRec(Filename, yyin));
|
||||||
|
Filelineno = 1; // Reset line numbering...
|
||||||
|
//yyrestart(yyin); // Start lexing the new file...
|
||||||
|
|
||||||
|
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// yywrap - This is called when the lexer runs out of input in one of the files.
|
||||||
|
// Switch back to an includer if an includee has run out of input.
|
||||||
|
//
|
||||||
|
extern "C"
|
||||||
|
int yywrap() {
|
||||||
|
if (IncludeStack.back().File != stdin)
|
||||||
|
fclose(IncludeStack.back().File);
|
||||||
|
IncludeStack.pop_back();
|
||||||
|
if (IncludeStack.empty()) return 1; // Top-level file is done.
|
||||||
|
|
||||||
|
// Otherwise, we need to switch back to a file which included the current one.
|
||||||
|
Filelineno = IncludeStack.back().LineNo; // Restore current line number
|
||||||
|
yy_switch_to_buffer(IncludeStack.back().Buffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
Comment \/\/.*
|
Comment \/\/.*
|
||||||
@@ -61,11 +137,15 @@ Comment \/\/.*
|
|||||||
Identifier [a-zA-Z_][0-9a-zA-Z_]*
|
Identifier [a-zA-Z_][0-9a-zA-Z_]*
|
||||||
Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
|
Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
|
||||||
StringVal \"[^"]*\"
|
StringVal \"[^"]*\"
|
||||||
|
IncludeStr include[ \t\n]+\"[^"]*\"
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
{Comment} { /* Ignore comments */ }
|
{Comment} { /* Ignore comments */ }
|
||||||
|
|
||||||
|
{IncludeStr} { HandleInclude(yytext); }
|
||||||
|
|
||||||
|
|
||||||
int { return INT; }
|
int { return INT; }
|
||||||
bit { return BIT; }
|
bit { return BIT; }
|
||||||
bits { return BITS; }
|
bits { return BITS; }
|
||||||
@@ -87,7 +167,6 @@ in { return IN; }
|
|||||||
{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
|
{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
|
||||||
|
|
||||||
[ \t\n]+ { /* Ignore whitespace */ }
|
[ \t\n]+ { /* Ignore whitespace */ }
|
||||||
. { return Filetext[0]; }
|
|
||||||
|
|
||||||
|
|
||||||
"/*" { BEGIN(comment); CommentDepth++; }
|
"/*" { BEGIN(comment); CommentDepth++; }
|
||||||
@@ -96,6 +175,8 @@ in { return IN; }
|
|||||||
<comment>"/*" { ++CommentDepth; }
|
<comment>"/*" { ++CommentDepth; }
|
||||||
<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
|
<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
|
||||||
<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
|
<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
|
||||||
<comment><<EOF>> { fprintf(stderr, "Unterminated comment!\n"); abort(); }
|
<comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
|
||||||
|
|
||||||
|
. { return Filetext[0]; }
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@@ -21,9 +21,7 @@ typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
|
|||||||
static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
|
static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
|
||||||
Init*> > SetStack;
|
Init*> > SetStack;
|
||||||
|
|
||||||
static std::ostream &err() {
|
extern std::ostream &err();
|
||||||
return std::cerr << "Parsing Line #" << Filelineno << ": ";
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addValue(const RecordVal &RV) {
|
static void addValue(const RecordVal &RV) {
|
||||||
if (CurRec->getValue(RV.getName())) {
|
if (CurRec->getValue(RV.getName())) {
|
||||||
|
Reference in New Issue
Block a user