-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathrstparser.cc
More file actions
249 lines (227 loc) · 6.73 KB
/
rstparser.cc
File metadata and controls
249 lines (227 loc) · 6.73 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
A reStructuredText parser written in C++.
Copyright (c) 2013, Victor Zverovich
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mp/rstparser.h"
#include <cctype>
#include <cstring>
namespace {
inline bool IsSpace(char c) {
switch (c) {
case ' ': case '\t': case '\v': case '\f':
return true;
}
return false;
}
// Returns true if s ends with string end.
bool EndsWith(const std::string &s, const char *end) {
std::size_t size = s.size(), end_size = std::strlen(end);
return size >= end_size ? std::strcmp(&s[size - end_size], end) == 0 : false;
}
}
rst::ContentHandler::~ContentHandler() {}
void rst::Parser::SkipSpace() {
while (IsSpace(*ptr_))
++ptr_;
}
std::string rst::Parser::ParseDirectiveType() {
const char *s = ptr_;
if (!std::isalnum(*s))
return std::string();
for (;;) {
++s;
if (std::isalnum(*s))
continue;
switch (*s) {
case '-': case '_': case '+': case ':': case '.':
if (std::isalnum(s[1])) {
++s;
continue;
}
// Fall through.
}
break;
}
std::string type;
if (s != ptr_)
type.assign(ptr_, s);
ptr_ = s;
return type;
}
void rst::Parser::EnterBlock(rst::BlockType &prev_type, rst::BlockType type) {
if (type == prev_type)
return;
if (prev_type == LIST_ITEM)
handler_->EndBlock();
if (type == LIST_ITEM)
handler_->StartBlock(BULLET_LIST);
prev_type = type;
}
void rst::Parser::ParseBlock(
rst::BlockType type, rst::BlockType &prev_type, int indent) {
std::string text;
for (bool first = true; ; first = false) {
const char *line_start = ptr_;
if (!first) {
// Check indentation.
SkipSpace();
if (ptr_ - line_start != indent)
break;
if (*ptr_ == '\n') {
++ptr_;
break; // Empty line ends the block.
}
if (!*ptr_)
break; // End of input.
}
// Strip indentation.
line_start = ptr_;
// Find the end of the line.
while (*ptr_ && *ptr_ != '\n')
++ptr_;
// Strip whitespace at the end of the line.
const char *end = ptr_;
while (end != line_start && IsSpace(end[-1]))
--end;
// Copy text converting all whitespace characters to spaces.
text.reserve(end - line_start + 1);
if (!first)
text.push_back('\n');
enum {TAB_WIDTH = 8};
for (const char *s = line_start; s != end; ++s) {
char c = *s;
if (c == '\t') {
text.append(" ",
TAB_WIDTH - ((indent + s - line_start) % TAB_WIDTH));
} else if (IsSpace(c)) {
text.push_back(' ');
} else {
text.push_back(*s);
}
}
if (*ptr_ == '\n')
++ptr_;
}
// Remove a trailing newline.
if (*text.rbegin() == '\n')
text.resize(text.size() - 1);
bool literal = type == PARAGRAPH && EndsWith(text, "::");
if (!literal || text.size() != 2) {
std::size_t size = text.size();
if (literal)
--size;
EnterBlock(prev_type, type);
handler_->StartBlock(type);
handler_->HandleText(text.c_str(), size);
handler_->EndBlock();
}
if (literal) {
// Parse a literal block.
const char *line_start = ptr_;
SkipSpace();
int new_indent = static_cast<int>(ptr_ - line_start);
if (new_indent > indent)
ParseBlock(LITERAL_BLOCK, prev_type, new_indent);
}
}
void rst::Parser::ParseLineBlock(rst::BlockType &prev_type, int indent) {
std::string text;
for (bool first = true; ; first = false) {
const char *line_start = ptr_;
if (!first) {
// Check indentation.
SkipSpace();
if (*ptr_ != '|' || !IsSpace(ptr_[1]) || ptr_ - line_start != indent)
break;
ptr_ += 2;
if (!*ptr_)
break; // End of input.
}
// Strip indentation.
line_start = ptr_;
// Find the end of the line.
while (*ptr_ && *ptr_ != '\n')
++ptr_;
if (*ptr_ == '\n')
++ptr_;
text.append(line_start, ptr_);
}
EnterBlock(prev_type, rst::LINE_BLOCK);
handler_->StartBlock(rst::LINE_BLOCK);
handler_->HandleText(text.c_str(), text.size());
handler_->EndBlock();
}
void rst::Parser::Parse(const char *s) {
BlockType prev_type = PARAGRAPH;
ptr_ = s;
while (*ptr_) {
// Skip whitespace and empty lines.
const char *line_start = ptr_;
SkipSpace();
if (*ptr_ == '\n') {
++ptr_;
continue;
}
switch (*ptr_) {
case '.':
if (ptr_[1] == '.') {
char c = ptr_[2];
if (!IsSpace(c) && c != '\n' && c)
break;
// Parse a directive or a comment.
ptr_ += 2;
SkipSpace();
std::string type = ParseDirectiveType();
if (!type.empty() && ptr_[0] == ':' && ptr_[1] == ':') {
ptr_ += 2;
handler_->HandleDirective(type.c_str());
}
// Skip everything till the end of the line.
while (*ptr_ && *ptr_ != '\n')
++ptr_;
if (*ptr_ == '\n')
++ptr_;
continue;
}
break;
case '*': case '+': case '-':
if (IsSpace(ptr_[1])) {
// Parse a bullet list item.
ptr_ += 2;
ParseBlock(LIST_ITEM, prev_type, static_cast<int>(ptr_ - line_start));
continue;
}
break;
case '|':
if (IsSpace(ptr_[1])) {
// Parse a line block.
int indent = static_cast<int>(ptr_ - line_start);
ptr_ += 2;
ParseLineBlock(prev_type, indent);
continue;
}
break;
}
ParseBlock(std::isspace(line_start[0]) ? BLOCK_QUOTE : PARAGRAPH,
prev_type, static_cast<int>(ptr_ - line_start));
}
EnterBlock(prev_type, PARAGRAPH);
}