root / trunk / compiler / ooasCompiler / src / org / momut / ooas / ast / identifiers / Identifier.java @ 9
1 |
/**
|
---|---|
2 |
*
|
3 |
* OOAS Compiler
|
4 |
*
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology.
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of
|
9 |
* Technology. All rights reserved.
|
10 |
*
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
12 |
*
|
13 |
* If you modify the file please update the list of contributors below to in-
|
14 |
* clude your name. Please also stick to the coding convention of using TABs
|
15 |
* to do the basic (block-level) indentation and spaces for anything after
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious
|
17 |
* what this means.) Also, remove all trailing whitespace.
|
18 |
*
|
19 |
* Contributors:
|
20 |
* Willibald Krenn (AIT)
|
21 |
* Stephan Zimmerer (AIT)
|
22 |
* Markus Demetz (AIT)
|
23 |
* Christoph Czurda (AIT)
|
24 |
*
|
25 |
*/
|
26 |
|
27 |
|
28 |
package org.momut.ooas.ast.identifiers; |
29 |
|
30 |
import org.antlr.runtime.Token; |
31 |
import org.momut.ooas.ast.AstNode; |
32 |
import org.momut.ooas.ast.AstNodeTypeEnum; |
33 |
import org.momut.ooas.ast.IAst; |
34 |
import org.momut.ooas.ast.IAstVisitor; |
35 |
import org.momut.ooas.ast.IScope; |
36 |
import org.momut.ooas.ast.types.Type; |
37 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
38 |
import org.momut.ooas.utils.exceptions.NotImplementedException; |
39 |
|
40 |
/* ABSTRACT class Identifier.
|
41 |
* */
|
42 |
public abstract class Identifier extends AstNode implements IAst, Comparable<Identifier> |
43 |
{ |
44 |
protected int m_line; |
45 |
protected int m_column; |
46 |
protected IScope m_definingScope;
|
47 |
protected IdentifierKind m_identifierKind;
|
48 |
protected String m_tokenText; |
49 |
protected Type m_Type; |
50 |
|
51 |
// basic attributes of an Identifier
|
52 |
public final IdentifierKind kind() { return m_identifierKind; } // kind of identifier |
53 |
public final Type type() { return m_Type; } // type of identifier |
54 |
public final IScope definingScope() { return m_definingScope; } // scope where it's defined |
55 |
|
56 |
// from lexer
|
57 |
public final String tokenText() { return m_tokenText; } |
58 |
public final int line() { return m_line; } |
59 |
public final int column() { return m_column; } |
60 |
|
61 |
@Override
|
62 |
public AstNodeTypeEnum nodeType() { return AstNodeTypeEnum.identifier; } |
63 |
|
64 |
|
65 |
public Identifier(Token aToken,
|
66 |
Type aType,
|
67 |
IdentifierKind aKind, |
68 |
IScope aDefiningScope) |
69 |
{ |
70 |
m_tokenText = aToken.getText(); |
71 |
m_line = aToken.getLine(); |
72 |
m_column = aToken.getCharPositionInLine(); |
73 |
m_Type = aType; |
74 |
m_identifierKind = aKind; |
75 |
m_definingScope = aDefiningScope; |
76 |
} |
77 |
|
78 |
public Identifier(String name, Type aType, IdentifierKind aKind, IScope aDefiningScope) |
79 |
{ |
80 |
m_tokenText = name; |
81 |
m_line = 0;
|
82 |
m_column = 0;
|
83 |
m_Type = aType; |
84 |
m_identifierKind = aKind; |
85 |
m_definingScope = aDefiningScope; |
86 |
} |
87 |
|
88 |
public Identifier(Identifier toCopy)
|
89 |
{ |
90 |
m_tokenText = toCopy.m_tokenText; |
91 |
m_line = toCopy.m_line; |
92 |
m_column = toCopy.m_column; |
93 |
m_Type = toCopy.m_Type; |
94 |
m_identifierKind = toCopy.m_identifierKind; |
95 |
m_definingScope = toCopy.m_definingScope; |
96 |
} |
97 |
|
98 |
public /*virtual*/ Identifier Clone() |
99 |
{ |
100 |
throw new NotImplementedException(); |
101 |
} |
102 |
|
103 |
|
104 |
@Override
|
105 |
public /*virtual*/ void Accept(IAstVisitor visitor) |
106 |
{ |
107 |
throw new NotImplementedException(); |
108 |
} |
109 |
|
110 |
public final void SetType(Type aType) |
111 |
{ |
112 |
if (aType == null) |
113 |
throw new ArgumentException("New type can not be null."); |
114 |
|
115 |
m_Type = aType; |
116 |
} |
117 |
|
118 |
public final void SetTokenText(String aText) |
119 |
{ |
120 |
m_tokenText = aText; |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public /*override*/ String toString() |
125 |
{ |
126 |
return tokenText();
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
public int compareTo(Identifier obj) |
131 |
{ |
132 |
return tokenText().compareTo(obj.toString());
|
133 |
} |
134 |
|
135 |
@Override
|
136 |
public boolean equals(Object obj) { |
137 |
if (obj instanceof Identifier) |
138 |
return tokenText() == ((Identifier) obj).tokenText();
|
139 |
else
|
140 |
return false; |
141 |
} |
142 |
} |