File indexing completed on 2024-05-19 15:42:36

0001 /*
0002     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2007 Piyush verma <piyush.verma@gmail.com>
0004     SPDX-FileCopyrightText: 2010-2014 Sven Brauch <svenbrauch@googlemail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "parsesession.h"
0010 #include "astbuilder.h"
0011 
0012 #include <QDebug>
0013 #include "parserdebug.h"
0014 
0015 using namespace KDevelop;
0016 
0017 namespace Python
0018 {
0019 
0020 ParseSession::ParseSession()
0021     : ast(nullptr)
0022     , m_currentDocument(KDevelop::IndexedString("<invalid>"))
0023     , m_futureModificationRevision()
0024 {
0025 }
0026 ParseSession::~ParseSession()
0027 {
0028     ast.clear();
0029 }
0030 
0031 void ParseSession::setCurrentDocument(const IndexedString& url)
0032 {
0033     m_currentDocument = url;
0034 }
0035 
0036 IndexedString ParseSession::currentDocument()
0037 {
0038     return m_currentDocument;
0039 }
0040 
0041 const ModificationRevision& ParseSession::futureModificationRevision() const
0042 {
0043     return m_futureModificationRevision;
0044 }
0045 
0046 void ParseSession::setFutureModificationRevision(const ModificationRevision& revision)
0047 {
0048     m_futureModificationRevision = revision;
0049 }
0050 
0051 QString ParseSession::contents() const
0052 {
0053     return m_contents;
0054 }
0055 
0056 void ParseSession::setContents( const QString& contents )
0057 {
0058     m_contents = contents;
0059 }
0060 
0061 QPair<CodeAst::Ptr, bool> ParseSession::parse()
0062 {
0063     AstBuilder pythonparser;
0064     QPair<CodeAst::Ptr, bool> matched;
0065     matched.first = pythonparser.parse(m_currentDocument.toUrl(), m_contents);
0066     matched.second = matched.first ? true : false; // check whether an AST was returned and react accordingly
0067     
0068     m_problems = pythonparser.m_problems;
0069     
0070     if( matched.second )
0071     {
0072         qCDebug(KDEV_PYTHON_PARSER) << "Successfully parsed";
0073     }else
0074     {
0075         matched.first.clear();
0076         qCDebug(KDEV_PYTHON_PARSER) << "Couldn't parse content";
0077     }
0078     return matched;
0079 }
0080 
0081 }