Package: VDMToken

VDMToken

nameinstructionbranchcomplexitylinemethod
VDMToken(String, int, String, String, Dialect[])
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
lookup(String, Dialect)
M: 2 C: 20
91%
M: 1 C: 3
75%
M: 1 C: 3
75%
M: 1 C: 4
80%
M: 0 C: 1
100%
static {...}
M: 0 C: 5120
100%
M: 1 C: 9
90%
M: 1 C: 6
86%
M: 0 C: 151
100%
M: 0 C: 1
100%
toString()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.ast.lex;
2:
3: /*******************************************************************************
4: *
5: *        Copyright (c) 2008 Fujitsu Services Ltd.
6: *
7: *        Author: Nick Battle
8: *
9: *        This file is part of VDMJ.
10: *
11: *        VDMJ is free software: you can redistribute it and/or modify
12: *        it under the terms of the GNU General Public License as published by
13: *        the Free Software Foundation, either version 3 of the License, or
14: *        (at your option) any later version.
15: *
16: *        VDMJ is distributed in the hope that it will be useful,
17: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
18: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: *        GNU General Public License for more details.
20: *
21: *        You should have received a copy of the GNU General Public License
22: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
23: *
24: ******************************************************************************/
25:
26: import static org.overture.ast.lex.Dialect.VDM_PP;
27: import static org.overture.ast.lex.Dialect.VDM_RT;
28: import static org.overture.ast.lex.Dialect.VDM_SL;
29:
30: import java.io.Serializable;
31: import java.util.Arrays;
32: import java.util.HashMap;
33: import java.util.List;
34: import java.util.Map;
35:
36: /**
37: * An enumeration for the basic token types recognised by the lexical analyser.
38: */
39:
40: public enum VDMToken implements Serializable
41: {
42:         // Variables
43:         IDENTIFIER(null, "id", VDM_SL, VDM_PP, VDM_RT), NAME(null, "name", VDM_SL,
44:                         VDM_PP, VDM_RT),
45:
46:         // Literals
47:         NUMBER(null, "number", VDM_SL, VDM_PP, VDM_RT), REALNUMBER(null, "real",
48:                         VDM_SL, VDM_PP, VDM_RT), CHARACTER(null, "char", VDM_SL, VDM_PP,
49:                         VDM_RT), STRING(null, "string", VDM_SL, VDM_PP, VDM_RT), QUOTE(
50:                         null, "<quote>", VDM_SL, VDM_PP, VDM_RT),
51:
52:         // Basic types
53:         BOOL("bool", "bool", VDM_SL, VDM_PP, VDM_RT), NAT("nat", "nat", VDM_SL,
54:                         VDM_PP, VDM_RT), NAT1("nat1", "nat1", VDM_SL, VDM_PP, VDM_RT), INT(
55:                         "int", "int", VDM_SL, VDM_PP, VDM_RT), RAT("rat", "rat", VDM_SL,
56:                         VDM_PP, VDM_RT), REAL("real", "real", VDM_SL, VDM_PP, VDM_RT), CHAR(
57:                         "char", "char", VDM_SL, VDM_PP, VDM_RT), TOKEN("token", "token",
58:                         VDM_SL, VDM_PP, VDM_RT),
59:
60:         // Operators
61:         PLUS(null, "+", VDM_SL, VDM_PP, VDM_RT), MINUS(null, "-", VDM_SL, VDM_PP,
62:                         VDM_RT), TIMES(null, "*", VDM_SL, VDM_PP, VDM_RT), DIVIDE(null,
63:                         "/", VDM_SL, VDM_PP, VDM_RT), REM("rem", "rem", VDM_SL, VDM_PP,
64:                         VDM_RT), MOD("mod", "mod", VDM_SL, VDM_PP, VDM_RT), DIV("div",
65:                         "div", VDM_SL, VDM_PP, VDM_RT), LT(null, "<", VDM_SL, VDM_PP,
66:                         VDM_RT), LE(null, "<=", VDM_SL, VDM_PP, VDM_RT), GT(null, ">",
67:                         VDM_SL, VDM_PP, VDM_RT), GE(null, ">=", VDM_SL, VDM_PP, VDM_RT), NE(
68:                         null, "<>", VDM_SL, VDM_PP, VDM_RT), EQUALS(null, "=", VDM_SL,
69:                         VDM_PP, VDM_RT), EQUALSEQUALS(null, "==", VDM_SL, VDM_PP, VDM_RT), EQUIVALENT(
70:                         null, "<=>", VDM_SL, VDM_PP, VDM_RT), IMPLIES(null, "=>", VDM_SL,
71:                         VDM_PP, VDM_RT), SUBSET("subset", "subset", VDM_SL, VDM_PP, VDM_RT), PSUBSET(
72:                         "psubset", "psubset", VDM_SL, VDM_PP, VDM_RT), INSET("in set",
73:                         "in set", VDM_SL, VDM_PP, VDM_RT), NOTINSET("not in set",
74:                         "not in set", VDM_SL, VDM_PP, VDM_RT), SETDIFF(null, "\\", VDM_SL,
75:                         VDM_PP, VDM_RT), MUNION("munion", "munion", VDM_SL, VDM_PP, VDM_RT), PLUSPLUS(
76:                         null, "++", VDM_SL, VDM_PP, VDM_RT), STARSTAR(null, "**", VDM_SL,
77:                         VDM_PP, VDM_RT), UNION("union", "union", VDM_SL, VDM_PP, VDM_RT), INTER(
78:                         "inter", "inter", VDM_SL, VDM_PP, VDM_RT), INVERSE("inverse",
79:                         "inverse", VDM_SL, VDM_PP, VDM_RT), CONCATENATE(null, "^", VDM_SL,
80:                         VDM_PP, VDM_RT), MAPLET(null, "|->", VDM_SL, VDM_PP, VDM_RT), RANGE(
81:                         null, "...", VDM_SL, VDM_PP, VDM_RT), DOMRESTO(null, "<:", VDM_SL,
82:                         VDM_PP, VDM_RT), DOMRESBY(null, "<-:", VDM_SL, VDM_PP, VDM_RT), RANGERESTO(
83:                         null, ":>", VDM_SL, VDM_PP, VDM_RT), RANGERESBY(null, ":->",
84:                         VDM_SL, VDM_PP, VDM_RT), CARD("card", "card", VDM_SL, VDM_PP,
85:                         VDM_RT), DOM("dom", "dom", VDM_SL, VDM_PP, VDM_RT), LEN("len",
86:                         "len", VDM_SL, VDM_PP, VDM_RT), POWER("power", "power", VDM_SL,
87:                         VDM_PP, VDM_RT), RNG("rng", "rng", VDM_SL, VDM_PP, VDM_RT), ELEMS(
88:                         "elems", "elems", VDM_SL, VDM_PP, VDM_RT), ABS("abs", "abs",
89:                         VDM_SL, VDM_PP, VDM_RT), DINTER("dinter", "dinter", VDM_SL, VDM_PP,
90:                         VDM_RT), MERGE("merge", "merge", VDM_SL, VDM_PP, VDM_RT), HEAD(
91:                         "hd", "hd", VDM_SL, VDM_PP, VDM_RT), TAIL("tl", "tl", VDM_SL,
92:                         VDM_PP, VDM_RT), FLOOR("floor", "floor", VDM_SL, VDM_PP, VDM_RT), DUNION(
93:                         "dunion", "dunion", VDM_SL, VDM_PP, VDM_RT), DISTCONC("conc",
94:                         "conc", VDM_SL, VDM_PP, VDM_RT), INDS("inds", "inds", VDM_SL,
95:                         VDM_PP, VDM_RT), POINT(null, ".", VDM_SL, VDM_PP, VDM_RT), COMP(
96:                         "comp", "comp", VDM_SL, VDM_PP, VDM_RT), FORALL("forall", "forall",
97:                         VDM_SL, VDM_PP, VDM_RT), EXISTS("exists", "exists", VDM_SL, VDM_PP,
98:                         VDM_RT), EXISTS1("exists1", "exists1", VDM_SL, VDM_PP, VDM_RT), IOTA(
99:                         "iota", "iota", VDM_SL, VDM_PP, VDM_RT), LAMBDA("lambda", "lambda",
100:                         VDM_SL, VDM_PP, VDM_RT),
101:
102:         // Functions
103:         TOTAL_FUNCTION(null, "+>", VDM_SL, VDM_PP, VDM_RT), ARROW(null, "->",
104:                         VDM_SL, VDM_PP, VDM_RT), OPDEF(null, "==>", VDM_SL, VDM_PP, VDM_RT), MEASURE(
105:                         "measure", "measure", VDM_SL, VDM_PP, VDM_RT),
106:
107:         // Various
108:         BRA(null, "(", VDM_SL, VDM_PP, VDM_RT), KET(null, ")", VDM_SL, VDM_PP,
109:                         VDM_RT), COMMA(null, ",", VDM_SL, VDM_PP, VDM_RT), SEMICOLON(null,
110:                         ";", VDM_SL, VDM_PP, VDM_RT), QMARK(null, "?", VDM_SL, VDM_PP,
111:                         VDM_RT), COLON(null, ":", VDM_SL, VDM_PP, VDM_RT), COLONCOLON(null,
112:                         "::", VDM_SL, VDM_PP, VDM_RT), AMPERSAND(null, "&", VDM_SL, VDM_PP,
113:                         VDM_RT), EQABST(null, ":-", VDM_SL, VDM_PP, VDM_RT), PIPE(null,
114:                         "|", VDM_SL, VDM_PP, VDM_RT), PIPEPIPE(null, "||", VDM_SL, VDM_PP,
115:                         VDM_RT), HASH(null, "#", VDM_SL, VDM_PP, VDM_RT), AT(null, "@",
116:                         VDM_SL, VDM_PP, VDM_RT), SET_OPEN(null, "{", VDM_SL, VDM_PP, VDM_RT), SET_CLOSE(
117:                         null, "}", VDM_SL, VDM_PP, VDM_RT), SEQ_OPEN(null, "[", VDM_SL,
118:                         VDM_PP, VDM_RT), SEQ_CLOSE(null, "]", VDM_SL, VDM_PP, VDM_RT), ASSIGN(
119:                         null, ":=", VDM_SL, VDM_PP, VDM_RT),
120:
121:         // Keywords
122:         COMPOSE("compose", "compose", VDM_SL, VDM_PP, VDM_RT), END("end", "end",
123:                         VDM_SL, VDM_PP, VDM_RT), MAP("map", "map", VDM_SL, VDM_PP, VDM_RT), INMAP(
124:                         "inmap", "inmap", VDM_SL, VDM_PP, VDM_RT), SET("set", "set",
125:                         VDM_SL, VDM_PP, VDM_RT), SEQ("seq", "seq", VDM_SL, VDM_PP, VDM_RT), SEQ1(
126:                         "seq1", "seq1", VDM_SL, VDM_PP, VDM_RT), OF("of", "of", VDM_SL,
127:                         VDM_PP, VDM_RT), TO("to", "to", VDM_SL, VDM_PP, VDM_RT), PRE("pre",
128:                         "pre", VDM_SL, VDM_PP, VDM_RT), POST("post", "post", VDM_SL,
129:                         VDM_PP, VDM_RT), INV("inv", "inv", VDM_SL, VDM_PP, VDM_RT), INIT(
130:                         "init", "init", VDM_SL), TRUE("true", "true", VDM_SL, VDM_PP,
131:                         VDM_RT), FALSE("false", "false", VDM_SL, VDM_PP, VDM_RT), AND(
132:                         "and", "and", VDM_SL, VDM_PP, VDM_RT), OR("or", "or", VDM_SL,
133:                         VDM_PP, VDM_RT), NOT("not", "not", VDM_SL, VDM_PP, VDM_RT), NIL(
134:                         "nil", "nil", VDM_SL, VDM_PP, VDM_RT), UNDEFINED("undefined",
135:                         "undefined", VDM_SL, VDM_PP, VDM_RT), EXTERNAL("ext", "ext",
136:                         VDM_SL, VDM_PP, VDM_RT), READ("rd", "rd", VDM_SL, VDM_PP, VDM_RT), WRITE(
137:                         "wr", "wr", VDM_SL, VDM_PP, VDM_RT), ERRS("errs", "errs", VDM_SL,
138:                         VDM_PP, VDM_RT), DCL("dcl", "dcl", VDM_SL, VDM_PP, VDM_RT), DEF(
139:                         "def", "def", VDM_SL, VDM_PP, VDM_RT), IS("is", "is", VDM_SL,
140:                         VDM_PP, VDM_RT), YET("yet", "yet", VDM_SL, VDM_PP, VDM_RT), SPECIFIED(
141:                         "specified", "specified", VDM_SL, VDM_PP, VDM_RT), PURE("pure", "pure",
142:                         VDM_SL, VDM_PP, VDM_RT), SET1("set1", "set1", VDM_SL, VDM_PP, VDM_RT),
143:          EQ("eq", "eq", VDM_SL, VDM_PP, VDM_RT),
144:          ORD("ord", "ord", VDM_SL, VDM_PP, VDM_RT),
145:
146:         // Expressions
147:         LET("let", "let", VDM_SL, VDM_PP, VDM_RT), IN("in", "in", VDM_SL, VDM_PP,
148:                         VDM_RT), BE("be", "be", VDM_SL, VDM_PP, VDM_RT), ST("st", "st",
149:                         VDM_SL, VDM_PP, VDM_RT), IF("if", "if", VDM_SL, VDM_PP, VDM_RT), THEN(
150:                         "then", "then", VDM_SL, VDM_PP, VDM_RT), ELSE("else", "else",
151:                         VDM_SL, VDM_PP, VDM_RT), ELSEIF("elseif", "elseif", VDM_SL, VDM_PP,
152:                         VDM_RT), CASES("cases", "cases", VDM_SL, VDM_PP, VDM_RT), OTHERS(
153:                         "others", "others", VDM_SL, VDM_PP, VDM_RT),
154:
155:         // Statements
156:         RETURN("return", "return", VDM_SL, VDM_PP, VDM_RT), FOR("for", "for",
157:                         VDM_SL, VDM_PP, VDM_RT), ALL("all", "all", VDM_SL, VDM_PP, VDM_RT), REVERSE(
158:                         "reverse", "reverse", VDM_SL, VDM_PP, VDM_RT), DO("do", "do",
159:                         VDM_SL, VDM_PP, VDM_RT), BY("by", "by", VDM_SL, VDM_PP, VDM_RT), WHILE(
160:                         "while", "while", VDM_SL, VDM_PP, VDM_RT), USING("using", "using",
161:                         VDM_SL, VDM_PP, VDM_RT), ALWAYS("always", "always", VDM_SL, VDM_PP,
162:                         VDM_RT), ATOMIC("atomic", "atomic", VDM_SL, VDM_PP, VDM_RT), TRAP(
163:                         "trap", "trap", VDM_SL, VDM_PP, VDM_RT), WITH("with", "with",
164:                         VDM_SL, VDM_PP, VDM_RT), TIXE("tixe", "tixe", VDM_SL, VDM_PP,
165:                         VDM_RT), EXIT("exit", "exit", VDM_SL, VDM_PP, VDM_RT), ERROR(
166:                         "error", "error", VDM_SL, VDM_PP, VDM_RT), SKIP("skip", "skip",
167:                         VDM_SL, VDM_PP, VDM_RT),
168:
169:         // Sections
170:         TYPES("types", "types", VDM_SL, VDM_PP, VDM_RT), VALUES("values", "values",
171:                         VDM_SL, VDM_PP, VDM_RT), FUNCTIONS("functions", "functions",
172:                         VDM_SL, VDM_PP, VDM_RT), OPERATIONS("operations", "operations",
173:                         VDM_SL, VDM_PP, VDM_RT), THREAD("thread", "thread", VDM_PP, VDM_RT), SYNC(
174:                         "sync", "sync", VDM_PP, VDM_RT), TRACES("traces", "traces", VDM_SL,
175:                         VDM_PP, VDM_RT), INSTANCE("instance", "instance", VDM_PP, VDM_RT), VARIABLES(
176:                         "variables", "variables", VDM_PP, VDM_RT),
177:
178:         // Modules (VDM-SL only)
179:         MODULE("module", "module", VDM_SL), DLMODULE("dlmodule", "dlmodule", VDM_SL), USELIB(
180:                         "uselib", "uselib", VDM_SL), IMPORTS("imports", "imports", VDM_SL), FROM(
181:                         "from", "from", VDM_SL), RENAMED("renamed", "renamed", VDM_SL), EXPORTS(
182:                         "exports", "exports", VDM_SL), DEFINITIONS("definitions",
183:                         "definitions", VDM_SL), STRUCT("struct", "struct", VDM_SL), STATE(
184:                         "state", "state", VDM_SL),
185:
186:         // VDM++ extra tokens
187:         CLASS("class", "class", VDM_PP, VDM_RT), SUBCLASS("subclass", "subclass",
188:                         VDM_PP, VDM_RT), STATIC("static", "static", VDM_PP, VDM_RT), PUBLIC(
189:                         "public", "public", VDM_PP, VDM_RT), PRIVATE("private", "private",
190:                         VDM_PP, VDM_RT), PROTECTED("protected", "protected", VDM_PP, VDM_RT), SELF(
191:                         "self", "self", VDM_PP, VDM_RT), NEW("new", "new", VDM_PP, VDM_RT), RESPONSIBILITY(
192:                         "responsibility", "responsibility", VDM_PP, VDM_RT), ISOFBASECLASS(
193:                         "isofbaseclass", "isofbaseclass", VDM_PP, VDM_RT), ISOFCLASS(
194:                         "isofclass", "isofclass", VDM_PP, VDM_RT), SAMEBASECLASS(
195:                         "samebaseclass", "samebaseclass", VDM_PP, VDM_RT), SAMECLASS(
196:                         "sameclass", "sameclass", VDM_PP, VDM_RT), THREADID("threadid",
197:                         "threadid", VDM_PP, VDM_RT), PERIODIC("periodic", "periodic",
198:                         VDM_PP, VDM_RT), SPORADIC("sporadic", "sporadic", VDM_PP, VDM_RT), PER(
199:                         "per", "per", VDM_PP, VDM_RT), MUTEX("mutex", "mutex", VDM_PP,
200:                         VDM_RT), REQ("#req", "#req", VDM_PP, VDM_RT), ACT("#act", "#act",
201:                         VDM_PP, VDM_RT), FIN("#fin", "#fin", VDM_PP, VDM_RT), ACTIVE(
202:                         "#active", "#active", VDM_PP, VDM_RT), WAITING("#waiting",
203:                         "#waiting", VDM_PP, VDM_RT), START("start", "start", VDM_PP, VDM_RT), STARTLIST(
204:                         "startlist", "startlist", VDM_PP, VDM_RT), STOP("stop", "stop",
205:                         VDM_PP, VDM_RT), STOPLIST("stoplist", "stoplist", VDM_PP, VDM_RT),
206:
207:         // VICE extra tokens
208:         TIME("time", "time", VDM_RT), ASYNC("async", "async", VDM_RT), CYCLES(
209:                         "cycles", "cycles", VDM_RT), DURATION("duration", "duration",
210:                         VDM_RT), SYSTEM("system", "system", VDM_RT),
211:
212:         // No more tokens
213:         EOF(null, "EOF", VDM_SL);
214:
215:         /** The keyword associated with a token, if any. */
216:         private String keyword = null;
217:
218:         /** The displayable form of the token. */
219:         private String display = null;
220:
221:         /** The dialect(s) of the keyword, VDM-SL, VDM++ or VICE. */
222:         private List<Dialect> dialects = null;
223:
224:         /** Maps to speed up the lookup of individual token strings. */
225:         private static Map<String, VDMToken> sltokens;
226:         private static Map<String, VDMToken> pptokens;
227:         private static Map<String, VDMToken> rttokens;
228:
229:         static
230:         {
231:                 // This has to happen in a static block because an enum always
232:                 // initializes its members before any statics (or member inits)
233:                 // are performed.
234:
235:                 sltokens = new HashMap<String, VDMToken>(256);
236:                 pptokens = new HashMap<String, VDMToken>(256);
237:                 rttokens = new HashMap<String, VDMToken>(256);
238:
239:•                for (VDMToken token : values())
240:                 {
241:•                        if (token.keyword != null)
242:                         {
243:•                                for (Dialect dialect : token.dialects)
244:                                 {
245:•                                        switch (dialect)
246:                                         {
247:                                                 case VDM_SL:
248:                                                         sltokens.put(token.keyword, token);
249:                                                         break;
250:
251:                                                 case VDM_PP:
252:                                                         pptokens.put(token.keyword, token);
253:                                                         break;
254:
255:                                                 case VDM_RT:
256:                                                         rttokens.put(token.keyword, token);
257:                                                         break;
258:                                                 default:
259:                                                         break;
260:                                         }
261:                                 }
262:                         }
263:                 }
264:         }
265:
266:         /**
267:          * Construct a token with the associated keyword, display and dialect.
268:          */
269:
270:         private VDMToken(String keyword, String display, Dialect... dialects)
271:         {
272:                 this.keyword = keyword;
273:                 this.display = display;
274:                 this.dialects = Arrays.asList(dialects);
275:         }
276:
277:         /**
278:          * Lookup a keyword/dialect and return a Token, or null.
279:          *
280:          * @param word
281:          * @param dialect
282:          * @return
283:          */
284:
285:         public static VDMToken lookup(String word, Dialect dialect)
286:         {
287:•                switch (dialect)
288:                 {
289:                         case VDM_SL:
290:                                 return sltokens.get(word);
291:
292:                         case VDM_PP:
293:                                 return pptokens.get(word);
294:
295:                         case VDM_RT:
296:                                 return rttokens.get(word);
297:                         default:
298:                                 break;
299:                 }
300:
301:                 return null;
302:         }
303:
304:         @Override
305:         public String toString()
306:         {
307:                 return display;
308:         }
309: }