Skip to main content
summaryrefslogtreecommitdiffstats
blob: 405cb065d42be5a89edcda2957b1330025d6279d (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.eclipse.xpand3.node;

public class NodeUtil {
	public static int start(CompositeNode n) {
		if (n.getChildren().isEmpty()) {
			return -1;
		}
		return n.getChildren().get(0).start();
	}

	public static int end(CompositeNode n) {
		if (n.getChildren().isEmpty()) {
			return -1;
		}
		return n.getChildren().get(n.getChildren().size()-1).end();
	}

	public static int line(CompositeNode n) {
		if (n.getChildren().isEmpty()) {
			return -1;
		}
		return n.getChildren().get(0).line();
	}

	public static String text(CompositeNode cn) {
		StringBuffer buff = new StringBuffer();
		for (Node n : cn.getChildren()) {
			buff.append(n.text());
		}
		return buff.toString();
	}
	
	public static int start(LeafNode ln) {
		return ln.getToken().getStart();
	}

	public static int end(LeafNode ln) {
		return ln.getToken().getEnd();
	}

	public static int line(LeafNode ln) {
		return ln.getToken().getLine();
	}

	public static String text(LeafNode ln) {
		return ln.getToken().getText();
	}
	
	public static String toString(Node n) {
		if (n instanceof CompositeNode) {
			return toString((CompositeNode)n);
		} else if (n instanceof LeafNode) {
			return toString((LeafNode)n);
		}
		throw new IllegalArgumentException();
	}
	
	public static String toString(CompositeNode n) {
		String s = indent(n)+"Rule: "+n.getRule()+"\n";
		for (Node node : n.getChildren()) {
			s += toString(node);
		}
		return s;
	}
	
	public static String toString(LeafNode n) {
		return indent(n)+n.getToken().getText()+"\n";
	}
	
	private static String indent(Node n) {
		return n.eContainer()==null ? "" : "\t"+indent((Node) n.eContainer());
	}
	public static String serialize(Node n) {
		if (n instanceof CompositeNode) {
			return serialize((CompositeNode)n);
		} else if (n instanceof LeafNode) {
			return serialize((LeafNode)n);
		}
		throw new IllegalArgumentException();
	}
	public static String serialize(CompositeNode n) {
		String s = "";
		for (Node node : n.getChildren()) {
			s += serialize(node);
		}
		return s;
	}
	
	public static String serialize(LeafNode n) {
		return n.getToken().getText()+"\n";
	}
	
}

Back to the top