This repository was archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyleMain.h
126 lines (110 loc) · 3.29 KB
/
StyleMain.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef STYLE_MAIN_H_
#define STYLE_MAIN_H_
#include "Scanner.h"
#include "LosslessScanner.h"
#include <fstream>
#include <sstream>
#include "BraceChecker.h"
#include "IndentChecker.h"
#include "NonConstGlobalChecker.h"
#include "SingleLetterVarWarner.h"
#include <string>
#ifdef DEBUG
#include <iostream>
#endif
struct StyleFeedbackResponse {
bool passed;
bool warned;
std::string msg;
StyleFeedbackResponse(bool b, bool w, const std::string& m)
: passed(b), warned(w), msg(m) {}
};
StyleFeedbackResponse getStyleFeedback(const std::string &fileName) {
bool passed = true;
bool warned = false;
std::string warnedMsg;
std::ifstream in(fileName);
std::ostringstream out;
std::vector<LosslessToken> losslessTokens = losslessScan(in);
in.clear();
in.seekg(0, in.beg);
std::vector<Lossy::Token> tokens = scan(in);
#ifdef DEBUG
for (Lossy::Token t : tokens) {
std::cout << tokenToStr(t);
}
#endif
int missingBraceLine = getFirstMissingBraceLine(tokens);
if (missingBraceLine != -1) {
passed = false;
out << "ERROR: Line " << missingBraceLine << ": Expected open brace\n";
return StyleFeedbackResponse(passed, warned, out.str());
}
int singleLetterVarLine = getFirstSingleLetterVarLine(losslessTokens);
if (singleLetterVarLine != -1) {
warned = true;
warnedMsg = "WARNING: Line " + std::to_string(singleLetterVarLine)
+ ": Single-letter variable name outside of a for loop\n"
"\tConsider declaring for loop iterators in their respective for loops\n"
"\t or using a more descriptive name\n";
}
int diffStyleLine = getFirstDiffBraceStyleLine(tokens);
if (diffStyleLine != -1) {
passed = false;
out << "ERROR: Line " << diffStyleLine << ": Different brace style\n";
if (warned) {
out << warnedMsg;
}
return StyleFeedbackResponse(passed, warned, out.str());
}
int badIndentLine = getFirstBadIndentLine(tokens);
if (badIndentLine != -1) {
passed = false;
out << "ERROR: Line " << badIndentLine << ": Bad indentation\n";
if (warned) {
out << warnedMsg;
}
return StyleFeedbackResponse(passed, warned, out.str());
}
int nonConstGlobalLine = getFirstNonConstGlobalLine(tokens);
if (nonConstGlobalLine != -1) {
passed = false;
out << "ERROR: Line " << nonConstGlobalLine << ": Non-constant global variable\n";
if (warned) {
out << warnedMsg;
}
return StyleFeedbackResponse(passed, warned, out.str());
}
if (warned) {
out << warnedMsg;
}
if (passed && !warned) {
out << "No style issues detected\n";
}
return StyleFeedbackResponse(passed, warned, out.str());
}
bool styleTestPassed(std::ofstream& testFeedback, const std::vector<std::string>& fileNames) {
bool passedAll = true;
bool havePrinted = false;
for (const std::string &fileName : fileNames) {
StyleFeedbackResponse res = getStyleFeedback("../" + fileName);
if (!res.passed || res.warned) {
if (havePrinted) {
testFeedback << '\n';
}
havePrinted = true;
if (!res.passed) {
passedAll = false;
}
if (fileNames.size() > 1) {
testFeedback << fileName << ":";
}
testFeedback << res.msg;
}
}
if (!havePrinted) {
testFeedback << "No auto-graded style issues detected";
}
return passedAll;
}
#endif