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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include "stdafx.h"
#include "Parse.h"
#include "Tokenizer.h"
#include "Exception.h"
#include "Core/Io/Text.h"
#include "Core/Str.h"
namespace storm {
namespace syntax {
// Parses a name, eg foo.bar or foo<bar>.baz.
static SrcName *parseName(Engine &e, Tokenizer &tok) {
SrcName *result = new (e) SrcName(tok.position());
bool first = true;
do {
// Skip the previous dot.
if (!first)
tok.expect(S("."));
first = false;
Token id = tok.next();
if (tok.skipIf(S("<"))) {
Array<Name *> *params = new (e) Array<Name *>();
while (tok.peek() != S(">")) {
tok.skipIf(S(","));
params->push(parseName(e, tok));
}
tok.expect(S(">"));
result->add(new (e) RecPart(id.toS(), params));
} else {
result->add(id.toS());
}
} while (tok.peek() == S("."));
return result;
}
// See if a token is a separator.
static bool isTokenSep(const Token &t) {
return t == S(",")
|| t == S("-")
|| t == S(";")
|| t == S("=")
|| t == S("(")
|| t == S(")")
|| t == S("[")
|| t == S("]");
}
// Parse a parameter list (actual parameters).
static Array<Str *> *parseActuals(Engine &e, Tokenizer &tok) {
if (!tok.skipIf(S("(")))
return null;
if (tok.skipIf(S(",")))
throw new (e) SyntaxError(tok.position(), S("Actual parameters to a token may not start with a comma.")
S(" If you meant to start a capture, use - to disambiguate."));
Array<Str *> *r = new (e) Array<Str *>();
while (tok.peek() != S(")")) {
tok.skipIf(S(","));
r->push(tok.next().toS());
}
tok.expect(S(")"));
return r;
}
// Parse a token color.
static TokenColor parseTokenColor(Engine &e, Tokenizer &tok) {
TokenColor c = tokenColor(tok.next().toS());
if (c == tNone)
throw new (e) SyntaxError(tok.position(), S("Expected a color name."));
return c;
}
// Parse a token.
static TokenDecl *parseToken(Engine &e, Tokenizer &tok) {
TokenDecl *result = null;
if (tok.skipIf(S("-"))) {
return null;
} else if (tok.skipIf(S(","))) {
// The optional delimiter token. Might not be bound to anything.
return new (e) DelimTokenDecl(delim::optional);
} else if (tok.skipIf(S("~"))) {
// The required delimiter token. Might not be bound to anything.
return new (e) DelimTokenDecl(delim::required);
} else if (tok.peek().isStrLiteral()) {
// Regex.
Token regex = tok.next();
result = new (e) RegexTokenDecl(regex.strLiteral());
} else {
// Should be the name of something else.
SrcName *rule = parseName(e, tok);
RuleTokenDecl *r = new (e) RuleTokenDecl(rule->pos, rule);
r->params = parseActuals(e, tok);
result = r;
}
if (isTokenSep(tok.peek()))
return result;
if (tok.skipIf(S("@"))) {
result->raw = true;
if (isTokenSep(tok.peek()))
return result;
}
if (tok.skipIf(S("->"))) {
if (isTokenSep(tok.peek()))
throw new (e) SyntaxError(tok.position(), S("Expected identifier."));
result->invoke = tok.next().toS();
// Maybe a color name as well.
if (tok.skipIf(S("#")))
result->color = parseTokenColor(e, tok);
} else if (tok.skipIf(S("#"))) {
result->color = parseTokenColor(e, tok);
} else {
// Simple identifier.
result->store = tok.next().toS();
// Maybe a color name as well.
if (tok.skipIf(S("#")))
result->color = parseTokenColor(e, tok);
}
return result;
}
// Parse the bindings of a capture.
static TokenDecl *parseCapture(Engine &e, Tokenizer &tok, const Token &rep) {
if (rep == S("->")) {
TokenDecl *decl = new (e) TokenDecl();
decl->invoke = tok.next().toS();
return decl;
} else if (isTokenSep(rep)) {
throw new (e) SyntaxError(rep.pos, S("Expected -> or identifier."));
} else {
TokenDecl *decl = new (e) TokenDecl();
decl->store = rep.toS();
return decl;
}
}
// Parse a production.
static ProductionDecl *parseProduction(Engine &e, Tokenizer &tok, SrcName *rule) {
tok.expect(S(":"));
ProductionDecl *result = new (e) ProductionDecl(rule->pos, rule);
while (tok.peek() != S(";") && tok.peek() != S("=")) {
if (tok.skipIf(S("("))) {
// Start of a capture/repeat!
result->repStart = result->tokens->count();
} else if (tok.skipIf(S(")"))) {
// End of a capture/repeat!
result->repEnd = result->tokens->count();
Token rep = tok.next();
if (rep == S("?")) {
result->repType = repZeroOne;
} else if (rep == S("*")) {
result->repType = repZeroPlus;
} else if (rep == S("+")) {
result->repType = repOnePlus;
} else if (rep == S("@")) {
rep = tok.next();
result->repCapture = parseCapture(e, tok, rep);
result->repCapture->raw = true;
} else if (isTokenSep(rep)) {
throw new (e) SyntaxError(rep.pos, S("Expected ?, *, +, ->, @ or identifier."));
} else {
result->repCapture = parseCapture(e, tok, rep);
}
} else if (tok.skipIf(S("["))) {
// Start of an indented block.
result->indentStart = result->tokens->count();
} else if (tok.skipIf(S("]"))) {
// End of an indented block. See what kind of indentation to use.
result->indentEnd = result->tokens->count();
Token kind = tok.next();
if (kind == S("+")) {
result->indentType = indentIncrease;
} else if (kind == S("-")) {
result->indentType = indentDecrease;
} else if (kind == S("?")) {
result->indentType = indentWeakIncrease;
} else if (kind == S("@")) {
result->indentType = indentAlignBegin;
} else if (kind == S("$")) {
result->indentType = indentAlignEnd;
} else {
throw new (e) SyntaxError(kind.pos, TO_S(e, S("Unexpected indentation kind: ") << kind.toS()));
}
} else {
TokenDecl *token = parseToken(e, tok);
if (token)
result->tokens->push(token);
}
}
if (tok.skipIf(S("=")))
result->name = tok.next().toS();
return result;
}
// Parse a production with a result.
static ProductionDecl *parseProductionResult(Engine &e, Tokenizer &tok, SrcName *rule) {
tok.expect(S("=>"));
Name *name = parseName(e, tok);
Array<Str *> *params = parseActuals(e, tok);
ProductionDecl *result = parseProduction(e, tok, rule);
result->result = name;
result->resultParams = params;
return result;
}
// Parse a production with a priority.
static ProductionDecl *parseProductionPriority(Engine &e, Tokenizer &tok, SrcName *rule) {
tok.expect(S("["));
Int prio = 0;
try {
if (tok.skipIf(S("-"))) {
prio = -tok.next().toS()->toInt();
} else if (tok.skipIf(S("+"))) {
prio = tok.next().toS()->toInt();
} else {
prio = tok.next().toS()->toInt();
}
} catch (const StrError *e) {
throw new (e) SyntaxError(tok.position(), e->message());
}
tok.expect(S("]"));
ProductionDecl *result = null;
Token sep = tok.peek();
if (sep == S(":")) {
result = parseProduction(e, tok, rule);
} else if (sep == S("=>")) {
result = parseProductionResult(e, tok, rule);
} else {
throw new (e) SyntaxError(sep.pos, TO_S(e, S("Unexpected token: ") << sep.toS()));
}
result->priority = prio;
return result;
}
// Parse a rule declaration.
static RuleDecl *parseRule(Engine &e, Tokenizer &tok, SrcName *result) {
Token name = tok.next();
RuleDecl *r = new (e) RuleDecl(name.pos, name.toS(), result);
tok.expect(S("("));
while (tok.peek() != S(")")) {
tok.skipIf(S(","));
Name *type = parseName(e, tok);
Str *name = tok.next().toS();
r->params->push(ParamDecl(type, name));
}
tok.expect(S(")"));
if (tok.skipIf(S("#")))
r->color = parseTokenColor(e, tok);
return r;
}
static FileContents *parseFile(Engine &e, Tokenizer &tok) {
FileContents *r = new (e) FileContents();
while (tok.more()) {
if (tok.skipIf(S("use"))) {
r->use->push(parseName(e, tok));
} else if (tok.skipIf(S("optional"))) {
tok.expect(S("delimiter"));
tok.expect(S("="));
r->optionalDelimiter = parseName(e, tok);
} else if (tok.skipIf(S("required"))) {
tok.expect(S("delimiter"));
tok.expect(S("="));
r->requiredDelimiter = parseName(e, tok);
} else if (tok.skipIf(S("delimiter"))) {
tok.expect(S("="));
r->requiredDelimiter = r->optionalDelimiter = parseName(e, tok);
} else {
SrcName *name = parseName(e, tok);
Token sep = tok.peek();
// Comment containind documentation. May be empty, but that's fine.
SrcPos comment = tok.comment();
if (sep == S(":")) {
r->productions->push(applyDoc(comment, parseProduction(e, tok, name)));
} else if (sep == S("=>")) {
r->productions->push(applyDoc(comment, parseProductionResult(e, tok, name)));
} else if (sep == S("[")) {
r->productions->push(applyDoc(comment, parseProductionPriority(e, tok, name)));
} else {
r->rules->push(applyDoc(comment, parseRule(e, tok, name)));
}
}
tok.clearComment();
tok.expect(S(";"));
}
return r;
}
FileContents *parseSyntax(Str *data, Url *url, Str::Iter start) {
Tokenizer tok(url, data, start.offset());
return parseFile(data->engine(), tok);
}
}
}
|