Skip to content

Commit fc518e9

Browse files
authored
Merge pull request #2369 from DanielEScherzer/grammar-check-forced-keywords
Grammar check: update rustc for forced keywords
2 parents 52ffdc0 + 048dfca commit fc518e9

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

‎tools/grammar-check/src/tools/rustc.rs‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ pub fn tokenize(src: &str, edition: Edition) -> Result<Vec<Node>, ParseError> {
102102
.original_relative_byte_pos(parser.token.span.hi())
103103
.0 as usize;
104104

105-
let token = Node::new(to_reference_name(&parser.token.kind), Range { start, end });
105+
let token = match to_reference_name(&parser.token.kind) {
106+
Ok(name) => Node::new(name, Range { start, end }),
107+
Err(message) => {
108+
psess
109+
.dcx()
110+
.struct_span_err(parser.token.span, message)
111+
.emit();
112+
FatalError.raise();
113+
}
114+
};
106115
tokens.push(token);
107116
parser.bump();
108117
}
@@ -149,8 +158,8 @@ pub fn tokenize(src: &str, edition: Edition) -> Result<Vec<Node>, ParseError> {
149158
})
150159
}
151160

152-
fn to_reference_name(kind: &TokenKind) -> String {
153-
match kind {
161+
fn to_reference_name(kind: &TokenKind) -> Result<String, &'static str> {
162+
let reference_name = match kind {
154163
TokenKind::Eq
155164
| TokenKind::Lt
156165
| TokenKind::Le
@@ -223,6 +232,9 @@ fn to_reference_name(kind: &TokenKind) -> String {
223232
rustc_ast::token::LitKind::Err(_) => "Literal Error",
224233
},
225234
TokenKind::Ident(_, IdentKind::Normal) => "IDENTIFIER_OR_KEYWORD",
235+
TokenKind::Ident(_, IdentKind::ForcedKeyword) => {
236+
return Err("Reference does not support forced keywords");
237+
}
226238
TokenKind::Ident(_, IdentKind::Raw) => "RAW_IDENTIFIER",
227239
TokenKind::NtIdent(..) => panic!("unexpected NtIdent"),
228240
TokenKind::Lifetime(..) => "LIFETIME_TOKEN",
@@ -232,8 +244,8 @@ fn to_reference_name(kind: &TokenKind) -> String {
232244
TokenKind::DocComment(CommentKind::Block, AttrStyle::Inner, ..) => "INNER_BLOCK_DOC",
233245
TokenKind::DocComment(CommentKind::Block, AttrStyle::Outer, ..) => "OUTER_BLOCK_DOC",
234246
TokenKind::Eof => panic!("unexpected EOF"),
235-
}
236-
.to_string()
247+
};
248+
Ok(reference_name.to_string())
237249
}
238250

239251
fn diagnostics(output: &[u8]) -> Vec<Diagnostic> {

0 commit comments

Comments
 (0)