diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java index 956b22e106e..9013a697a8a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java @@ -13,6 +13,7 @@ package org.eclipse.cdt.core.dom.ast; import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator; import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; import org.eclipse.cdt.core.parser.IScanner; +import org.eclipse.cdt.core.parser.IToken; /** @@ -166,4 +167,39 @@ public interface INodeFactory { public IASTCompositeTypeSpecifier newCompositeTypeSpecifier(int key, IASTName name); + /** + * Provides the offsets for a node. The offsets are artificial numbers that identify the + * position of a node in the translation unit. They are not file-offsets. You can obtain + * valid offsets via {@link IToken#getOffset()} or {@link IToken#getEndOffset()} from tokens + * provided by the scanner for this translation unit. + * May throw an exception when the node provided was not created by this factory. + * @param node a node created by this factory + * @offset the offset (inclusive) for the node + * @param endOffset the end offset (exclusive) for the node + * @see #newTranslationUnit(IScanner) + * @since 5.2 + */ + public void setOffsets(IASTNode node, int offset, int endOffset); + + /** + * Provides the end offset for a node. The offset is an artificial numbers that identifies the + * position of a node in the translation unit. It is not a file-offset. You can obtain a + * valid offset via {@link IToken#getEndOffset()} from a token provided by the scanner for + * this translation unit. + * May throw an exception when the node provided was not created by this factory. + * @param node a node created by this factory + * @param endOffset the end offset (exclusive) for the node + * @see #newTranslationUnit(IScanner) + * @since 5.2 + */ + void setEndOffset(IASTNode node, int endOffset); + + /** + * Adjusts the end-offset of a node to be the same as the end-offset of a given node. + * May throw an exception when either one of the nodes provided was not created by this factory. + * @param node a node created by this factory + * @param endNode a node created by this factory defining the end for the other node. + * @since 5.2 + */ + void setEndOffset(IASTNode node, IASTNode endNode); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/NodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/NodeFactory.java new file mode 100644 index 00000000000..bcc6d2caeef --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/NodeFactory.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2009 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Markus Schorn - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.dom.parser; + +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.INodeFactory; + +/** + * Abstract base class for node factories. + */ +public abstract class NodeFactory implements INodeFactory { + + public final void setOffsets(IASTNode node, int offset, int endOffset) { + ((ASTNode) node).setOffsetAndLength(offset, endOffset-offset); + } + + public final void setEndOffset(IASTNode node, int endOffset) { + ASTNode a= (ASTNode) node; + a.setLength(endOffset - a.getOffset()); + } + + public final void setEndOffset(IASTNode node, IASTNode endNode) { + ASTNode a= (ASTNode) node; + ASTNode e= (ASTNode) endNode; + a.setLength(e.getOffset() + e.getLength() - a.getOffset()); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java index 367fe2ded1e..de7c37d7d38 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java @@ -6,7 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM Corporation - initial API and implementation + * Mike Kucera (IBM Corporation) - initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.c; @@ -78,15 +79,14 @@ import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator; import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTSimpleDeclSpecifier; import org.eclipse.cdt.core.parser.IScanner; +import org.eclipse.cdt.internal.core.dom.parser.NodeFactory; /** * Abstract factory implementation that creates AST nodes for C99. * These can be overridden in subclasses to change the * implementations of the nodes. - * - * @author Mike Kucera */ -public class CNodeFactory implements ICNodeFactory { +public class CNodeFactory extends NodeFactory implements ICNodeFactory { private static final CNodeFactory DEFAULT_INSTANCE = new CNodeFactory(); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java index 4d6bbc80a2b..367c2467411 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java @@ -7,6 +7,7 @@ * * Contributors: * Mike Kucera (IBM) - initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; @@ -104,12 +105,13 @@ import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointerToMember; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier; import org.eclipse.cdt.core.parser.IScanner; +import org.eclipse.cdt.internal.core.dom.parser.NodeFactory; /** * Abstract factory implementation that creates C++ AST nodes. */ -public class CPPNodeFactory implements ICPPNodeFactory { +public class CPPNodeFactory extends NodeFactory implements ICPPNodeFactory { private static final CPPNodeFactory DEFAULT_INSTANCE = new CPPNodeFactory();