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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
#include "stdafx.h"
#include "Doc.h"
#include "Function.h"
#include "Type.h"
#include "Class.h"
#include "Compiler/Exception.h"
#include "Core/Io/Text.h"
#include "Core/Io/Stream.h"
namespace storm {
namespace bs {
struct State {
enum S {
start,
start2,
done,
singleStart,
singleInside,
singleNewline,
singleHalf,
singleBefore,
multiStart,
multiInside,
multiNewline,
multiBefore,
};
// State.
S state;
// Number of spaces after the comment.
Nat space;
// Current number of spaces.
Nat curr;
// Number of empty lines encountered.
Nat empty;
};
static bool whitespace(Char ch) {
switch (ch.codepoint()) {
case ' ':
case '\n':
case '\r':
case '\t':
return true;
default:
return false;
}
}
static const wchar *parseBody(StrBuf *out, State &s, Char ch) {
switch (s.state) {
case State::start:
// Find a '/'.
if (ch == Char('/')) {
s.state = State::start2;
return null;
}
if (whitespace(ch))
return null;
return S("Could not find the start of the comment.");
case State::start2:
// Either * or /
if (ch == Char('*'))
s.state = State::multiStart;
else if (ch == Char('/'))
s.state = State::singleStart;
else
return S("Unknown comment type.");
return null;
case State::singleStart:
if (whitespace(ch)) {
s.space++;
} else {
*out << ch;
s.state = State::singleInside;
}
return null;
case State::singleInside:
if (ch == Char('\n')) {
s.curr = 0;
s.empty = 1;
s.state = State::singleNewline;
} else {
*out << ch;
}
return null;
case State::singleNewline:
if (ch == Char('/'))
s.state = State::singleHalf;
else if (!whitespace(ch))
s.state = State::done;
return null;
case State::singleHalf:
if (ch == Char('/'))
s.state = State::singleBefore;
else
s.state = State::done;
return null;
case State::singleBefore:
if (ch == Char(' ')) {
s.curr++;
} else if (ch == Char('\n')) {
s.empty++;
s.curr = 0;
s.state = State::singleNewline;
} else if (!whitespace(ch)) {
while (s.empty) {
*out << Char('\n');
s.empty--;
}
if (s.curr < s.space)
return S("Not enough indentation after //");
for (Nat i = s.space; i < s.curr; i++)
*out << Char(' ');
s.curr = 0;
*out << ch;
s.state = State::singleInside;
}
return null;
case State::multiStart:
if (ch == Char('*')) {
s.space = 0;
} else if (whitespace(ch)) {
s.space++;
} else {
*out << ch;
s.state = State::multiInside;
}
return null;
case State::multiInside:
if (ch == Char('\n')) {
s.curr = 0;
s.empty = 1;
s.state = State::multiNewline;
} else {
*out << ch;
}
return null;
case State::multiNewline:
if (ch == Char('*'))
s.state = State::multiBefore;
else if (!whitespace(ch))
s.state = State::done;
return null;
case State::multiBefore:
if (s.curr == 0 && ch == Char('/')) {
s.state = State::done;
} else if (ch == Char(' ')) {
s.curr++;
} else if (ch == Char('\n')) {
s.empty++;
s.curr = 0;
s.state = State::multiNewline;
} else if (!whitespace(ch)) {
while (s.empty) {
*out << Char('\n');
s.empty--;
}
if (s.curr < s.space)
return S("Not enough indentation after the first *");
for (Nat i = s.space; i < s.curr; i++)
*out << Char(' ');
s.curr = 0;
*out << ch;
s.state = State::multiInside;
}
return null;
}
return S("Internal error.");
}
static Str *parseBody(SrcPos pos, TextInput *src) {
StrBuf *out = new (src) StrBuf();
State state = { State::start, 0, 0, 0 };
while (state.state != State::done && src->more()) {
Char ch = src->read();
if (ch == Char('\r'))
continue;
if (const wchar *msg = parseBody(out, state, ch)) {
*out << S("\n\n<incomplete comment: ") << msg << S(" Is the source changed?>");
state.state = State::done;
}
}
if (state.state != State::done)
*out << S("\n\n<incomplete comment>");
return out->toS();
}
BSDoc::BSDoc(SrcPos docPos, Named *entity) : docPos(docPos), entity(entity) {}
Doc *BSDoc::get() {
Str *body = readBody();
Array<DocParam> *params = createParams();
return doc(entity, params, body);
}
Str *BSDoc::readBody() {
if (!docPos.file)
return new (this) Str(S(""));
IStream *s = docPos.file->read();
TextInput *src = readText(s);
// Skip until we reach the indicated position.
Nat pos = 0;
while (src->more() && pos < docPos.start) {
src->read();
pos++;
}
if (pos != docPos.start) {
s->close();
Str *msg = TO_S(engine(), S("Unable to find a comment in ") << docPos << S(". Is the file changed?"));
throw new (this) DocError(msg);
}
try {
Str *body = parseBody(docPos, src);
s->close();
return body;
} catch (...) {
s->close();
throw;
}
}
Array<DocParam> *BSDoc::createParams() {
if (BSRawFn *bs = as<BSRawFn>(entity)) {
return bs->docParams();
} else {
Str *empty = new (this) Str(S(""));
Array<DocParam> *r = new (this) Array<DocParam>();
r->reserve(entity->params->count());
for (Nat i = 0; i < entity->params->count(); i++)
r->push(DocParam(empty, entity->params->at(i)));
return r;
}
}
TObject *applyDoc(SrcPos pos, TObject *to) {
if (Named *named = as<Named>(to)) {
named->documentation = new (named) BSDoc(pos, named);
} else if (NamedDecl *decl = as<NamedDecl>(to)) {
// Tell the declaration where the documentation is. It will call us again later so
// that we can set the documentation properly.
decl->docPos = pos;
} else if (MemberWrap *wrap = as<MemberWrap>(to)) {
// Remember where the documentation is. It will call us again later so that we can
// set the documentation properly.
wrap->docPos = pos;
} else {
// TODO: Handle documentation for generators as well!
// PVAR(runtime::typeOf(to)->identifier());
}
return to;
}
}
}
|