Package: ClassReader

ClassReader

nameinstructionbranchcomplexitylinemethod
ClassReader(LexTokenReader)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
readClass()
M: 20 C: 91
82%
M: 3 C: 7
70%
M: 3 C: 3
50%
M: 3 C: 21
88%
M: 0 C: 1
100%
readClasses()
M: 22 C: 87
80%
M: 5 C: 11
69%
M: 4 C: 5
56%
M: 4 C: 24
86%
M: 0 C: 1
100%
readSystem()
M: 46 C: 86
65%
M: 6 C: 11
65%
M: 6 C: 4
40%
M: 10 C: 20
67%
M: 0 C: 1
100%

Coverage

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.overture.parser.syntax;
25:
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.overture.ast.annotations.PAnnotation;
30: import org.overture.ast.definitions.ASystemClassDefinition;
31: import org.overture.ast.definitions.PDefinition;
32: import org.overture.ast.definitions.SClassDefinition;
33: import org.overture.ast.factory.AstFactory;
34: import org.overture.ast.intf.lex.ILexCommentList;
35: import org.overture.ast.intf.lex.ILexNameToken;
36: import org.overture.ast.lex.Dialect;
37: import org.overture.ast.lex.LexIdentifierToken;
38: import org.overture.ast.lex.LexNameList;
39: import org.overture.ast.lex.VDMToken;
40: import org.overture.ast.util.definitions.ClassList;
41: import org.overture.config.Settings;
42: import org.overture.parser.lex.LexException;
43: import org.overture.parser.lex.LexTokenReader;
44: import org.overture.parser.messages.LocatedException;
45:
46: /**
47: * A syntax analyser to parse class definitions.
48: */
49:
50: public class ClassReader extends SyntaxReader
51: {
52:         public ClassReader(LexTokenReader reader)
53:         {
54:                 super(reader);
55:         }
56:
57:         public ClassList readClasses()
58:         {
59:                 ClassList list = new ClassList();
60:
61:                 try
62:                 {
63:•                        if (lastToken().is(VDMToken.EOF))
64:                         {
65:                                 return list; // The file is empty
66:                         }
67:
68:•                        if (lastToken().isNot(VDMToken.CLASS)
69:•                                        && lastToken().isNot(VDMToken.SYSTEM))
70:                         {
71:                                 warning(5015, "LaTeX source should start with %comment, \\document, \\section or \\subsection", lastToken().location);
72:
73:•                                throwMessage(2005, Settings.dialect == Dialect.VDM_RT ? "Expecting list of 'class' or 'system' definitions"
74:                                                 : "Expecting list of 'class' definitions");
75:                         }
76:
77:•                        while (lastToken().is(VDMToken.CLASS)
78:•                                        || lastToken().is(VDMToken.SYSTEM))
79:                         {
80:                                 ILexCommentList comments = getComments();
81:                                 List<PAnnotation> annotations = readAnnotations(comments);
82:                                 beforeAnnotations(this, annotations);
83:                                 SClassDefinition clazz = null;
84:
85:•                                if (lastToken().is(VDMToken.CLASS))
86:                                 {
87:                                         clazz = readClass();
88:                                 }
89:                                 else
90:                                 {
91:                                         clazz = readSystem();
92:                                 }
93:                                 
94:                                 afterAnnotations(this, annotations, clazz);
95:                                 clazz.setAnnotations(annotations);
96:                                 clazz.setComments(comments);
97:                                 list.add(clazz);
98:                         }
99:
100:•                        if (lastToken().isNot(VDMToken.EOF))
101:                         {
102:                                 throwMessage(2006, "Found tokens after class definitions");
103:                         }
104:                 } catch (LocatedException e)
105:                 {
106:                         VDMToken[] end = new VDMToken[0];
107:                         report(e, end, end);
108:                 }
109:
110:                 return list;
111:         }
112:
113:         private SClassDefinition readClass() throws ParserException, LexException
114:         {
115:                 LexNameList superclasses = new LexNameList();
116:
117:•                if (lastToken().is(VDMToken.CLASS))
118:                 {
119:                         setCurrentModule("");
120:                         nextToken();
121:                         LexIdentifierToken classId = readIdToken("Expecting class ID");
122:                         ILexNameToken className = classId.getClassName();
123:                         setCurrentModule(classId.getName());
124:
125:•                        if (lastToken().is(VDMToken.IS))
126:                         {
127:                                 nextToken();
128:                                 checkFor(VDMToken.SUBCLASS, 2075, "Expecting 'is subclass of'");
129:                                 checkFor(VDMToken.OF, 2076, "Expecting 'is subclass of'");
130:
131:                                 LexIdentifierToken id = readIdToken("Expecting class identifier");
132:                                 superclasses.add(id.getClassName());
133:
134:•                                while (ignore(VDMToken.COMMA))
135:                                 {
136:                                         id = readIdToken("Expecting class identifier");
137:                                         superclasses.add(id.getClassName());
138:                                 }
139:                         }
140:
141:                         List<PDefinition> members = getDefinitionReader().readDefinitions();
142:                         checkFor(VDMToken.END, 2077, "Expecting 'end' after class members");
143:
144:                         LexIdentifierToken endname = readIdToken("Expecting 'end <name>' after class members");
145:
146:•                        if (classId != null && !classId.equals(endname))
147:                         {
148:                                 throwMessage(2007, "Expecting 'end " + classId.getName() + "'");
149:                         }
150:
151:                         return AstFactory.newAClassClassDefinition(className, superclasses, members);
152:
153:                         // SClassDefinition def = new
154:                         // AClassClassDefinition(className.location,className,NameScope.CLASSNAME,true,null,new
155:                         // AAccessSpecifierAccessSpecifier(new APublicAccess(), null, null),null,null, null, superclasses,
156:                         // members,null,null,false,ClassDefinitionSettings.UNSET , null, false, null, false,false,false,
157:                         // null,false,null);
158:                         // PDefinitionAssistant.setClassDefinition(def,def);
159:                         // return def;
160:
161:                 } else
162:                 {
163:                         throwMessage(2008, "Class does not start with 'class'");
164:                 }
165:
166:                 return null;
167:         }
168:
169:         private ASystemClassDefinition readSystem() throws ParserException,
170:                         LexException
171:         {
172:•                if (lastToken().is(VDMToken.SYSTEM))
173:                 {
174:                         setCurrentModule("");
175:                         nextToken();
176:                         LexIdentifierToken classId = readIdToken("Expecting class ID");
177:                         ILexNameToken className = classId.getClassName();
178:                         setCurrentModule(classId.getName());
179:
180:•                        if (lastToken().is(VDMToken.IS))
181:                         {
182:                                 nextToken();
183:                                 checkFor(VDMToken.SUBCLASS, 2075, "Expecting 'is subclass of'");
184:                                 checkFor(VDMToken.OF, 2076, "Expecting 'is subclass of'");
185:
186:                                 throwMessage(2280, "System class cannot be a subclass");
187:                         }
188:
189:                         List<PDefinition> members = new Vector<PDefinition>();
190:                         DefinitionReader dr = getDefinitionReader();
191:
192:•                        while (lastToken().is(VDMToken.INSTANCE)
193:•                                        || lastToken().is(VDMToken.OPERATIONS))
194:                         {
195:•                                if (lastToken().is(VDMToken.INSTANCE))
196:                                 {
197:                                         members.addAll(dr.readInstanceVariables());
198:                                 } else
199:                                 {
200:                                         members.addAll(dr.readOperations());
201:                                 }
202:                         }
203:
204:•                        switch (lastToken().type)
205:                         {
206:                                 case TYPES:
207:                                 case VALUES:
208:                                 case FUNCTIONS:
209:                                 case THREAD:
210:                                 case SYNC:
211:                                         throwMessage(2290, "System class can only define instance variables and a constructor");
212:                                         break;
213:
214:                                 case END:
215:                                         nextToken();
216:                                         break;
217:
218:                                 default:
219:                                         throwMessage(2077, "Expecting 'end' after system members");
220:                         }
221:
222:                         LexIdentifierToken endname = readIdToken("Expecting 'end <name>' after system members");
223:
224:•                        if (classId != null && !classId.equals(endname))
225:                         {
226:                                 throwMessage(2007, "Expecting 'end " + classId.getName() + "'");
227:                         }
228:
229:                         return AstFactory.newASystemClassDefinition(className, members);// new
230:                                                                                                                                                         // ASystemClassDefinition(className.location,className,NameScope.CLASSNAME,true,null,new
231:                                                                                                                                                         // AAccessSpecifierAccessSpecifier(new
232:                                                                                                                                                         // APublicAccess(), null, null),null,
233:                                                                                                                                                         // null,null, new LexNameList(),
234:                                                                                                                                                         // members,null,null,false,null, null,
235:                                                                                                                                                         // false, null,
236:                                                                                                                                                         // false,false,false,null,false,null);
237:                         // for (PDefinition pDefinition : def.getDefinitions())
238:                         // {
239:                         // pDefinition.setClassDefinition(def);
240:                         // }
241:                         // return def;
242:                 } else
243:                 {
244:                         throwMessage(2008, "System class does not start with 'system'");
245:                 }
246:
247:                 return null;
248:         }
249: }