Package: LatexSourceFile

LatexSourceFile

nameinstructionbranchcomplexitylinemethod
LatexSourceFile(File, String)
M: 55 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
LatexSourceFile(SourceFile)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createCoverageTable(CoverageUtil)
M: 142 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
getListingEnvironment()
M: 13 C: 0
0%
M: 4 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
latexLabel(String)
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
latexQuote(String)
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
markup(String, List)
M: 77 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
print(PrintWriter, boolean, boolean, boolean, boolean, CoverageUtil)
M: 229 C: 0
0%
M: 36 C: 0
0%
M: 19 C: 0
0%
M: 52 C: 0
0%
M: 1 C: 0
0%
printCoverage(PrintWriter, boolean, CoverageUtil)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
printCoverage(PrintWriter, boolean, boolean, boolean, CoverageUtil)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.interpreter.runtime;
2:
3: import java.io.BufferedReader;
4: import java.io.File;
5: import java.io.FileInputStream;
6: import java.io.IOException;
7: import java.io.InputStreamReader;
8: import java.io.PrintWriter;
9: import java.util.Collections;
10: import java.util.List;
11: import java.util.Map;
12: import java.util.Vector;
13:
14: import org.overture.ast.intf.lex.ILexLocation;
15: import org.overture.ast.intf.lex.ILexNameToken;
16: import org.overture.ast.lex.CoverageUtil;
17: import org.overture.ast.lex.LexLocation;
18: import org.overture.ast.lex.LexNameList;
19: import org.overture.config.Settings;
20: import org.overture.parser.config.Properties;
21:
22: public class LatexSourceFile extends SourceFile
23: {
24:         private static final String CURLY_BRACKET_VDM_AL = "{vdm_al}";
25:         private static final String CURLY_BRACKET_VDM_SL = "{vdmsl}";
26:         private static final String CURLY_BRACKET_VDM_PP = "{vdmpp}";
27:         private static final String CURLY_BRACKET_VDM_RT = "{vdmrt}";
28:         private static final String BREAKLINES_OPTION = "[breaklines=true]";
29:         private static final String BEGIN = "\\begin";
30:         private static final String END = "\\end";
31:         public List<String> rawLines = new Vector<String>();
32:         public final boolean hasVdm_al;
33:         // The argument to lstset is: escapeinside={(*@}{@*)}
34:         public final String LST_ESCAPE_BEGIN = "(*@";
35:         public final String LST_ESCAPE_END = "@*)";
36:
37:         public LatexSourceFile(SourceFile source) throws IOException
38:         {
39:                 this(source.filename, source.charset);
40:         }
41:
42:         public LatexSourceFile(File filename, String charset) throws IOException
43:         {
44:                 super(filename, charset);
45:
46:                 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), charset));
47:
48:                 String line = br.readLine();
49:                 boolean vdm_al = false;
50:
51:•                while (line != null)
52:                 {
53:•                        if (line.startsWith(BEGIN + CURLY_BRACKET_VDM_AL))
54:                         {
55:                                 vdm_al = true;
56:                         }
57:
58:                         rawLines.add(line);
59:                         line = br.readLine();
60:                 }
61:
62:                 hasVdm_al = vdm_al;
63:                 br.close();
64:         }
65:
66:         public void printCoverage(PrintWriter out, boolean headers, CoverageUtil coverageUtil)
67:         {
68:                 printCoverage(out, headers, false, true, coverageUtil);
69:         }
70:
71:         public void printCoverage(PrintWriter out, boolean headers,
72:                         boolean modelOnly, boolean includeCoverageTable, CoverageUtil coverageUtil)
73:         {
74:                 print(out, headers, modelOnly, includeCoverageTable, true, coverageUtil);
75:         }
76:
77:         public void print(PrintWriter out, boolean headers, boolean modelOnly,
78:                         boolean includeCoverageTable, boolean markCoverage, CoverageUtil coverageUtil)
79:         {
80:                 Map<Integer, List<ILexLocation>> hits = null;
81:                 
82:•                if(includeCoverageTable || markCoverage)
83:                 {
84:                         hits = coverageUtil.getMissLocations(filename);
85:                 }
86:
87:•                if (headers)
88:                 {
89:                         out.println("\\documentclass[a4paper]{article}");
90:                         out.println("\\usepackage{longtable}");
91:                         out.println("\\usepackage[color]{vdmlisting}");
92:                         out.println("\\usepackage{fullpage}");
93:                         out.println("\\usepackage{hyperref}");
94:                         out.println("\\begin{document}");
95:                         out.println("\\title{}");
96:                         out.println("\\author{}");
97:                 }
98:
99:•                if (!hasVdm_al)
100:                 {
101:                         out.println(BEGIN + getListingEnvironment()+BREAKLINES_OPTION);
102:                 }
103:
104:                 boolean endDocFound = false;
105:                 boolean inVdmAlModelTag = false;
106:
107:                 LexNameList spans = coverageUtil.getSpanNames(filename);
108:
109:•                for (int lnum = 1; lnum <= rawLines.size(); lnum++)
110:                 {
111:•                        for (ILexNameToken name : spans)
112:                         {
113:•                                if (name.getLocation().getStartLine() == lnum)
114:                                 {
115:                                         out.println(LST_ESCAPE_BEGIN);
116:                                         out.println("\\label{" + latexLabel(name.getName()) + ":"
117:                                                         + name.getLocation().getStartLine() + "}");
118:                                         out.println(LST_ESCAPE_END);
119:                                 }
120:                         }
121:
122:                         String line = rawLines.get(lnum - 1);
123:
124:•                        if (line.contains("\\end{document}"))
125:                         {
126:                                 endDocFound = true;
127:                                 break;
128:                         }
129:
130:•                        if (line.contains(BEGIN + CURLY_BRACKET_VDM_AL))
131:                         {
132:                                 inVdmAlModelTag = true;
133:                         }
134:
135:•                        if (hasVdm_al && modelOnly && !inVdmAlModelTag)
136:                         {
137:                                 continue;
138:                         }
139:
140:                         String spaced = detab(line, Properties.parser_tabstop);
141:                         spaced = spaced.replace(BEGIN + CURLY_BRACKET_VDM_AL, BEGIN
142:                                         + getListingEnvironment()+BREAKLINES_OPTION).replace(END
143:                                         + CURLY_BRACKET_VDM_AL, END + getListingEnvironment());
144:
145:•                        if (markCoverage)
146:                         {
147:                                 List<ILexLocation> list = hits.get(lnum);
148:                                 out.println(markup(spaced, list));
149:                         } else
150:                         {
151:                                 out.println(spaced);
152:                         }
153:
154:•                        if (line.contains(END + CURLY_BRACKET_VDM_AL))
155:                         {
156:                                 inVdmAlModelTag = false;
157:                         }
158:                 }
159:
160:•                if (!hasVdm_al)
161:                 {
162:                         out.println(END + getListingEnvironment());
163:                 }
164:
165:•                if (includeCoverageTable)
166:                 {
167:                         out.println("\\bigskip");
168:                         out.println(createCoverageTable(coverageUtil));
169:                 }
170:•                if (headers || endDocFound)
171:                 {
172:                         out.println("\\end{document}");
173:                 }
174:         }
175:
176:         private String getListingEnvironment()
177:         {
178:•                switch (Settings.dialect)
179:                 {
180:                         case VDM_PP:
181:                                 return CURLY_BRACKET_VDM_PP;
182:                         case VDM_RT:
183:                                 return CURLY_BRACKET_VDM_RT;
184:                         case VDM_SL:
185:                                 return CURLY_BRACKET_VDM_SL;
186:                         case CML:
187:                         default:
188:                                 return CURLY_BRACKET_VDM_AL;
189:                 }
190:         }
191:
192:         private String createCoverageTable(CoverageUtil coverageUtil)
193:         {
194:                 
195:                 StringBuilder sb = new StringBuilder();
196:                 sb.append("\\begin{longtable}{|l|r|r|r|}" + "\n");
197:                 sb.append("\\hline" + "\n");
198:                 sb.append("Function or operation & Line & Coverage & Calls \\\\" + "\n");
199:                 sb.append("\\hline" + "\n");
200:                 sb.append("\\hline" + "\n");
201:
202:                 long total = 0;
203:
204:                 LexNameList spans = coverageUtil.getSpanNames(filename);
205:                 Collections.sort(spans);
206:
207:•                for (ILexNameToken name : spans)
208:                 {
209:                         long calls = coverageUtil.getSpanCalls(name);
210:                         total += calls;
211:
212:                         sb.append("\\hyperref[" + latexLabel(name.getName()) + ":"
213:                                         + name.getLocation().getStartLine() + "]{"
214:                                         + latexQuote(name.toString()) + "} & "
215:                                         + name.getLocation().getStartLine() + "&"
216:                                         + coverageUtil.getSpanPercent(name) + "\\% & " + calls
217:                                         + " \\\\" + "\n");
218:                         sb.append("\\hline" + "\n");
219:                 }
220:
221:                 sb.append("\\hline" + "\n");
222:                 sb.append(latexQuote(filename.getName()) + " & & "
223:                                 + coverageUtil.getHitPercent(filename) + "\\% & " + total
224:                                 + " \\\\" + "\n");
225:
226:                 sb.append("\\hline" + "\n");
227:                 sb.append("\\end{longtable}" + "\n");
228:                 return sb.toString();
229:         }
230:
231:         private String markup(String line, List<ILexLocation> list)
232:         {
233:•                if (list == null)
234:                 {
235:                         return line;
236:                 } else
237:                 {
238:                         StringBuilder sb = new StringBuilder();
239:                         int p = 0;
240:
241:•                        for (ILexLocation m : list)
242:                         {
243:                                 int start = m.getStartPos() - 1;
244:•                                int end = m.getStartLine() == m.getEndLine() ? m.getEndPos() - 1
245:                                                 : line.length();
246:
247:•                                if (start >= p) // Backtracker produces duplicate tokens
248:                                 {
249:                                         sb.append(line.substring(p, start));
250:                                         sb.append(LST_ESCAPE_BEGIN + "\\vdmnotcovered{");
251:                                         sb.append(latexQuote(line.substring(start, end)));
252:                                         sb.append("}" + LST_ESCAPE_END); // \u00A3");
253:
254:                                         p = end;
255:                                 }
256:                         }
257:
258:                         sb.append(line.substring(p));
259:                         return sb.toString();
260:                 }
261:         }
262:
263:         private String latexQuote(String s)
264:         {
265:                 // Latex specials: \# \$ \% \^{} \& \_ \{ \} \~{} \\
266:
267:                 return s.replace("\\", "\\textbackslash ").replace("#", "\\#").replace("$", "\\$").replace("%", "\\%").replace("&", "\\&").replace("_", "\\_").replace("{", "\\{").replace("}", "\\}").replace("~", "\\~").replaceAll("\\^{1}", "\\\\^{}");
268:         }
269:
270:         private String latexLabel(String s)
271:         {
272:                 // Latex specials: \# \$ \% \^{} \& \_ \{ \} \~{} \\
273:                 return s.replace("\\", ":").replace("#", ":").replace("$", ":").replace("%", ":").replace("&", ":").replace("_", ":").replace("{", ":").replace("}", ":").replace("~", ":").replaceAll("\\^{1}", ":");
274:         }
275: }