aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeAdaptor.as
blob: b89fc3b33ad9903477b19e00c1a888319aa2e691 (plain)
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
package org.antlr.runtime.tree {
	import org.antlr.runtime.CommonToken;
	import org.antlr.runtime.Token;
	import org.antlr.runtime.TokenConstants;
	
	/** A TreeAdaptor that works with any Tree implementation.  It provides
	 *  really just factory methods; all the work is done by BaseTreeAdaptor.
	 *  If you would like to have different tokens created than ClassicToken
	 *  objects, you need to override this and then set the parser tree adaptor to
	 *  use your subclass.
	 *
	 *  To get your parser to build nodes of a different type, override
     *  create(Token), errorNode(), and to be safe, YourTreeClass.dupNode().
     *  dupNode is called to duplicate nodes during rewrite operations.
	 */
	public class CommonTreeAdaptor extends BaseTreeAdaptor {
		/** Duplicate a node.  This is part of the factory;
		 *	override if you want another kind of node to be built.
		 *
		 *  I could use reflection to prevent having to override this
		 *  but reflection is slow.
		 */
		public override function dupNode(t:Object):Object {
			if ( t==null ) {
				return null;
			}
			return (Tree(t)).dupNode();
		}
	
		public override function createWithPayload(payload:Token):Object {
			return CommonTree.createFromToken(payload);
		}
	
		/** Tell me how to create a token for use with imaginary token nodes.
		 *  For example, there is probably no input symbol associated with imaginary
		 *  token DECL, but you need to create it as a payload or whatever for
		 *  the DECL node as in ^(DECL type ID).
		 *
		 *  If you care what the token payload objects' type is, you should
		 *  override this method and any other createToken variant.
		 */
		public override function createTokenFromType(tokenType:int, text:String):Token {
			return new CommonToken(tokenType, text);
		}
	
		/** Tell me how to create a token for use with imaginary token nodes.
		 *  For example, there is probably no input symbol associated with imaginary
		 *  token DECL, but you need to create it as a payload or whatever for
		 *  the DECL node as in ^(DECL type ID).
		 *
		 *  This is a variant of createToken where the new token is derived from
		 *  an actual real input token.  Typically this is for converting '{'
		 *  tokens to BLOCK etc...  You'll see
		 *
		 *    r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;
		 *
		 *  If you care what the token payload objects' type is, you should
		 *  override this method and any other createToken variant.
		 */
		public override function createToken(fromToken:Token):Token {
			return CommonToken.cloneToken(fromToken);
		}
	
		/** Track start/stop token for subtree root created for a rule.
		 *  Only works with Tree nodes.  For rules that match nothing,
		 *  seems like this will yield start=i and stop=i-1 in a nil node.
		 *  Might be useful info so I'll not force to be i..i.
		 */
		public override function setTokenBoundaries(t:Object, startToken:Token, stopToken:Token):void {
			if ( t==null ) {
				return;
			}
			var start:int = 0;
			var stop:int = 0;
			if ( startToken!=null ) {
				start = startToken.tokenIndex;
			}
			if ( stopToken!=null ) {
				stop = stopToken.tokenIndex;
			}
			Tree(t).tokenStartIndex = start;
			Tree(t).tokenStopIndex = stop;
		}
	
		public override function getTokenStartIndex(t:Object):int {
			if ( t==null ) {
				return -1;
			}
			return Tree(t).tokenStartIndex;
		}
	
		public override function getTokenStopIndex(t:Object):int {
			if ( t==null ) {
				return -1;
			}
			return Tree(t).tokenStopIndex;
		}
	
		public override function getText(t:Object):String {
			if ( t==null ) {
				return null;
			}
			return Tree(t).text;
		}
	
	    public override function getType(t:Object):int {
			if ( t==null ) {
				return TokenConstants.INVALID_TOKEN_TYPE;
			}
			return Tree(t).type;;
		}
	
		/** What is the Token associated with this node?  If
		 *  you are not using CommonTree, then you must
		 *  override this in your own adaptor.
		 */
		public override function getToken(t:Object):Token {
			if ( t is CommonTree ) {
				return CommonTree(t).token;
			}
			return null; // no idea what to do
		}
	
		public override function getChild(t:Object, i:int):Object {
			if ( t==null ) {
				return null;
			}
	        return Tree(t).getChild(i);
	    }
	
	    public override function getChildCount(t:Object):int {
			if ( t==null ) {
				return 0;
			}
	        return Tree(t).childCount;
	    }

		public override function getParent(t:Object):Object {
		    if (t == null) {
		        return null;
		    }
			return Tree(t).parent;
		}
	
		public override function setParent(t:Object, parent:Object):void {
			if (t != null) {
			     Tree(t).parent = Tree(parent);
			}
		}
	
		public override function getChildIndex(t:Object):int {
		    if (t == null) {
		        return 0;
		    }
			return Tree(t).childIndex;
		}
	
		public override function setChildIndex(t:Object, index:int):void {
			if (t != null) {
			     Tree(t).childIndex = index;
			}
		}
	
		public override function replaceChildren(parent:Object, startChildIndex:int, stopChildIndex:int, t:Object):void {
			if ( parent!=null ) {
				Tree(parent).replaceChildren(startChildIndex, stopChildIndex, t);
			}
		}
		
	}
	
}