Package: ParserUtil

ParserUtil

nameinstructionbranchcomplexitylinemethod
ParserUtil()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getReader(File, Dialect, String)
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getReader(String, Dialect, String)
M: 7 C: 8
53%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
parseExpression(String)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
parseOo(File)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
parseOo(File, String)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
parseOo(List)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseOo(List, String)
M: 8 C: 22
73%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 6
86%
M: 0 C: 1
100%
parseOo(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseOo(String, String)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
parseSl(File)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
parseSl(File, String)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
parseSl(List)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseSl(List, String)
M: 0 C: 29
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
parseSl(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseSl(String, String)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * The VDM parser
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.parser.util;
23:
24: import java.io.File;
25: import java.util.List;
26:
27: import org.overture.ast.definitions.SClassDefinition;
28: import org.overture.ast.expressions.PExp;
29: import org.overture.ast.lex.Dialect;
30: import org.overture.ast.modules.AModuleModules;
31: import org.overture.config.Settings;
32: import org.overture.parser.lex.LexException;
33: import org.overture.parser.lex.LexTokenReader;
34: import org.overture.parser.messages.VDMError;
35: import org.overture.parser.messages.VDMWarning;
36: import org.overture.parser.syntax.ClassReader;
37: import org.overture.parser.syntax.ExpressionReader;
38: import org.overture.parser.syntax.ModuleReader;
39: import org.overture.parser.syntax.ParserException;
40:
41: public class ParserUtil
42: {
43:         public static class ParserResult<T>
44:         {
45:                 public static interface IResultCombiner<T>
46:                 {
47:                         public void combine(T source, T other);
48:                 }
49:
50:                 public final T result;
51:                 public final List<VDMWarning> warnings;
52:                 public final List<VDMError> errors;
53:
54:                 public ParserResult(T result, List<VDMWarning> warnings,
55:                                 List<VDMError> errors)
56:                 {
57:                         this.result = result;
58:                         this.warnings = warnings;
59:                         this.errors = errors;
60:                 }
61:
62:                 public void combine(ParserResult<T> other, IResultCombiner<T> combiner)
63:                 {
64:                         this.errors.addAll(other.errors);
65:                         this.warnings.addAll(other.warnings);
66:                         combiner.combine(this.result, other.result);
67:                 }
68:
69:                 public String getErrorString()
70:                 {
71:                         StringBuilder sb = new StringBuilder();
72:                         sb.append("\tErrors:");
73:                         for (VDMError err : errors)
74:                         {
75:                                 sb.append("\n\t" + err);
76:                         }
77:
78:                         return sb.toString();
79:                 }
80:
81:                 public String getWarningString()
82:                 {
83:                         StringBuilder sb = new StringBuilder();
84:                         sb.append("\tWarnings:");
85:                         for (VDMWarning err : warnings)
86:                         {
87:                                 sb.append("\n\t" + err);
88:                         }
89:                         return sb.toString();
90:                 }
91:         }
92:
93:         public static ParserResult<List<SClassDefinition>> parseOo(List<File> files)
94:         {
95:                 return parseOo(files, null);
96:         }
97:
98:         public static ParserResult<List<SClassDefinition>> parseOo(
99:                         List<File> files, String charset)
100:         {
101:                 ParserResult<List<SClassDefinition>> res = null;
102:•                for (File file : files)
103:                 {
104:•                        if (res == null)
105:                         {
106:                                 res = parseOo(file, charset);
107:                         } else
108:                         {
109:                                 res.combine(parseOo(file, charset), new ParserResult.IResultCombiner<List<SClassDefinition>>()
110:                                 {
111:
112:                                         @Override
113:                                         public void combine(List<SClassDefinition> source,
114:                                                         List<SClassDefinition> other)
115:                                         {
116:                                                 source.addAll(other);
117:                                         }
118:                                 });
119:                         }
120:                 }
121:
122:                 return res;
123:         }
124:
125:         public static ParserResult<List<SClassDefinition>> parseOo(File file)
126:         {
127:                 return parseOo(file, null);
128:         }
129:
130:         public static ParserResult<List<SClassDefinition>> parseOo(File file,
131:                         String charset)
132:         {
133:                 LexTokenReader ltr = getReader(file, Settings.dialect, charset);
134:                 ClassReader reader = null;
135:                 List<SClassDefinition> result = null;
136:
137:                 reader = new ClassReader(ltr);
138:                 result = reader.readClasses();
139:
140:                 return new ParserResult<List<SClassDefinition>>(result, reader.getWarnings(), reader.getErrors());
141:         }
142:
143:         private static LexTokenReader getReader(File file, Dialect dialect,
144:                         String charset)
145:         {
146:•                if (charset == null)
147:                         return new LexTokenReader(file, Settings.dialect);
148:                 else
149:                         return new LexTokenReader(file, Settings.dialect, charset);
150:         }
151:
152:         private static LexTokenReader getReader(String content, Dialect dialect,
153:                         String charset)
154:         {
155:•                if (charset == null)
156:                         return new LexTokenReader(content, Settings.dialect);
157:                 else
158:                         return new LexTokenReader(content, Settings.dialect, charset);
159:         }
160:
161:         public static ParserResult<List<SClassDefinition>> parseOo(String content)
162:         {
163:                 return parseOo(content, null);
164:         }
165:
166:         public static ParserResult<List<SClassDefinition>> parseOo(String content,
167:                         String charset)
168:         {
169:                 LexTokenReader ltr = getReader(content, Settings.dialect, charset);
170:                 ClassReader reader = null;
171:                 List<SClassDefinition> result = null;
172:
173:                 reader = new ClassReader(ltr);
174:                 result = reader.readClasses();
175:
176:                 return new ParserResult<List<SClassDefinition>>(result, reader.getWarnings(), reader.getErrors());
177:         }
178:
179:         public static ParserResult<List<AModuleModules>> parseSl(List<File> files)
180:         {
181:                 return parseSl(files, null);
182:         }
183:
184:         public static ParserResult<List<AModuleModules>> parseSl(List<File> files,
185:                         String charset)
186:         {
187:                 ParserResult<List<AModuleModules>> res = null;
188:•                for (File file : files)
189:                 {
190:•                        if (res == null)
191:                         {
192:                                 res = parseSl(file);
193:                         } else
194:                         {
195:                                 res.combine(parseSl(file, charset), new ParserResult.IResultCombiner<List<AModuleModules>>()
196:                                 {
197:
198:                                         @Override
199:                                         public void combine(List<AModuleModules> source,
200:                                                         List<AModuleModules> other)
201:                                         {
202:                                                 source.addAll(other);
203:                                         }
204:                                 });
205:                         }
206:                 }
207:
208:                 return res;
209:         }
210:
211:         public static ParserResult<List<AModuleModules>> parseSl(File file)
212:         {
213:                 return parseSl(file, null);
214:         }
215:
216:         public static ParserResult<List<AModuleModules>> parseSl(File file,
217:                         String charset)
218:         {
219:                 LexTokenReader ltr = getReader(file, Settings.dialect, charset);
220:                 ModuleReader reader = null;
221:                 List<AModuleModules> result = null;
222:
223:                 reader = new ModuleReader(ltr);
224:                 result = reader.readModules();
225:
226:                 return new ParserResult<List<AModuleModules>>(result, reader.getWarnings(), reader.getErrors());
227:         }
228:
229:         public static ParserResult<List<AModuleModules>> parseSl(String content)
230:         {
231:                 return parseSl(content, null);
232:         }
233:
234:         public static ParserResult<List<AModuleModules>> parseSl(String content,
235:                         String charset)
236:         {
237:                 LexTokenReader ltr = getReader(content, Settings.dialect, charset);
238:                 ModuleReader reader = null;
239:                 List<AModuleModules> result = null;
240:
241:                 reader = new ModuleReader(ltr);
242:                 result = reader.readModules();
243:
244:                 return new ParserResult<List<AModuleModules>>(result, reader.getWarnings(), reader.getErrors());
245:         }
246:
247:         public static ParserResult<PExp> parseExpression(String content)
248:                         throws ParserException, LexException
249:         {
250:                 LexTokenReader ltr = new LexTokenReader(content, Settings.dialect);
251:                 PExp result = null;
252:                 ExpressionReader reader = new ExpressionReader(ltr);
253:                 // reader.setCurrentModule(getDefaultName());
254:                 result = reader.readExpression();
255:
256:                 return new ParserResult<PExp>(result, reader.getWarnings(), reader.getErrors());
257:         }
258:
259: }