Skip to main content
summaryrefslogtreecommitdiffstats
blob: 37c6e610898fd73811511a236ec3fc7dce4b9300 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.eclipse.xpand3.parser;

import java.util.Set;

import org.antlr.runtime.CommonToken;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.TreeAdaptor;
import org.eclipse.xpand3.node.CompositeNode;
import org.eclipse.xpand3.node.LeafNode;
import org.eclipse.xpand3.node.LexedToken;
import org.eclipse.xpand3.node.Node;
import org.eclipse.xpand3.node.NodeFactory;

public abstract class AbstractXpand3NodeParser extends Xpand3Parser {

	private CompositeNode current = null;
	private Node rootNode = null;

	public AbstractXpand3NodeParser(TokenStream input) {
		super(input);
		setTreeAdaptor(new TreeAdaptor(){

			public void addChild(Object t, Object child) {
			}

			public Object becomeRoot(Object newRoot, Object oldRoot) {
				return null;
			}

			public Object becomeRoot(Token newRoot, Object oldRoot) {
				return null;
			}

			public Object create(Token payload) {
				return null;
			}

			public Object create(int tokenType, Token fromToken) {
				return null;
			}

			public Object create(int tokenType, Token fromToken, String text) {
				return null;
			}

			public Object create(int tokenType, String text) {
				return null;
			}

			public Object dupNode(Object treeNode) {
				return null;
			}

			public Object dupTree(Object tree) {
				return null;
			}

			public Object getChild(Object t, int i) {
				return null;
			}

			public int getChildCount(Object t) {
				return 0;
			}

			public String getText(Object t) {
				return null;
			}

			public Token getToken(Object t) {
				return null;
			}

			public int getTokenStartIndex(Object t) {
				return 0;
			}

			public int getTokenStopIndex(Object t) {
				return 0;
			}

			public int getType(Object t) {
				return 0;
			}

			public int getUniqueID(Object node) {
				return 0;
			}

			public boolean isNil(Object tree) {
				return false;
			}

			public Object nil() {
				return null;
			}

			public Object rulePostProcessing(Object root) {
				return null;
			}

			public void setText(Object t, String text) {
			}

			public void setTokenBoundaries(Object t, Token startToken,
					Token stopToken) {
			}

			public void setType(Object t, int type) {
			}});
	}

	protected abstract Set<String> normalizableRules();

	@Override
	public void ruleEntered(String rulename) {
		CompositeNode newOne = NodeFactory.eINSTANCE.createCompositeNode();
		newOne.setRule(rulename);
		newOne.setAlias(next);
		next=null;
		if (current != null) {
			current.getChildren().add(newOne);
		} else {
			rootNode = newOne;
		}
		current = newOne;
	}
	private String next=null;
	@Override
	protected void assignNextNodeTo(String alias) {
		this.next = alias;
	}

	
	@Override
	public void ruleLeft(String ruleName) {
		CompositeNode parent = (CompositeNode) current.eContainer();
		if (normalizableRules().contains(current.getRule())
				&& current.getChildren().size() == 1
				&& (current.getChildren().get(0) instanceof CompositeNode)) {
			Node child = current.getChildren().get(0);
			if (parent != null) {
				int i = parent.getChildren().indexOf(current);
				parent.getChildren().remove(i);
				parent.getChildren().add(i, child);
			} else {
				rootNode = child;
			}
		}
		current = parent;
	}

	@Override
	public void reportError(RecognitionException arg0) {
		throw new RuntimeException(getErrorMessage(arg0, getTokenNames()), arg0);
	}

	@Override
	public void tokenConsumed(String var, CommonToken ct) {
		if (ct.getText() != null) {
			LeafNode n = NodeFactory.eINSTANCE.createLeafNode();
			current.getChildren().add(n);
			if (var != null)
				n.setAlias(var);
			LexedToken myToken = NodeFactory.eINSTANCE.createLexedToken();
			n.setToken(myToken);
			myToken.setText(ct.getText());
			myToken.setStart(ct.getStartIndex());
			myToken.setEnd(ct.getStopIndex());
			myToken.setLine(ct.getLine());
		}
	}

	public Node getRootNode() {
		return rootNode;
	}

}

Back to the top