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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
//===-- Parser.cpp --------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llbuild/Ninja/Parser.h"
#include "llbuild/Basic/LLVM.h"
#include "llbuild/Ninja/Lexer.h"
using namespace llbuild;
using namespace llbuild::ninja;
#pragma mark - ParseActions
ParseActions::~ParseActions() {
}
#pragma mark - Parser Implementation
class Parser::ParserImpl {
Lexer lexer;
ParseActions &actions;
/// The currently lexed token.
Token tok;
/// @name Diagnostics Support
/// @{
void error(StringRef message, const Token& at) {
actions.error(message, at);
}
void error(StringRef message) {
error(message, tok);
}
/// @}
void getNextNonCommentToken() {
do {
lexer.lex(tok);
} while (tok.tokenKind == Token::Kind::Comment);
}
/// Consume the current 'peek token' and lex the next one.
void consumeToken() {
getNextNonCommentToken();
}
/// Check that the current token is of the expected kind and consume it,
/// returning the token.
Token consumeExpectedToken(Token::Kind kind) {
assert(tok.tokenKind == kind && "Unexpected token!");
Token result = tok;
getNextNonCommentToken();
return result;
}
/// Consume the current token if it matches the given kind, returning whether
/// or not it was consumed.
bool consumeIfToken(Token::Kind kind) {
if (tok.tokenKind == kind) {
getNextNonCommentToken();
return true;
} else {
return false;
}
}
/// Consume tokens until past the next newline (or end of file).
void skipPastEOL() {
while (tok.tokenKind != Token::Kind::Newline &&
tok.tokenKind != Token::Kind::EndOfFile)
lexer.lex(tok);
// Always consume at least one token.
consumeToken();
}
/// Parse a top-level declaration.
void parseDecl();
/// Parse a variable binding.
///
/// \param name_out [out] On success, the identifier token for the binding
/// name.
///
/// \param value_out [out] On success, the string token for the binding
/// value.
///
/// \returns True on success. On failure, the lexer will be advanced past the
/// next newline (see \see skipPastEOL()). In either case, the lexer will be
/// taken out of any specific parsing mode.
bool parseBindingInternal(Token* name_out, Token* value_out);
void parseBindingDecl();
void parseDefaultDecl();
void parseIncludeDecl();
void parseParameterizedDecl();
bool parseBuildSpecifier(ParseActions::BuildResult *decl_out);
bool parsePoolSpecifier(ParseActions::PoolResult *decl_out);
bool parseRuleSpecifier(ParseActions::RuleResult *decl_out);
public:
ParserImpl(StringRef data, ParseActions& actions);
void parse();
ParseActions& getActions() { return actions; }
const class Lexer& getLexer() const { return lexer; }
};
Parser::ParserImpl::ParserImpl(StringRef data, ParseActions &actions)
: lexer(data), actions(actions) {}
/// Parse the file.
void Parser::ParserImpl::parse() {
// Initialize the Lexer.
getNextNonCommentToken();
actions.actOnBeginManifest("<main>");
while (tok.tokenKind != Token::Kind::EndOfFile) {
// Check that no one accidentally left the lexer in the wrong mode.
assert(lexer.getMode() == Lexer::LexingMode::None);
parseDecl();
}
actions.actOnEndManifest();
}
/// Parse a declaration.
///
/// decl ::= default-decl | include-decl | binding-decl | parameterized-decl
void Parser::ParserImpl::parseDecl() {
switch (tok.tokenKind) {
case Token::Kind::Newline:
consumeToken();
break;
case Token::Kind::KWBuild:
case Token::Kind::KWRule:
case Token::Kind::KWPool:
parseParameterizedDecl();
break;
case Token::Kind::KWDefault:
parseDefaultDecl();
break;
case Token::Kind::KWInclude:
case Token::Kind::KWSubninja:
parseIncludeDecl();
break;
case Token::Kind::Identifier:
parseBindingDecl();
break;
default:
error("unexpected token");
skipPastEOL();
}
}
/// binding-decl ::= identifier '=' var-string '\n'
bool Parser::ParserImpl::parseBindingInternal(Token* name_out,
Token* value_out) {
// The leading token should be an identifier.
if (tok.tokenKind != Token::Kind::Identifier) {
error("expected variable name");
lexer.setMode(Lexer::LexingMode::None);
skipPastEOL();
return false;
}
*name_out = consumeExpectedToken(Token::Kind::Identifier);
// Expect the variable name to be followed by '='.
if (tok.tokenKind != Token::Kind::Equals) {
error("expected '=' token");
lexer.setMode(Lexer::LexingMode::None);
skipPastEOL();
return false;
}
// Consume the '=' and the RHS.
//
// We must consume the RHS in variable lexing mode, so given our lookahead
// design we switch modes, consume the equals (which will advance the lexer),
// and then immediately switch back.
lexer.setMode(Lexer::LexingMode::VariableString);
consumeExpectedToken(Token::Kind::Equals);
lexer.setMode(Lexer::LexingMode::None);
// If the RHS is a newline, we have an empty binding which we handle as a
// special case.
if (tok.tokenKind == Token::Kind::Newline) {
// Derive an empty string token from the newline.
*value_out = consumeExpectedToken(Token::Kind::Newline);
value_out->tokenKind = Token::Kind::String;
value_out->length = 0;
return true;
}
// Otherwise, check that the RHS is a string.
if (tok.tokenKind != Token::Kind::String) {
error("expected variable value");
skipPastEOL();
return false;
}
*value_out = consumeExpectedToken(Token::Kind::String);
// The binding should be terminated by a newline.
if (!consumeIfToken(Token::Kind::Newline)) {
error("expected newline token");
skipPastEOL();
return false;
}
return true;
}
/// binding-decl ::= identifier '=' var-string '\n'
void Parser::ParserImpl::parseBindingDecl() {
Token name, value;
if (parseBindingInternal(&name, &value))
actions.actOnBindingDecl(name, value);
}
/// default-decl ::= "default" path-string-list '\n'
void Parser::ParserImpl::parseDefaultDecl() {
// Put the lexer in path string mode.
lexer.setMode(Lexer::LexingMode::PathString);
consumeExpectedToken(Token::Kind::KWDefault);
// Consume all the strings.
SmallVector<Token, 8> names;
while (tok.tokenKind == Token::Kind::String) {
names.push_back(consumeExpectedToken(Token::Kind::String));
}
// Reset the string mode.
lexer.setMode(Lexer::LexingMode::None);
// Verify we have at least one name.
if (names.empty()) {
error("expected target path string");
return skipPastEOL();
}
// The list should be terminated by a newline.
if (!consumeIfToken(Token::Kind::Newline)) {
error("expected newline token");
return skipPastEOL();
}
actions.actOnDefaultDecl(names);
}
/// include-decl ::= ( "include" | "subninja" ) path-string '\n'
void Parser::ParserImpl::parseIncludeDecl() {
// Put the lexer in path string mode for one token.
lexer.setMode(Lexer::LexingMode::PathString);
bool isInclude = tok.tokenKind == Token::Kind::KWInclude;
consumeExpectedToken(isInclude ? Token::Kind::KWInclude :
Token::Kind::KWSubninja);
lexer.setMode(Lexer::LexingMode::None);
if (tok.tokenKind != Token::Kind::String) {
error("expected path string");
return skipPastEOL();
}
Token path = consumeExpectedToken(Token::Kind::String);
if (!consumeIfToken(Token::Kind::Newline)) {
error("expected newline token");
return skipPastEOL();
}
actions.actOnIncludeDecl(isInclude, path);
}
/// Parse a parameterized declaration (one followed by variable bindings).
///
/// parameterized-decl ::= parameterized-specifier indented-binding*
/// parameterized-specifier ::= build-spec | pool-spec | rule-spec
/// indented-binding := indention binding-decl
void Parser::ParserImpl::parseParameterizedDecl() {
// Begin by parsing the specifier.
union {
ParseActions::BuildResult asBuild;
ParseActions::PoolResult asPool;
ParseActions::RuleResult asRule;
} decl;
bool success;
Token startTok = tok;
Token::Kind kind = tok.tokenKind;
switch (kind) {
case Token::Kind::KWBuild:
success = parseBuildSpecifier(&decl.asBuild);
break;
case Token::Kind::KWPool:
success = parsePoolSpecifier(&decl.asPool);
break;
default:
assert(kind == Token::Kind::KWRule);
success = parseRuleSpecifier(&decl.asRule);
break;
}
// If parsing the specifier failed, skip forward until we reach a non-indented
// line.
if (!success) {
do {
skipPastEOL();
} while (tok.tokenKind == Token::Kind::Indentation);
return;
}
// Otherwise, parse the set of indented bindings.
while (tok.tokenKind == Token::Kind::Indentation) {
// Put the lexer in identifier specific mode for the binding. It will
// automatically be taken out by parseBindingInternal switching for the
// value.
lexer.setMode(Lexer::LexingMode::IdentifierSpecific);
consumeExpectedToken(Token::Kind::Indentation);
// Allow blank lines in parameterized decls.
if (tok.tokenKind == Token::Kind::Newline) {
// Ensure we switch out of identifier specific mode.
lexer.setMode(Lexer::LexingMode::None);
consumeExpectedToken(Token::Kind::Newline);
continue;
}
Token name, value;
if (parseBindingInternal(&name, &value)) {
// Dispatch to the appropriate parser action.
switch (kind) {
case Token::Kind::KWBuild:
actions.actOnBuildBindingDecl(decl.asBuild, name, value);
break;
case Token::Kind::KWPool:
actions.actOnPoolBindingDecl(decl.asPool, name, value);
break;
default:
assert(kind == Token::Kind::KWRule);
actions.actOnRuleBindingDecl(decl.asRule, name, value);
break;
}
}
}
switch (kind) {
case Token::Kind::KWBuild:
actions.actOnEndBuildDecl(decl.asBuild, startTok);
break;
case Token::Kind::KWPool:
actions.actOnEndPoolDecl(decl.asPool, startTok);
break;
default:
assert(kind == Token::Kind::KWRule);
actions.actOnEndRuleDecl(decl.asRule, startTok);
break;
}
}
/// build-spec ::= "build" path-string-list ":" path-string path-string-list
/// [ "|" path-string-list ] [ "||" path-string-list" ] '\n'
bool Parser::ParserImpl::parseBuildSpecifier(
ParseActions::BuildResult* decl_out) {
// Put the lexer in path string mode for the entire specifier parsing.
lexer.setMode(Lexer::LexingMode::PathString);
consumeExpectedToken(Token::Kind::KWBuild);
// Parse the output list.
if (tok.tokenKind != Token::Kind::String) {
error("expected output path string");
lexer.setMode(Lexer::LexingMode::None);
return false;
}
SmallVector<Token, 8> outputs;
do {
outputs.push_back(consumeExpectedToken(Token::Kind::String));
} while (tok.tokenKind == Token::Kind::String);
// Expect the string list to be terminated by a colon.
if (tok.tokenKind != Token::Kind::Colon) {
error("expected ':' token");
lexer.setMode(Lexer::LexingMode::None);
return false;
}
// Parse the rule name identifier, switching out of path string mode for this
// one lex.
lexer.setMode(Lexer::LexingMode::IdentifierSpecific);
consumeExpectedToken(Token::Kind::Colon);
lexer.setMode(Lexer::LexingMode::PathString);
if (tok.tokenKind != Token::Kind::Identifier) {
error("expected rule name identifier");
lexer.setMode(Lexer::LexingMode::None);
return false;
}
Token name = consumeExpectedToken(Token::Kind::Identifier);
// Parse the explicit inputs.
SmallVector<Token, 8> inputs;
while (tok.tokenKind == Token::Kind::String) {
inputs.push_back(consumeExpectedToken(Token::Kind::String));
}
unsigned numExplicitInputs = inputs.size();
// Parse the implicit inputs, if present.
if (consumeIfToken(Token::Kind::Pipe)) {
while (tok.tokenKind == Token::Kind::String) {
inputs.push_back(consumeExpectedToken(Token::Kind::String));
}
}
unsigned numImplicitInputs = inputs.size() - numExplicitInputs;
// Parse the order-only inputs, if present.
if (consumeIfToken(Token::Kind::PipePipe)) {
while (tok.tokenKind == Token::Kind::String) {
inputs.push_back(consumeExpectedToken(Token::Kind::String));
}
}
// Reset the lexer mode.
lexer.setMode(Lexer::LexingMode::None);
if (!consumeIfToken(Token::Kind::Newline)) {
error("expected newline token");
return false;
}
*decl_out = actions.actOnBeginBuildDecl(name, outputs, inputs,
numExplicitInputs, numImplicitInputs);
return true;
}
/// pool-spec ::= "pool" identifier '\n'
bool Parser::ParserImpl::parsePoolSpecifier(
ParseActions::PoolResult* decl_out) {
// Put the lexer in identifier specific mode for the name.
lexer.setMode(Lexer::LexingMode::IdentifierSpecific);
consumeExpectedToken(Token::Kind::KWPool);
lexer.setMode(Lexer::LexingMode::None);
if (tok.tokenKind != Token::Kind::Identifier) {
error("expected pool name identifier");
return false;
}
Token name = consumeExpectedToken(Token::Kind::Identifier);
if (!consumeIfToken(Token::Kind::Newline)) {
error("expected newline token");
return false;
}
*decl_out = actions.actOnBeginPoolDecl(name);
return true;
}
/// rule-spec ::= "rule" identifier '\n'
bool Parser::ParserImpl::parseRuleSpecifier(
ParseActions::RuleResult* decl_out) {
// Put the lexer in identifier specific mode for the name.
lexer.setMode(Lexer::LexingMode::IdentifierSpecific);
consumeExpectedToken(Token::Kind::KWRule);
lexer.setMode(Lexer::LexingMode::None);
if (tok.tokenKind != Token::Kind::Identifier) {
error("expected rule name identifier");
return false;
}
Token name = consumeExpectedToken(Token::Kind::Identifier);
if (!consumeIfToken(Token::Kind::Newline)) {
error("expected newline token");
return false;
}
*decl_out = actions.actOnBeginRuleDecl(name);
return true;
}
#pragma mark - Parser
Parser::Parser(StringRef data, ParseActions& actions)
: impl(new ParserImpl(data, actions)) {}
Parser::~Parser() = default;
void Parser::parse() {
// Initialize the actions.
impl->getActions().initialize(this);
impl->parse();
}
const Lexer& Parser::getLexer() const {
return impl->getLexer();
}
|