antlrext.h

来自「基于ANTLR的简单编译器源码version0.1」· C头文件 代码 · 共 87 行

H
87
字号
/*
	作用:扩展CommonAST 以获得源文件的行信息。用于错误定位。.
	
*/

#ifndef _ASTNODE_
#define _ASTNODE_

#include "antlr/ASTFactory.hpp"
#include "antlr/CommonAST.hpp"

class ASTNodeExt;

typedef ANTLR_USE_NAMESPACE(antlr)ASTRefCount<ASTNodeExt> RefASTNodeExt;

class ASTNodeExt : public ANTLR_USE_NAMESPACE(antlr)CommonAST {
public:
	// 拷贝构造函数
	ASTNodeExt(const ASTNodeExt& other): 
		CommonAST(other),
		line(other.line),
		isTopLevelCall(false) {}

	//默认构造函数
	ASTNodeExt():
		CommonAST(), 
		line(0),
		isTopLevelCall(false) {}

	virtual ~ASTNodeExt() {}

	// get the line number of the node (or try to derive it from the child node
	virtual int getLine() const {
		// most of the time the line number is not set if the node is a
		// imaginary one. Usually this means it has a child. Refer to the
		// child line number. Of course this could be extended a bit.
		// based on an example by Peter Morling.
		if (line != 0)
			return line;
		
		ASTNodeExt* node = RefASTNodeExt(getFirstChild());
		while (node && ! node->getLine()) {
			node = RefASTNodeExt(node->getNextSibling());
		}
		
		if (node)
			return node->getLine();
		else
			return 0;
	}

	virtual void setLine(int l) {
		line = l;
	}

	virtual void initialize(int t, const ANTLR_USE_NAMESPACE(std)string& txt) {
		CommonAST::initialize(t,txt);
		line = 0;
	}

	virtual void initialize(ANTLR_USE_NAMESPACE(antlr)RefToken t) {
		CommonAST::initialize(t);
		line = t->getLine();
	}

	virtual void initialize(RefASTNodeExt ast) {
		CommonAST::initialize(ANTLR_USE_NAMESPACE(antlr)RefAST(ast));
		line = ast->getLine();
	}

	// 拷贝兄弟结点或子结点
	virtual ANTLR_USE_NAMESPACE(antlr)RefAST clone() {
		return ANTLR_USE_NAMESPACE(antlr)RefAST(new ASTNodeExt(*this));
	}

	static ANTLR_USE_NAMESPACE(antlr)RefAST factory() {
		return ANTLR_USE_NAMESPACE(antlr)RefAST(RefASTNodeExt(new ASTNodeExt()));
	}

	bool isTopLevelCall;

private:
	int line;
};

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?