Skip to main content
summaryrefslogtreecommitdiffstats
blob: 53e7d5c8501810e93548b6a48908b5ccbf16ffdd (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
/*******************************************************************************
 * Copyright (c) 2001 Rational Software Corp. and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     Rational Software - initial implementation
 ******************************************************************************/
package org.eclipse.cdt.internal.core.parser;

import org.eclipse.cdt.core.parser.IToken;

public class Token implements IToken {

	public Token(int t, String i, IScannerContext context ) {
		type = t;
		image = i;
		filename = context.getFilename();
		offset = context.getOffset() - image.length() - context.undoStackSize();
        macroOffset = context.getMacroOffset();
        macroLength = context.getMacroLength();
		
		if( type == tLSTRING || type == tSTRING || type == tCHAR ){
			offset--;
		}
	}
	
	public Token(int t, String i) {
		type = t;
		image = i;
	}
	
	public String toString()
	{
		return "Token type=" + type + "  image =" + image + " offset=" + offset; 	
	}
	
	public int type;
	public int getType() { return type; }
	
	protected String image;
	public String getImage() { return image; }

	public  String filename;
    
	protected int offset;
    protected int macroOffset = -1;
    protected int macroLength = -1;
    // All the tokens generated by the macro expansion 
    // will have dimensions (offset and length) equal to the expanding symbol.
	public int getOffset() { return (macroOffset < 0) ? offset : macroOffset; }
	public int getLength() { return (macroLength < 0) ? image.length() : macroLength; }
	public int getEndOffset() { return getOffset() + getLength(); }
	
	
	public int getDelta( IToken other )
	{
		return other.getOffset() + other.getLength() - getOffset();
	}
	
	private IToken next;
	public IToken getNext() { return next; }
	public void setNext(IToken t) { next = t; }
	
	public boolean looksLikeExpression()
	{
		switch( getType() )
		{
			case tINTEGER:
			case t_false:
			case t_true:
			case tSTRING:
			case tLSTRING:
			case tFLOATINGPT:
			case tCHAR:
			case tAMPER:
			case tDOT:
			case tLPAREN:
			case tMINUS:
			case tSTAR: 
			case tPLUS: 
			case tNOT:
			case tCOMPL:
				return true;
			default:
				break;
		}
	
		
		return false;
	}
	
	public boolean isPointer()
	{
		return (getType() == tAMPER || getType() == tSTAR);
	}
	
	public boolean isOperator()
	{
		switch( getType() )
		{
			case IToken.t_new:
			case IToken.t_delete:
			case IToken.tPLUS:
			case IToken.tMINUS:
			case IToken.tSTAR:
			case IToken.tDIV:
			case IToken.tXOR:
			case IToken.tMOD:
			case IToken.tAMPER:
			case IToken.tBITOR:
			case IToken.tCOMPL:
			case IToken.tNOT:
			case IToken.tASSIGN:
			case IToken.tLT:
			case IToken.tGT:
			case IToken.tPLUSASSIGN:
			case IToken.tMINUSASSIGN:
			case IToken.tSTARASSIGN:
			case IToken.tDIVASSIGN:
			case IToken.tMODASSIGN:
			case IToken.tBITORASSIGN:
			case IToken.tAMPERASSIGN:
			case IToken.tXORASSIGN:
			case IToken.tSHIFTL:
			case IToken.tSHIFTR:
			case IToken.tSHIFTLASSIGN:
			case IToken.tSHIFTRASSIGN:
			case IToken.tEQUAL:
			case IToken.tNOTEQUAL:
			case IToken.tLTEQUAL:
			case IToken.tGTEQUAL:
			case IToken.tAND:
			case IToken.tOR:
			case IToken.tINCR:
			case IToken.tDECR:
			case IToken.tCOMMA:
			case IToken.tARROW:
			case IToken.tARROWSTAR:
				return true;
			default:
				return false;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.IToken#setImage()
	 */
	public void setImage( String i ) {
		image = i; 
	}
	
	

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object other) {
		if( other == null ) return false;
		if( !( other instanceof IToken ) ) 
			return false;
		if( !(((IToken)other).getImage().equals( image ))) 
			return false;
		if( ((IToken)other).getType() != type ) 
			return false;
		return true;
	}

}

Back to the top