View Javadoc

1   /********************************************************************************
2    *
3    *	Copyright (c) 2008 Fujitsu Services Ltd.
4    *
5    *	Author: Nick Battle
6    *
7    *	This file is part of VDMJ.
8    *
9    *	VDMJ is free software: you can redistribute it and/or modify
10   *	it under the terms of the GNU General Public License as published by
11   *	the Free Software Foundation, either version 3 of the License, or
12   *	(at your option) any later version.
13   *
14   *	VDMJ is distributed in the hope that it will be useful,
15   *	but WITHOUT ANY WARRANTY; without even the implied warranty of
16   *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   *	GNU General Public License for more details.
18   *
19   *	You should have received a copy of the GNU General Public License
20   *	along with VDMJ.  If not, see <http://www.gnu.org/licenses/>.
21   *
22   ******************************************************************************/
23  
24  package org.overturetool.vdmj.lex;
25  
26  import java.io.Serializable;
27  
28  import org.overturetool.vdmj.runtime.Context;
29  import org.overturetool.vdmj.runtime.ContextException;
30  import org.overturetool.vdmj.typechecker.TypeChecker;
31  
32  /***
33   * The parent class for all lexical token types.
34   */
35  
36  abstract public class LexToken implements Serializable
37  {
38  	private static final long serialVersionUID = 1L;
39  
40  	/*** The textual location of the token. */
41  	public final LexLocation location;
42  	/*** The basic type of the token. */
43  	public final Token type;
44  
45  	/***
46  	 * Create a token of the given type at the given location.
47  	 *
48  	 * @param location	The location of the token.
49  	 * @param type		The basic type of the token.
50  	 */
51  
52  	public LexToken(LexLocation location, Token type)
53  	{
54  		this.location = location;
55  		this.type = type;
56  	}
57  
58  	/***
59  	 * Test whether this token is a given basic type.
60  	 *
61  	 * @param ttype	The type to test.
62  	 * @return	True if this is of that type.
63  	 */
64  
65  	public boolean is(Token ttype)
66  	{
67  		return this.type == ttype;
68  	}
69  
70  	/***
71  	 * Test whether this token is not a given basic type.
72  	 *
73  	 * @param ttype	The type to test.
74  	 * @return	True if this is not of that type.
75  	 */
76  
77  	public boolean isNot(Token ttype)
78  	{
79  		return this.type != ttype;
80  	}
81  
82  	@Override
83  	public String toString()
84  	{
85  		return type.toString();
86  	}
87  
88  	/***
89  	 * @see org.overturetool.vdmj.definitions.Definition#report
90  	 */
91  
92  	public void report(int number, String msg)
93  	{
94  		TypeChecker.report(number, msg, location);
95  	}
96  
97  	/***
98  	 * @see org.overturetool.vdmj.definitions.Definition#warning
99  	 */
100 
101 	public void warning(int number, String msg)
102 	{
103 		TypeChecker.warning(number, msg, location);
104 	}
105 
106 	/***
107 	 * This is used when a problem would be an error if the type it applies
108 	 * to is unambiguous, or a warning otherwise.
109 	 * @param serious True if this should be an error
110 	 * @param number The error number.
111 	 * @param msg The problem.
112 	 */
113 
114 	public void concern(boolean serious, int number, String msg)
115 	{
116 		if (serious)
117 		{
118 			TypeChecker.report(number, msg, location);
119 		}
120 		else
121 		{
122 			TypeChecker.warning(number, msg, location);
123 		}
124 	}
125 
126 	/***
127 	 * @see org.overturetool.vdmj.definitions.Definition#abort
128 	 */
129 
130 	public void abort(int number, String msg, Context ctxt)
131 	{
132 		throw new ContextException(number, msg, location, ctxt);
133 	}
134 
135 	/***
136 	 * @see org.overturetool.vdmj.definitions.Definition#detail
137 	 */
138 
139 	public void detail(String tag, Object obj)
140 	{
141 		TypeChecker.detail(tag, obj);
142 	}
143 
144 	/***
145 	 * @see org.overturetool.vdmj.definitions.Definition#detail2
146 	 */
147 
148 	public void detail2(String tag1, Object obj1, String tag2, Object obj2)
149 	{
150 		TypeChecker.detail2(tag1, obj1, tag2, obj2);
151 	}
152 }