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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// TestLexers.cxx : Test lexers through Lexilla
//
#include <cassert>
#include <string>
#include <string_view>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
#include <fstream>
#include <filesystem>
#include "ILexer.h"
#include "TestDocument.h"
#include "LexillaAccess.h"
namespace {
std::string ReadFile(std::filesystem::path path) {
std::ifstream ifs(path, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
return content;
}
std::string MarkedDocument(const Scintilla::IDocument *pdoc) {
std::ostringstream os(std::ios::binary);
char prevStyle = -1;
for (Sci_Position pos = 0; pos < pdoc->Length(); pos++) {
const char styleNow = pdoc->StyleAt(pos);
if (styleNow != prevStyle) {
os << "{" << static_cast<unsigned int>(styleNow) << "}";
prevStyle = styleNow;
}
char ch = '\0';
pdoc->GetCharRange(&ch, pos, 1);
os << ch;
}
return os.str();
}
std::map<std::string, std::string> PropertiesFromFile(std::filesystem::path path) {
std::map<std::string, std::string> m;
std::ifstream ifs(path);
std::string line;
std::string logicalLine;
while (std::getline(ifs, line)) {
logicalLine += line;
if (logicalLine.ends_with("\\")) {
logicalLine.pop_back();
} else {
const size_t positionEquals = logicalLine.find("=");
if (positionEquals != std::string::npos) {
const std::string key = logicalLine.substr(0, positionEquals);
const std::string value = logicalLine.substr(positionEquals+1);
m[key] = value;
}
logicalLine.clear();
}
}
return m;
}
const std::string BOM = "\xEF\xBB\xBF";
bool TestFile(std::filesystem::path path,
std::map<std::string, std::string> properties) {
// Find and create correct lexer
std::string language;
Scintilla::ILexer5 *plex = nullptr;
for (auto const &[key, val] : properties) {
if (key.starts_with("lexer.*")) {
language = val;
plex = CreateLexer(language);
break;
}
}
if (!plex) {
return false;
}
// Set parameters of lexer
const std::string keywords = "keywords";
for (auto const &[key, val] : properties) {
if (key.starts_with("#")) {
// Ignore comments
} else if (key.starts_with("lexer.*")) {
// Ignore
} else if (key.starts_with("keywords")) {
// Get character after keywords
std::string afterKeywords = key.substr(keywords.length(), 1);
char characterAfterKeywords = afterKeywords.empty() ? '1' : afterKeywords[0];
if (characterAfterKeywords < '1' || characterAfterKeywords > '9')
characterAfterKeywords = '1';
const int wordSet = characterAfterKeywords - '1';
plex->WordListSet(wordSet, val.c_str());
} else {
plex->PropertySet(key.c_str(), val.c_str());
}
}
std::string text = ReadFile(path);
if (text.starts_with(BOM)) {
text.erase(0, BOM.length());
}
TestDocument doc;
doc.Set(text);
Scintilla::IDocument *pdoc = &doc;
plex->Lex(0, pdoc->Length(), 0, pdoc);
const std::string styledTextNew = MarkedDocument(pdoc);
std::filesystem::path pathStyled = path;
pathStyled += ".styled";
const std::string styledText = ReadFile(pathStyled);
if (styledTextNew != styledText) {
std::cout << "\n" << path.string() << ":1: is different\n\n";
std::filesystem::path pathNew = path;
pathNew += ".new";
std::ofstream ofs(pathNew, std::ios::binary);
ofs << styledTextNew;
}
return true;
}
bool TestDirectory(std::filesystem::path directory, std::filesystem::path basePath) {
const std::map<std::string, std::string> properties = PropertiesFromFile(directory / "SciTE.properties");
bool success = true;
for (auto &p : std::filesystem::directory_iterator(directory)) {
if (!p.is_directory()) {
const std::string extension = p.path().extension().string();
if (extension != ".properties" && extension != ".styled" && extension != ".new") {
const std::filesystem::path relativePath = p.path().lexically_relative(basePath);
std::cout << "Lexing " << relativePath.string() << '\n';
if (!TestFile(p, properties)) {
success = false;
}
}
}
}
return success;
}
bool AccessLexilla(std::filesystem::path basePath) {
if (!std::filesystem::exists(basePath)) {
std::cout << "No examples at " << basePath.string() << "\n";
return false;
}
bool success = true;
for (auto &p : std::filesystem::recursive_directory_iterator(basePath)) {
if (p.is_directory()) {
//std::cout << p.path().string() << '\n';
if (!TestDirectory(p, basePath)) {
success = false;
}
}
}
return success;
}
std::filesystem::path FindScintillaDirectory(std::filesystem::path startDirectory) {
std::filesystem::path directory = startDirectory;
while (!directory.empty()) {
const std::filesystem::path localScintilla = directory / "scintilla";
const std::filesystem::directory_entry entry(localScintilla);
if (entry.is_directory()) {
std::cout << "Found Scintilla at " << entry.path().string() << "\n";
return localScintilla;
}
const std::filesystem::path parent = directory.parent_path();
if (parent == directory) {
std::cout << "Reached root at " << directory.string() << "\n";
return std::filesystem::path();
}
directory = parent;
}
return std::filesystem::path();
}
}
int main() {
// TODO: Allow specifying the base directory through a command line argument
const std::filesystem::path baseDirectory = FindScintillaDirectory(std::filesystem::current_path());
if (!baseDirectory.empty()) {
if (LoadLexilla(baseDirectory)) {
AccessLexilla(baseDirectory / "lexilla" / "test" / "examples");
}
}
}
|