-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathparser2.cpp
More file actions
209 lines (166 loc) · 4.16 KB
/
Copy pathparser2.cpp
File metadata and controls
209 lines (166 loc) · 4.16 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <cpp11/list.hpp>
#include <cpp11/list_of.hpp>
#include <cpp11/strings.hpp>
#include <cpp11/protect.hpp>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
class RoxygenLine {
std::string line_;
const char* begin_;
const char* end_;
const char* cur_;
public:
RoxygenLine(const std::string& line) : line_(line) {
begin_ = cur_ = line_.data();
end_ = begin_ + line_.size();
}
bool consumeChar(char c) {
if (cur_ == end_ || *cur_ != c)
return false;
cur_++;
return true;
}
int consumeWhitespace(int max = -1) {
int i = 0;
while (cur_ != end_ && std::isspace(*cur_)) {
cur_++;
i++;
if (max > 0 && i >= max)
break;
}
return i;
}
bool consumeRoxygenComment() {
consumeWhitespace();
if (!consumeChar('#'))
return false;
while (consumeChar('#'));
if (!consumeChar('\''))
return false;
consumeWhitespace(1);
return true;
}
bool consumeTag(std::string* pOut) {
if (!consumeChar('@'))
return false;
while(cur_ != end_ && std::isalnum(*cur_) ) {
pOut->push_back(*cur_);
cur_++;
}
return true;
}
bool consumeText(std::string* pOut) {
while (cur_ != end_) {
if (isEscapedAt()) {
pOut->push_back('@');
cur_ += 2;
} else {
pOut->push_back(*cur_);
cur_++;
}
}
return true;
}
bool isEscapedAt() {
if (cur_ == end_)
return false;
if (*cur_ != '@')
return false;
const char* next = cur_ + 1;
if (next == end_)
return false;
return *next == '@';
}
};
std::string stripTrailingNewline(std::string x) {
if (x[x.size() - 1] == '\n') {
x.resize(x.size() - 1);
}
return x;
}
[[cpp11::register]]
cpp11::list tokenise_block(cpp11::strings lines, std::string file,
int offset) {
std::vector<std::string> tags, vals;
std::vector<int> rows;
int curRow = 0;
std::string curTag(""), curVal("");
for (int i = 0; i < lines.size(); ++i) {
RoxygenLine line((std::string(lines[i])));
if (!line.consumeRoxygenComment()) {
// Increment curRow for non-roxygen comments at start of block
if (curVal.empty())
curRow++;
continue;
}
std::string tag;
if (line.consumeTag(&tag)) {
line.consumeWhitespace(1);
if (curVal != "" || curTag != "") {
rows.push_back(curRow);
tags.push_back(curTag);
vals.push_back(curVal);
}
curRow = i;
curTag.assign(tag);
curVal.assign("");
}
line.consumeText(&curVal);
curVal.push_back('\n');
}
if (curVal != "" || curTag != "") {
rows.push_back(curRow);
tags.push_back(curTag);
vals.push_back(curVal);
}
// Convert to a list
R_xlen_t n = rows.size();
cpp11::writable::list out(n);
using namespace cpp11::literals;
for (R_xlen_t i = 0; i < n; ++i) {
cpp11::writable::list x({
"file"_nm = file,
"line"_nm = rows[i] + offset,
"tag"_nm = tags[i],
"raw"_nm = stripTrailingNewline(vals[i]),
"val"_nm = R_NilValue
});
std::string tag("roxy_tag_");
tag += tags[i];
x.attr("class") = {tag.c_str(), "roxy_tag"};
out[i] = x;
}
return out;
}
[[cpp11::register]]
cpp11::strings find_includes(std::string path) {
std::vector<std::string> includes;
std::string path_native = Rf_translateChar(cpp11::r_string(path));
std::ifstream file(path_native.c_str());
if (!file.good())
cpp11::stop("Failed to open %s", path.c_str());
std::string rawline;
while (std::getline(file, rawline)) {
RoxygenLine line(rawline);
if (!line.consumeRoxygenComment())
continue;
std::string tag, value;
if (!line.consumeTag(&tag))
continue;
if (tag != "include")
continue;
line.consumeWhitespace(1);
// Split value by whitespace
// http://stackoverflow.com/questions/236129/split-a-string-in-c
line.consumeText(&value);
std::istringstream words(value);
copy(
std::istream_iterator<std::string>(words),
std::istream_iterator<std::string>(),
back_inserter(includes)
);
}
return cpp11::as_sexp(includes);
}