Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f09d570f002831c2d46bff14da4a56510d9c7e0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*******************************************************************************
 * Copyright (c) 2008, 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.ASTGenericVisitor;
import org.eclipse.cdt.core.dom.ast.IASTNode;

/**
 * Utility class to search for siblings of an ast node
 */
public class ASTNodeSearch extends ASTGenericVisitor {
	private static final int LEFT = 0;
	private static final int RIGHT= 1;
	private int fMode;
	private IASTNode fLeft;
	private IASTNode fRight;
	private final IASTNode fNode;
	private final IASTNode fParent;

	public ASTNodeSearch(IASTNode node) {
		super(true);
		fNode= node;
		fParent= node.getParent();
	}
	
	public IASTNode findLeftSibling() {
		if (fParent == null)
			return null;

		fMode= LEFT;
		fLeft= fRight= null;
		fParent.accept(this);
		return fLeft;
	}

	public IASTNode findRightSibling() {
		if (fParent == null)
			return null;

		fMode= RIGHT;
		fLeft= fRight= null;
		fParent.accept(this);
		return fRight;
	}

	@Override
	protected int genericVisit(IASTNode node) {
		if (node == fParent)
			return PROCESS_CONTINUE;
		
		switch(fMode) {
		case LEFT:
			if (node == fNode)
				return PROCESS_ABORT;
			fLeft= node;
			return PROCESS_SKIP;
		case RIGHT:
			if (node == fNode) {
				fLeft= fNode;
			} else if (fLeft != null) {
				fRight= node;
				return PROCESS_ABORT;
			}
			return PROCESS_SKIP;
		}
		return PROCESS_SKIP;
	}
}

Back to the top