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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
sourcereader : Faust source file reader
This component is in charge of mapping filenames to
the list of faust definitions they contain.
*/
#include <iostream>
#include <map>
#include <list>
#include <string>
#include <libgen.h>
#include "sourcereader.hh"
#include "sourcefetcher.hh"
#include "enrobage.hh"
#include "ppbox.hh"
#include "Text.hh"
using namespace std;
extern map<Tree, set<Tree> > gMetaDataSet;
extern string gMasterDocument;
extern vector<Tree> gDocVector;
extern bool gLatexDocSwitch;
/****************************************************************
Parser variables
*****************************************************************/
int yyparse();
void yyrestart( FILE *new_file );
struct yy_buffer_state* yy_scan_string (const char *yy_str ); // In principle YY_BUFFER_STATE
extern int yyerr;
extern int yydebug;
extern FILE* yyin;
extern int yylineno;
extern const char * yyfilename;
extern Tree gResult;
extern Tree gResult2;
/**
* Checks an argument list for containing only
* standard identifiers, no patterns and
* is linear.
* @param args the argument list to check
* @return true if it contains only identifiers
*/
static bool standardArgList(Tree args)
{
map<Tree,int> L;
while (isList(args)) {
if (!isBoxIdent(hd(args))) return false;
if (++L[hd(args)] > 1) return false;
args = tl(args);
}
return true;
}
static void printPatternError(Tree symbol, Tree lhs1, Tree rhs1, Tree lhs2, Tree rhs2)
{
if (symbol==NULL) {
cerr << "ERROR (file " << yyfilename << ":" << yylineno << ") : Inconsistent number of parameters in pattern-matching rule: "
<< boxpp(reverse(lhs2)) << " => " << boxpp(rhs2) << ";"
<< " previous rule was: "
<< boxpp(reverse(lhs1)) << " => " << boxpp(rhs1) << ";"
<< endl;
} else {
cerr << "ERROR (file " << yyfilename << ":" << yylineno << ") : in the definition of " << boxpp(symbol) << "\n"
<< "Inconsistent number of parameters in pattern-matching rule: "
<< boxpp(reverse(lhs2)) << " => " << boxpp(rhs2) << ";"
<< " previous rule was: "
<< boxpp(reverse(lhs1)) << " => " << boxpp(rhs1) << ";"
<< endl;
}
}
Tree checkRulelist (Tree lr)
{
Tree lrules = lr;
if (isNil(lrules)) { cerr << "ERROR (file " << yyfilename << ":" << yylineno << ") : a case expression can't be empty" << endl; exit(1); }
// first pattern used as a reference
Tree lhs1 = hd(hd(lrules));
Tree rhs1 = tl(hd(lrules));
int npat = len(lhs1);
lrules = tl(lrules);
while (! isNil(lrules)) {
Tree lhs2 = hd(hd(lrules));
Tree rhs2 = tl(hd(lrules));
if (npat != len(lhs2)) {
printPatternError(NULL, lhs1,rhs1,lhs2,rhs2);
exit(1);
}
lhs1 = lhs2;
rhs1 = rhs2;
lrules = tl(lrules);
}
return lr;
}
static void printRedefinitionError(Tree symbol, list<Tree>& variants)
{
cerr << "ERROR (file " << yyfilename << ":" << yylineno << ") : multiple definitions of symbol " << boxpp(symbol) << endl;
for (list<Tree>::iterator p=variants.begin(); p!=variants.end(); p++) {
Tree params = hd(*p);
Tree body = tl(*p);
if (isNil(params)) {
cerr << boxpp(symbol) << " = " << boxpp(body) << ";" << endl;
} else {
cerr << boxpp(symbol) << boxpp(params) << " = " << boxpp(body) << ";" << endl;
}
}
}
/**
* Transforms a list of variants (arglist.body)
* into an abstraction or a boxCase.
* @param symbol name only used in case of error
* @param variants list of variants (arglist.body)
* @return the corresponding box expression
*/
static Tree makeDefinition(Tree symbol, list<Tree>& variants)
{
if (variants.size() == 1) {
Tree rhs = *(variants.begin());
Tree args= hd(rhs);
Tree body= tl(rhs);
if (isNil(args)) {
return body;
} else if (standardArgList(args)) {
return buildBoxAbstr(args, body);
} else {
return boxCase(cons(rhs,nil));
}
} else {
list<Tree>::iterator p;
Tree l = nil;
Tree prev = *variants.begin();
int npat = len(hd(prev));
if (npat==0) {
printRedefinitionError(symbol, variants);
exit(1);
}
for (p=variants.begin(); p!=variants.end(); p++) {
Tree cur = *p;
if ((npat==0) || (npat != len(hd(cur)))) {
printPatternError(symbol, hd(prev), tl(prev), hd(cur), tl(cur));
exit(1);
}
prev = cur;
l = cons(*p,l);
}
return boxCase(l);
}
}
/**
* Formats a list of raw definitions represented by triplets
* <name,arglist,body> into abstractions or pattern
* matching rules when appropriate.
*
* @param rldef list of raw definitions in reverse order
* @return the list of formatted definitions
*/
Tree formatDefinitions(Tree rldef)
{
map<Tree,list<Tree> > dic;
map<Tree,list<Tree> >::iterator p;
Tree ldef2 = nil;
Tree file;
//cout << "Format definitions " << *rldef << endl;
// collects the definitions in a dictionnary
while (!isNil(rldef)) {
Tree def = hd(rldef);
rldef = tl(rldef);
if (isImportFile(def, file)) {
ldef2 = cons(def,ldef2);
} else if (!isNil(def)) {
//cout << " def : " << *def << endl;
dic[hd(def)].push_front(tl(def));
}
}
// produce the definitions
for (p=dic.begin(); p!=dic.end(); p++) {
ldef2 = cons (cons(p->first, makeDefinition(p->first,p->second)), ldef2);
}
//cout << "list of definitions : " << *ldef2 << endl;
return ldef2;
}
void SourceReader::checkName()
{
if (gMasterDocument == yyfilename) {
Tree name = tree("name");
if (gMetaDataSet.find(name) == gMetaDataSet.end()) {
gMetaDataSet[name].insert(tree(quote(strip_end(basename((char*)yyfilename), ".dsp"))));
}
}
}
/**
* Parse a single faust source file. returns the list of
* definitions it contains.
*
* @param fname the name of the file to parse
* @return the list of definitions it contains
*/
Tree SourceReader::parse(const char* fname)
{
string fullpath;
char* fileBuf = 0;
yyerr = 0;
yyfilename = fname;
if (strstr(yyfilename,"http://") != 0) {
// We are requested to parse an URL file
int ret = http_fetch(yyfilename, &fileBuf);
if (ret == -1) {
http_perror("http fetch");
exit(1);
}
yy_scan_string(fileBuf);
yylineno = 1;
int r = yyparse();
if (r) {
fprintf(stderr, "Parse error : code = %d\n", r);
}
if (yyerr > 0) {
//fprintf(stderr, "Erreur de parsing 2, count = %d \n", yyerr);
exit(1);
}
// we have parsed a valid file
fFilePathnames.push_back(fullpath);
// 'http_fetch' result must be deallocated
free(fileBuf);
checkName();
return gResult;
} else {
// test for local url
if (strstr(yyfilename,"file://") != 0) {
yyfilename = &yyfilename[7]; // skip 'file://'
}
// We are requested to parse a regular file
FILE* tmp_file = yyin = fopensearch(yyfilename, fullpath);
if (yyin == NULL) {
cerr << "ERROR : Unable to open file " << yyfilename << endl;
exit(1);
}
yyrestart(yyin); // make sure we scan from file again (in case we scanned a string just before)
yylineno = 1;
int r = yyparse();
if (r) {
cerr << "ERROR (file " << yyfilename << ":" << yylineno << ") : Parse error code " << r << endl;
}
if (yyerr > 0) {
//fprintf(stderr, "Erreur de parsing 2, count = %d \n", yyerr);
exit(1);
}
// we have parsed a valid file
fFilePathnames.push_back(fullpath);
fclose(tmp_file);
checkName();
return gResult;
}
}
/**
* Check if a file as been read and is in the "cache"
*
* @param fname the name of the file to check
* @return true if the file is in the cache
*/
bool SourceReader::cached(string fname)
{
return fFileCache.find(fname) != fFileCache.end();
}
/**
* Return the list of definitions file contains. Cache the result.
*
* @param fname the name of the file to check
* @return the list of definitions it contains
*/
Tree SourceReader::getlist(const char* fname)
{
if (!cached(fname)) {
fFileCache[fname] = parse(fname);
}
if (fFileCache[fname] == 0) exit(1);
return fFileCache[fname];
}
/**
* Return a vector of pathnames representing the list
* of all the source files that have been required
* to evaluate process (those in fFileCache)
*/
vector<string> SourceReader::listSrcFiles()
{
// vector<string> srcfiles;
// for (map<string, Tree>::const_iterator p = fFileCache.begin(); p != fFileCache.end(); p++) {
// srcfiles.push_back(p->first);
// }
// return srcfiles;
return fFilePathnames;
}
/**
* Return the list of definitions where all imports have been expanded.
*
* @param ldef the list of definitions to expand
* @return the expanded list of definitions
*/
Tree SourceReader::expandlist(Tree ldef)
{
set<string> visited;
return expandrec(ldef, visited, nil);
}
Tree SourceReader::expandrec(Tree ldef, set<string>& visited, Tree lresult)
{
for (;!isNil(ldef); ldef = tl(ldef)) {
Tree d = hd(ldef);
Tree fname;
if (isNil(d)) {
// skill null definitions produced by declarations
} else if (isImportFile(d,fname)) {
const char* f = tree2str(fname);
//cerr << "import(" << f << ")" << endl;
//string f = tree2str(fname);
if (visited.find(f) == visited.end()) {
visited.insert(f);
//Tree l = getlist(f);
lresult = expandrec(getlist(f), visited, lresult);
}
} else {
lresult = cons(d, lresult);
}
}
return lresult;
}
void declareMetadata(Tree key, Tree value)
{
if (gMasterDocument == yyfilename) {
// inside master document, no prefix needed to declare metadata
gMetaDataSet[key].insert(value);
} else {
string fkey(yyfilename);
fkey += "/";
fkey += tree2str(key);
gMetaDataSet[tree(fkey.c_str())].insert(value);
}
//cout << "Master " << gMasterDocument << ", file " << yyfilename << " : declare " << *key << "," << *value << endl;
}
void declareDoc(Tree t)
{
//gLatexDocSwitch = true;
gDocVector.push_back(t);
}
|