|
@@ -68,46 +68,46 @@ class Parser::PrettyStackTraceParseState : public llvm::PrettyStackTraceEntry {
|
|
|
private:
|
|
private:
|
|
|
auto Print(llvm::raw_ostream& output, TokenizedBuffer::Token token) const
|
|
auto Print(llvm::raw_ostream& output, TokenizedBuffer::Token token) const
|
|
|
-> void {
|
|
-> void {
|
|
|
- auto line = parser_->tokens_.GetLine(token);
|
|
|
|
|
- output << " @ " << parser_->tokens_.GetLineNumber(line) << ":"
|
|
|
|
|
- << parser_->tokens_.GetColumnNumber(token) << ":"
|
|
|
|
|
|
|
+ auto line = parser_->tokens_->GetLine(token);
|
|
|
|
|
+ output << " @ " << parser_->tokens_->GetLineNumber(line) << ":"
|
|
|
|
|
+ << parser_->tokens_->GetColumnNumber(token) << ":"
|
|
|
<< " token " << token << " : "
|
|
<< " token " << token << " : "
|
|
|
- << parser_->tokens_.GetKind(token).Name() << "\n";
|
|
|
|
|
|
|
+ << parser_->tokens_->GetKind(token).Name() << "\n";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const Parser* parser_;
|
|
const Parser* parser_;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-Parser::Parser(ParseTree& tree_arg, TokenizedBuffer& tokens_arg,
|
|
|
|
|
|
|
+Parser::Parser(ParseTree& tree, TokenizedBuffer& tokens,
|
|
|
TokenDiagnosticEmitter& emitter)
|
|
TokenDiagnosticEmitter& emitter)
|
|
|
- : tree_(tree_arg),
|
|
|
|
|
- tokens_(tokens_arg),
|
|
|
|
|
- emitter_(emitter),
|
|
|
|
|
- position_(tokens_.tokens().begin()),
|
|
|
|
|
- end_(tokens_.tokens().end()) {
|
|
|
|
|
|
|
+ : tree_(&tree),
|
|
|
|
|
+ tokens_(&tokens),
|
|
|
|
|
+ emitter_(&emitter),
|
|
|
|
|
+ position_(tokens_->tokens().begin()),
|
|
|
|
|
+ end_(tokens_->tokens().end()) {
|
|
|
CARBON_CHECK(position_ != end_) << "Empty TokenizedBuffer";
|
|
CARBON_CHECK(position_ != end_) << "Empty TokenizedBuffer";
|
|
|
--end_;
|
|
--end_;
|
|
|
- CARBON_CHECK(tokens_.GetKind(*end_) == TokenKind::EndOfFile())
|
|
|
|
|
|
|
+ CARBON_CHECK(tokens_->GetKind(*end_) == TokenKind::EndOfFile())
|
|
|
<< "TokenizedBuffer should end with EndOfFile, ended with "
|
|
<< "TokenizedBuffer should end with EndOfFile, ended with "
|
|
|
- << tokens_.GetKind(*end_).Name();
|
|
|
|
|
|
|
+ << tokens_->GetKind(*end_).Name();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
auto Parser::AddLeafNode(ParseNodeKind kind, TokenizedBuffer::Token token,
|
|
auto Parser::AddLeafNode(ParseNodeKind kind, TokenizedBuffer::Token token,
|
|
|
bool has_error) -> void {
|
|
bool has_error) -> void {
|
|
|
- tree_.node_impls_.push_back(
|
|
|
|
|
|
|
+ tree_->node_impls_.push_back(
|
|
|
ParseTree::NodeImpl(kind, has_error, token, /*subtree_size=*/1));
|
|
ParseTree::NodeImpl(kind, has_error, token, /*subtree_size=*/1));
|
|
|
if (has_error) {
|
|
if (has_error) {
|
|
|
- tree_.has_errors_ = true;
|
|
|
|
|
|
|
+ tree_->has_errors_ = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
auto Parser::AddNode(ParseNodeKind kind, TokenizedBuffer::Token token,
|
|
auto Parser::AddNode(ParseNodeKind kind, TokenizedBuffer::Token token,
|
|
|
int subtree_start, bool has_error) -> void {
|
|
int subtree_start, bool has_error) -> void {
|
|
|
- int subtree_size = tree_.size() - subtree_start + 1;
|
|
|
|
|
- tree_.node_impls_.push_back(
|
|
|
|
|
|
|
+ int subtree_size = tree_->size() - subtree_start + 1;
|
|
|
|
|
+ tree_->node_impls_.push_back(
|
|
|
ParseTree::NodeImpl(kind, has_error, token, subtree_size));
|
|
ParseTree::NodeImpl(kind, has_error, token, subtree_size));
|
|
|
if (has_error) {
|
|
if (has_error) {
|
|
|
- tree_.has_errors_ = true;
|
|
|
|
|
|
|
+ tree_->has_errors_ = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -119,9 +119,9 @@ auto Parser::ConsumeAndAddCloseParen(TokenizedBuffer::Token open_paren,
|
|
|
|
|
|
|
|
// TODO: Include the location of the matching open_paren in the diagnostic.
|
|
// TODO: Include the location of the matching open_paren in the diagnostic.
|
|
|
CARBON_DIAGNOSTIC(ExpectedCloseParen, Error, "Unexpected tokens before `)`.");
|
|
CARBON_DIAGNOSTIC(ExpectedCloseParen, Error, "Unexpected tokens before `)`.");
|
|
|
- emitter_.Emit(*position_, ExpectedCloseParen);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedCloseParen);
|
|
|
|
|
|
|
|
- SkipTo(tokens_.GetMatchedClosingToken(open_paren));
|
|
|
|
|
|
|
+ SkipTo(tokens_->GetMatchedClosingToken(open_paren));
|
|
|
AddLeafNode(close_kind, Consume());
|
|
AddLeafNode(close_kind, Consume());
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
@@ -150,7 +150,7 @@ auto Parser::FindNextOf(std::initializer_list<TokenKind> desired_kinds)
|
|
|
auto new_position = position_;
|
|
auto new_position = position_;
|
|
|
while (true) {
|
|
while (true) {
|
|
|
TokenizedBuffer::Token token = *new_position;
|
|
TokenizedBuffer::Token token = *new_position;
|
|
|
- TokenKind kind = tokens_.GetKind(token);
|
|
|
|
|
|
|
+ TokenKind kind = tokens_->GetKind(token);
|
|
|
if (kind.IsOneOf(desired_kinds)) {
|
|
if (kind.IsOneOf(desired_kinds)) {
|
|
|
return token;
|
|
return token;
|
|
|
}
|
|
}
|
|
@@ -160,8 +160,8 @@ auto Parser::FindNextOf(std::initializer_list<TokenKind> desired_kinds)
|
|
|
// There are no more tokens at this level.
|
|
// There are no more tokens at this level.
|
|
|
return llvm::None;
|
|
return llvm::None;
|
|
|
} else if (kind.IsOpeningSymbol()) {
|
|
} else if (kind.IsOpeningSymbol()) {
|
|
|
- new_position =
|
|
|
|
|
- TokenizedBuffer::TokenIterator(tokens_.GetMatchedClosingToken(token));
|
|
|
|
|
|
|
+ new_position = TokenizedBuffer::TokenIterator(
|
|
|
|
|
+ tokens_->GetMatchedClosingToken(token));
|
|
|
// Advance past the closing token.
|
|
// Advance past the closing token.
|
|
|
++new_position;
|
|
++new_position;
|
|
|
} else {
|
|
} else {
|
|
@@ -175,7 +175,7 @@ auto Parser::SkipMatchingGroup() -> bool {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- SkipTo(tokens_.GetMatchedClosingToken(*position_));
|
|
|
|
|
|
|
+ SkipTo(tokens_->GetMatchedClosingToken(*position_));
|
|
|
++position_;
|
|
++position_;
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
@@ -186,19 +186,19 @@ auto Parser::SkipPastLikelyEnd(TokenizedBuffer::Token skip_root)
|
|
|
return llvm::None;
|
|
return llvm::None;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- TokenizedBuffer::Line root_line = tokens_.GetLine(skip_root);
|
|
|
|
|
- int root_line_indent = tokens_.GetIndentColumnNumber(root_line);
|
|
|
|
|
|
|
+ TokenizedBuffer::Line root_line = tokens_->GetLine(skip_root);
|
|
|
|
|
+ int root_line_indent = tokens_->GetIndentColumnNumber(root_line);
|
|
|
|
|
|
|
|
// We will keep scanning through tokens on the same line as the root or
|
|
// We will keep scanning through tokens on the same line as the root or
|
|
|
// lines with greater indentation than root's line.
|
|
// lines with greater indentation than root's line.
|
|
|
auto is_same_line_or_indent_greater_than_root =
|
|
auto is_same_line_or_indent_greater_than_root =
|
|
|
[&](TokenizedBuffer::Token t) {
|
|
[&](TokenizedBuffer::Token t) {
|
|
|
- TokenizedBuffer::Line l = tokens_.GetLine(t);
|
|
|
|
|
|
|
+ TokenizedBuffer::Line l = tokens_->GetLine(t);
|
|
|
if (l == root_line) {
|
|
if (l == root_line) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return tokens_.GetIndentColumnNumber(l) > root_line_indent;
|
|
|
|
|
|
|
+ return tokens_->GetIndentColumnNumber(l) > root_line_indent;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
do {
|
|
do {
|
|
@@ -214,7 +214,7 @@ auto Parser::SkipPastLikelyEnd(TokenizedBuffer::Token skip_root)
|
|
|
return semi;
|
|
return semi;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Skip over any matching group of tokens_.
|
|
|
|
|
|
|
+ // Skip over any matching group of tokens_->
|
|
|
if (SkipMatchingGroup()) {
|
|
if (SkipMatchingGroup()) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
@@ -247,7 +247,7 @@ auto Parser::HandleCodeBlockState() -> void {
|
|
|
|
|
|
|
|
// Recover by parsing a single statement.
|
|
// Recover by parsing a single statement.
|
|
|
CARBON_DIAGNOSTIC(ExpectedCodeBlock, Error, "Expected braced code block.");
|
|
CARBON_DIAGNOSTIC(ExpectedCodeBlock, Error, "Expected braced code block.");
|
|
|
- emitter_.Emit(*position_, ExpectedCodeBlock);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedCodeBlock);
|
|
|
|
|
|
|
|
PushState(ParserState::Statement());
|
|
PushState(ParserState::Statement());
|
|
|
}
|
|
}
|
|
@@ -282,8 +282,8 @@ static auto IsPossibleStartOfOperand(TokenKind kind) -> bool {
|
|
|
auto Parser::IsLexicallyValidInfixOperator() -> bool {
|
|
auto Parser::IsLexicallyValidInfixOperator() -> bool {
|
|
|
CARBON_CHECK(position_ != end_) << "Expected an operator token.";
|
|
CARBON_CHECK(position_ != end_) << "Expected an operator token.";
|
|
|
|
|
|
|
|
- bool leading_space = tokens_.HasLeadingWhitespace(*position_);
|
|
|
|
|
- bool trailing_space = tokens_.HasTrailingWhitespace(*position_);
|
|
|
|
|
|
|
+ bool leading_space = tokens_->HasLeadingWhitespace(*position_);
|
|
|
|
|
+ bool trailing_space = tokens_->HasTrailingWhitespace(*position_);
|
|
|
|
|
|
|
|
// If there's whitespace on both sides, it's an infix operator.
|
|
// If there's whitespace on both sides, it's an infix operator.
|
|
|
if (leading_space && trailing_space) {
|
|
if (leading_space && trailing_space) {
|
|
@@ -298,9 +298,9 @@ auto Parser::IsLexicallyValidInfixOperator() -> bool {
|
|
|
// Otherwise, for an infix operator, the preceding token must be any close
|
|
// Otherwise, for an infix operator, the preceding token must be any close
|
|
|
// bracket, identifier, or literal and the next token must be an open paren,
|
|
// bracket, identifier, or literal and the next token must be an open paren,
|
|
|
// identifier, or literal.
|
|
// identifier, or literal.
|
|
|
- if (position_ == tokens_.tokens().begin() ||
|
|
|
|
|
- !IsAssumedEndOfOperand(tokens_.GetKind(*(position_ - 1))) ||
|
|
|
|
|
- !IsAssumedStartOfOperand(tokens_.GetKind(*(position_ + 1)))) {
|
|
|
|
|
|
|
+ if (position_ == tokens_->tokens().begin() ||
|
|
|
|
|
+ !IsAssumedEndOfOperand(tokens_->GetKind(*(position_ - 1))) ||
|
|
|
|
|
+ !IsAssumedStartOfOperand(tokens_->GetKind(*(position_ + 1)))) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -315,7 +315,7 @@ auto Parser::IsTrailingOperatorInfix() -> bool {
|
|
|
// An operator that follows the infix operator rules is parsed as
|
|
// An operator that follows the infix operator rules is parsed as
|
|
|
// infix, unless the next token means that it can't possibly be.
|
|
// infix, unless the next token means that it can't possibly be.
|
|
|
if (IsLexicallyValidInfixOperator() &&
|
|
if (IsLexicallyValidInfixOperator() &&
|
|
|
- IsPossibleStartOfOperand(tokens_.GetKind(*(position_ + 1)))) {
|
|
|
|
|
|
|
+ IsPossibleStartOfOperand(tokens_->GetKind(*(position_ + 1)))) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -323,8 +323,8 @@ auto Parser::IsTrailingOperatorInfix() -> bool {
|
|
|
// not valid at all. If the next token looks like the start of an operand,
|
|
// not valid at all. If the next token looks like the start of an operand,
|
|
|
// then parse as infix, otherwise as postfix. Either way we'll produce a
|
|
// then parse as infix, otherwise as postfix. Either way we'll produce a
|
|
|
// diagnostic later on.
|
|
// diagnostic later on.
|
|
|
- if (tokens_.HasLeadingWhitespace(*position_) &&
|
|
|
|
|
- IsAssumedStartOfOperand(tokens_.GetKind(*(position_ + 1)))) {
|
|
|
|
|
|
|
+ if (tokens_->HasLeadingWhitespace(*position_) &&
|
|
|
|
|
+ IsAssumedStartOfOperand(tokens_->GetKind(*(position_ + 1)))) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -338,12 +338,12 @@ auto Parser::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
|
|
|
CARBON_DIAGNOSTIC(BinaryOperatorRequiresWhitespace, Error,
|
|
CARBON_DIAGNOSTIC(BinaryOperatorRequiresWhitespace, Error,
|
|
|
"Whitespace missing {0} binary operator.",
|
|
"Whitespace missing {0} binary operator.",
|
|
|
RelativeLocation);
|
|
RelativeLocation);
|
|
|
- emitter_.Emit(*position_, BinaryOperatorRequiresWhitespace,
|
|
|
|
|
- tokens_.HasLeadingWhitespace(*position_)
|
|
|
|
|
- ? RelativeLocation::After
|
|
|
|
|
- : (tokens_.HasTrailingWhitespace(*position_)
|
|
|
|
|
- ? RelativeLocation::Before
|
|
|
|
|
- : RelativeLocation::Around));
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, BinaryOperatorRequiresWhitespace,
|
|
|
|
|
+ tokens_->HasLeadingWhitespace(*position_)
|
|
|
|
|
+ ? RelativeLocation::After
|
|
|
|
|
+ : (tokens_->HasTrailingWhitespace(*position_)
|
|
|
|
|
+ ? RelativeLocation::Before
|
|
|
|
|
+ : RelativeLocation::Around));
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
bool prefix = fixity == OperatorFixity::Prefix;
|
|
bool prefix = fixity == OperatorFixity::Prefix;
|
|
@@ -351,12 +351,12 @@ auto Parser::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
|
|
|
// Whitespace is not permitted between a symbolic pre/postfix operator and
|
|
// Whitespace is not permitted between a symbolic pre/postfix operator and
|
|
|
// its operand.
|
|
// its operand.
|
|
|
if (PositionKind().IsSymbol() &&
|
|
if (PositionKind().IsSymbol() &&
|
|
|
- (prefix ? tokens_.HasTrailingWhitespace(*position_)
|
|
|
|
|
- : tokens_.HasLeadingWhitespace(*position_))) {
|
|
|
|
|
|
|
+ (prefix ? tokens_->HasTrailingWhitespace(*position_)
|
|
|
|
|
+ : tokens_->HasLeadingWhitespace(*position_))) {
|
|
|
CARBON_DIAGNOSTIC(UnaryOperatorHasWhitespace, Error,
|
|
CARBON_DIAGNOSTIC(UnaryOperatorHasWhitespace, Error,
|
|
|
"Whitespace is not allowed {0} this unary operator.",
|
|
"Whitespace is not allowed {0} this unary operator.",
|
|
|
RelativeLocation);
|
|
RelativeLocation);
|
|
|
- emitter_.Emit(
|
|
|
|
|
|
|
+ emitter_->Emit(
|
|
|
*position_, UnaryOperatorHasWhitespace,
|
|
*position_, UnaryOperatorHasWhitespace,
|
|
|
prefix ? RelativeLocation::After : RelativeLocation::Before);
|
|
prefix ? RelativeLocation::After : RelativeLocation::Before);
|
|
|
}
|
|
}
|
|
@@ -365,7 +365,7 @@ auto Parser::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
|
|
|
CARBON_DIAGNOSTIC(UnaryOperatorRequiresWhitespace, Error,
|
|
CARBON_DIAGNOSTIC(UnaryOperatorRequiresWhitespace, Error,
|
|
|
"Whitespace is required {0} this unary operator.",
|
|
"Whitespace is required {0} this unary operator.",
|
|
|
RelativeLocation);
|
|
RelativeLocation);
|
|
|
- emitter_.Emit(
|
|
|
|
|
|
|
+ emitter_->Emit(
|
|
|
*position_, UnaryOperatorRequiresWhitespace,
|
|
*position_, UnaryOperatorRequiresWhitespace,
|
|
|
prefix ? RelativeLocation::Before : RelativeLocation::After);
|
|
prefix ? RelativeLocation::Before : RelativeLocation::After);
|
|
|
}
|
|
}
|
|
@@ -379,7 +379,7 @@ auto Parser::ConsumeListToken(ParseNodeKind comma_kind, TokenKind close_kind,
|
|
|
if (!already_has_error) {
|
|
if (!already_has_error) {
|
|
|
CARBON_DIAGNOSTIC(UnexpectedTokenAfterListElement, Error,
|
|
CARBON_DIAGNOSTIC(UnexpectedTokenAfterListElement, Error,
|
|
|
"Expected `,` or `{0}`.", TokenKind);
|
|
"Expected `,` or `{0}`.", TokenKind);
|
|
|
- emitter_.Emit(*position_, UnexpectedTokenAfterListElement, close_kind);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, UnexpectedTokenAfterListElement, close_kind);
|
|
|
ReturnErrorOnState();
|
|
ReturnErrorOnState();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -459,10 +459,10 @@ auto Parser::HandleBraceExpressionParameterError(StateStackEntry state,
|
|
|
llvm::StringRef, llvm::StringRef, llvm::StringRef);
|
|
llvm::StringRef, llvm::StringRef, llvm::StringRef);
|
|
|
bool can_be_type = kind != BraceExpressionKind::Value;
|
|
bool can_be_type = kind != BraceExpressionKind::Value;
|
|
|
bool can_be_value = kind != BraceExpressionKind::Type;
|
|
bool can_be_value = kind != BraceExpressionKind::Type;
|
|
|
- emitter_.Emit(*position_, ExpectedStructLiteralField,
|
|
|
|
|
- can_be_type ? "`.field: type`" : "",
|
|
|
|
|
- (can_be_type && can_be_value) ? " or " : "",
|
|
|
|
|
- can_be_value ? "`.field = value`" : "");
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedStructLiteralField,
|
|
|
|
|
+ can_be_type ? "`.field: type`" : "",
|
|
|
|
|
+ (can_be_type && can_be_value) ? " or " : "",
|
|
|
|
|
+ can_be_value ? "`.field = value`" : "");
|
|
|
|
|
|
|
|
state.state = BraceExpressionKindToParserState(
|
|
state.state = BraceExpressionKindToParserState(
|
|
|
kind, ParserState::BraceExpressionParameterFinishAsType(),
|
|
kind, ParserState::BraceExpressionParameterFinishAsType(),
|
|
@@ -507,7 +507,8 @@ auto Parser::HandleBraceExpressionParameterAfterDesignator(
|
|
|
if (state.has_error) {
|
|
if (state.has_error) {
|
|
|
auto recovery_pos = FindNextOf(
|
|
auto recovery_pos = FindNextOf(
|
|
|
{TokenKind::Equal(), TokenKind::Colon(), TokenKind::Comma()});
|
|
{TokenKind::Equal(), TokenKind::Colon(), TokenKind::Comma()});
|
|
|
- if (!recovery_pos || tokens_.GetKind(*recovery_pos) == TokenKind::Comma()) {
|
|
|
|
|
|
|
+ if (!recovery_pos ||
|
|
|
|
|
+ tokens_->GetKind(*recovery_pos) == TokenKind::Comma()) {
|
|
|
state.state = BraceExpressionKindToParserState(
|
|
state.state = BraceExpressionKindToParserState(
|
|
|
kind, ParserState::BraceExpressionParameterFinishAsType(),
|
|
kind, ParserState::BraceExpressionParameterFinishAsType(),
|
|
|
ParserState::BraceExpressionParameterFinishAsValue(),
|
|
ParserState::BraceExpressionParameterFinishAsValue(),
|
|
@@ -665,7 +666,7 @@ auto Parser::HandleCodeBlockFinishState() -> void {
|
|
|
auto state = PopState();
|
|
auto state = PopState();
|
|
|
|
|
|
|
|
// If the block started with an open curly, this is a close curly.
|
|
// If the block started with an open curly, this is a close curly.
|
|
|
- if (tokens_.GetKind(state.token) == TokenKind::OpenCurlyBrace()) {
|
|
|
|
|
|
|
+ if (tokens_->GetKind(state.token) == TokenKind::OpenCurlyBrace()) {
|
|
|
AddNode(ParseNodeKind::CodeBlock(), Consume(), state.subtree_start,
|
|
AddNode(ParseNodeKind::CodeBlock(), Consume(), state.subtree_start,
|
|
|
state.has_error);
|
|
state.has_error);
|
|
|
} else {
|
|
} else {
|
|
@@ -703,8 +704,8 @@ auto Parser::HandleDeclarationLoopState() -> void {
|
|
|
default: {
|
|
default: {
|
|
|
CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
|
|
CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
|
|
|
"Unrecognized declaration introducer.");
|
|
"Unrecognized declaration introducer.");
|
|
|
- emitter_.Emit(*position_, UnrecognizedDeclaration);
|
|
|
|
|
- tree_.has_errors_ = true;
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, UnrecognizedDeclaration);
|
|
|
|
|
+ tree_->has_errors_ = true;
|
|
|
if (auto semi = SkipPastLikelyEnd(*position_)) {
|
|
if (auto semi = SkipPastLikelyEnd(*position_)) {
|
|
|
AddLeafNode(ParseNodeKind::EmptyDeclaration(), *semi,
|
|
AddLeafNode(ParseNodeKind::EmptyDeclaration(), *semi,
|
|
|
/*has_error=*/true);
|
|
/*has_error=*/true);
|
|
@@ -724,7 +725,7 @@ auto Parser::HandleDesignator(bool as_struct) -> void {
|
|
|
ParseNodeKind::DesignatedName())) {
|
|
ParseNodeKind::DesignatedName())) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDot, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDot, Error,
|
|
|
"Expected identifier after `.`.");
|
|
"Expected identifier after `.`.");
|
|
|
- emitter_.Emit(*position_, ExpectedIdentifierAfterDot);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedIdentifierAfterDot);
|
|
|
// If we see a keyword, assume it was intended to be the designated name.
|
|
// If we see a keyword, assume it was intended to be the designated name.
|
|
|
// TODO: Should keywords be valid in designators?
|
|
// TODO: Should keywords be valid in designators?
|
|
|
if (PositionKind().IsKeyword()) {
|
|
if (PositionKind().IsKeyword()) {
|
|
@@ -759,7 +760,7 @@ auto Parser::HandleExpressionState() -> void {
|
|
|
OperatorPriority::RightFirst) {
|
|
OperatorPriority::RightFirst) {
|
|
|
// The precedence rules don't permit this prefix operator in this
|
|
// The precedence rules don't permit this prefix operator in this
|
|
|
// context. Diagnose this, but carry on and parse it anyway.
|
|
// context. Diagnose this, but carry on and parse it anyway.
|
|
|
- emitter_.Emit(*position_, OperatorRequiresParentheses);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, OperatorRequiresParentheses);
|
|
|
} else {
|
|
} else {
|
|
|
// Check that this operator follows the proper whitespace rules.
|
|
// Check that this operator follows the proper whitespace rules.
|
|
|
DiagnoseOperatorFixity(OperatorFixity::Prefix);
|
|
DiagnoseOperatorFixity(OperatorFixity::Prefix);
|
|
@@ -814,7 +815,7 @@ auto Parser::HandleExpressionInPostfixState() -> void {
|
|
|
}
|
|
}
|
|
|
default: {
|
|
default: {
|
|
|
CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
|
|
CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
|
|
|
- emitter_.Emit(*position_, ExpectedExpression);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedExpression);
|
|
|
ReturnErrorOnState();
|
|
ReturnErrorOnState();
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -882,7 +883,7 @@ auto Parser::HandleExpressionLoopState() -> void {
|
|
|
// Either the LHS operator and this operator are ambiguous, or the
|
|
// Either the LHS operator and this operator are ambiguous, or the
|
|
|
// LHS operator is a unary operator that can't be nested within
|
|
// LHS operator is a unary operator that can't be nested within
|
|
|
// this operator. Either way, parentheses are required.
|
|
// this operator. Either way, parentheses are required.
|
|
|
- emitter_.Emit(*position_, OperatorRequiresParentheses);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, OperatorRequiresParentheses);
|
|
|
state.has_error = true;
|
|
state.has_error = true;
|
|
|
} else {
|
|
} else {
|
|
|
DiagnoseOperatorFixity(is_binary ? OperatorFixity::Infix
|
|
DiagnoseOperatorFixity(is_binary ? OperatorFixity::Infix
|
|
@@ -934,7 +935,7 @@ auto Parser::HandleExpressionStatementFinishState() -> void {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!state.has_error) {
|
|
if (!state.has_error) {
|
|
|
- emitter_.Emit(*position_, ExpectedSemiAfterExpression);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedSemiAfterExpression);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (auto semi_token = SkipPastLikelyEnd(state.token)) {
|
|
if (auto semi_token = SkipPastLikelyEnd(state.token)) {
|
|
@@ -967,7 +968,7 @@ auto Parser::HandleFunctionIntroducerState() -> void {
|
|
|
ParseNodeKind::DeclaredName())) {
|
|
ParseNodeKind::DeclaredName())) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedFunctionName, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedFunctionName, Error,
|
|
|
"Expected function name after `fn` keyword.");
|
|
"Expected function name after `fn` keyword.");
|
|
|
- emitter_.Emit(*position_, ExpectedFunctionName);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedFunctionName);
|
|
|
// TODO: We could change the lexer to allow us to synthesize certain
|
|
// TODO: We could change the lexer to allow us to synthesize certain
|
|
|
// kinds of tokens and try to "recover" here, but unclear that this is
|
|
// kinds of tokens and try to "recover" here, but unclear that this is
|
|
|
// really useful.
|
|
// really useful.
|
|
@@ -978,7 +979,7 @@ auto Parser::HandleFunctionIntroducerState() -> void {
|
|
|
if (!PositionIs(TokenKind::OpenParen())) {
|
|
if (!PositionIs(TokenKind::OpenParen())) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedFunctionParams, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedFunctionParams, Error,
|
|
|
"Expected `(` after function name.");
|
|
"Expected `(` after function name.");
|
|
|
- emitter_.Emit(*position_, ExpectedFunctionParams);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedFunctionParams);
|
|
|
HandleFunctionError(state, true);
|
|
HandleFunctionError(state, true);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -1069,10 +1070,10 @@ auto Parser::HandleFunctionSignatureFinishState() -> void {
|
|
|
CARBON_DIAGNOSTIC(
|
|
CARBON_DIAGNOSTIC(
|
|
|
ExpectedFunctionBodyOrSemi, Error,
|
|
ExpectedFunctionBodyOrSemi, Error,
|
|
|
"Expected function definition or `;` after function declaration.");
|
|
"Expected function definition or `;` after function declaration.");
|
|
|
- emitter_.Emit(*position_, ExpectedFunctionBodyOrSemi);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedFunctionBodyOrSemi);
|
|
|
// Only need to skip if we've not already found a new line.
|
|
// Only need to skip if we've not already found a new line.
|
|
|
bool skip_past_likely_end =
|
|
bool skip_past_likely_end =
|
|
|
- tokens_.GetLine(*position_) == tokens_.GetLine(state.token);
|
|
|
|
|
|
|
+ tokens_->GetLine(*position_) == tokens_->GetLine(state.token);
|
|
|
HandleFunctionError(state, skip_past_likely_end);
|
|
HandleFunctionError(state, skip_past_likely_end);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -1100,21 +1101,21 @@ auto Parser::HandlePackageState() -> void {
|
|
|
ParseNodeKind::DeclaredName())) {
|
|
ParseNodeKind::DeclaredName())) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPackage, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPackage, Error,
|
|
|
"Expected identifier after `package`.");
|
|
"Expected identifier after `package`.");
|
|
|
- emitter_.Emit(*position_, ExpectedIdentifierAfterPackage);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedIdentifierAfterPackage);
|
|
|
exit_on_parse_error();
|
|
exit_on_parse_error();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool library_parsed = false;
|
|
bool library_parsed = false;
|
|
|
if (auto library_token = ConsumeIf(TokenKind::Library())) {
|
|
if (auto library_token = ConsumeIf(TokenKind::Library())) {
|
|
|
- auto library_start = tree_.size();
|
|
|
|
|
|
|
+ auto library_start = tree_->size();
|
|
|
|
|
|
|
|
if (!ConsumeAndAddLeafNodeIf(TokenKind::StringLiteral(),
|
|
if (!ConsumeAndAddLeafNodeIf(TokenKind::StringLiteral(),
|
|
|
ParseNodeKind::Literal())) {
|
|
ParseNodeKind::Literal())) {
|
|
|
CARBON_DIAGNOSTIC(
|
|
CARBON_DIAGNOSTIC(
|
|
|
ExpectedLibraryName, Error,
|
|
ExpectedLibraryName, Error,
|
|
|
"Expected a string literal to specify the library name.");
|
|
"Expected a string literal to specify the library name.");
|
|
|
- emitter_.Emit(*position_, ExpectedLibraryName);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedLibraryName);
|
|
|
exit_on_parse_error();
|
|
exit_on_parse_error();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -1124,7 +1125,7 @@ auto Parser::HandlePackageState() -> void {
|
|
|
library_parsed = true;
|
|
library_parsed = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- switch (auto api_or_impl_token = tokens_.GetKind(*(position_))) {
|
|
|
|
|
|
|
+ switch (auto api_or_impl_token = tokens_->GetKind(*(position_))) {
|
|
|
case TokenKind::Api(): {
|
|
case TokenKind::Api(): {
|
|
|
AddLeafNode(ParseNodeKind::PackageApi(), Consume());
|
|
AddLeafNode(ParseNodeKind::PackageApi(), Consume());
|
|
|
break;
|
|
break;
|
|
@@ -1140,11 +1141,11 @@ auto Parser::HandlePackageState() -> void {
|
|
|
// before the library name.
|
|
// before the library name.
|
|
|
CARBON_DIAGNOSTIC(MissingLibraryKeyword, Error,
|
|
CARBON_DIAGNOSTIC(MissingLibraryKeyword, Error,
|
|
|
"Missing `library` keyword.");
|
|
"Missing `library` keyword.");
|
|
|
- emitter_.Emit(*position_, MissingLibraryKeyword);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, MissingLibraryKeyword);
|
|
|
} else {
|
|
} else {
|
|
|
CARBON_DIAGNOSTIC(ExpectedApiOrImpl, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedApiOrImpl, Error,
|
|
|
"Expected a `api` or `impl`.");
|
|
"Expected a `api` or `impl`.");
|
|
|
- emitter_.Emit(*position_, ExpectedApiOrImpl);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedApiOrImpl);
|
|
|
}
|
|
}
|
|
|
exit_on_parse_error();
|
|
exit_on_parse_error();
|
|
|
return;
|
|
return;
|
|
@@ -1155,7 +1156,7 @@ auto Parser::HandlePackageState() -> void {
|
|
|
ParseNodeKind::PackageEnd())) {
|
|
ParseNodeKind::PackageEnd())) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedSemiToEndPackageDirective, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedSemiToEndPackageDirective, Error,
|
|
|
"Expected `;` to end package directive.");
|
|
"Expected `;` to end package directive.");
|
|
|
- emitter_.Emit(*position_, ExpectedSemiToEndPackageDirective);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedSemiToEndPackageDirective);
|
|
|
exit_on_parse_error();
|
|
exit_on_parse_error();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -1173,7 +1174,8 @@ auto Parser::HandleParenConditionState() -> void {
|
|
|
} else {
|
|
} else {
|
|
|
CARBON_DIAGNOSTIC(ExpectedParenAfter, Error, "Expected `(` after `{0}`.",
|
|
CARBON_DIAGNOSTIC(ExpectedParenAfter, Error, "Expected `(` after `{0}`.",
|
|
|
TokenKind);
|
|
TokenKind);
|
|
|
- emitter_.Emit(*position_, ExpectedParenAfter, tokens_.GetKind(state.token));
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedParenAfter,
|
|
|
|
|
+ tokens_->GetKind(state.token));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// TODO: This should be adding a ConditionStart here instead of ConditionEnd
|
|
// TODO: This should be adding a ConditionStart here instead of ConditionEnd
|
|
@@ -1186,7 +1188,7 @@ auto Parser::HandleParenConditionState() -> void {
|
|
|
auto Parser::HandleParenConditionFinishState() -> void {
|
|
auto Parser::HandleParenConditionFinishState() -> void {
|
|
|
auto state = PopState();
|
|
auto state = PopState();
|
|
|
|
|
|
|
|
- if (tokens_.GetKind(state.token) != TokenKind::OpenParen()) {
|
|
|
|
|
|
|
+ if (tokens_->GetKind(state.token) != TokenKind::OpenParen()) {
|
|
|
// Don't expect a matching closing paren if there wasn't an opening paren.
|
|
// Don't expect a matching closing paren if there wasn't an opening paren.
|
|
|
// TODO: Should probably push nodes on this state in order to have the
|
|
// TODO: Should probably push nodes on this state in order to have the
|
|
|
// condition wrapped, but it wasn't before, so not doing it for consistency.
|
|
// condition wrapped, but it wasn't before, so not doing it for consistency.
|
|
@@ -1283,18 +1285,18 @@ auto Parser::HandlePattern(PatternKind pattern_kind) -> void {
|
|
|
|
|
|
|
|
// Handle an invalid pattern introducer.
|
|
// Handle an invalid pattern introducer.
|
|
|
if (!PositionIs(TokenKind::Identifier()) ||
|
|
if (!PositionIs(TokenKind::Identifier()) ||
|
|
|
- tokens_.GetKind(*(position_ + 1)) != TokenKind::Colon()) {
|
|
|
|
|
|
|
+ tokens_->GetKind(*(position_ + 1)) != TokenKind::Colon()) {
|
|
|
switch (pattern_kind) {
|
|
switch (pattern_kind) {
|
|
|
case PatternKind::Parameter: {
|
|
case PatternKind::Parameter: {
|
|
|
CARBON_DIAGNOSTIC(ExpectedParameterName, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedParameterName, Error,
|
|
|
"Expected parameter declaration.");
|
|
"Expected parameter declaration.");
|
|
|
- emitter_.Emit(*position_, ExpectedParameterName);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedParameterName);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
case PatternKind::Variable: {
|
|
case PatternKind::Variable: {
|
|
|
CARBON_DIAGNOSTIC(ExpectedVariableName, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedVariableName, Error,
|
|
|
"Expected pattern in `var` declaration.");
|
|
"Expected pattern in `var` declaration.");
|
|
|
- emitter_.Emit(*position_, ExpectedVariableName);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedVariableName);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -1398,7 +1400,7 @@ auto Parser::HandleStatementForHeaderState() -> void {
|
|
|
"Expected `(` after `{0}`. Recovering from missing `(` "
|
|
"Expected `(` after `{0}`. Recovering from missing `(` "
|
|
|
"not implemented yet!",
|
|
"not implemented yet!",
|
|
|
TokenKind);
|
|
TokenKind);
|
|
|
- emitter_.Emit(*position_, ExpectedParenAfter, TokenKind::For());
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedParenAfter, TokenKind::For());
|
|
|
// TODO: A proper recovery strategy is needed here. For now, I assume
|
|
// TODO: A proper recovery strategy is needed here. For now, I assume
|
|
|
// that all brackets are properly balanced (i.e. each open bracket has a
|
|
// that all brackets are properly balanced (i.e. each open bracket has a
|
|
|
// closing one).
|
|
// closing one).
|
|
@@ -1417,7 +1419,7 @@ auto Parser::HandleStatementForHeaderState() -> void {
|
|
|
} else {
|
|
} else {
|
|
|
CARBON_DIAGNOSTIC(ExpectedVariableDeclaration, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedVariableDeclaration, Error,
|
|
|
"Expected `var` declaration.");
|
|
"Expected `var` declaration.");
|
|
|
- emitter_.Emit(*position_, ExpectedVariableDeclaration);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedVariableDeclaration);
|
|
|
|
|
|
|
|
if (auto next_in = FindNextOf({TokenKind::In()})) {
|
|
if (auto next_in = FindNextOf({TokenKind::In()})) {
|
|
|
SkipTo(*next_in);
|
|
SkipTo(*next_in);
|
|
@@ -1435,13 +1437,13 @@ auto Parser::HandleStatementForHeaderInState() -> void {
|
|
|
if (!ConsumeAndAddLeafNodeIf(TokenKind::In(), ParseNodeKind::ForIn())) {
|
|
if (!ConsumeAndAddLeafNodeIf(TokenKind::In(), ParseNodeKind::ForIn())) {
|
|
|
if (auto colon = ConsumeIf(TokenKind::Colon())) {
|
|
if (auto colon = ConsumeIf(TokenKind::Colon())) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedIn, Error, "`:` should be replaced by `in`.");
|
|
CARBON_DIAGNOSTIC(ExpectedIn, Error, "`:` should be replaced by `in`.");
|
|
|
- emitter_.Emit(*colon, ExpectedIn);
|
|
|
|
|
|
|
+ emitter_->Emit(*colon, ExpectedIn);
|
|
|
AddLeafNode(ParseNodeKind::ForIn(), *colon, /*has_error=*/true);
|
|
AddLeafNode(ParseNodeKind::ForIn(), *colon, /*has_error=*/true);
|
|
|
} else {
|
|
} else {
|
|
|
CARBON_DIAGNOSTIC(ExpectedIn, Error,
|
|
CARBON_DIAGNOSTIC(ExpectedIn, Error,
|
|
|
"Expected `in` after loop `var` declaration.");
|
|
"Expected `in` after loop `var` declaration.");
|
|
|
- emitter_.Emit(*position_, ExpectedIn);
|
|
|
|
|
- SkipTo(tokens_.GetMatchedClosingToken(state.token));
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedIn);
|
|
|
|
|
+ SkipTo(tokens_->GetMatchedClosingToken(state.token));
|
|
|
|
|
|
|
|
state.has_error = true;
|
|
state.has_error = true;
|
|
|
PushState(state);
|
|
PushState(state);
|
|
@@ -1518,7 +1520,8 @@ auto Parser::HandleStatementKeywordFinish(ParseNodeKind node_kind) -> void {
|
|
|
if (!semi) {
|
|
if (!semi) {
|
|
|
CARBON_DIAGNOSTIC(ExpectedSemiAfter, Error, "Expected `;` after `{0}`.",
|
|
CARBON_DIAGNOSTIC(ExpectedSemiAfter, Error, "Expected `;` after `{0}`.",
|
|
|
TokenKind);
|
|
TokenKind);
|
|
|
- emitter_.Emit(*position_, ExpectedSemiAfter, tokens_.GetKind(state.token));
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedSemiAfter,
|
|
|
|
|
+ tokens_->GetKind(state.token));
|
|
|
state.has_error = true;
|
|
state.has_error = true;
|
|
|
// Recover to the next semicolon if possible, otherwise indicate the
|
|
// Recover to the next semicolon if possible, otherwise indicate the
|
|
|
// keyword for the error.
|
|
// keyword for the error.
|
|
@@ -1632,7 +1635,7 @@ auto Parser::HandleVarFinish(bool require_semicolon) -> void {
|
|
|
auto semi = ConsumeAndAddLeafNodeIf(TokenKind::Semi(),
|
|
auto semi = ConsumeAndAddLeafNodeIf(TokenKind::Semi(),
|
|
|
ParseNodeKind::DeclarationEnd());
|
|
ParseNodeKind::DeclarationEnd());
|
|
|
if (!semi) {
|
|
if (!semi) {
|
|
|
- emitter_.Emit(*position_, ExpectedSemiAfterExpression);
|
|
|
|
|
|
|
+ emitter_->Emit(*position_, ExpectedSemiAfterExpression);
|
|
|
if (auto semi_token = SkipPastLikelyEnd(state.token)) {
|
|
if (auto semi_token = SkipPastLikelyEnd(state.token)) {
|
|
|
AddLeafNode(ParseNodeKind::DeclarationEnd(), *semi_token,
|
|
AddLeafNode(ParseNodeKind::DeclarationEnd(), *semi_token,
|
|
|
/*has_error=*/true);
|
|
/*has_error=*/true);
|