File indexing completed on 2024-04-14 04:31:15

0001 /* This file is part of KDevelop
0002  *
0003  * Copyright (C) 2011-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #ifndef RUBY_PARSER_H
0020 #define RUBY_PARSER_H
0021 
0022 #include <language/duchain/problem.h>
0023 
0024 #include <parser/ast.h>
0025 #include <parser/export.h>
0026 
0027 namespace ruby {
0028 
0029 /**
0030  * @class Parser
0031  *
0032  * This class represents an interface to the "pure" parser and it may
0033  * be used across the plugin.
0034  */
0035 class KDEVRUBYPARSER_EXPORT Parser
0036 {
0037     /// Convenient alias that packs a DUContextPointer and a RangeInRevision.
0038     using SimpleUse = QPair<KDevelop::DUContextPointer,
0039                             KDevelop::RangeInRevision>;
0040 
0041 public:
0042     Parser();
0043     Parser(const KDevelop::IndexedString &fileName, const QByteArray &contents);
0044     virtual ~Parser();
0045 
0046     /**
0047      * Set the contents of the document.
0048      *
0049      * @param contents the contents of the file to parse.
0050      */
0051     void setContents(const QByteArray &contents);
0052 
0053     /**
0054      * Set the version of the Ruby interpreter.
0055      */
0056     void setRubyVersion(enum ruby_version version);
0057 
0058     /**
0059      * This method parses the current document.
0060      *
0061      * @return the generated Ast.
0062      */
0063     Ast * parse();
0064 
0065     /**
0066      * Implemented to make the AbstractUseBuilder happy.
0067      */
0068     void mapAstUse(Ast *node, const SimpleUse &use);
0069 
0070     /**
0071      * @return a QString that represents the value of the node
0072      * (not the node's name).
0073      */
0074     const QString symbol(const Node *node) const;
0075 
0076 private:
0077     /**
0078      * @internal called when there are errors in the current document and we
0079      * want to append one of them in a list of ProblemPointer's.
0080      *
0081      * @param error the error provided to this method.
0082      */
0083     void appendProblem(const struct error_t *error);
0084 
0085 public:
0086     QVector<KDevelop::ProblemPointer> problems;
0087     KDevelop::IndexedString currentDocument;
0088     Ast *ast;
0089 
0090 private:
0091     QByteArray m_contents;
0092     enum ruby_version m_version;
0093 };
0094 
0095 }
0096 
0097 #endif // RUBY_PARSER_H
0098