File indexing completed on 2024-04-14 14:47:46

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003     SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "expressionparser.h"
0009 #include "parsesession.h"
0010 #include "editorintegrator.h"
0011 #include "phpast.h"
0012 #include "phpparser.h"
0013 #include "phpdebugvisitor.h"
0014 #include "expressionvisitor.h"
0015 
0016 #include <language/duchain/ducontext.h>
0017 #include <language/duchain/duchainlock.h>
0018 #include <language/duchain/duchain.h>
0019 
0020 #include "duchaindebug.h"
0021 
0022 using namespace KDevelop;
0023 
0024 namespace Php
0025 {
0026 
0027 ExpressionParser::ExpressionParser(bool debug)
0028         : m_debug(debug), m_createProblems(false)
0029 {
0030 }
0031 
0032 void ExpressionParser::setCreateProblems(bool v)
0033 {
0034     m_createProblems = v;
0035 }
0036 
0037 ExpressionEvaluationResult ExpressionParser::evaluateType(const QByteArray& expression, DUContextPointer context,
0038                                                           const CursorInRevision &offset)
0039 {
0040     if (m_debug)
0041         qCDebug(DUCHAIN) << "==== .Evaluating ..:\n" << expression;
0042 
0043     auto* session = new ParseSession();
0044     session->setContents(expression);
0045     Parser* parser = session->createParser(Parser::DefaultState);
0046     ExprAst* ast = nullptr;
0047     if (!parser->parseExpr(&ast)) {
0048         qCDebug(DUCHAIN) << "Failed to parse \"" << expression << "\"";
0049         delete session;
0050         delete parser;
0051         return ExpressionEvaluationResult();
0052     }
0053     ast->ducontext = dynamic_cast<DUContext*>(context.data());
0054 
0055     auto* editor = new EditorIntegrator(session);
0056     ExpressionEvaluationResult ret = evaluateType(ast, editor, offset);
0057     delete editor;
0058     delete session;
0059     delete parser;
0060 
0061     return ret;
0062 }
0063 
0064 ExpressionEvaluationResult ExpressionParser::evaluateType(AstNode* ast, EditorIntegrator* editor)
0065 {
0066     return evaluateType(ast, editor, CursorInRevision::invalid());
0067 }
0068 
0069 ExpressionEvaluationResult ExpressionParser::evaluateType(AstNode* ast, EditorIntegrator* editor,
0070                                                           const CursorInRevision &offset)
0071 {
0072     if (m_debug) {
0073         qCDebug(DUCHAIN) << "===== AST:";
0074         DebugVisitor debugVisitor(editor->parseSession()->tokenStream(), editor->parseSession()->contents());
0075         debugVisitor.visitNode(ast);
0076     }
0077 
0078     ExpressionVisitor v(editor);
0079     v.setOffset(offset);
0080     v.setCreateProblems(m_createProblems);
0081     v.visitNode(ast);
0082 
0083     return v.result();
0084 }
0085 
0086 }