https://github.com/leozhang1/QtInterpreter 中 ParserException类所属头文件,代码展示
时间: 2023-09-30 19:12:44 浏览: 42
ParserException类所属的头文件是parserexception.h,下面是该类的代码展示:
```c++
#ifndef PARSEREXCEPTION_H
#define PARSEREXCEPTION_H
#include <exception>
#include <string>
namespace interpreter {
class ParserException : public std::exception {
public:
explicit ParserException(const std::string& message);
const char* what() const noexcept override;
private:
std::string m_message;
};
}
#endif // PARSEREXCEPTION_H
```
这是一个自定义异常类,继承自标准库中的std::exception类。它有一个构造函数,用于传入异常信息,以及一个what()函数,用于返回异常信息。在这个异常类中,我们可以自定义一些异常信息,比如语法错误等,然后在解释器中抛出该异常。这样,当出现异常时,我们就可以得到清晰的错误提示。
相关问题
https://github.com/leozhang1/QtInterpreter 中 ParserException.cpp代码展示
以下是 `ParserException.cpp` 的代码展示:
```c++
#include "ParserException.h"
ParserException::ParserException(const std::string& msg, size_t line, size_t column) :
std::runtime_error(msg), m_line(line), m_column(column) {}
const char* ParserException::what() const noexcept {
std::ostringstream os;
os << "ParserError: " << std::runtime_error::what() << " (line " << m_line << ", column " << m_column << ")";
return os.str().c_str();
}
```
这是一个 C++ 类,名为 `ParserException`。它继承自 `std::runtime_error`,并且包含了两个成员变量 `m_line` 和 `m_column`,分别表示出错的行号和列号。构造函数接受三个参数:`msg` 表示错误信息,`line` 表示出错的行号,`column` 表示出错的列号。`what()` 函数返回一个字符串,其中包含了错误类型、错误信息、以及出错的位置。
https://github.com/leozhang1/QtInterpreter ASTNode.h代码展示
下面是ASTNode.h的代码展示:
```c++
#ifndef ASTNODE_H
#define ASTNODE_H
#include <vector>
#include <string>
#include <memory>
// Forward declaration of ASTNode class
class ASTNode;
// Type definitions for shared pointer and vector of ASTNode objects
typedef std::shared_ptr<ASTNode> ASTNodePtr;
typedef std::vector<ASTNodePtr> ASTNodeList;
// Enum for different node types
enum class NodeType {
Program,
Statement,
Expression,
NumberLiteral,
StringLiteral,
Identifier,
BinaryExpression,
UnaryExpression,
AssignmentExpression,
VariableDeclaration,
FunctionDeclaration,
CallExpression,
IfStatement,
WhileStatement,
ForStatement,
ReturnStatement
};
// Class for AST nodes
class ASTNode {
public:
// Constructor
ASTNode(NodeType type);
// Destructor
virtual ~ASTNode() {}
// Getter and setter for node type
NodeType getType() const { return m_type; }
void setType(NodeType type) { m_type = type; }
// Getter and setter for node location in source code
int getLine() const { return m_line; }
void setLine(int line) { m_line = line; }
int getColumn() const { return m_column; }
void setColumn(int column) { m_column = column; }
// Add a child node to the list of children
void addChild(const ASTNodePtr& child);
// Get the list of children nodes
const ASTNodeList& getChildren() const { return m_children; }
// Accept method for visitor pattern
virtual void accept(class ASTVisitor& visitor);
private:
NodeType m_type; // Node type
int m_line; // Line number in source code
int m_column; // Column number in source code
ASTNodeList m_children; // List of child nodes
};
#endif // ASTNODE_H
```
这个头文件定义了 ASTNode 类,用于表示抽象语法树中的节点。它包括了节点的类型、位置信息、子节点列表等属性,并提供了一些方法用于添加和获取子节点、访问者模式等。此外,还定义了一些枚举类型用于表示不同的节点类型。