View Javadoc

1   /*
2    * LinkedListTokenSource.java
3    * 
4    * Copyright (c) 2006 David Holroyd
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package uk.co.badgersinfoil.metaas.impl.antlr;
20  
21  import org.antlr.runtime.Token;
22  import org.antlr.runtime.TokenSource;
23  
24  
25  public class LinkedListTokenSource implements TokenSource {
26  	private TokenSource delegate;
27  	private LinkedListToken last = null;
28  
29  	public LinkedListTokenSource(TokenSource delegate) {
30  		this.delegate = delegate;
31  	}
32  
33  	public Token nextToken() {
34  		LinkedListToken curr = createToken(delegate.nextToken());
35  		if (last != null) {
36  			last.setNext(curr);
37  		}
38  		curr.setPrev(last);
39  		last = curr;
40  		return curr;
41  	}
42  
43  	private LinkedListToken createToken(Token tok) {
44  		LinkedListToken result = new LinkedListToken(tok.getType(), tok.getText());
45  		result.setLine(tok.getLine());
46  		result.setCharPositionInLine(tok.getCharPositionInLine());
47  		result.setChannel(tok.getChannel());
48  		result.setTokenIndex(tok.getTokenIndex());
49  		return result;
50  	}
51  
52  	/**
53  	 * Redefines the TokenSource to which this object delagates the task of
54  	 * token creation.  This can be used to switch Lexers when an island
55  	 * grammar is required, for instance.
56  	 */
57  	public void setDelegate(TokenSource delegate) {
58  		this.delegate = delegate;
59  	}
60  
61  	/**
62  	 * Overrides the 'last' token which this object is remembering in order
63  	 * to build next/previous links.
64  	 */
65  	public void setLast(LinkedListToken tok) {
66  	}
67  }