Skip to content

Commit 39bfc12

Browse files
committed
Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API.
These APIs return SourceLocs, and eventually the Parser should consume tokens, which now include source trivia such as whitespace and comments, and package them into a purely syntactic tree. Just a tiny step. NFC.
1 parent d6e2b58 commit 39bfc12

File tree

10 files changed

+529
-448
lines changed

10 files changed

+529
-448
lines changed

include/swift/Parse/Parser.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,10 @@ class Parser {
321321
void restoreParserPosition(ParserPosition PP) {
322322
L->restoreState(PP.LS);
323323

324-
// We might be at tok::eof now, so ensure that consumeToken()
324+
// We might be at tok::eof now, so ensure that consumeLoc()
325325
// does not assert about lexing past eof.
326326
Tok = syntax::Token::unknown();
327-
consumeToken();
327+
consumeLoc();
328328

329329
PreviousLoc = PP.PreviousLoc;
330330
}
@@ -334,10 +334,10 @@ class Parser {
334334

335335
L->backtrackToState(PP.LS);
336336

337-
// We might be at tok::eof now, so ensure that consumeToken()
337+
// We might be at tok::eof now, so ensure that consumeLoc()
338338
// does not assert about lexing past eof.
339339
Tok = syntax::Token::unknown();
340-
consumeToken();
340+
consumeLoc();
341341

342342
PreviousLoc = PP.PreviousLoc;
343343
}
@@ -386,21 +386,22 @@ class Parser {
386386
//===--------------------------------------------------------------------===//
387387
// Utilities
388388

389-
/// \brief Return the next token that will be installed by \c consumeToken.
389+
/// \brief Return the next token that will be installed by \c consumeLoc.
390390
const syntax::Token &peekToken();
391391

392-
SourceLoc consumeToken();
393-
SourceLoc consumeToken(tok K) {
392+
syntax::Token consumeToken();
393+
SourceLoc consumeLoc();
394+
SourceLoc consumeLoc(tok K) {
394395
assert(Tok.is(K) && "Consuming wrong token kind");
395-
return consumeToken();
396+
return consumeLoc();
396397
}
397398

398399
SourceLoc consumeIdentifier(Identifier *Result = nullptr) {
399400
assert(Tok.isAny(tok::identifier, tok::kw_self,
400401
tok::kw_Self, tok::kw_throws));
401402
if (Result)
402403
*Result = Context.getIdentifier(Tok.getText().str());
403-
return consumeToken();
404+
return consumeLoc();
404405
}
405406

406407
/// \brief Retrieve the location just past the end of the previous
@@ -411,15 +412,15 @@ class Parser {
411412
/// return true. Otherwise, return false without consuming it.
412413
bool consumeIf(tok K) {
413414
if (Tok.isNot(K)) return false;
414-
consumeToken(K);
415+
consumeLoc(K);
415416
return true;
416417
}
417418

418419
/// \brief If the current token is the specified kind, consume it and
419420
/// return true. Otherwise, return false without consuming it.
420421
bool consumeIf(tok K, SourceLoc &consumedLoc) {
421422
if (Tok.isNot(K)) return false;
422-
consumedLoc = consumeToken(K);
423+
consumedLoc = consumeLoc(K);
423424
return true;
424425
}
425426

include/swift/Syntax/RawSyntax.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===--- Trivia.h - Swift Raw Syntax Nodes ----------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the RawSyntax class - the immutable, persistent backing
14+
// for syntactic information in the AST. These are raw containers of child
15+
// relationships and the layout of a particular node. For example:
16+
//
17+
// func foo() {
18+
// // body ...
19+
// }
20+
//
21+
// This function declaration is laid out as following:
22+
// - func token
23+
// - foo identifier token
24+
// - child: argument list
25+
// - child: brace statement
26+
//
27+
// And would have two children.
28+
//
29+
//===----------------------------------------------------------------------===//
30+
31+
#ifndef SWIFT_SYNTAX_RAWSYNTAX_H
32+
#define SWIFT_SYNTAX_RAWSYNTAX_H
33+
34+
namespace swift {
35+
namespace syntax {
36+
37+
struct RawSyntax;
38+
using RawSyntaxRef = llvm::IntrusiveRefCntPtr<RawSyntax>;
39+
40+
enum class RawSyntaxLayoutKind {
41+
/// The layout element is a terminal token.
42+
Token,
43+
44+
/// The layout element is a child syntax node.
45+
ChildIndex,
46+
};
47+
48+
struct RawSyntaxLayout {
49+
const RawSyntaxLayoutKind Kind;
50+
private:
51+
union {
52+
syntax::Token Tok;
53+
size_t ChildIndex;
54+
} Data;
55+
56+
public:
57+
const syntax::Token &getToken() const {
58+
assert(Kind == RawSyntaxLayoutKind::Token);
59+
return Data.Tok;
60+
}
61+
62+
size_t getChildIndex() const {
63+
assert(Kind == RawSyntaxLayoutKind::ChildIndex);
64+
return Data.ChildIndex;
65+
}
66+
};
67+
68+
struct RawSyntax : public llvm::ThreadSafeRefCountedBase<RawSyntax> {
69+
const std::vector<RawSyntaxRef> Children;
70+
const std::vector<RawSyntaxLayout> Layout;
71+
};
72+
73+
} // end namespace syntax
74+
} // end namespace swift
75+
76+
#endif // SWIFT_SYNTAX_RAWSYNTAX_H

0 commit comments

Comments
 (0)