~ubuntu-branches/ubuntu/utopic/kdevelop-php/utopic

« back to all changes in this revision

Viewing changes to duchain/expressionparser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2010-01-17 17:10:22 UTC
  • Revision ID: james.westby@ubuntu.com-20100117171022-q2xlgd9ekewo2ijx
Tags: upstream-1.0.0~beta2
ImportĀ upstreamĀ versionĀ 1.0.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   This file is part of KDevelop                                         *
 
3
 *   Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>     *
 
4
 *   Copyright 2008 Niko Sams <niko.sams@gmail.com>                        *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU Library General Public License as       *
 
8
 *   published by the Free Software Foundation; either version 2 of the    *
 
9
 *   License, or (at your option) any later version.                       *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this program; if not, write to the                 *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 
20
 ***************************************************************************/
 
21
#include "expressionparser.h"
 
22
#include "parsesession.h"
 
23
#include "editorintegrator.h"
 
24
#include "phpast.h"
 
25
#include "phpparser.h"
 
26
#include "phpdebugvisitor.h"
 
27
#include "expressionvisitor.h"
 
28
 
 
29
#include <language/duchain/ducontext.h>
 
30
#include <language/duchain/duchainlock.h>
 
31
#include <language/duchain/duchain.h>
 
32
 
 
33
#include <kdebug.h>
 
34
using namespace KDevelop;
 
35
 
 
36
namespace Php
 
37
{
 
38
 
 
39
ExpressionParser::ExpressionParser(bool debug)
 
40
        : m_debug(debug), m_createProblems(false)
 
41
{
 
42
}
 
43
 
 
44
void ExpressionParser::setCreateProblems(bool v)
 
45
{
 
46
    m_createProblems = v;
 
47
}
 
48
 
 
49
ExpressionEvaluationResult ExpressionParser::evaluateType(const QByteArray& expression, DUContextPointer context,
 
50
                                                          const SimpleCursor &offset)
 
51
{
 
52
    if (m_debug)
 
53
        kDebug() << "==== .Evaluating ..:" << endl << expression;
 
54
 
 
55
    ParseSession* session = new ParseSession();
 
56
    session->setContents(expression);
 
57
    Parser* parser = session->createParser(Parser::DefaultState);
 
58
    ExprAst* ast = 0;
 
59
    if (!parser->parseExpr(&ast)) {
 
60
        kDebug() << "Failed to parse \"" << expression << "\"";
 
61
        delete session;
 
62
        delete parser;
 
63
        return ExpressionEvaluationResult();
 
64
    }
 
65
    ast->ducontext = dynamic_cast<DUContext*>(context.data());
 
66
 
 
67
    EditorIntegrator* editor = new EditorIntegrator(session);
 
68
    ExpressionEvaluationResult ret = evaluateType(ast, editor, offset);
 
69
    delete editor;
 
70
    delete session;
 
71
    delete parser;
 
72
 
 
73
    return ret;
 
74
}
 
75
 
 
76
ExpressionEvaluationResult ExpressionParser::evaluateType(AstNode* ast, EditorIntegrator* editor)
 
77
{
 
78
    return evaluateType(ast, editor, SimpleCursor::invalid());
 
79
}
 
80
 
 
81
ExpressionEvaluationResult ExpressionParser::evaluateType(AstNode* ast, EditorIntegrator* editor,
 
82
                                                          const SimpleCursor &offset)
 
83
{
 
84
    if (m_debug) {
 
85
        kDebug() << "===== AST:";
 
86
        DebugVisitor debugVisitor(editor->parseSession()->tokenStream(), editor->parseSession()->contents());
 
87
        debugVisitor.visitNode(ast);
 
88
    }
 
89
 
 
90
    ExpressionVisitor v(editor);
 
91
    v.setOffset(offset);
 
92
    v.setCreateProblems(m_createProblems);
 
93
    v.visitNode(ast);
 
94
 
 
95
    return v.result();
 
96
}
 
97
 
 
98
}