示例#1
0
void ClassAnalyzer::parseFile(const QString& fileName)
{
    //qDebug() << "parsing file: " << fileName;
    m_currentFile = fileName;

    QFile file(fileName);

    if (!file.open(QFile::ReadOnly | QFile::Text))
    {
        qDebug() << "could not open file: " << fileName;
    }

    QTextStream in(&file);
    QString code = in.readAll();

    removeComments(code);

    /*QFile fileOut(fileName + "_removedComments");

    if (!fileOut.open(QFile::WriteOnly | QFile::Text))
    {
      qDebug() << "could not open file: " << fileName;
    }

    QTextStream out(&fileOut);
    out << code;
    fileOut.close();*/

    parseNamespaces(code);

    int position = 0;

    int numberOfClassDeclarationsPerFile = 0;
    while (position > -1)
    {
        position = code.indexOf(QRegExp("\\sclass\\s"), position + 1);

        if (position > -1)
        {
            position += 5;

            int posNextSemicolon = code.indexOf(";", position);
            int posNextOpeningCurlyBracket = code.indexOf("{", position);
            int posNextClosingRoundBracket = code.indexOf(")", position);
            int posNextClosingAngleBracket = code.indexOf(">", position);
            int posNextColon = code.indexOf(":", position);

            if (posNextOpeningCurlyBracket < 0)
            {
                //qDebug() << "no class body found";
                continue;
            }

            if (posNextSemicolon < posNextOpeningCurlyBracket)
            {
                //qDebug() << "ignore forward declaration, typedefs etc.";
                continue;
            }

            if (posNextClosingRoundBracket > -1 && posNextClosingRoundBracket < posNextOpeningCurlyBracket)
            {
                if (!(posNextColon > -1 && posNextColon < posNextOpeningCurlyBracket))
                {
                    //qDebug() << "ignore parameter etc.";
                    continue;
                }
            }

            if (posNextClosingAngleBracket > -1 && posNextClosingAngleBracket < posNextOpeningCurlyBracket)
            {
                if (posNextColon > -1 && posNextColon < posNextOpeningCurlyBracket)
                {
                    if (posNextClosingAngleBracket < posNextColon)
                    {
                        //qDebug() << "ignore template parameters";
                        continue;
                    }
                }
                else
                {
                    //qDebug() << "ignore template parameters";
                    continue;
                }
            }

            numberOfClassDeclarationsPerFile++;

            QString classDeclaration = code.mid(position, posNextOpeningCurlyBracket - position);
            removeSequences(classDeclaration, "<", ">");

            parseClassDeclaration(classDeclaration.simplified(), position);
        }
    }

    m_numberOfClassesPerFile.insert(numberOfClassDeclarationsPerFile, fileName);

    //qDebug() << "finished parsing file";
}
示例#2
0
Value InstructionParser::doParse() {
    
    if (context->in_class) {
        return parseClassDeclaration();
    }
    
    if (token_stream->has(Token::OP_ASSIGN)) {
        std::string name = current_token->value;
        expect(Token::NAME);
        expect(Token::OP_ASSIGN);
        
        expression_parser = new ExpressionParser(token_stream, current_token, context);    

        Value lv = expression_parser->parse();
        Value v = context->scope->set(name, lv);
        
        return v;
    }
    
    if (current_token->is(Token::CLASS)) {
        expect(Token::CLASS);
        std::string name = current_token->value;
        expect(Token::NAME);
        
        context->in_class = true;
        context->current_class_name = name;
        
        return NIL_VALUE;
    }
    
    // For now return an expr. has no really different effect than just typing this expr.
    if (current_token->is(Token::RETURN)) {
        expect(Token::RETURN);
        
        if (current_token->is(Token::END_OF_FILE) || current_token->is(Token::END_OF_LINE)) {
            return NIL_VALUE;
        }
        
    }
    
    expression_parser = new ExpressionParser(token_stream, current_token, context);    
    return expression_parser->parse();
    
    // if (current_token->is(Token::NAME)) {
    //     Token* token = current_token;
    //     name = current_token->value;
    //     expect(Token::NAME);
    //     
    //     if (current_token->is(Token::END_OF_LINE) || current_token->is(Token::END_OF_FILE)) {
    //         return context->scope->get(name);
    //     }
    //     
    //     if (context->getClass(name) != NULL) {
    //         return parseMethodCall(name);
    //     } else {
    //         if (current_token->is(Token::OP_ASSIGN)) {
    //             expect(Token::OP_ASSIGN);
    //             
    //             Value lv = doParse();
    //             Value v = context->scope->set(name, lv);
    //             
    //             return v;
    //         }
    //     }
    // }
}