Ejemplo n.º 1
0
 //public interface
 ParseResultPtr parse(const FileReaderPtr& file) {
   ParseResultPtr ret = newParseResult();
   ret->filename = file->filename();
   if (!file->isOpen()) {
     ret->addDiag(Diagnostic(DiagnosticType_Error, "Can't open file '" + file->filename() + "'"));
     return ret;
   }
   Parser p(file);
   return p.result();
 }
Ejemplo n.º 2
0
ParseResultPtr PackageManager::parseFile(const FileReaderPtr& file)
{
    qi::Path fsfname = qi::Path(file->filename());
    std::string filename = fsfname.absolute().str();
    if (!fsfname.isRegularFile())
        throw std::runtime_error(file->filename() + " is not a regular file");
    qiLogVerbose() << "Parsing file: " << filename;
    if (_sources.find(filename) != _sources.end()) {
        qiLogVerbose() << "already parsed, skipping '" << filename << "'";
        return package(_sources[filename])->_contents[filename];
    }

    ParseResultPtr ret = qilang::parse(file);
    if (addFileToPackage(filename, file, ret))
        _sources[filename] = ret->package;
    return ret;
}
Ejemplo n.º 3
0
 Parser::Parser(const FileReaderPtr &file)
   : file(file)
   , _result(newParseResult())
   , _parsed(false)
   , parser(this)
   , linesSinceLastComment(0)
 {
   _result->filename = file->filename();
   qilang_lex_init(&scanner);
   qilang_set_extra(this, scanner);
 }
Ejemplo n.º 4
0
/** 1 / Check for missing or multiple package declaration
 *  2 / Check that the directory path and package name match
 *  3 / register the content of the file to the package
 */
bool PackageManager::addFileToPackage(const std::string& absfile, const FileReaderPtr& file, ParseResultPtr& pr) {

    // 1
    NodePtrVector result;
    result = findNode(pr->ast, NodeType_Package);
    if (result.size() == 0) {
        pr->addDiag(Diagnostic(DiagnosticType_Error, "missing package declaration", Location(file->filename())));
        return false;
    }
    if (result.size() > 1) {
        for (unsigned i = 1; i < result.size(); ++i) {
            pr->addDiag(Diagnostic(DiagnosticType_Error, "extra package declaration", result.at(i)->loc()));

        }
        pr->addDiag(Diagnostic(DiagnosticType_Info, "previous declared here", result.at(0)->loc()));
        return false;
    }
    std::string pkgname = extractPackageName(result.at(0));
    // 2
    qi::Path pf(file->filename());
    StringVector leafs = splitPkgName(pkgname);


    qi::Path dirname;
    qi::Path cur = pf.parent().absolute();
    for (unsigned i = 0; i < leafs.size(); ++i)
    {
        dirname = qi::Path(cur.filename()) / dirname;
        cur = cur.parent();
        if (cur.isEmpty())
            break;
    }

    qi::Path p = pf.parent().absolute();
    for (int i = leafs.size() - 1; i >= 0; --i) {
        std::string par = p.filename();
        if (par != leafs.at(i)) {
            pr->addDiag(Diagnostic(DiagnosticType_Error,
                                   "package name '" + pkgname + "' do not match parent directory name '" + (std::string)dirname + "'",
                                   result.at(0)->loc()));
            return false;
        }
        p = p.parent();
    }

    std::string pkgpath = p.absolute().str();

    addInclude(pkgpath);
    addPackage(pkgname);
    pr->package = pkgname;
    package(pkgname)->setContent(absfile, pr);
    return true;
}