apply T! macro where it is possible

This commit is contained in:
Sergey Parilin 2019-05-15 15:35:47 +03:00
parent d77175ce28
commit 993abedd77
38 changed files with 619 additions and 623 deletions

View file

@ -3,7 +3,7 @@ use hir::{
db::HirDatabase, db::HirDatabase,
}; };
use ra_syntax::{ use ra_syntax::{
SyntaxKind, T,
ast::{LetStmt, PatKind, NameOwner, AstNode} ast::{LetStmt, PatKind, NameOwner, AstNode}
}; };
@ -24,7 +24,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
let name = pat.name()?; let name = pat.name()?;
let name_range = name.syntax().range(); let name_range = name.syntax().range();
// Assist not applicable if the type has already been specified // Assist not applicable if the type has already been specified
if stmt.syntax().children_with_tokens().any(|child| child.kind() == SyntaxKind::COLON) { if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
return None; return None;
} }
// Infer type // Infer type

View file

@ -2,7 +2,7 @@ use std::{iter, ops::RangeInclusive};
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use ra_text_edit::TextEditBuilder; use ra_text_edit::TextEditBuilder;
use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction}; use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction, T};
use ra_fmt::leading_indent; use ra_fmt::leading_indent;
use hir::Name; use hir::Name;
@ -49,7 +49,7 @@ impl<N: AstNode> AstEditor<N> {
fn do_make_multiline(&mut self) { fn do_make_multiline(&mut self) {
let l_curly = let l_curly =
match self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) { match self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{']) {
Some(it) => it, Some(it) => it,
None => return, None => return,
}; };
@ -124,7 +124,7 @@ impl AstEditor<ast::NamedFieldList> {
if let Some(comma) = $anchor if let Some(comma) = $anchor
.syntax() .syntax()
.siblings_with_tokens(Direction::Next) .siblings_with_tokens(Direction::Next)
.find(|it| it.kind() == COMMA) .find(|it| it.kind() == T![,])
{ {
InsertPosition::After(comma) InsertPosition::After(comma)
} else { } else {
@ -154,7 +154,7 @@ impl AstEditor<ast::NamedFieldList> {
} }
fn l_curly(&self) -> Option<SyntaxElement> { fn l_curly(&self) -> Option<SyntaxElement> {
self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{'])
} }
} }
@ -188,7 +188,7 @@ impl AstEditor<ast::ItemList> {
} }
fn l_curly(&self) -> Option<SyntaxElement> { fn l_curly(&self) -> Option<SyntaxElement> {
self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{'])
} }
} }
@ -290,7 +290,7 @@ fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> {
mod tokens { mod tokens {
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use ra_syntax::{AstNode, SourceFile, TreeArc, SyntaxToken, SyntaxKind::*}; use ra_syntax::{AstNode, SourceFile, TreeArc, SyntaxToken, SyntaxKind::*, T};
static SOURCE_FILE: Lazy<TreeArc<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;")); static SOURCE_FILE: Lazy<TreeArc<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;"));
@ -299,7 +299,7 @@ mod tokens {
.syntax() .syntax()
.descendants_with_tokens() .descendants_with_tokens()
.filter_map(|it| it.as_token()) .filter_map(|it| it.as_token())
.find(|it| it.kind() == COMMA) .find(|it| it.kind() == T![,])
.unwrap() .unwrap()
} }

View file

@ -2,8 +2,9 @@ use ra_text_edit::TextEditBuilder;
use hir::{ self, db::HirDatabase}; use hir::{ self, db::HirDatabase};
use ra_syntax::{ use ra_syntax::{
T,
ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, SmolStr, ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, SmolStr,
SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA } SyntaxKind::{ PATH, PATH_SEGMENT }
}; };
use crate::{ use crate::{
AssistId, AssistId,
@ -23,7 +24,7 @@ fn collect_path_segments_raw<'a>(
children.next().map(|n| (n, n.kind())), children.next().map(|n| (n, n.kind())),
); );
match (first, second, third) { match (first, second, third) {
(Some((subpath, PATH)), Some((_, COLONCOLON)), Some((segment, PATH_SEGMENT))) => { (Some((subpath, PATH)), Some((_, T![::])), Some((segment, PATH_SEGMENT))) => {
path = ast::Path::cast(subpath.as_node()?)?; path = ast::Path::cast(subpath.as_node()?)?;
segments.push(ast::PathSegment::cast(segment.as_node()?)?); segments.push(ast::PathSegment::cast(segment.as_node()?)?);
} }
@ -421,7 +422,7 @@ fn make_assist_add_in_tree_list(
let last = tree_list.use_trees().last(); let last = tree_list.use_trees().last();
if let Some(last) = last { if let Some(last) = last {
let mut buf = String::new(); let mut buf = String::new();
let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == COMMA); let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == T![,]);
let offset = if let Some(comma) = comma { let offset = if let Some(comma) = comma {
comma.range().end() comma.range().end()
} else { } else {

View file

@ -1,8 +1,9 @@
use hir::db::HirDatabase; use hir::db::HirDatabase;
use ra_syntax::{ use ra_syntax::{
T,
AstNode, SyntaxNode, TextUnit, AstNode, SyntaxNode, TextUnit,
ast::{self, VisibilityOwner, NameOwner}, ast::{self, VisibilityOwner, NameOwner},
SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR}, SyntaxKind::{VISIBILITY, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR},
}; };
use crate::{AssistCtx, Assist, AssistId}; use crate::{AssistCtx, Assist, AssistId};
@ -16,7 +17,7 @@ pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() {
FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true,
_ => false, _ => false,
}); });

View file

@ -1,14 +1,14 @@
use hir::db::HirDatabase; use hir::db::HirDatabase;
use ra_syntax::{ use ra_syntax::{
T,
Direction, Direction,
SyntaxKind::COMMA,
algo::non_trivia_sibling, algo::non_trivia_sibling,
}; };
use crate::{AssistCtx, Assist, AssistId}; use crate::{AssistCtx, Assist, AssistId};
pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == COMMA)?; let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?;
let prev = non_trivia_sibling(comma.into(), Direction::Prev)?; let prev = non_trivia_sibling(comma.into(), Direction::Prev)?;
let next = non_trivia_sibling(comma.into(), Direction::Next)?; let next = non_trivia_sibling(comma.into(), Direction::Next)?;
ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| {

View file

@ -2,9 +2,7 @@ use hir::db::HirDatabase;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
TextUnit, TextUnit,
SyntaxKind::{ T
L_PAREN, R_PAREN, L_CURLY, R_CURLY, L_BRACK, R_BRACK, EXCL
},
}; };
use crate::{AssistCtx, Assist, AssistId}; use crate::{AssistCtx, Assist, AssistId};
@ -64,7 +62,7 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<b
// Make sure it is actually a dbg-macro call, dbg followed by ! // Make sure it is actually a dbg-macro call, dbg followed by !
let excl = path.syntax().next_sibling_or_token()?; let excl = path.syntax().next_sibling_or_token()?;
if name_ref.text() != macro_name || excl.kind() != EXCL { if name_ref.text() != macro_name || excl.kind() != T![!] {
return None; return None;
} }
@ -73,7 +71,7 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<b
let last_child = node.last_child_or_token()?; let last_child = node.last_child_or_token()?;
match (first_child.kind(), last_child.kind()) { match (first_child.kind(), last_child.kind()) {
(L_PAREN, R_PAREN) | (L_BRACK, R_BRACK) | (L_CURLY, R_CURLY) => Some(true), (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true),
_ => Some(false), _ => Some(false),
} }
} }

View file

@ -2,14 +2,15 @@ use std::iter::successors;
use hir::db::HirDatabase; use hir::db::HirDatabase;
use ra_syntax::{ use ra_syntax::{
TextUnit, AstNode, SyntaxKind::COLONCOLON, T,
TextUnit, AstNode,
ast, ast,
}; };
use crate::{AssistCtx, Assist, AssistId}; use crate::{AssistCtx, Assist, AssistId};
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == T![::])?;
let path = ast::Path::cast(colon_colon.parent())?; let path = ast::Path::cast(colon_colon.parent())?;
let top_path = successors(Some(path), |it| it.parent_path()).last()?; let top_path = successors(Some(path), |it| it.parent_path()).last()?;

View file

@ -3,7 +3,7 @@
use std::iter::successors; use std::iter::successors;
use itertools::Itertools; use itertools::Itertools;
use ra_syntax::{ use ra_syntax::{
SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, T,
ast::{self, AstNode, AstToken}, ast::{self, AstNode, AstToken},
}; };
@ -38,7 +38,7 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
return None; return None;
} }
let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
WHITESPACE | L_CURLY | R_CURLY => false, WHITESPACE | T!['{'] | T!['}'] => false,
_ => it != &expr.syntax(), _ => it != &expr.syntax(),
}); });
if non_trivial_children.count() > 0 { if non_trivial_children.count() > 0 {
@ -49,8 +49,8 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str { pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
match left { match left {
L_PAREN | L_BRACK => return "", T!['('] | T!['['] => return "",
L_CURLY => { T!['{'] => {
if let USE_TREE = right { if let USE_TREE = right {
return ""; return "";
} }
@ -58,13 +58,13 @@ pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
_ => (), _ => (),
} }
match right { match right {
R_PAREN | R_BRACK => return "", T![')'] | T![']'] => return "",
R_CURLY => { T!['}'] => {
if let USE_TREE = left { if let USE_TREE = left {
return ""; return "";
} }
} }
DOT => return "", T![.] => return "",
_ => (), _ => (),
} }
" " " "

View file

@ -4,7 +4,7 @@ use itertools::Itertools;
use hir::{source_binder, diagnostics::{Diagnostic as _, DiagnosticSink}}; use hir::{source_binder, diagnostics::{Diagnostic as _, DiagnosticSink}};
use ra_db::SourceDatabase; use ra_db::SourceDatabase;
use ra_syntax::{ use ra_syntax::{
Location, SourceFile, SyntaxKind, TextRange, SyntaxNode, T, Location, SourceFile, TextRange, SyntaxNode,
ast::{self, AstNode, NamedFieldList, NamedField}, ast::{self, AstNode, NamedFieldList, NamedField},
}; };
use ra_assists::ast_editor::{AstEditor, AstBuilder}; use ra_assists::ast_editor::{AstEditor, AstBuilder};
@ -130,9 +130,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
single_use_tree: &ast::UseTree, single_use_tree: &ast::UseTree,
) -> Option<TextEdit> { ) -> Option<TextEdit> {
let use_tree_list_node = single_use_tree.syntax().parent()?; let use_tree_list_node = single_use_tree.syntax().parent()?;
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] {
== SyntaxKind::SELF_KW
{
let start = use_tree_list_node.prev_sibling_or_token()?.range().start(); let start = use_tree_list_node.prev_sibling_or_token()?.range().start();
let end = use_tree_list_node.range().end(); let end = use_tree_list_node.range().end();
let range = TextRange::from_to(start, end); let range = TextRange::from_to(start, end);

View file

@ -157,7 +157,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
}) })
.next() .next()
.and_then(|it| it.as_token()) .and_then(|it| it.as_token())
.filter(|node| node.kind() == COMMA) .filter(|node| node.kind() == T![,])
} }
if let Some(comma_node) = nearby_comma(node, Direction::Prev) { if let Some(comma_node) = nearby_comma(node, Direction::Prev) {

View file

@ -1,7 +1,8 @@
use itertools::Itertools; use itertools::Itertools;
use ra_syntax::{ use ra_syntax::{
T,
SourceFile, TextRange, TextUnit, SyntaxNode, SyntaxElement, SyntaxToken, SourceFile, TextRange, TextUnit, SyntaxNode, SyntaxElement, SyntaxToken,
SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, SyntaxKind::{self, WHITESPACE},
algo::{find_covering_element, non_trivia_sibling}, algo::{find_covering_element, non_trivia_sibling},
ast::{self, AstNode, AstToken}, ast::{self, AstNode, AstToken},
Direction, Direction,
@ -89,7 +90,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: SyntaxToken, offset: TextUn
if is_trailing_comma(prev.kind(), next.kind()) { if is_trailing_comma(prev.kind(), next.kind()) {
// Removes: trailing comma, newline (incl. surrounding whitespace) // Removes: trailing comma, newline (incl. surrounding whitespace)
edit.delete(TextRange::from_to(prev.range().start(), token.range().end())); edit.delete(TextRange::from_to(prev.range().start(), token.range().end()));
} else if prev.kind() == COMMA && next.kind() == R_CURLY { } else if prev.kind() == T![,] && next.kind() == T!['}'] {
// Removes: comma, newline (incl. surrounding whitespace) // Removes: comma, newline (incl. surrounding whitespace)
let space = if let Some(left) = prev.prev_sibling_or_token() { let space = if let Some(left) = prev.prev_sibling_or_token() {
compute_ws(left.kind(), next.kind()) compute_ws(left.kind(), next.kind())
@ -116,7 +117,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: SyntaxToken, offset: TextUn
fn has_comma_after(node: &SyntaxNode) -> bool { fn has_comma_after(node: &SyntaxNode) -> bool {
match non_trivia_sibling(node.into(), Direction::Next) { match non_trivia_sibling(node.into(), Direction::Next) {
Some(n) => n.kind() == COMMA, Some(n) => n.kind() == T![,],
_ => false, _ => false,
} }
} }
@ -150,7 +151,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, token: SyntaxToken) -> Optio
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool { fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
match (left, right) { match (left, right) {
(COMMA, R_PAREN) | (COMMA, R_BRACK) => true, (T![,], T![')']) | (T![,], T![']']) => true,
_ => false, _ => false,
} }
} }

View file

@ -1,13 +1,14 @@
use ra_syntax::{ use ra_syntax::{
SourceFile, TextUnit, SourceFile, TextUnit,
algo::find_token_at_offset, algo::find_token_at_offset,
SyntaxKind::{self, *}, SyntaxKind::{self},
ast::AstNode, ast::AstNode,
T
}; };
pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> { pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
const BRACES: &[SyntaxKind] = const BRACES: &[SyntaxKind] =
&[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE]; &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>]];
let (brace_node, brace_idx) = find_token_at_offset(file.syntax(), offset) let (brace_node, brace_idx) = find_token_at_offset(file.syntax(), offset)
.filter_map(|node| { .filter_map(|node| {
let idx = BRACES.iter().position(|&brace| brace == node.kind())?; let idx = BRACES.iter().position(|&brace| brace == node.kind())?;

View file

@ -1,6 +1,6 @@
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement}; use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement, T};
use ra_db::SourceDatabase; use ra_db::SourceDatabase;
use crate::{FileId, db::RootDatabase}; use crate::{FileId, db::RootDatabase};
@ -40,7 +40,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
let mut range_end = name_ref.syntax().range().end(); let mut range_end = name_ref.syntax().range().end();
for sibling in path.syntax().siblings_with_tokens(Direction::Next) { for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
match sibling.kind() { match sibling.kind() {
EXCL | IDENT => range_end = sibling.range().end(), T![!] | IDENT => range_end = sibling.range().end(),
_ => (), _ => (),
} }
} }

View file

@ -1,5 +1,5 @@
use ra_parser::{TokenSource}; use ra_parser::{TokenSource};
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*}; use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
use std::cell::{RefCell}; use std::cell::{RefCell};
// A Sequece of Token, // A Sequece of Token,
@ -284,9 +284,9 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken { fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken {
let (kinds, texts) = match d { let (kinds, texts) = match d {
tt::Delimiter::Parenthesis => ([L_PAREN, R_PAREN], "()"), tt::Delimiter::Parenthesis => ([T!['('], T![')']], "()"),
tt::Delimiter::Brace => ([L_CURLY, R_CURLY], "{}"), tt::Delimiter::Brace => ([T!['{'], T!['}']], "{}"),
tt::Delimiter::Bracket => ([L_BRACK, R_BRACK], "[]"), tt::Delimiter::Bracket => ([T!['['], T![']']], "[]"),
tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""), tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""),
}; };
@ -299,8 +299,8 @@ fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken {
fn convert_literal(l: &tt::Literal) -> TtToken { fn convert_literal(l: &tt::Literal) -> TtToken {
let kind = let kind =
classify_literal(&l.text).map(|tkn| tkn.kind).unwrap_or_else(|| match l.text.as_ref() { classify_literal(&l.text).map(|tkn| tkn.kind).unwrap_or_else(|| match l.text.as_ref() {
"true" => SyntaxKind::TRUE_KW, "true" => T![true],
"false" => SyntaxKind::FALSE_KW, "false" => T![false],
_ => panic!("Fail to convert given literal {:#?}", &l), _ => panic!("Fail to convert given literal {:#?}", &l),
}); });
@ -320,11 +320,11 @@ fn convert_ident(ident: &tt::Ident) -> TtToken {
fn convert_punct(p: &tt::Punct) -> TtToken { fn convert_punct(p: &tt::Punct) -> TtToken {
let kind = match p.char { let kind = match p.char {
// lexer may produce compound tokens for these ones // lexer may produce compound tokens for these ones
'.' => DOT, '.' => T![.],
':' => COLON, ':' => T![:],
'=' => EQ, '=' => T![=],
'!' => EXCL, '!' => T![!],
'-' => MINUS, '-' => T![-],
c => SyntaxKind::from_char(c).unwrap(), c => SyntaxKind::from_char(c).unwrap(),
}; };
let text = { let text = {

View file

@ -1,7 +1,7 @@
use ra_parser::{TreeSink, ParseError}; use ra_parser::{TreeSink, ParseError};
use ra_syntax::{ use ra_syntax::{
AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement, AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement,
ast, SyntaxKind::*, TextUnit ast, SyntaxKind::*, TextUnit, T
}; };
use crate::subtree_source::{SubtreeTokenSource, Querier}; use crate::subtree_source::{SubtreeTokenSource, Querier};
@ -211,9 +211,9 @@ fn convert_tt(
let first_child = tt.first_child_or_token()?; let first_child = tt.first_child_or_token()?;
let last_child = tt.last_child_or_token()?; let last_child = tt.last_child_or_token()?;
let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) { let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) {
(L_PAREN, R_PAREN) => (tt::Delimiter::Parenthesis, true), (T!['('], T![')']) => (tt::Delimiter::Parenthesis, true),
(L_CURLY, R_CURLY) => (tt::Delimiter::Brace, true), (T!['{'], T!['}']) => (tt::Delimiter::Brace, true),
(L_BRACK, R_BRACK) => (tt::Delimiter::Bracket, true), (T!['['], T![']']) => (tt::Delimiter::Bracket, true),
_ => (tt::Delimiter::None, false), _ => (tt::Delimiter::None, false),
}; };
@ -248,9 +248,8 @@ fn convert_tt(
token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into());
} else { } else {
let child: tt::TokenTree = if token.kind() == SyntaxKind::TRUE_KW let child: tt::TokenTree =
|| token.kind() == SyntaxKind::FALSE_KW if token.kind() == T![true] || token.kind() == T![false] {
{
tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() tt::Leaf::from(tt::Literal { text: token.text().clone() }).into()
} else if token.kind().is_keyword() } else if token.kind().is_keyword()
|| token.kind() == IDENT || token.kind() == IDENT
@ -305,10 +304,8 @@ impl<'a, Q: Querier> TtTreeSink<'a, Q> {
} }
fn is_delimiter(kind: SyntaxKind) -> bool { fn is_delimiter(kind: SyntaxKind) -> bool {
use SyntaxKind::*;
match kind { match kind {
L_PAREN | L_BRACK | L_CURLY | R_PAREN | R_BRACK | R_CURLY => true, T!['('] | T!['['] | T!['{'] | T![')'] | T![']'] | T!['}'] => true,
_ => false, _ => false,
} }
} }

View file

@ -38,7 +38,7 @@ pub(crate) enum Event {
/// The events for it would look like this: /// The events for it would look like this:
/// ///
/// ///
/// START(PATH) IDENT('foo') FINISH START(PATH) COLONCOLON IDENT('bar') FINISH /// START(PATH) IDENT('foo') FINISH START(PATH) T![::] IDENT('bar') FINISH
/// | /\ /// | /\
/// | | /// | |
/// +------forward-parent------+ /// +------forward-parent------+

View file

@ -59,7 +59,7 @@ pub(crate) fn macro_stmts(p: &mut Parser) {
let m = p.start(); let m = p.start();
while !p.at(EOF) { while !p.at(EOF) {
if p.current() == SEMI { if p.current() == T![;] {
p.bump(); p.bump();
continue; continue;
} }
@ -103,7 +103,7 @@ pub(crate) fn block(p: &mut Parser) {
pub(crate) fn meta_item(p: &mut Parser) { pub(crate) fn meta_item(p: &mut Parser) {
fn is_delimiter(p: &mut Parser) -> bool { fn is_delimiter(p: &mut Parser) -> bool {
match p.current() { match p.current() {
L_CURLY | L_PAREN | L_BRACK => true, T!['{'] | T!['('] | T!['['] => true,
_ => false, _ => false,
} }
} }
@ -123,12 +123,12 @@ pub(crate) fn meta_item(p: &mut Parser) {
// https://doc.rust-lang.org/reference/paths.html#simple-paths // https://doc.rust-lang.org/reference/paths.html#simple-paths
// The start of an meta must be a simple path // The start of an meta must be a simple path
match p.current() { match p.current() {
IDENT | COLONCOLON | SUPER_KW | SELF_KW | CRATE_KW => p.bump(), IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump(),
EQ => { T![=] => {
p.bump(); p.bump();
match p.current() { match p.current() {
c if c.is_literal() => p.bump(), c if c.is_literal() => p.bump(),
TRUE_KW | FALSE_KW => p.bump(), T![true] | T![false] => p.bump(),
_ => {} _ => {}
} }
break; break;
@ -158,7 +158,7 @@ pub(crate) fn reparser(
MATCH_ARM_LIST => items::match_arm_list, MATCH_ARM_LIST => items::match_arm_list,
USE_TREE_LIST => items::use_tree_list, USE_TREE_LIST => items::use_tree_list,
EXTERN_ITEM_LIST => items::extern_item_list, EXTERN_ITEM_LIST => items::extern_item_list,
TOKEN_TREE if first_child? == L_CURLY => items::token_tree, TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
ITEM_LIST => match parent? { ITEM_LIST => match parent? {
IMPL_BLOCK => items::impl_item_list, IMPL_BLOCK => items::impl_item_list,
TRAIT_DEF => items::trait_item_list, TRAIT_DEF => items::trait_item_list,
@ -184,26 +184,26 @@ impl BlockLike {
pub(crate) fn opt_visibility(p: &mut Parser) -> bool { pub(crate) fn opt_visibility(p: &mut Parser) -> bool {
match p.current() { match p.current() {
PUB_KW => { T![pub] => {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
if p.at(L_PAREN) { if p.at(T!['(']) {
match p.nth(1) { match p.nth(1) {
// test crate_visibility // test crate_visibility
// pub(crate) struct S; // pub(crate) struct S;
// pub(self) struct S; // pub(self) struct S;
// pub(self) struct S; // pub(self) struct S;
// pub(self) struct S; // pub(self) struct S;
CRATE_KW | SELF_KW | SUPER_KW => { T![crate] | T![self] | T![super] => {
p.bump(); p.bump();
p.bump(); p.bump();
p.expect(R_PAREN); p.expect(T![')']);
} }
IN_KW => { T![in] => {
p.bump(); p.bump();
p.bump(); p.bump();
paths::use_path(p); paths::use_path(p);
p.expect(R_PAREN); p.expect(T![')']);
} }
_ => (), _ => (),
} }
@ -217,7 +217,7 @@ pub(crate) fn opt_visibility(p: &mut Parser) -> bool {
// //
// test crate_keyword_path // test crate_keyword_path
// fn foo() { crate::foo(); } // fn foo() { crate::foo(); }
CRATE_KW if p.nth(1) != COLONCOLON => { T![crate] if p.nth(1) != T![::] => {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
m.complete(p, VISIBILITY); m.complete(p, VISIBILITY);
@ -228,10 +228,10 @@ pub(crate) fn opt_visibility(p: &mut Parser) -> bool {
} }
fn opt_alias(p: &mut Parser) { fn opt_alias(p: &mut Parser) {
if p.at(AS_KW) { if p.at(T![as]) {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
if !p.eat(UNDERSCORE) { if !p.eat(T![_]) {
name(p); name(p);
} }
m.complete(p, ALIAS); m.complete(p, ALIAS);
@ -239,7 +239,7 @@ fn opt_alias(p: &mut Parser) {
} }
fn abi(p: &mut Parser) { fn abi(p: &mut Parser) {
assert!(p.at(EXTERN_KW)); assert!(p.at(T![extern]));
let abi = p.start(); let abi = p.start();
p.bump(); p.bump();
match p.current() { match p.current() {
@ -250,7 +250,7 @@ fn abi(p: &mut Parser) {
} }
fn opt_fn_ret_type(p: &mut Parser) -> bool { fn opt_fn_ret_type(p: &mut Parser) -> bool {
if p.at(THIN_ARROW) { if p.at(T![->]) {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
types::type_(p); types::type_(p);
@ -280,21 +280,21 @@ fn name_ref(p: &mut Parser) {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
m.complete(p, NAME_REF); m.complete(p, NAME_REF);
} else if p.at(SELF_KW) { } else if p.at(T![self]) {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
m.complete(p, SELF_KW); m.complete(p, T![self]);
} else { } else {
p.err_and_bump("expected identifier"); p.err_and_bump("expected identifier");
} }
} }
fn error_block(p: &mut Parser, message: &str) { fn error_block(p: &mut Parser, message: &str) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.error(message); p.error(message);
p.bump(); p.bump();
expressions::expr_block_contents(p); expressions::expr_block_contents(p);
p.eat(R_CURLY); p.eat(T!['}']);
m.complete(p, ERROR); m.complete(p, ERROR);
} }

View file

@ -1,28 +1,28 @@
use super::*; use super::*;
pub(super) fn inner_attributes(p: &mut Parser) { pub(super) fn inner_attributes(p: &mut Parser) {
while p.current() == POUND && p.nth(1) == EXCL { while p.current() == T![#] && p.nth(1) == T![!] {
attribute(p, true) attribute(p, true)
} }
} }
pub(super) fn outer_attributes(p: &mut Parser) { pub(super) fn outer_attributes(p: &mut Parser) {
while p.at(POUND) { while p.at(T![#]) {
attribute(p, false) attribute(p, false)
} }
} }
fn attribute(p: &mut Parser, inner: bool) { fn attribute(p: &mut Parser, inner: bool) {
let attr = p.start(); let attr = p.start();
assert!(p.at(POUND)); assert!(p.at(T![#]));
p.bump(); p.bump();
if inner { if inner {
assert!(p.at(EXCL)); assert!(p.at(T![!]));
p.bump(); p.bump();
} }
if p.at(L_BRACK) { if p.at(T!['[']) {
items::token_tree(p); items::token_tree(p);
} else { } else {
p.error("expected `[`"); p.error("expected `[`");

View file

@ -36,14 +36,14 @@ fn expr_no_struct(p: &mut Parser) {
// fn c() { 1; 2; } // fn c() { 1; 2; }
// fn d() { 1; 2 } // fn d() { 1; 2 }
pub(crate) fn block(p: &mut Parser) { pub(crate) fn block(p: &mut Parser) {
if !p.at(L_CURLY) { if !p.at(T!['{']) {
p.error("expected a block"); p.error("expected a block");
return; return;
} }
let m = p.start(); let m = p.start();
p.bump(); p.bump();
expr_block_contents(p); expr_block_contents(p);
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, BLOCK); m.complete(p, BLOCK);
} }
@ -65,10 +65,10 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
// #[C] #[D] {} // #[C] #[D] {}
// #[D] return (); // #[D] return ();
// } // }
let has_attrs = p.at(POUND); let has_attrs = p.at(T![#]);
attributes::outer_attributes(p); attributes::outer_attributes(p);
if p.at(LET_KW) { if p.at(T![let]) {
let_stmt(p, m, with_semi); let_stmt(p, m, with_semi);
return; return;
} }
@ -90,7 +90,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
p.error(format!("attributes are not allowed on {:?}", kind)); p.error(format!("attributes are not allowed on {:?}", kind));
} }
if p.at(R_CURLY) { if p.at(T!['}']) {
// test attr_on_last_expr_in_block // test attr_on_last_expr_in_block
// fn foo() { // fn foo() {
// { #[A] bar!()? } // { #[A] bar!()? }
@ -121,15 +121,15 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
match with_semi { match with_semi {
StmtWithSemi::Yes => { StmtWithSemi::Yes => {
if blocklike.is_block() { if blocklike.is_block() {
p.eat(SEMI); p.eat(T![;]);
} else { } else {
p.expect(SEMI); p.expect(T![;]);
} }
} }
StmtWithSemi::No => {} StmtWithSemi::No => {}
StmtWithSemi::Optional => { StmtWithSemi::Optional => {
if p.at(SEMI) { if p.at(T![;]) {
p.eat(SEMI); p.eat(T![;]);
} }
} }
} }
@ -145,24 +145,24 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
// let d: i32 = 92; // let d: i32 = 92;
// } // }
fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) { fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) {
assert!(p.at(LET_KW)); assert!(p.at(T![let]));
p.bump(); p.bump();
patterns::pattern(p); patterns::pattern(p);
if p.at(COLON) { if p.at(T![:]) {
types::ascription(p); types::ascription(p);
} }
if p.eat(EQ) { if p.eat(T![=]) {
expressions::expr(p); expressions::expr(p);
} }
match with_semi { match with_semi {
StmtWithSemi::Yes => { StmtWithSemi::Yes => {
p.expect(SEMI); p.expect(T![;]);
} }
StmtWithSemi::No => {} StmtWithSemi::No => {}
StmtWithSemi::Optional => { StmtWithSemi::Optional => {
if p.at(SEMI) { if p.at(T![;]) {
p.eat(SEMI); p.eat(T![;]);
} }
} }
} }
@ -174,12 +174,12 @@ pub(crate) fn expr_block_contents(p: &mut Parser) {
// This is checked by a validator // This is checked by a validator
attributes::inner_attributes(p); attributes::inner_attributes(p);
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
// test nocontentexpr // test nocontentexpr
// fn foo(){ // fn foo(){
// ;;;some_expr();;;;{;;;};;;;Ok(()) // ;;;some_expr();;;;{;;;};;;;Ok(())
// } // }
if p.current() == SEMI { if p.current() == T![;] {
p.bump(); p.bump();
continue; continue;
} }
@ -202,41 +202,41 @@ enum Op {
fn current_op(p: &Parser) -> (u8, Op) { fn current_op(p: &Parser) -> (u8, Op) {
if let Some(t) = p.current3() { if let Some(t) = p.current3() {
match t { match t {
(L_ANGLE, L_ANGLE, EQ) => return (1, Op::Composite(SHLEQ, 3)), (T![<], T![<], T![=]) => return (1, Op::Composite(T![<<=], 3)),
(R_ANGLE, R_ANGLE, EQ) => return (1, Op::Composite(SHREQ, 3)), (T![>], T![>], T![=]) => return (1, Op::Composite(T![>>=], 3)),
_ => (), _ => (),
} }
} }
if let Some(t) = p.current2() { if let Some(t) = p.current2() {
match t { match t {
(PLUS, EQ) => return (1, Op::Composite(PLUSEQ, 2)), (T![+], T![=]) => return (1, Op::Composite(T![+=], 2)),
(MINUS, EQ) => return (1, Op::Composite(MINUSEQ, 2)), (T![-], T![=]) => return (1, Op::Composite(T![-=], 2)),
(STAR, EQ) => return (1, Op::Composite(STAREQ, 2)), (T![*], T![=]) => return (1, Op::Composite(T![*=], 2)),
(PERCENT, EQ) => return (1, Op::Composite(PERCENTEQ, 2)), (T![%], T![=]) => return (1, Op::Composite(T![%=], 2)),
(SLASH, EQ) => return (1, Op::Composite(SLASHEQ, 2)), (T![/], T![=]) => return (1, Op::Composite(T![/=], 2)),
(PIPE, EQ) => return (1, Op::Composite(PIPEEQ, 2)), (T![|], T![=]) => return (1, Op::Composite(T![|=], 2)),
(AMP, EQ) => return (1, Op::Composite(AMPEQ, 2)), (T![&], T![=]) => return (1, Op::Composite(T![&=], 2)),
(CARET, EQ) => return (1, Op::Composite(CARETEQ, 2)), (T![^], T![=]) => return (1, Op::Composite(T![^=], 2)),
(PIPE, PIPE) => return (3, Op::Composite(PIPEPIPE, 2)), (T![|], T![|]) => return (3, Op::Composite(T![||], 2)),
(AMP, AMP) => return (4, Op::Composite(AMPAMP, 2)), (T![&], T![&]) => return (4, Op::Composite(T![&&], 2)),
(L_ANGLE, EQ) => return (5, Op::Composite(LTEQ, 2)), (T![<], T![=]) => return (5, Op::Composite(T![<=], 2)),
(R_ANGLE, EQ) => return (5, Op::Composite(GTEQ, 2)), (T![>], T![=]) => return (5, Op::Composite(T![>=], 2)),
(L_ANGLE, L_ANGLE) => return (9, Op::Composite(SHL, 2)), (T![<], T![<]) => return (9, Op::Composite(T![<<], 2)),
(R_ANGLE, R_ANGLE) => return (9, Op::Composite(SHR, 2)), (T![>], T![>]) => return (9, Op::Composite(T![>>], 2)),
_ => (), _ => (),
} }
} }
let bp = match p.current() { let bp = match p.current() {
EQ => 1, T![=] => 1,
DOTDOT | DOTDOTEQ => 2, T![..] | T![..=] => 2,
EQEQ | NEQ | L_ANGLE | R_ANGLE => 5, T![==] | T![!=] | T![<] | T![>] => 5,
PIPE => 6, T![|] => 6,
CARET => 7, T![^] => 7,
AMP => 8, T![&] => 8,
MINUS | PLUS => 10, T![-] | T![+] => 10,
STAR | SLASH | PERCENT => 11, T![*] | T![/] | T![%] => 11,
_ => 0, _ => 0,
}; };
(bp, Op::Simple) (bp, Op::Simple)
@ -284,7 +284,7 @@ fn expr_bp(
newly_dollar_open = false; newly_dollar_open = false;
} }
let is_range = p.current() == DOTDOT || p.current() == DOTDOTEQ; let is_range = p.current() == T![..] || p.current() == T![..=];
let (op_bp, op) = current_op(p); let (op_bp, op) = current_op(p);
if op_bp < bp { if op_bp < bp {
break; break;
@ -318,10 +318,10 @@ fn lhs(
// let _ = &1; // let _ = &1;
// let _ = &mut &f(); // let _ = &mut &f();
// } // }
AMP => { T![&] => {
m = p.start(); m = p.start();
p.bump(); p.bump();
p.eat(MUT_KW); p.eat(T![mut]);
REF_EXPR REF_EXPR
} }
// test unary_expr // test unary_expr
@ -330,14 +330,14 @@ fn lhs(
// !!true; // !!true;
// --1; // --1;
// } // }
STAR | EXCL | MINUS => { T![*] | T![!] | T![-] => {
m = p.start(); m = p.start();
p.bump(); p.bump();
PREFIX_EXPR PREFIX_EXPR
} }
// test full_range_expr // test full_range_expr
// fn foo() { xs[..]; } // fn foo() { xs[..]; }
DOTDOT | DOTDOTEQ => { T![..] | T![..=] => {
m = p.start(); m = p.start();
p.bump(); p.bump();
if p.at_ts(EXPR_FIRST) { if p.at_ts(EXPR_FIRST) {
@ -375,21 +375,21 @@ fn postfix_expr(
// [] => {} // [] => {}
// } // }
// } // }
L_PAREN if allow_calls => call_expr(p, lhs), T!['('] if allow_calls => call_expr(p, lhs),
L_BRACK if allow_calls => index_expr(p, lhs), T!['['] if allow_calls => index_expr(p, lhs),
DOT if p.nth(1) == IDENT && (p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON) => { T![.] if p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth(2) == T![::]) => {
method_call_expr(p, lhs) method_call_expr(p, lhs)
} }
DOT => field_expr(p, lhs), T![.] => field_expr(p, lhs),
// test postfix_range // test postfix_range
// fn foo() { let x = 1..; } // fn foo() { let x = 1..; }
DOTDOT | DOTDOTEQ if !EXPR_FIRST.contains(p.nth(1)) => { T![..] | T![..=] if !EXPR_FIRST.contains(p.nth(1)) => {
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
m.complete(p, RANGE_EXPR) m.complete(p, RANGE_EXPR)
} }
QUESTION => try_expr(p, lhs), T![?] => try_expr(p, lhs),
AS_KW => cast_expr(p, lhs), T![as] => cast_expr(p, lhs),
_ => break, _ => break,
}; };
allow_calls = true allow_calls = true
@ -405,7 +405,7 @@ fn postfix_expr(
// f(<Foo as Trait>::func()); // f(<Foo as Trait>::func());
// } // }
fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
let m = lhs.precede(p); let m = lhs.precede(p);
arg_list(p); arg_list(p);
m.complete(p, CALL_EXPR) m.complete(p, CALL_EXPR)
@ -416,11 +416,11 @@ fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
// x[1][2]; // x[1][2];
// } // }
fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(L_BRACK)); assert!(p.at(T!['[']));
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
expr(p); expr(p);
p.expect(R_BRACK); p.expect(T![']']);
m.complete(p, INDEX_EXPR) m.complete(p, INDEX_EXPR)
} }
@ -430,12 +430,12 @@ fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
// y.bar::<T>(1, 2,); // y.bar::<T>(1, 2,);
// } // }
fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(DOT) && p.nth(1) == IDENT && (p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON)); assert!(p.at(T![.]) && p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth(2) == T![::]));
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
name_ref(p); name_ref(p);
type_args::opt_type_arg_list(p, true); type_args::opt_type_arg_list(p, true);
if p.at(L_PAREN) { if p.at(T!['(']) {
arg_list(p); arg_list(p);
} }
m.complete(p, METHOD_CALL_EXPR) m.complete(p, METHOD_CALL_EXPR)
@ -455,7 +455,7 @@ fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
// x.0x01; // x.0x01;
// } // }
fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(DOT)); assert!(p.at(T![.]));
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
if p.at(IDENT) { if p.at(IDENT) {
@ -463,7 +463,7 @@ fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
} else if p.at(INT_NUMBER) { } else if p.at(INT_NUMBER) {
p.bump(); p.bump();
} else if p.at(FLOAT_NUMBER) { } else if p.at(FLOAT_NUMBER) {
// FIXME: How to recover and instead parse INT + DOT? // FIXME: How to recover and instead parse INT + T![.]?
p.bump(); p.bump();
} else { } else {
p.error("expected field name or number") p.error("expected field name or number")
@ -476,7 +476,7 @@ fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
// x?; // x?;
// } // }
fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(QUESTION)); assert!(p.at(T![?]));
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
m.complete(p, TRY_EXPR) m.complete(p, TRY_EXPR)
@ -490,7 +490,7 @@ fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
// 0x36 as u8 <= 0x37; // 0x36 as u8 <= 0x37;
// } // }
fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(AS_KW)); assert!(p.at(T![as]));
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
// Use type_no_bounds(), because cast expressions are not // Use type_no_bounds(), because cast expressions are not
@ -500,20 +500,20 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
} }
fn arg_list(p: &mut Parser) { fn arg_list(p: &mut Parser) {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(R_PAREN) && !p.at(EOF) { while !p.at(T![')']) && !p.at(EOF) {
if !p.at_ts(EXPR_FIRST) { if !p.at_ts(EXPR_FIRST) {
p.error("expected expression"); p.error("expected expression");
break; break;
} }
expr(p); expr(p);
if !p.at(R_PAREN) && !p.expect(COMMA) { if !p.at(T![')']) && !p.expect(T![,]) {
break; break;
} }
} }
p.eat(R_PAREN); p.eat(T![')']);
m.complete(p, ARG_LIST); m.complete(p, ARG_LIST);
} }
@ -525,15 +525,15 @@ fn arg_list(p: &mut Parser) {
// let _ = format!(); // let _ = format!();
// } // }
fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) { fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) {
assert!(paths::is_path_start(p) || p.at(L_ANGLE)); assert!(paths::is_path_start(p) || p.at(T![<]));
let m = p.start(); let m = p.start();
paths::expr_path(p); paths::expr_path(p);
match p.current() { match p.current() {
L_CURLY if !r.forbid_structs => { T!['{'] if !r.forbid_structs => {
named_field_list(p); named_field_list(p);
(m.complete(p, STRUCT_LIT), BlockLike::NotBlock) (m.complete(p, STRUCT_LIT), BlockLike::NotBlock)
} }
EXCL => { T![!] => {
let block_like = items::macro_call_after_excl(p); let block_like = items::macro_call_after_excl(p);
(m.complete(p, MACRO_CALL), block_like) (m.complete(p, MACRO_CALL), block_like)
} }
@ -548,35 +548,35 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) {
// S { x, y: 32, ..Default::default() }; // S { x, y: 32, ..Default::default() };
// } // }
pub(crate) fn named_field_list(p: &mut Parser) { pub(crate) fn named_field_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
match p.current() { match p.current() {
// test struct_literal_field_with_attr // test struct_literal_field_with_attr
// fn main() { // fn main() {
// S { #[cfg(test)] field: 1 } // S { #[cfg(test)] field: 1 }
// } // }
IDENT | POUND => { IDENT | T![#] => {
let m = p.start(); let m = p.start();
attributes::outer_attributes(p); attributes::outer_attributes(p);
name_ref(p); name_ref(p);
if p.eat(COLON) { if p.eat(T![:]) {
expr(p); expr(p);
} }
m.complete(p, NAMED_FIELD); m.complete(p, NAMED_FIELD);
} }
DOTDOT => { T![..] => {
p.bump(); p.bump();
expr(p); expr(p);
} }
L_CURLY => error_block(p, "expected a field"), T!['{'] => error_block(p, "expected a field"),
_ => p.err_and_bump("expected identifier"), _ => p.err_and_bump("expected identifier"),
} }
if !p.at(R_CURLY) { if !p.at(T!['}']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, NAMED_FIELD_LIST); m.complete(p, NAMED_FIELD_LIST);
} }

View file

@ -60,29 +60,29 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
if let Some(m) = literal(p) { if let Some(m) = literal(p) {
return Some((m, BlockLike::NotBlock)); return Some((m, BlockLike::NotBlock));
} }
if paths::is_path_start(p) || p.at(L_ANGLE) { if paths::is_path_start(p) || p.at(T![<]) {
return Some(path_expr(p, r)); return Some(path_expr(p, r));
} }
let la = p.nth(1); let la = p.nth(1);
let done = match p.current() { let done = match p.current() {
L_PAREN => tuple_expr(p), T!['('] => tuple_expr(p),
L_BRACK => array_expr(p), T!['['] => array_expr(p),
PIPE => lambda_expr(p), T![|] => lambda_expr(p),
MOVE_KW if la == PIPE => lambda_expr(p), T![move] if la == T![|] => lambda_expr(p),
ASYNC_KW if la == PIPE || (la == MOVE_KW && p.nth(2) == PIPE) => lambda_expr(p), T![async] if la == T![|] || (la == T![move] && p.nth(2) == T![|]) => lambda_expr(p),
IF_KW => if_expr(p), T![if] => if_expr(p),
LOOP_KW => loop_expr(p, None), T![loop] => loop_expr(p, None),
FOR_KW => for_expr(p, None), T![for] => for_expr(p, None),
WHILE_KW => while_expr(p, None), T![while] => while_expr(p, None),
LIFETIME if la == COLON => { LIFETIME if la == T![:] => {
let m = p.start(); let m = p.start();
label(p); label(p);
match p.current() { match p.current() {
LOOP_KW => loop_expr(p, Some(m)), T![loop] => loop_expr(p, Some(m)),
FOR_KW => for_expr(p, Some(m)), T![for] => for_expr(p, Some(m)),
WHILE_KW => while_expr(p, Some(m)), T![while] => while_expr(p, Some(m)),
L_CURLY => block_expr(p, Some(m)), T!['{'] => block_expr(p, Some(m)),
_ => { _ => {
// test_err misplaced_label_err // test_err misplaced_label_err
// fn main() { // fn main() {
@ -94,22 +94,22 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
} }
} }
} }
ASYNC_KW if la == L_CURLY || (la == MOVE_KW && p.nth(2) == L_CURLY) => { T![async] if la == T!['{'] || (la == T![move] && p.nth(2) == T!['{']) => {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
p.eat(MOVE_KW); p.eat(T![move]);
block_expr(p, Some(m)) block_expr(p, Some(m))
} }
MATCH_KW => match_expr(p), T![match] => match_expr(p),
UNSAFE_KW if la == L_CURLY => { T![unsafe] if la == T!['{'] => {
let m = p.start(); let m = p.start();
p.bump(); p.bump();
block_expr(p, Some(m)) block_expr(p, Some(m))
} }
L_CURLY => block_expr(p, None), T!['{'] => block_expr(p, None),
RETURN_KW => return_expr(p), T![return] => return_expr(p),
CONTINUE_KW => continue_expr(p), T![continue] => continue_expr(p),
BREAK_KW => break_expr(p, r), T![break] => break_expr(p, r),
_ => { _ => {
p.err_recover("expected expression", EXPR_RECOVERY_SET); p.err_recover("expected expression", EXPR_RECOVERY_SET);
return None; return None;
@ -129,25 +129,25 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
// (1,); // (1,);
// } // }
fn tuple_expr(p: &mut Parser) -> CompletedMarker { fn tuple_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
let m = p.start(); let m = p.start();
p.expect(L_PAREN); p.expect(T!['(']);
let mut saw_comma = false; let mut saw_comma = false;
let mut saw_expr = false; let mut saw_expr = false;
while !p.at(EOF) && !p.at(R_PAREN) { while !p.at(EOF) && !p.at(T![')']) {
saw_expr = true; saw_expr = true;
if !p.at_ts(EXPR_FIRST) { if !p.at_ts(EXPR_FIRST) {
p.error("expected expression"); p.error("expected expression");
break; break;
} }
expr(p); expr(p);
if !p.at(R_PAREN) { if !p.at(T![')']) {
saw_comma = true; saw_comma = true;
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_PAREN); p.expect(T![')']);
m.complete(p, if saw_expr && !saw_comma { PAREN_EXPR } else { TUPLE_EXPR }) m.complete(p, if saw_expr && !saw_comma { PAREN_EXPR } else { TUPLE_EXPR })
} }
@ -159,21 +159,21 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
// [1; 2]; // [1; 2];
// } // }
fn array_expr(p: &mut Parser) -> CompletedMarker { fn array_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_BRACK)); assert!(p.at(T!['[']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
if p.eat(R_BRACK) { if p.eat(T![']']) {
return m.complete(p, ARRAY_EXPR); return m.complete(p, ARRAY_EXPR);
} }
expr(p); expr(p);
if p.eat(SEMI) { if p.eat(T![;]) {
expr(p); expr(p);
p.expect(R_BRACK); p.expect(T![']']);
return m.complete(p, ARRAY_EXPR); return m.complete(p, ARRAY_EXPR);
} }
while !p.at(EOF) && !p.at(R_BRACK) { while !p.at(EOF) && !p.at(T![']']) {
p.expect(COMMA); p.expect(T![,]);
if p.at(R_BRACK) { if p.at(T![']']) {
break; break;
} }
if !p.at_ts(EXPR_FIRST) { if !p.at_ts(EXPR_FIRST) {
@ -182,7 +182,7 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
} }
expr(p); expr(p);
} }
p.expect(R_BRACK); p.expect(T![']']);
m.complete(p, ARRAY_EXPR) m.complete(p, ARRAY_EXPR)
} }
@ -198,17 +198,17 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
// } // }
fn lambda_expr(p: &mut Parser) -> CompletedMarker { fn lambda_expr(p: &mut Parser) -> CompletedMarker {
assert!( assert!(
p.at(PIPE) p.at(T![|])
|| (p.at(MOVE_KW) && p.nth(1) == PIPE) || (p.at(T![move]) && p.nth(1) == T![|])
|| (p.at(ASYNC_KW) && p.nth(1) == PIPE) || (p.at(T![async]) && p.nth(1) == T![|])
|| (p.at(ASYNC_KW) && p.nth(1) == MOVE_KW && p.nth(2) == PIPE) || (p.at(T![async]) && p.nth(1) == T![move] && p.nth(2) == T![|])
); );
let m = p.start(); let m = p.start();
p.eat(ASYNC_KW); p.eat(T![async]);
p.eat(MOVE_KW); p.eat(T![move]);
params::param_list_opt_types(p); params::param_list_opt_types(p);
if opt_fn_ret_type(p) { if opt_fn_ret_type(p) {
if !p.at(L_CURLY) { if !p.at(T!['{']) {
p.error("expected `{`"); p.error("expected `{`");
} }
} }
@ -224,14 +224,14 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
// if S {}; // if S {};
// } // }
fn if_expr(p: &mut Parser) -> CompletedMarker { fn if_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(IF_KW)); assert!(p.at(T![if]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
cond(p); cond(p);
block(p); block(p);
if p.at(ELSE_KW) { if p.at(T![else]) {
p.bump(); p.bump();
if p.at(IF_KW) { if p.at(T![if]) {
if_expr(p); if_expr(p);
} else { } else {
block(p); block(p);
@ -247,7 +247,7 @@ fn if_expr(p: &mut Parser) -> CompletedMarker {
// 'c: for x in () {} // 'c: for x in () {}
// } // }
fn label(p: &mut Parser) { fn label(p: &mut Parser) {
assert!(p.at(LIFETIME) && p.nth(1) == COLON); assert!(p.at(LIFETIME) && p.nth(1) == T![:]);
let m = p.start(); let m = p.start();
p.bump(); p.bump();
p.bump(); p.bump();
@ -259,7 +259,7 @@ fn label(p: &mut Parser) {
// loop {}; // loop {};
// } // }
fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(LOOP_KW)); assert!(p.at(T![loop]));
let m = m.unwrap_or_else(|| p.start()); let m = m.unwrap_or_else(|| p.start());
p.bump(); p.bump();
block(p); block(p);
@ -272,7 +272,7 @@ fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
// while let Some(x) = it.next() {}; // while let Some(x) = it.next() {};
// } // }
fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(WHILE_KW)); assert!(p.at(T![while]));
let m = m.unwrap_or_else(|| p.start()); let m = m.unwrap_or_else(|| p.start());
p.bump(); p.bump();
cond(p); cond(p);
@ -285,11 +285,11 @@ fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
// for x in [] {}; // for x in [] {};
// } // }
fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(FOR_KW)); assert!(p.at(T![for]));
let m = m.unwrap_or_else(|| p.start()); let m = m.unwrap_or_else(|| p.start());
p.bump(); p.bump();
patterns::pattern(p); patterns::pattern(p);
p.expect(IN_KW); p.expect(T![in]);
expr_no_struct(p); expr_no_struct(p);
block(p); block(p);
m.complete(p, FOR_EXPR) m.complete(p, FOR_EXPR)
@ -305,9 +305,9 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
// } // }
fn cond(p: &mut Parser) { fn cond(p: &mut Parser) {
let m = p.start(); let m = p.start();
if p.eat(LET_KW) { if p.eat(T![let]) {
patterns::pattern_list(p); patterns::pattern_list(p);
p.expect(EQ); p.expect(T![=]);
} }
expr_no_struct(p); expr_no_struct(p);
m.complete(p, CONDITION); m.complete(p, CONDITION);
@ -319,11 +319,11 @@ fn cond(p: &mut Parser) {
// match S {}; // match S {};
// } // }
fn match_expr(p: &mut Parser) -> CompletedMarker { fn match_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(MATCH_KW)); assert!(p.at(T![match]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
expr_no_struct(p); expr_no_struct(p);
if p.at(L_CURLY) { if p.at(T!['{']) {
match_arm_list(p); match_arm_list(p);
} else { } else {
p.error("expected `{`") p.error("expected `{`")
@ -332,9 +332,9 @@ fn match_expr(p: &mut Parser) -> CompletedMarker {
} }
pub(crate) fn match_arm_list(p: &mut Parser) { pub(crate) fn match_arm_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.eat(L_CURLY); p.eat(T!['{']);
// test match_arms_inner_attribute // test match_arms_inner_attribute
// fn foo() { // fn foo() {
@ -347,8 +347,8 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
// } // }
attributes::inner_attributes(p); attributes::inner_attributes(p);
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
if p.at(L_CURLY) { if p.at(T!['{']) {
error_block(p, "expected match arm"); error_block(p, "expected match arm");
continue; continue;
} }
@ -362,12 +362,12 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
// } // }
// } // }
if match_arm(p).is_block() { if match_arm(p).is_block() {
p.eat(COMMA); p.eat(T![,]);
} else if !p.at(R_CURLY) { } else if !p.at(T!['}']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, MATCH_ARM_LIST); m.complete(p, MATCH_ARM_LIST);
} }
@ -399,10 +399,10 @@ fn match_arm(p: &mut Parser) -> BlockLike {
attributes::outer_attributes(p); attributes::outer_attributes(p);
patterns::pattern_list_r(p, TokenSet::empty()); patterns::pattern_list_r(p, TokenSet::empty());
if p.at(IF_KW) { if p.at(T![if]) {
match_guard(p); match_guard(p);
} }
p.expect(FAT_ARROW); p.expect(T![=>]);
let blocklike = expr_stmt(p).1; let blocklike = expr_stmt(p).1;
m.complete(p, MATCH_ARM); m.complete(p, MATCH_ARM);
blocklike blocklike
@ -415,7 +415,7 @@ fn match_arm(p: &mut Parser) -> BlockLike {
// } // }
// } // }
fn match_guard(p: &mut Parser) -> CompletedMarker { fn match_guard(p: &mut Parser) -> CompletedMarker {
assert!(p.at(IF_KW)); assert!(p.at(T![if]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
expr(p); expr(p);
@ -429,7 +429,7 @@ fn match_guard(p: &mut Parser) -> CompletedMarker {
// 'label: {}; // 'label: {};
// } // }
fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = m.unwrap_or_else(|| p.start()); let m = m.unwrap_or_else(|| p.start());
block(p); block(p);
m.complete(p, BLOCK_EXPR) m.complete(p, BLOCK_EXPR)
@ -441,7 +441,7 @@ fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
// return 92; // return 92;
// } // }
fn return_expr(p: &mut Parser) -> CompletedMarker { fn return_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(RETURN_KW)); assert!(p.at(T![return]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
if p.at_ts(EXPR_FIRST) { if p.at_ts(EXPR_FIRST) {
@ -458,7 +458,7 @@ fn return_expr(p: &mut Parser) -> CompletedMarker {
// } // }
// } // }
fn continue_expr(p: &mut Parser) -> CompletedMarker { fn continue_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(CONTINUE_KW)); assert!(p.at(T![continue]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
p.eat(LIFETIME); p.eat(LIFETIME);
@ -475,7 +475,7 @@ fn continue_expr(p: &mut Parser) -> CompletedMarker {
// } // }
// } // }
fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
assert!(p.at(BREAK_KW)); assert!(p.at(T![break]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
p.eat(LIFETIME); p.eat(LIFETIME);
@ -486,7 +486,7 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
// for i in break {} // for i in break {}
// match break {} // match break {}
// } // }
if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(L_CURLY)) { if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(T!['{'])) {
expr(p); expr(p);
} }
m.complete(p, BREAK_EXPR) m.complete(p, BREAK_EXPR)

View file

@ -19,7 +19,7 @@ use super::*;
// struct S; // struct S;
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
attributes::inner_attributes(p); attributes::inner_attributes(p);
while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { while !p.at(EOF) && !(stop_on_r_curly && p.at(T!['}'])) {
item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod)
} }
} }
@ -45,20 +45,20 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF
match macro_call(p) { match macro_call(p) {
BlockLike::Block => (), BlockLike::Block => (),
BlockLike::NotBlock => { BlockLike::NotBlock => {
p.expect(SEMI); p.expect(T![;]);
} }
} }
m.complete(p, MACRO_CALL); m.complete(p, MACRO_CALL);
} else { } else {
m.abandon(p); m.abandon(p);
if p.at(L_CURLY) { if p.at(T!['{']) {
error_block(p, "expected an item"); error_block(p, "expected an item");
} else if p.at(R_CURLY) && !stop_on_r_curly { } else if p.at(T!['}']) && !stop_on_r_curly {
let e = p.start(); let e = p.start();
p.error("unmatched `}`"); p.error("unmatched `}`");
p.bump(); p.bump();
e.complete(p, ERROR); e.complete(p, ERROR);
} else if !p.at(EOF) && !p.at(R_CURLY) { } else if !p.at(EOF) && !p.at(T!['}']) {
p.err_and_bump("expected an item"); p.err_and_bump("expected an item");
} else { } else {
p.error("expected an item"); p.error("expected an item");
@ -79,32 +79,32 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
let mut has_mods = false; let mut has_mods = false;
// modifiers // modifiers
has_mods |= p.eat(CONST_KW); has_mods |= p.eat(T![const]);
// test_err unsafe_block_in_mod // test_err unsafe_block_in_mod
// fn foo(){} unsafe { } fn bar(){} // fn foo(){} unsafe { } fn bar(){}
if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY { if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
p.eat(UNSAFE_KW); p.eat(T![unsafe]);
has_mods = true; has_mods = true;
} }
// test_err async_without_semicolon // test_err async_without_semicolon
// fn foo() { let _ = async {} } // fn foo() { let _ = async {} }
if p.at(ASYNC_KW) && p.nth(1) != L_CURLY && p.nth(1) != MOVE_KW && p.nth(1) != PIPE { if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
p.eat(ASYNC_KW); p.eat(T![async]);
has_mods = true; has_mods = true;
} }
if p.at(EXTERN_KW) { if p.at(T![extern]) {
has_mods = true; has_mods = true;
abi(p); abi(p);
} }
if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW { if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
p.bump_remap(AUTO_KW); p.bump_remap(T![auto]);
has_mods = true; has_mods = true;
} }
if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW { if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == T![impl ] {
p.bump_remap(DEFAULT_KW); p.bump_remap(T![default]);
has_mods = true; has_mods = true;
} }
@ -135,7 +135,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
// test_err wrong_order_fns // test_err wrong_order_fns
// async unsafe fn foo() {} // async unsafe fn foo() {}
// unsafe const fn bar() {} // unsafe const fn bar() {}
FN_KW => { T![fn] => {
fn_def(p, flavor); fn_def(p, flavor);
m.complete(p, FN_DEF); m.complete(p, FN_DEF);
} }
@ -148,7 +148,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
// test unsafe_auto_trait // test unsafe_auto_trait
// unsafe auto trait T {} // unsafe auto trait T {}
TRAIT_KW => { T![trait] => {
traits::trait_def(p); traits::trait_def(p);
m.complete(p, TRAIT_DEF); m.complete(p, TRAIT_DEF);
} }
@ -161,7 +161,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
// test unsafe_default_impl // test unsafe_default_impl
// unsafe default impl Foo {} // unsafe default impl Foo {}
IMPL_KW => { T![impl ] => {
traits::impl_block(p); traits::impl_block(p);
m.complete(p, IMPL_BLOCK); m.complete(p, IMPL_BLOCK);
} }
@ -186,10 +186,10 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
match p.current() { match p.current() {
// test extern_crate // test extern_crate
// extern crate foo; // extern crate foo;
EXTERN_KW if la == CRATE_KW => extern_crate_item(p, m), T![extern] if la == T![crate] => extern_crate_item(p, m),
TYPE_KW => type_def(p, m), T![type] => type_def(p, m),
MOD_KW => mod_item(p, m), T![mod] => mod_item(p, m),
STRUCT_KW => { T![struct] => {
// test struct_items // test struct_items
// struct Foo; // struct Foo;
// struct Foo {} // struct Foo {}
@ -199,7 +199,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// a: i32, // a: i32,
// b: f32, // b: f32,
// } // }
nominal::struct_def(p, m, STRUCT_KW); nominal::struct_def(p, m, T![struct]);
} }
IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => { IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
// test union_items // test union_items
@ -208,16 +208,16 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// a: i32, // a: i32,
// b: f32, // b: f32,
// } // }
nominal::struct_def(p, m, UNION_KW); nominal::struct_def(p, m, T![union]);
} }
ENUM_KW => nominal::enum_def(p, m), T![enum] => nominal::enum_def(p, m),
USE_KW => use_item::use_item(p, m), T![use] => use_item::use_item(p, m),
CONST_KW if (la == IDENT || la == MUT_KW) => consts::const_def(p, m), T![const] if (la == IDENT || la == T![mut]) => consts::const_def(p, m),
STATIC_KW => consts::static_def(p, m), T![static] => consts::static_def(p, m),
// test extern_block // test extern_block
// extern {} // extern {}
EXTERN_KW T![extern]
if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) =>
{ {
abi(p); abi(p);
extern_item_list(p); extern_item_list(p);
@ -225,7 +225,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
} }
_ => return Err(m), _ => return Err(m),
}; };
if p.at(SEMI) { if p.at(T![;]) {
p.err_and_bump( p.err_and_bump(
"expected item, found `;`\n\ "expected item, found `;`\n\
consider removing this semicolon", consider removing this semicolon",
@ -235,27 +235,27 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
} }
fn extern_crate_item(p: &mut Parser, m: Marker) { fn extern_crate_item(p: &mut Parser, m: Marker) {
assert!(p.at(EXTERN_KW)); assert!(p.at(T![extern]));
p.bump(); p.bump();
assert!(p.at(CRATE_KW)); assert!(p.at(T![crate]));
p.bump(); p.bump();
name_ref(p); name_ref(p);
opt_alias(p); opt_alias(p);
p.expect(SEMI); p.expect(T![;]);
m.complete(p, EXTERN_CRATE_ITEM); m.complete(p, EXTERN_CRATE_ITEM);
} }
pub(crate) fn extern_item_list(p: &mut Parser) { pub(crate) fn extern_item_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
mod_contents(p, true); mod_contents(p, true);
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, EXTERN_ITEM_LIST); m.complete(p, EXTERN_ITEM_LIST);
} }
fn fn_def(p: &mut Parser, flavor: ItemFlavor) { fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
assert!(p.at(FN_KW)); assert!(p.at(T![fn]));
p.bump(); p.bump();
name_r(p, ITEM_RECOVERY_SET); name_r(p, ITEM_RECOVERY_SET);
@ -263,7 +263,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
// fn foo<T: Clone + Copy>(){} // fn foo<T: Clone + Copy>(){}
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
if p.at(L_PAREN) { if p.at(T!['(']) {
match flavor { match flavor {
ItemFlavor::Mod => params::param_list(p), ItemFlavor::Mod => params::param_list(p),
ItemFlavor::Trait => params::param_list_opt_patterns(p), ItemFlavor::Trait => params::param_list_opt_patterns(p),
@ -282,7 +282,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
// test fn_decl // test fn_decl
// trait T { fn foo(); } // trait T { fn foo(); }
if p.at(SEMI) { if p.at(T![;]) {
p.bump(); p.bump();
} else { } else {
expressions::block(p) expressions::block(p)
@ -292,7 +292,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
// test type_item // test type_item
// type Foo = Bar; // type Foo = Bar;
fn type_def(p: &mut Parser, m: Marker) { fn type_def(p: &mut Parser, m: Marker) {
assert!(p.at(TYPE_KW)); assert!(p.at(T![type]));
p.bump(); p.bump();
name(p); name(p);
@ -301,7 +301,7 @@ fn type_def(p: &mut Parser, m: Marker) {
// type Result<T> = (); // type Result<T> = ();
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
if p.at(COLON) { if p.at(T![:]) {
type_params::bounds(p); type_params::bounds(p);
} }
@ -309,32 +309,32 @@ fn type_def(p: &mut Parser, m: Marker) {
// type Foo where Foo: Copy = (); // type Foo where Foo: Copy = ();
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
if p.eat(EQ) { if p.eat(T![=]) {
types::type_(p); types::type_(p);
} }
p.expect(SEMI); p.expect(T![;]);
m.complete(p, TYPE_ALIAS_DEF); m.complete(p, TYPE_ALIAS_DEF);
} }
pub(crate) fn mod_item(p: &mut Parser, m: Marker) { pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
assert!(p.at(MOD_KW)); assert!(p.at(T![mod]));
p.bump(); p.bump();
name(p); name(p);
if p.at(L_CURLY) { if p.at(T!['{']) {
mod_item_list(p); mod_item_list(p);
} else if !p.eat(SEMI) { } else if !p.eat(T![;]) {
p.error("expected `;` or `{`"); p.error("expected `;` or `{`");
} }
m.complete(p, MODULE); m.complete(p, MODULE);
} }
pub(crate) fn mod_item_list(p: &mut Parser) { pub(crate) fn mod_item_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
mod_contents(p, true); mod_contents(p, true);
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, ITEM_LIST); m.complete(p, ITEM_LIST);
} }
@ -345,16 +345,16 @@ fn macro_call(p: &mut Parser) -> BlockLike {
} }
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
p.expect(EXCL); p.expect(T![!]);
if p.at(IDENT) { if p.at(IDENT) {
name(p); name(p);
} }
match p.current() { match p.current() {
L_CURLY => { T!['{'] => {
token_tree(p); token_tree(p);
BlockLike::Block BlockLike::Block
} }
L_PAREN | L_BRACK => { T!['('] | T!['['] => {
token_tree(p); token_tree(p);
BlockLike::NotBlock BlockLike::NotBlock
} }
@ -367,22 +367,22 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
pub(crate) fn token_tree(p: &mut Parser) { pub(crate) fn token_tree(p: &mut Parser) {
let closing_paren_kind = match p.current() { let closing_paren_kind = match p.current() {
L_CURLY => R_CURLY, T!['{'] => T!['}'],
L_PAREN => R_PAREN, T!['('] => T![')'],
L_BRACK => R_BRACK, T!['['] => T![']'],
_ => unreachable!(), _ => unreachable!(),
}; };
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(closing_paren_kind) { while !p.at(EOF) && !p.at(closing_paren_kind) {
match p.current() { match p.current() {
L_CURLY | L_PAREN | L_BRACK => token_tree(p), T!['{'] | T!['('] | T!['['] => token_tree(p),
R_CURLY => { T!['}'] => {
p.error("unmatched `}`"); p.error("unmatched `}`");
m.complete(p, TOKEN_TREE); m.complete(p, TOKEN_TREE);
return; return;
} }
R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"), T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
_ => p.bump_raw(), _ => p.bump_raw(),
} }
} }

View file

@ -1,22 +1,22 @@
use super::*; use super::*;
pub(super) fn static_def(p: &mut Parser, m: Marker) { pub(super) fn static_def(p: &mut Parser, m: Marker) {
const_or_static(p, m, STATIC_KW, STATIC_DEF) const_or_static(p, m, T![static], STATIC_DEF)
} }
pub(super) fn const_def(p: &mut Parser, m: Marker) { pub(super) fn const_def(p: &mut Parser, m: Marker) {
const_or_static(p, m, CONST_KW, CONST_DEF) const_or_static(p, m, T![const], CONST_DEF)
} }
fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
assert!(p.at(kw)); assert!(p.at(kw));
p.bump(); p.bump();
p.eat(MUT_KW); // FIXME: validator to forbid const mut p.eat(T![mut]); // FIXME: validator to forbid const mut
name(p); name(p);
types::ascription(p); types::ascription(p);
if p.eat(EQ) { if p.eat(T![=]) {
expressions::expr(p); expressions::expr(p);
} }
p.expect(SEMI); p.expect(T![;]);
m.complete(p, def); m.complete(p, def);
} }

View file

@ -1,38 +1,38 @@
use super::*; use super::*;
pub(super) fn struct_def(p: &mut Parser, m: Marker, kind: SyntaxKind) { pub(super) fn struct_def(p: &mut Parser, m: Marker, kind: SyntaxKind) {
assert!(p.at(STRUCT_KW) || p.at_contextual_kw("union")); assert!(p.at(T![struct]) || p.at_contextual_kw("union"));
p.bump_remap(kind); p.bump_remap(kind);
name_r(p, ITEM_RECOVERY_SET); name_r(p, ITEM_RECOVERY_SET);
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
match p.current() { match p.current() {
WHERE_KW => { T![where] => {
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
match p.current() { match p.current() {
SEMI => { T![;] => {
p.bump(); p.bump();
} }
L_CURLY => named_field_def_list(p), T!['{'] => named_field_def_list(p),
_ => { _ => {
//FIXME: special case `(` error message //FIXME: special case `(` error message
p.error("expected `;` or `{`"); p.error("expected `;` or `{`");
} }
} }
} }
SEMI if kind == STRUCT_KW => { T![;] if kind == T![struct] => {
p.bump(); p.bump();
} }
L_CURLY => named_field_def_list(p), T!['{'] => named_field_def_list(p),
L_PAREN if kind == STRUCT_KW => { T!['('] if kind == T![struct] => {
pos_field_def_list(p); pos_field_def_list(p);
// test tuple_struct_where // test tuple_struct_where
// struct Test<T>(T) where T: Clone; // struct Test<T>(T) where T: Clone;
// struct Test<T>(T); // struct Test<T>(T);
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
p.expect(SEMI); p.expect(T![;]);
} }
_ if kind == STRUCT_KW => { _ if kind == T![struct] => {
p.error("expected `;`, `{`, or `(`"); p.error("expected `;`, `{`, or `(`");
} }
_ => { _ => {
@ -43,12 +43,12 @@ pub(super) fn struct_def(p: &mut Parser, m: Marker, kind: SyntaxKind) {
} }
pub(super) fn enum_def(p: &mut Parser, m: Marker) { pub(super) fn enum_def(p: &mut Parser, m: Marker) {
assert!(p.at(ENUM_KW)); assert!(p.at(T![enum]));
p.bump(); p.bump();
name_r(p, ITEM_RECOVERY_SET); name_r(p, ITEM_RECOVERY_SET);
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
if p.at(L_CURLY) { if p.at(T!['{']) {
enum_variant_list(p); enum_variant_list(p);
} else { } else {
p.error("expected `{`") p.error("expected `{`")
@ -57,11 +57,11 @@ pub(super) fn enum_def(p: &mut Parser, m: Marker) {
} }
pub(crate) fn enum_variant_list(p: &mut Parser) { pub(crate) fn enum_variant_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
if p.at(L_CURLY) { if p.at(T!['{']) {
error_block(p, "expected enum variant"); error_block(p, "expected enum variant");
continue; continue;
} }
@ -70,9 +70,9 @@ pub(crate) fn enum_variant_list(p: &mut Parser) {
if p.at(IDENT) { if p.at(IDENT) {
name(p); name(p);
match p.current() { match p.current() {
L_CURLY => named_field_def_list(p), T!['{'] => named_field_def_list(p),
L_PAREN => pos_field_def_list(p), T!['('] => pos_field_def_list(p),
EQ => { T![=] => {
p.bump(); p.bump();
expressions::expr(p); expressions::expr(p);
} }
@ -83,29 +83,29 @@ pub(crate) fn enum_variant_list(p: &mut Parser) {
var.abandon(p); var.abandon(p);
p.err_and_bump("expected enum variant"); p.err_and_bump("expected enum variant");
} }
if !p.at(R_CURLY) { if !p.at(T!['}']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, ENUM_VARIANT_LIST); m.complete(p, ENUM_VARIANT_LIST);
} }
pub(crate) fn named_field_def_list(p: &mut Parser) { pub(crate) fn named_field_def_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(R_CURLY) && !p.at(EOF) { while !p.at(T!['}']) && !p.at(EOF) {
if p.at(L_CURLY) { if p.at(T!['{']) {
error_block(p, "expected field"); error_block(p, "expected field");
continue; continue;
} }
named_field_def(p); named_field_def(p);
if !p.at(R_CURLY) { if !p.at(T!['}']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, NAMED_FIELD_DEF_LIST); m.complete(p, NAMED_FIELD_DEF_LIST);
fn named_field_def(p: &mut Parser) { fn named_field_def(p: &mut Parser) {
@ -119,7 +119,7 @@ pub(crate) fn named_field_def_list(p: &mut Parser) {
opt_visibility(p); opt_visibility(p);
if p.at(IDENT) { if p.at(IDENT) {
name(p); name(p);
p.expect(COLON); p.expect(T![:]);
types::type_(p); types::type_(p);
m.complete(p, NAMED_FIELD_DEF); m.complete(p, NAMED_FIELD_DEF);
} else { } else {
@ -130,12 +130,12 @@ pub(crate) fn named_field_def_list(p: &mut Parser) {
} }
fn pos_field_def_list(p: &mut Parser) { fn pos_field_def_list(p: &mut Parser) {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
let m = p.start(); let m = p.start();
if !p.expect(L_PAREN) { if !p.expect(T!['(']) {
return; return;
} }
while !p.at(R_PAREN) && !p.at(EOF) { while !p.at(T![')']) && !p.at(EOF) {
let m = p.start(); let m = p.start();
// test pos_field_attrs // test pos_field_attrs
// struct S ( // struct S (
@ -156,10 +156,10 @@ fn pos_field_def_list(p: &mut Parser) {
types::type_(p); types::type_(p);
m.complete(p, POS_FIELD_DEF); m.complete(p, POS_FIELD_DEF);
if !p.at(R_PAREN) { if !p.at(T![')']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_PAREN); p.expect(T![')']);
m.complete(p, POS_FIELD_DEF_LIST); m.complete(p, POS_FIELD_DEF_LIST);
} }

View file

@ -4,15 +4,15 @@ use super::*;
// trait T<U>: Hash + Clone where U: Copy {} // trait T<U>: Hash + Clone where U: Copy {}
// trait X<U: Debug + Display>: Hash + Clone where U: Copy {} // trait X<U: Debug + Display>: Hash + Clone where U: Copy {}
pub(super) fn trait_def(p: &mut Parser) { pub(super) fn trait_def(p: &mut Parser) {
assert!(p.at(TRAIT_KW)); assert!(p.at(T![trait]));
p.bump(); p.bump();
name_r(p, ITEM_RECOVERY_SET); name_r(p, ITEM_RECOVERY_SET);
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
if p.at(COLON) { if p.at(T![:]) {
type_params::bounds(p); type_params::bounds(p);
} }
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
if p.at(L_CURLY) { if p.at(T!['{']) {
trait_item_list(p); trait_item_list(p);
} else { } else {
p.error("expected `{`"); p.error("expected `{`");
@ -27,24 +27,24 @@ pub(super) fn trait_def(p: &mut Parser) {
// fn bar(&self); // fn bar(&self);
// } // }
pub(crate) fn trait_item_list(p: &mut Parser) { pub(crate) fn trait_item_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
if p.at(L_CURLY) { if p.at(T!['{']) {
error_block(p, "expected an item"); error_block(p, "expected an item");
continue; continue;
} }
item_or_macro(p, true, ItemFlavor::Trait); item_or_macro(p, true, ItemFlavor::Trait);
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, ITEM_LIST); m.complete(p, ITEM_LIST);
} }
// test impl_block // test impl_block
// impl Foo {} // impl Foo {}
pub(super) fn impl_block(p: &mut Parser) { pub(super) fn impl_block(p: &mut Parser) {
assert!(p.at(IMPL_KW)); assert!(p.at(T![impl ]));
p.bump(); p.bump();
if choose_type_params_over_qpath(p) { if choose_type_params_over_qpath(p) {
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
@ -55,13 +55,13 @@ pub(super) fn impl_block(p: &mut Parser) {
// test impl_block_neg // test impl_block_neg
// impl !Send for X {} // impl !Send for X {}
p.eat(EXCL); p.eat(T![!]);
impl_type(p); impl_type(p);
if p.eat(FOR_KW) { if p.eat(T![for]) {
impl_type(p); impl_type(p);
} }
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
if p.at(L_CURLY) { if p.at(T!['{']) {
impl_item_list(p); impl_item_list(p);
} else { } else {
p.error("expected `{`"); p.error("expected `{`");
@ -76,7 +76,7 @@ pub(super) fn impl_block(p: &mut Parser) {
// fn bar(&self) {} // fn bar(&self) {}
// } // }
pub(crate) fn impl_item_list(p: &mut Parser) { pub(crate) fn impl_item_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
// test impl_inner_attributes // test impl_inner_attributes
@ -87,14 +87,14 @@ pub(crate) fn impl_item_list(p: &mut Parser) {
// } // }
attributes::inner_attributes(p); attributes::inner_attributes(p);
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
if p.at(L_CURLY) { if p.at(T!['{']) {
error_block(p, "expected an item"); error_block(p, "expected an item");
continue; continue;
} }
item_or_macro(p, true, ItemFlavor::Mod); item_or_macro(p, true, ItemFlavor::Mod);
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, ITEM_LIST); m.complete(p, ITEM_LIST);
} }
@ -114,14 +114,14 @@ fn choose_type_params_over_qpath(p: &Parser) -> bool {
// we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`) // we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
// because this is what almost always expected in practice, qualified paths in impls // because this is what almost always expected in practice, qualified paths in impls
// (`impl <Type>::AssocTy { ... }`) aren't even allowed by type checker at the moment. // (`impl <Type>::AssocTy { ... }`) aren't even allowed by type checker at the moment.
if !p.at(L_ANGLE) { if !p.at(T![<]) {
return false; return false;
} }
if p.nth(1) == POUND || p.nth(1) == R_ANGLE { if p.nth(1) == T![#] || p.nth(1) == T![>] {
return true; return true;
} }
(p.nth(1) == LIFETIME || p.nth(1) == IDENT) (p.nth(1) == LIFETIME || p.nth(1) == IDENT)
&& (p.nth(2) == R_ANGLE || p.nth(2) == COMMA || p.nth(2) == COLON || p.nth(2) == EQ) && (p.nth(2) == T![>] || p.nth(2) == T![,] || p.nth(2) == T![:] || p.nth(2) == T![=])
} }
// test_err impl_type // test_err impl_type
@ -130,7 +130,7 @@ fn choose_type_params_over_qpath(p: &Parser) -> bool {
// impl impl NotType {} // impl impl NotType {}
// impl Trait2 for impl NotType {} // impl Trait2 for impl NotType {}
pub(crate) fn impl_type(p: &mut Parser) { pub(crate) fn impl_type(p: &mut Parser) {
if p.at(IMPL_KW) { if p.at(T![impl ]) {
p.error("expected trait or type"); p.error("expected trait or type");
return; return;
} }

View file

@ -1,10 +1,10 @@
use super::*; use super::*;
pub(super) fn use_item(p: &mut Parser, m: Marker) { pub(super) fn use_item(p: &mut Parser, m: Marker) {
assert!(p.at(USE_KW)); assert!(p.at(T![use]));
p.bump(); p.bump();
use_tree(p); use_tree(p);
p.expect(SEMI); p.expect(T![;]);
m.complete(p, USE_ITEM); m.complete(p, USE_ITEM);
} }
@ -28,8 +28,8 @@ fn use_tree(p: &mut Parser) {
// use ::*; // use ::*;
// use some::path::{*}; // use some::path::{*};
// use some::path::{::*}; // use some::path::{::*};
(STAR, _) => p.bump(), (T![*], _) => p.bump(),
(COLONCOLON, STAR) => { (T![::], T![*]) => {
// Parse `use ::*;`, which imports all from the crate root in Rust 2015 // Parse `use ::*;`, which imports all from the crate root in Rust 2015
// This is invalid inside a use_tree_list, (e.g. `use some::path::{::*}`) // This is invalid inside a use_tree_list, (e.g. `use some::path::{::*}`)
// but still parses and errors later: ('crate root in paths can only be used in start position') // but still parses and errors later: ('crate root in paths can only be used in start position')
@ -47,8 +47,8 @@ fn use_tree(p: &mut Parser) {
// use {path::from::root}; // Rust 2015 // use {path::from::root}; // Rust 2015
// use ::{some::arbritrary::path}; // Rust 2015 // use ::{some::arbritrary::path}; // Rust 2015
// use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig // use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig
(L_CURLY, _) | (COLONCOLON, L_CURLY) => { (T!['{'], _) | (T![::], T!['{']) => {
if p.at(COLONCOLON) { if p.at(T![::]) {
p.bump(); p.bump();
} }
use_tree_list(p); use_tree_list(p);
@ -68,7 +68,7 @@ fn use_tree(p: &mut Parser) {
_ if paths::is_path_start(p) => { _ if paths::is_path_start(p) => {
paths::use_path(p); paths::use_path(p);
match p.current() { match p.current() {
AS_KW => { T![as] => {
// test use_alias // test use_alias
// use some::path as some_name; // use some::path as some_name;
// use some::{ // use some::{
@ -80,16 +80,16 @@ fn use_tree(p: &mut Parser) {
// use Trait as _; // use Trait as _;
opt_alias(p); opt_alias(p);
} }
COLONCOLON => { T![::] => {
p.bump(); p.bump();
match p.current() { match p.current() {
STAR => { T![*] => {
p.bump(); p.bump();
} }
// test use_tree_list_after_path // test use_tree_list_after_path
// use crate::{Item}; // use crate::{Item};
// use self::{Item}; // use self::{Item};
L_CURLY => use_tree_list(p), T!['{'] => use_tree_list(p),
_ => { _ => {
// is this unreachable? // is this unreachable?
p.error("expected `{` or `*`"); p.error("expected `{` or `*`");
@ -109,15 +109,15 @@ fn use_tree(p: &mut Parser) {
} }
pub(crate) fn use_tree_list(p: &mut Parser) { pub(crate) fn use_tree_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
use_tree(p); use_tree(p);
if !p.at(R_CURLY) { if !p.at(T!['}']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, USE_TREE_LIST); m.complete(p, USE_TREE_LIST);
} }

View file

@ -36,27 +36,27 @@ impl Flavor {
} }
fn list_(p: &mut Parser, flavor: Flavor) { fn list_(p: &mut Parser, flavor: Flavor) {
let (bra, ket) = if flavor.type_required() { (L_PAREN, R_PAREN) } else { (PIPE, PIPE) }; let (bra, ket) = if flavor.type_required() { (T!['('], T![')']) } else { (T![|], T![|]) };
assert!(p.at(bra)); assert!(p.at(bra));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
if flavor.type_required() { if flavor.type_required() {
opt_self_param(p); opt_self_param(p);
} }
while !p.at(EOF) && !p.at(ket) && !(flavor.type_required() && p.at(DOTDOTDOT)) { while !p.at(EOF) && !p.at(ket) && !(flavor.type_required() && p.at(T![...])) {
if !p.at_ts(VALUE_PARAMETER_FIRST) { if !p.at_ts(VALUE_PARAMETER_FIRST) {
p.error("expected value parameter"); p.error("expected value parameter");
break; break;
} }
value_parameter(p, flavor); value_parameter(p, flavor);
if !p.at(ket) { if !p.at(ket) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
// test param_list_vararg // test param_list_vararg
// extern "C" { fn printf(format: *const i8, ...) -> i32; } // extern "C" { fn printf(format: *const i8, ...) -> i32; }
if flavor.type_required() { if flavor.type_required() {
p.eat(DOTDOTDOT); p.eat(T![...]);
} }
p.expect(ket); p.expect(ket);
m.complete(p, PARAM_LIST); m.complete(p, PARAM_LIST);
@ -69,7 +69,7 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) {
match flavor { match flavor {
Flavor::OptionalType | Flavor::Normal => { Flavor::OptionalType | Flavor::Normal => {
patterns::pattern(p); patterns::pattern(p);
if p.at(COLON) || flavor.type_required() { if p.at(T![:]) || flavor.type_required() {
types::ascription(p) types::ascription(p)
} }
} }
@ -85,10 +85,10 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) {
// trait Foo { // trait Foo {
// fn bar(_: u64, mut x: i32); // fn bar(_: u64, mut x: i32);
// } // }
if (la0 == IDENT || la0 == UNDERSCORE) && la1 == COLON if (la0 == IDENT || la0 == T![_]) && la1 == T![:]
|| la0 == MUT_KW && la1 == IDENT && la2 == COLON || la0 == T![mut] && la1 == IDENT && la2 == T![:]
|| la0 == AMP && la1 == IDENT && la2 == COLON || la0 == T![&] && la1 == IDENT && la2 == T![:]
|| la0 == AMP && la1 == MUT_KW && la2 == IDENT && la3 == COLON || la0 == T![&] && la1 == T![mut] && la2 == IDENT && la3 == T![:]
{ {
patterns::pattern(p); patterns::pattern(p);
types::ascription(p); types::ascription(p);
@ -110,16 +110,16 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) {
// } // }
fn opt_self_param(p: &mut Parser) { fn opt_self_param(p: &mut Parser) {
let m; let m;
if p.at(SELF_KW) || p.at(MUT_KW) && p.nth(1) == SELF_KW { if p.at(T![self]) || p.at(T![mut]) && p.nth(1) == T![self] {
m = p.start(); m = p.start();
p.eat(MUT_KW); p.eat(T![mut]);
p.eat(SELF_KW); p.eat(T![self]);
// test arb_self_types // test arb_self_types
// impl S { // impl S {
// fn a(self: &Self) {} // fn a(self: &Self) {}
// fn b(mut self: Box<Self>) {} // fn b(mut self: Box<Self>) {}
// } // }
if p.at(COLON) { if p.at(T![:]) {
types::ascription(p); types::ascription(p);
} }
} else { } else {
@ -127,10 +127,10 @@ fn opt_self_param(p: &mut Parser) {
let la2 = p.nth(2); let la2 = p.nth(2);
let la3 = p.nth(3); let la3 = p.nth(3);
let n_toks = match (p.current(), la1, la2, la3) { let n_toks = match (p.current(), la1, la2, la3) {
(AMP, SELF_KW, _, _) => 2, (T![&], T![self], _, _) => 2,
(AMP, MUT_KW, SELF_KW, _) => 3, (T![&], T![mut], T![self], _) => 3,
(AMP, LIFETIME, SELF_KW, _) => 3, (T![&], LIFETIME, T![self], _) => 3,
(AMP, LIFETIME, MUT_KW, SELF_KW) => 4, (T![&], LIFETIME, T![mut], T![self]) => 4,
_ => return, _ => return,
}; };
m = p.start(); m = p.start();
@ -139,7 +139,7 @@ fn opt_self_param(p: &mut Parser) {
} }
} }
m.complete(p, SELF_PARAM); m.complete(p, SELF_PARAM);
if !p.at(R_PAREN) { if !p.at(T![')']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }

View file

@ -5,7 +5,7 @@ pub(super) const PATH_FIRST: TokenSet =
pub(super) fn is_path_start(p: &Parser) -> bool { pub(super) fn is_path_start(p: &Parser) -> bool {
match p.current() { match p.current() {
IDENT | SELF_KW | SUPER_KW | CRATE_KW | COLONCOLON => true, IDENT | T![self] | T![super] | T![crate] | T![::] => true,
_ => false, _ => false,
} }
} }
@ -35,10 +35,10 @@ fn path(p: &mut Parser, mode: Mode) {
let mut qual = path.complete(p, PATH); let mut qual = path.complete(p, PATH);
loop { loop {
let use_tree = match p.nth(1) { let use_tree = match p.nth(1) {
STAR | L_CURLY => true, T![*] | T!['{'] => true,
_ => false, _ => false,
}; };
if p.at(COLONCOLON) && !use_tree { if p.at(T![::]) && !use_tree {
let path = qual.precede(p); let path = qual.precede(p);
p.bump(); p.bump();
path_segment(p, mode, false); path_segment(p, mode, false);
@ -55,19 +55,19 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
// test qual_paths // test qual_paths
// type X = <A as B>::Output; // type X = <A as B>::Output;
// fn foo() { <usize as Default>::default(); } // fn foo() { <usize as Default>::default(); }
if first && p.eat(L_ANGLE) { if first && p.eat(T![<]) {
types::type_(p); types::type_(p);
if p.eat(AS_KW) { if p.eat(T![as]) {
if is_path_start(p) { if is_path_start(p) {
types::path_type(p); types::path_type(p);
} else { } else {
p.error("expected a trait"); p.error("expected a trait");
} }
} }
p.expect(R_ANGLE); p.expect(T![>]);
} else { } else {
if first { if first {
p.eat(COLONCOLON); p.eat(T![::]);
} }
match p.current() { match p.current() {
IDENT => { IDENT => {
@ -76,7 +76,7 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
} }
// test crate_path // test crate_path
// use crate::foo; // use crate::foo;
SELF_KW | SUPER_KW | CRATE_KW => p.bump(), T![self] | T![super] | T![crate] => p.bump(),
_ => { _ => {
p.err_recover("expected identifier", items::ITEM_RECOVERY_SET); p.err_recover("expected identifier", items::ITEM_RECOVERY_SET);
} }
@ -91,7 +91,7 @@ fn opt_path_type_args(p: &mut Parser, mode: Mode) {
Mode::Type => { Mode::Type => {
// test path_fn_trait_args // test path_fn_trait_args
// type F = Box<Fn(x: i32) -> ()>; // type F = Box<Fn(x: i32) -> ()>;
if p.at(L_PAREN) { if p.at(T!['(']) {
params::param_list_opt_patterns(p); params::param_list_opt_patterns(p);
opt_fn_ret_type(p); opt_fn_ret_type(p);
} else { } else {

View file

@ -16,10 +16,10 @@ pub(super) fn pattern_list(p: &mut Parser) {
/// Parses a pattern list separated by pipes `|` /// Parses a pattern list separated by pipes `|`
/// using the given `recovery_set` /// using the given `recovery_set`
pub(super) fn pattern_list_r(p: &mut Parser, recovery_set: TokenSet) { pub(super) fn pattern_list_r(p: &mut Parser, recovery_set: TokenSet) {
p.eat(PIPE); p.eat(T![|]);
pattern_r(p, recovery_set); pattern_r(p, recovery_set);
while p.eat(PIPE) { while p.eat(T![|]) {
pattern_r(p, recovery_set); pattern_r(p, recovery_set);
} }
} }
@ -34,7 +34,7 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) {
// 200 .. 301=> (), // 200 .. 301=> (),
// } // }
// } // }
if p.at(DOTDOTDOT) || p.at(DOTDOTEQ) || p.at(DOTDOT) { if p.at(T![...]) || p.at(T![..=]) || p.at(T![..]) {
let m = lhs.precede(p); let m = lhs.precede(p);
p.bump(); p.bump();
atom_pat(p, recovery_set); atom_pat(p, recovery_set);
@ -44,7 +44,7 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) {
// fn main() { // fn main() {
// let m!(x) = 0; // let m!(x) = 0;
// } // }
else if lhs.kind() == PATH_PAT && p.at(EXCL) { else if lhs.kind() == PATH_PAT && p.at(T![!]) {
let m = lhs.precede(p); let m = lhs.precede(p);
items::macro_call_after_excl(p); items::macro_call_after_excl(p);
m.complete(p, MACRO_CALL); m.complete(p, MACRO_CALL);
@ -58,9 +58,9 @@ const PAT_RECOVERY_SET: TokenSet =
fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> { fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
let la0 = p.nth(0); let la0 = p.nth(0);
let la1 = p.nth(1); let la1 = p.nth(1);
if la0 == REF_KW if la0 == T![ref]
|| la0 == MUT_KW || la0 == T![mut]
|| (la0 == IDENT && !(la1 == COLONCOLON || la1 == L_PAREN || la1 == L_CURLY || la1 == EXCL)) || (la0 == IDENT && !(la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!]))
{ {
return Some(bind_pat(p, true)); return Some(bind_pat(p, true));
} }
@ -73,10 +73,10 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
} }
let m = match la0 { let m = match la0 {
UNDERSCORE => placeholder_pat(p), T![_] => placeholder_pat(p),
AMP => ref_pat(p), T![&] => ref_pat(p),
L_PAREN => tuple_pat(p), T!['('] => tuple_pat(p),
L_BRACK => slice_pat(p), T!['['] => slice_pat(p),
_ => { _ => {
p.err_recover("expected pattern", recovery_set); p.err_recover("expected pattern", recovery_set);
return None; return None;
@ -86,7 +86,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
} }
fn is_literal_pat_start(p: &mut Parser) -> bool { fn is_literal_pat_start(p: &mut Parser) -> bool {
p.at(MINUS) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER) p.at(T![-]) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER)
|| p.at_ts(expressions::LITERAL_FIRST) || p.at_ts(expressions::LITERAL_FIRST)
} }
@ -102,7 +102,7 @@ fn is_literal_pat_start(p: &mut Parser) -> bool {
fn literal_pat(p: &mut Parser) -> CompletedMarker { fn literal_pat(p: &mut Parser) -> CompletedMarker {
assert!(is_literal_pat_start(p)); assert!(is_literal_pat_start(p));
let m = p.start(); let m = p.start();
if p.at(MINUS) { if p.at(T![-]) {
p.bump(); p.bump();
} }
expressions::literal(p); expressions::literal(p);
@ -121,11 +121,11 @@ fn path_pat(p: &mut Parser) -> CompletedMarker {
let m = p.start(); let m = p.start();
paths::expr_path(p); paths::expr_path(p);
let kind = match p.current() { let kind = match p.current() {
L_PAREN => { T!['('] => {
tuple_pat_fields(p); tuple_pat_fields(p);
TUPLE_STRUCT_PAT TUPLE_STRUCT_PAT
} }
L_CURLY => { T!['{'] => {
field_pat_list(p); field_pat_list(p);
STRUCT_PAT STRUCT_PAT
} }
@ -142,10 +142,10 @@ fn path_pat(p: &mut Parser) -> CompletedMarker {
// let S(_, .. , x) = (); // let S(_, .. , x) = ();
// } // }
fn tuple_pat_fields(p: &mut Parser) { fn tuple_pat_fields(p: &mut Parser) {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
p.bump(); p.bump();
pat_list(p, R_PAREN); pat_list(p, T![')']);
p.expect(R_PAREN); p.expect(T![')']);
} }
// test field_pat_list // test field_pat_list
@ -156,29 +156,29 @@ fn tuple_pat_fields(p: &mut Parser) {
// let S { h: _, } = (); // let S { h: _, } = ();
// } // }
fn field_pat_list(p: &mut Parser) { fn field_pat_list(p: &mut Parser) {
assert!(p.at(L_CURLY)); assert!(p.at(T!['{']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(R_CURLY) { while !p.at(EOF) && !p.at(T!['}']) {
match p.current() { match p.current() {
DOTDOT => p.bump(), T![..] => p.bump(),
IDENT if p.nth(1) == COLON => field_pat(p), IDENT if p.nth(1) == T![:] => field_pat(p),
L_CURLY => error_block(p, "expected ident"), T!['{'] => error_block(p, "expected ident"),
_ => { _ => {
bind_pat(p, false); bind_pat(p, false);
} }
} }
if !p.at(R_CURLY) { if !p.at(T!['}']) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
p.expect(R_CURLY); p.expect(T!['}']);
m.complete(p, FIELD_PAT_LIST); m.complete(p, FIELD_PAT_LIST);
} }
fn field_pat(p: &mut Parser) { fn field_pat(p: &mut Parser) {
assert!(p.at(IDENT)); assert!(p.at(IDENT));
assert!(p.nth(1) == COLON); assert!(p.nth(1) == T![:]);
let m = p.start(); let m = p.start();
name(p); name(p);
@ -190,7 +190,7 @@ fn field_pat(p: &mut Parser) {
// test placeholder_pat // test placeholder_pat
// fn main() { let _ = (); } // fn main() { let _ = (); }
fn placeholder_pat(p: &mut Parser) -> CompletedMarker { fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(UNDERSCORE)); assert!(p.at(T![_]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
m.complete(p, PLACEHOLDER_PAT) m.complete(p, PLACEHOLDER_PAT)
@ -202,10 +202,10 @@ fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
// let &mut b = (); // let &mut b = ();
// } // }
fn ref_pat(p: &mut Parser) -> CompletedMarker { fn ref_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(AMP)); assert!(p.at(T![&]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
p.eat(MUT_KW); p.eat(T![mut]);
pattern(p); pattern(p);
m.complete(p, REF_PAT) m.complete(p, REF_PAT)
} }
@ -215,7 +215,7 @@ fn ref_pat(p: &mut Parser) -> CompletedMarker {
// let (a, b, ..) = (); // let (a, b, ..) = ();
// } // }
fn tuple_pat(p: &mut Parser) -> CompletedMarker { fn tuple_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
let m = p.start(); let m = p.start();
tuple_pat_fields(p); tuple_pat_fields(p);
m.complete(p, TUPLE_PAT) m.complete(p, TUPLE_PAT)
@ -226,18 +226,18 @@ fn tuple_pat(p: &mut Parser) -> CompletedMarker {
// let [a, b, ..] = []; // let [a, b, ..] = [];
// } // }
fn slice_pat(p: &mut Parser) -> CompletedMarker { fn slice_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_BRACK)); assert!(p.at(T!['[']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
pat_list(p, R_BRACK); pat_list(p, T![']']);
p.expect(R_BRACK); p.expect(T![']']);
m.complete(p, SLICE_PAT) m.complete(p, SLICE_PAT)
} }
fn pat_list(p: &mut Parser, ket: SyntaxKind) { fn pat_list(p: &mut Parser, ket: SyntaxKind) {
while !p.at(EOF) && !p.at(ket) { while !p.at(EOF) && !p.at(ket) {
match p.current() { match p.current() {
DOTDOT => p.bump(), T![..] => p.bump(),
_ => { _ => {
if !p.at_ts(PATTERN_FIRST) { if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern"); p.error("expected a pattern");
@ -247,7 +247,7 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) {
} }
} }
if !p.at(ket) { if !p.at(ket) {
p.expect(COMMA); p.expect(T![,]);
} }
} }
} }
@ -263,10 +263,10 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) {
// } // }
fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker { fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker {
let m = p.start(); let m = p.start();
p.eat(REF_KW); p.eat(T![ref]);
p.eat(MUT_KW); p.eat(T![mut]);
name(p); name(p);
if with_at && p.eat(AT) { if with_at && p.eat(T![@]) {
pattern(p); pattern(p);
} }
m.complete(p, BIND_PAT) m.complete(p, BIND_PAT)

View file

@ -3,26 +3,26 @@ use super::*;
pub(super) fn opt_type_arg_list(p: &mut Parser, colon_colon_required: bool) { pub(super) fn opt_type_arg_list(p: &mut Parser, colon_colon_required: bool) {
let m; let m;
match (colon_colon_required, p.nth(0), p.nth(1)) { match (colon_colon_required, p.nth(0), p.nth(1)) {
(_, COLONCOLON, L_ANGLE) => { (_, T![::], T![<]) => {
m = p.start(); m = p.start();
p.bump(); p.bump();
p.bump(); p.bump();
} }
(false, L_ANGLE, EQ) => return, (false, T![<], T![=]) => return,
(false, L_ANGLE, _) => { (false, T![<], _) => {
m = p.start(); m = p.start();
p.bump(); p.bump();
} }
_ => return, _ => return,
}; };
while !p.at(EOF) && !p.at(R_ANGLE) { while !p.at(EOF) && !p.at(T![>]) {
type_arg(p); type_arg(p);
if !p.at(R_ANGLE) && !p.expect(COMMA) { if !p.at(T![>]) && !p.expect(T![,]) {
break; break;
} }
} }
p.expect(R_ANGLE); p.expect(T![>]);
m.complete(p, TYPE_ARG_LIST); m.complete(p, TYPE_ARG_LIST);
} }
@ -35,7 +35,7 @@ fn type_arg(p: &mut Parser) {
p.bump(); p.bump();
m.complete(p, LIFETIME_ARG); m.complete(p, LIFETIME_ARG);
} }
IDENT if p.nth(1) == EQ => { IDENT if p.nth(1) == T![=] => {
name_ref(p); name_ref(p);
p.bump(); p.bump();
types::type_(p); types::type_(p);

View file

@ -1,18 +1,18 @@
use super::*; use super::*;
pub(super) fn opt_type_param_list(p: &mut Parser) { pub(super) fn opt_type_param_list(p: &mut Parser) {
if !p.at(L_ANGLE) { if !p.at(T![<]) {
return; return;
} }
type_param_list(p); type_param_list(p);
} }
fn type_param_list(p: &mut Parser) { fn type_param_list(p: &mut Parser) {
assert!(p.at(L_ANGLE)); assert!(p.at(T![<]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
while !p.at(EOF) && !p.at(R_ANGLE) { while !p.at(EOF) && !p.at(T![>]) {
let m = p.start(); let m = p.start();
// test generic_lifetime_type_attribute // test generic_lifetime_type_attribute
@ -28,18 +28,18 @@ fn type_param_list(p: &mut Parser) {
p.err_and_bump("expected type parameter") p.err_and_bump("expected type parameter")
} }
} }
if !p.at(R_ANGLE) && !p.expect(COMMA) { if !p.at(T![>]) && !p.expect(T![,]) {
break; break;
} }
} }
p.expect(R_ANGLE); p.expect(T![>]);
m.complete(p, TYPE_PARAM_LIST); m.complete(p, TYPE_PARAM_LIST);
} }
fn lifetime_param(p: &mut Parser, m: Marker) { fn lifetime_param(p: &mut Parser, m: Marker) {
assert!(p.at(LIFETIME)); assert!(p.at(LIFETIME));
p.bump(); p.bump();
if p.at(COLON) { if p.at(T![:]) {
lifetime_bounds(p); lifetime_bounds(p);
} }
m.complete(p, LIFETIME_PARAM); m.complete(p, LIFETIME_PARAM);
@ -48,12 +48,12 @@ fn lifetime_param(p: &mut Parser, m: Marker) {
fn type_param(p: &mut Parser, m: Marker) { fn type_param(p: &mut Parser, m: Marker) {
assert!(p.at(IDENT)); assert!(p.at(IDENT));
name(p); name(p);
if p.at(COLON) { if p.at(T![:]) {
bounds(p); bounds(p);
} }
// test type_param_default // test type_param_default
// struct S<T = i32>; // struct S<T = i32>;
if p.at(EQ) { if p.at(T![=]) {
p.bump(); p.bump();
types::type_(p) types::type_(p)
} }
@ -63,17 +63,17 @@ fn type_param(p: &mut Parser, m: Marker) {
// test type_param_bounds // test type_param_bounds
// struct S<T: 'a + ?Sized + (Copy)>; // struct S<T: 'a + ?Sized + (Copy)>;
pub(super) fn bounds(p: &mut Parser) { pub(super) fn bounds(p: &mut Parser) {
assert!(p.at(COLON)); assert!(p.at(T![:]));
p.bump(); p.bump();
bounds_without_colon(p); bounds_without_colon(p);
} }
fn lifetime_bounds(p: &mut Parser) { fn lifetime_bounds(p: &mut Parser) {
assert!(p.at(COLON)); assert!(p.at(T![:]));
p.bump(); p.bump();
while p.at(LIFETIME) { while p.at(LIFETIME) {
p.bump(); p.bump();
if !p.eat(PLUS) { if !p.eat(T![+]) {
break; break;
} }
} }
@ -81,7 +81,7 @@ fn lifetime_bounds(p: &mut Parser) {
pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker { pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker {
while type_bound(p) { while type_bound(p) {
if !p.eat(PLUS) { if !p.eat(T![+]) {
break; break;
} }
} }
@ -96,11 +96,11 @@ pub(super) fn bounds_without_colon(p: &mut Parser) {
fn type_bound(p: &mut Parser) -> bool { fn type_bound(p: &mut Parser) -> bool {
let m = p.start(); let m = p.start();
let has_paren = p.eat(L_PAREN); let has_paren = p.eat(T!['(']);
p.eat(QUESTION); p.eat(T![?]);
match p.current() { match p.current() {
LIFETIME => p.bump(), LIFETIME => p.bump(),
FOR_KW => types::for_type(p), T![for] => types::for_type(p),
_ if paths::is_path_start(p) => types::path_type_(p, false), _ if paths::is_path_start(p) => types::path_type_(p, false),
_ => { _ => {
m.abandon(p); m.abandon(p);
@ -108,7 +108,7 @@ fn type_bound(p: &mut Parser) -> bool {
} }
} }
if has_paren { if has_paren {
p.expect(R_PAREN); p.expect(T![')']);
} }
m.complete(p, TYPE_BOUND); m.complete(p, TYPE_BOUND);
@ -124,7 +124,7 @@ fn type_bound(p: &mut Parser) -> bool {
// <T as Iterator>::Item: 'a // <T as Iterator>::Item: 'a
// {} // {}
pub(super) fn opt_where_clause(p: &mut Parser) { pub(super) fn opt_where_clause(p: &mut Parser) {
if !p.at(WHERE_KW) { if !p.at(T![where]) {
return; return;
} }
let m = p.start(); let m = p.start();
@ -133,7 +133,7 @@ pub(super) fn opt_where_clause(p: &mut Parser) {
while is_where_predicate(p) { while is_where_predicate(p) {
where_predicate(p); where_predicate(p);
let comma = p.eat(COMMA); let comma = p.eat(T![,]);
if is_where_clause_end(p) { if is_where_clause_end(p) {
break; break;
@ -150,13 +150,13 @@ pub(super) fn opt_where_clause(p: &mut Parser) {
fn is_where_predicate(p: &mut Parser) -> bool { fn is_where_predicate(p: &mut Parser) -> bool {
match p.current() { match p.current() {
LIFETIME => true, LIFETIME => true,
IMPL_KW => false, T![impl ] => false,
token => types::TYPE_FIRST.contains(token), token => types::TYPE_FIRST.contains(token),
} }
} }
fn is_where_clause_end(p: &mut Parser) -> bool { fn is_where_clause_end(p: &mut Parser) -> bool {
p.current() == L_CURLY || p.current() == SEMI || p.current() == EQ p.current() == T!['{'] || p.current() == T![;] || p.current() == T![=]
} }
fn where_predicate(p: &mut Parser) { fn where_predicate(p: &mut Parser) {
@ -164,13 +164,13 @@ fn where_predicate(p: &mut Parser) {
match p.current() { match p.current() {
LIFETIME => { LIFETIME => {
p.bump(); p.bump();
if p.at(COLON) { if p.at(T![:]) {
bounds(p); bounds(p);
} else { } else {
p.error("expected colon"); p.error("expected colon");
} }
} }
IMPL_KW => { T![impl ] => {
p.error("expected lifetime or type"); p.error("expected lifetime or type");
} }
_ => { _ => {
@ -181,7 +181,7 @@ fn where_predicate(p: &mut Parser) {
// { } // { }
types::type_(p); types::type_(p);
if p.at(COLON) { if p.at(T![:]) {
bounds(p); bounds(p);
} else { } else {
p.error("expected colon"); p.error("expected colon");

View file

@ -17,18 +17,18 @@ pub(super) fn type_no_bounds(p: &mut Parser) {
fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) { fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) {
match p.current() { match p.current() {
L_PAREN => paren_or_tuple_type(p), T!['('] => paren_or_tuple_type(p),
EXCL => never_type(p), T![!] => never_type(p),
STAR => pointer_type(p), T![*] => pointer_type(p),
L_BRACK => array_or_slice_type(p), T!['['] => array_or_slice_type(p),
AMP => reference_type(p), T![&] => reference_type(p),
UNDERSCORE => placeholder_type(p), T![_] => placeholder_type(p),
FN_KW | UNSAFE_KW | EXTERN_KW => fn_pointer_type(p), T![fn] | T![unsafe] | T![extern] => fn_pointer_type(p),
FOR_KW => for_type(p), T![for] => for_type(p),
IMPL_KW => impl_trait_type(p), T![impl ] => impl_trait_type(p),
DYN_KW => dyn_trait_type(p), T![dyn ] => dyn_trait_type(p),
// Some path types are not allowed to have bounds (no plus) // Some path types are not allowed to have bounds (no plus)
L_ANGLE => path_type_(p, allow_bounds), T![<] => path_type_(p, allow_bounds),
_ if paths::is_path_start(p) => path_or_macro_type_(p, allow_bounds), _ if paths::is_path_start(p) => path_or_macro_type_(p, allow_bounds),
_ => { _ => {
p.err_recover("expected type", TYPE_RECOVERY_SET); p.err_recover("expected type", TYPE_RECOVERY_SET);
@ -37,27 +37,27 @@ fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) {
} }
pub(super) fn ascription(p: &mut Parser) { pub(super) fn ascription(p: &mut Parser) {
p.expect(COLON); p.expect(T![:]);
type_(p) type_(p)
} }
fn paren_or_tuple_type(p: &mut Parser) { fn paren_or_tuple_type(p: &mut Parser) {
assert!(p.at(L_PAREN)); assert!(p.at(T!['(']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
let mut n_types: u32 = 0; let mut n_types: u32 = 0;
let mut trailing_comma: bool = false; let mut trailing_comma: bool = false;
while !p.at(EOF) && !p.at(R_PAREN) { while !p.at(EOF) && !p.at(T![')']) {
n_types += 1; n_types += 1;
type_(p); type_(p);
if p.eat(COMMA) { if p.eat(T![,]) {
trailing_comma = true; trailing_comma = true;
} else { } else {
trailing_comma = false; trailing_comma = false;
break; break;
} }
} }
p.expect(R_PAREN); p.expect(T![')']);
let kind = if n_types == 1 && !trailing_comma { let kind = if n_types == 1 && !trailing_comma {
// test paren_type // test paren_type
@ -77,14 +77,14 @@ fn paren_or_tuple_type(p: &mut Parser) {
// test never_type // test never_type
// type Never = !; // type Never = !;
fn never_type(p: &mut Parser) { fn never_type(p: &mut Parser) {
assert!(p.at(EXCL)); assert!(p.at(T![!]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
m.complete(p, NEVER_TYPE); m.complete(p, NEVER_TYPE);
} }
fn pointer_type(p: &mut Parser) { fn pointer_type(p: &mut Parser) {
assert!(p.at(STAR)); assert!(p.at(T![*]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
@ -92,7 +92,7 @@ fn pointer_type(p: &mut Parser) {
// test pointer_type_mut // test pointer_type_mut
// type M = *mut (); // type M = *mut ();
// type C = *mut (); // type C = *mut ();
MUT_KW | CONST_KW => p.bump(), T![mut] | T![const] => p.bump(),
_ => { _ => {
// test_err pointer_type_no_mutability // test_err pointer_type_no_mutability
// type T = *(); // type T = *();
@ -108,7 +108,7 @@ fn pointer_type(p: &mut Parser) {
} }
fn array_or_slice_type(p: &mut Parser) { fn array_or_slice_type(p: &mut Parser) {
assert!(p.at(L_BRACK)); assert!(p.at(T!['[']));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
@ -116,17 +116,17 @@ fn array_or_slice_type(p: &mut Parser) {
let kind = match p.current() { let kind = match p.current() {
// test slice_type // test slice_type
// type T = [()]; // type T = [()];
R_BRACK => { T![']'] => {
p.bump(); p.bump();
SLICE_TYPE SLICE_TYPE
} }
// test array_type // test array_type
// type T = [(); 92]; // type T = [(); 92];
SEMI => { T![;] => {
p.bump(); p.bump();
expressions::expr(p); expressions::expr(p);
p.expect(R_BRACK); p.expect(T![']']);
ARRAY_TYPE ARRAY_TYPE
} }
// test_err array_type_missing_semi // test_err array_type_missing_semi
@ -144,11 +144,11 @@ fn array_or_slice_type(p: &mut Parser) {
// type B = &'static (); // type B = &'static ();
// type C = &mut (); // type C = &mut ();
fn reference_type(p: &mut Parser) { fn reference_type(p: &mut Parser) {
assert!(p.at(AMP)); assert!(p.at(T![&]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
p.eat(LIFETIME); p.eat(LIFETIME);
p.eat(MUT_KW); p.eat(T![mut]);
type_no_bounds(p); type_no_bounds(p);
m.complete(p, REFERENCE_TYPE); m.complete(p, REFERENCE_TYPE);
} }
@ -156,7 +156,7 @@ fn reference_type(p: &mut Parser) {
// test placeholder_type // test placeholder_type
// type Placeholder = _; // type Placeholder = _;
fn placeholder_type(p: &mut Parser) { fn placeholder_type(p: &mut Parser) {
assert!(p.at(UNDERSCORE)); assert!(p.at(T![_]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
m.complete(p, PLACEHOLDER_TYPE); m.complete(p, PLACEHOLDER_TYPE);
@ -169,18 +169,18 @@ fn placeholder_type(p: &mut Parser) {
// type D = extern "C" fn ( u8 , ... ) -> u8; // type D = extern "C" fn ( u8 , ... ) -> u8;
fn fn_pointer_type(p: &mut Parser) { fn fn_pointer_type(p: &mut Parser) {
let m = p.start(); let m = p.start();
p.eat(UNSAFE_KW); p.eat(T![unsafe]);
if p.at(EXTERN_KW) { if p.at(T![extern]) {
abi(p); abi(p);
} }
// test_err fn_pointer_type_missing_fn // test_err fn_pointer_type_missing_fn
// type F = unsafe (); // type F = unsafe ();
if !p.eat(FN_KW) { if !p.eat(T![fn]) {
m.abandon(p); m.abandon(p);
p.error("expected `fn`"); p.error("expected `fn`");
return; return;
} }
if p.at(L_PAREN) { if p.at(T!['(']) {
params::param_list_opt_patterns(p); params::param_list_opt_patterns(p);
} else { } else {
p.error("expected parameters") p.error("expected parameters")
@ -192,9 +192,9 @@ fn fn_pointer_type(p: &mut Parser) {
} }
pub(super) fn for_binder(p: &mut Parser) { pub(super) fn for_binder(p: &mut Parser) {
assert!(p.at(FOR_KW)); assert!(p.at(T![for]));
p.bump(); p.bump();
if p.at(L_ANGLE) { if p.at(T![<]) {
type_params::opt_type_param_list(p); type_params::opt_type_param_list(p);
} else { } else {
p.error("expected `<`"); p.error("expected `<`");
@ -206,12 +206,12 @@ pub(super) fn for_binder(p: &mut Parser) {
// fn foo<T>(_t: &T) where for<'a> &'a T: Iterator {} // fn foo<T>(_t: &T) where for<'a> &'a T: Iterator {}
// fn bar<T>(_t: &T) where for<'a> &'a mut T: Iterator {} // fn bar<T>(_t: &T) where for<'a> &'a mut T: Iterator {}
pub(super) fn for_type(p: &mut Parser) { pub(super) fn for_type(p: &mut Parser) {
assert!(p.at(FOR_KW)); assert!(p.at(T![for]));
let m = p.start(); let m = p.start();
for_binder(p); for_binder(p);
match p.current() { match p.current() {
FN_KW | UNSAFE_KW | EXTERN_KW => fn_pointer_type(p), T![fn] | T![unsafe] | T![extern] => fn_pointer_type(p),
AMP => reference_type(p), T![&] => reference_type(p),
_ if paths::is_path_start(p) => path_type_(p, false), _ if paths::is_path_start(p) => path_type_(p, false),
_ => p.error("expected a path"), _ => p.error("expected a path"),
} }
@ -221,7 +221,7 @@ pub(super) fn for_type(p: &mut Parser) {
// test impl_trait_type // test impl_trait_type
// type A = impl Iterator<Item=Foo<'a>> + 'a; // type A = impl Iterator<Item=Foo<'a>> + 'a;
fn impl_trait_type(p: &mut Parser) { fn impl_trait_type(p: &mut Parser) {
assert!(p.at(IMPL_KW)); assert!(p.at(T![impl ]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
type_params::bounds_without_colon(p); type_params::bounds_without_colon(p);
@ -231,7 +231,7 @@ fn impl_trait_type(p: &mut Parser) {
// test dyn_trait_type // test dyn_trait_type
// type A = dyn Iterator<Item=Foo<'a>> + 'a; // type A = dyn Iterator<Item=Foo<'a>> + 'a;
fn dyn_trait_type(p: &mut Parser) { fn dyn_trait_type(p: &mut Parser) {
assert!(p.at(DYN_KW)); assert!(p.at(T![dyn ]));
let m = p.start(); let m = p.start();
p.bump(); p.bump();
type_params::bounds_without_colon(p); type_params::bounds_without_colon(p);
@ -251,11 +251,11 @@ pub(super) fn path_type(p: &mut Parser) {
// type A = foo!(); // type A = foo!();
// type B = crate::foo!(); // type B = crate::foo!();
fn path_or_macro_type_(p: &mut Parser, allow_bounds: bool) { fn path_or_macro_type_(p: &mut Parser, allow_bounds: bool) {
assert!(paths::is_path_start(p) || p.at(L_ANGLE)); assert!(paths::is_path_start(p) || p.at(T![<]));
let m = p.start(); let m = p.start();
paths::type_path(p); paths::type_path(p);
let kind = if p.at(EXCL) { let kind = if p.at(T![!]) {
items::macro_call_after_excl(p); items::macro_call_after_excl(p);
MACRO_CALL MACRO_CALL
} else { } else {
@ -270,7 +270,7 @@ fn path_or_macro_type_(p: &mut Parser, allow_bounds: bool) {
} }
pub(super) fn path_type_(p: &mut Parser, allow_bounds: bool) { pub(super) fn path_type_(p: &mut Parser, allow_bounds: bool) {
assert!(paths::is_path_start(p) || p.at(L_ANGLE)); assert!(paths::is_path_start(p) || p.at(T![<]));
let m = p.start(); let m = p.start();
paths::type_path(p); paths::type_path(p);
@ -286,7 +286,7 @@ pub(super) fn path_type_(p: &mut Parser, allow_bounds: bool) {
/// This turns a parsed PATH_TYPE optionally into a DYN_TRAIT_TYPE /// This turns a parsed PATH_TYPE optionally into a DYN_TRAIT_TYPE
/// with a TYPE_BOUND_LIST /// with a TYPE_BOUND_LIST
fn opt_path_type_bounds_as_dyn_trait_type(p: &mut Parser, path_type_marker: CompletedMarker) { fn opt_path_type_bounds_as_dyn_trait_type(p: &mut Parser, path_type_marker: CompletedMarker) {
if !p.at(PLUS) { if !p.at(T![+]) {
return; return;
} }
@ -298,7 +298,7 @@ fn opt_path_type_bounds_as_dyn_trait_type(p: &mut Parser, path_type_marker: Comp
// This gets consumed here so it gets properly set // This gets consumed here so it gets properly set
// in the TYPE_BOUND_LIST // in the TYPE_BOUND_LIST
p.eat(PLUS); p.eat(T![+]);
// Parse rest of the bounds into the TYPE_BOUND_LIST // Parse rest of the bounds into the TYPE_BOUND_LIST
let m = type_params::bounds_without_colon_m(p, m); let m = type_params::bounds_without_colon_m(p, m);

View file

@ -6,6 +6,7 @@ use crate::{
SyntaxKind::{self, ERROR, EOF, TOMBSTONE}, SyntaxKind::{self, ERROR, EOF, TOMBSTONE},
TokenSource, ParseError, TokenSet, TokenSource, ParseError, TokenSet,
event::Event, event::Event,
T
}; };
/// `Parser` struct provides the low-level API for /// `Parser` struct provides the low-level API for
@ -155,10 +156,10 @@ impl<'t> Parser<'t> {
// Handle parser composites // Handle parser composites
match kind { match kind {
DOTDOTDOT | DOTDOTEQ => { T![...] | T![..=] => {
self.bump_compound(kind, 3); self.bump_compound(kind, 3);
} }
DOTDOT | COLONCOLON | EQEQ | FAT_ARROW | NEQ | THIN_ARROW => { T![..] | T![::] | T![==] | T![=>] | T![!=] | T![->] => {
self.bump_compound(kind, 2); self.bump_compound(kind, 2);
} }
_ => { _ => {
@ -223,7 +224,7 @@ impl<'t> Parser<'t> {
/// Create an error node and consume the next token. /// Create an error node and consume the next token.
pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) { pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) {
if self.at(SyntaxKind::L_CURLY) || self.at(SyntaxKind::R_CURLY) || self.at_ts(recovery) { if self.at(T!['{']) || self.at(T!['}']) || self.at_ts(recovery) {
self.error(message); self.error(message);
} else { } else {
let m = self.start(); let m = self.start();
@ -253,19 +254,17 @@ impl<'t> Parser<'t> {
let jn2 = self.token_source.is_token_joint_to_next(self.token_pos + n + 1); let jn2 = self.token_source.is_token_joint_to_next(self.token_pos + n + 1);
let la3 = self.token_source.token_kind(self.token_pos + n + 2); let la3 = self.token_source.token_kind(self.token_pos + n + 2);
use SyntaxKind::*;
match kind { match kind {
DOT if jn1 && la2 == DOT && jn2 && la3 == DOT => Some((DOTDOTDOT, 3)), T![.] if jn1 && la2 == T![.] && jn2 && la3 == T![.] => Some((T![...], 3)),
DOT if jn1 && la2 == DOT && la3 == EQ => Some((DOTDOTEQ, 3)), T![.] if jn1 && la2 == T![.] && la3 == T![=] => Some((T![..=], 3)),
DOT if jn1 && la2 == DOT => Some((DOTDOT, 2)), T![.] if jn1 && la2 == T![.] => Some((T![..], 2)),
COLON if jn1 && la2 == COLON => Some((COLONCOLON, 2)), T![:] if jn1 && la2 == T![:] => Some((T![::], 2)),
EQ if jn1 && la2 == EQ => Some((EQEQ, 2)), T![=] if jn1 && la2 == T![=] => Some((T![==], 2)),
EQ if jn1 && la2 == R_ANGLE => Some((FAT_ARROW, 2)), T![=] if jn1 && la2 == T![>] => Some((T![=>], 2)),
EXCL if la2 == EQ => Some((NEQ, 2)), T![!] if la2 == T![=] => Some((T![!=], 2)),
MINUS if la2 == R_ANGLE => Some((THIN_ARROW, 2)), T![-] if la2 == T![>] => Some((T![->], 2)),
_ => None, _ => None,
} }
} }

View file

@ -3,7 +3,8 @@
use crate::{ use crate::{
SyntaxToken, SyntaxElement, SmolStr, SyntaxToken, SyntaxElement, SmolStr,
ast::{self, AstNode, AstChildren, children, child_opt}, ast::{self, AstNode, AstChildren, children, child_opt},
SyntaxKind::* SyntaxKind::*,
T
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -34,7 +35,7 @@ impl ast::IfExpr {
impl ast::RefExpr { impl ast::RefExpr {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
} }
} }
@ -51,9 +52,9 @@ pub enum PrefixOp {
impl ast::PrefixExpr { impl ast::PrefixExpr {
pub fn op_kind(&self) -> Option<PrefixOp> { pub fn op_kind(&self) -> Option<PrefixOp> {
match self.op_token()?.kind() { match self.op_token()?.kind() {
STAR => Some(PrefixOp::Deref), T![*] => Some(PrefixOp::Deref),
EXCL => Some(PrefixOp::Not), T![!] => Some(PrefixOp::Not),
MINUS => Some(PrefixOp::Neg), T![-] => Some(PrefixOp::Neg),
_ => None, _ => None,
} }
} }
@ -133,37 +134,37 @@ impl ast::BinExpr {
fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { fn op_details(&self) -> Option<(SyntaxToken, BinOp)> {
self.syntax().children_with_tokens().filter_map(|it| it.as_token()).find_map(|c| { self.syntax().children_with_tokens().filter_map(|it| it.as_token()).find_map(|c| {
match c.kind() { match c.kind() {
PIPEPIPE => Some((c, BinOp::BooleanOr)), T![||] => Some((c, BinOp::BooleanOr)),
AMPAMP => Some((c, BinOp::BooleanAnd)), T![&&] => Some((c, BinOp::BooleanAnd)),
EQEQ => Some((c, BinOp::EqualityTest)), T![==] => Some((c, BinOp::EqualityTest)),
NEQ => Some((c, BinOp::NegatedEqualityTest)), T![!=] => Some((c, BinOp::NegatedEqualityTest)),
LTEQ => Some((c, BinOp::LesserEqualTest)), T![<=] => Some((c, BinOp::LesserEqualTest)),
GTEQ => Some((c, BinOp::GreaterEqualTest)), T![>=] => Some((c, BinOp::GreaterEqualTest)),
L_ANGLE => Some((c, BinOp::LesserTest)), T![<] => Some((c, BinOp::LesserTest)),
R_ANGLE => Some((c, BinOp::GreaterTest)), T![>] => Some((c, BinOp::GreaterTest)),
PLUS => Some((c, BinOp::Addition)), T![+] => Some((c, BinOp::Addition)),
STAR => Some((c, BinOp::Multiplication)), T![*] => Some((c, BinOp::Multiplication)),
MINUS => Some((c, BinOp::Subtraction)), T![-] => Some((c, BinOp::Subtraction)),
SLASH => Some((c, BinOp::Division)), T![/] => Some((c, BinOp::Division)),
PERCENT => Some((c, BinOp::Remainder)), T![%] => Some((c, BinOp::Remainder)),
SHL => Some((c, BinOp::LeftShift)), T![<<] => Some((c, BinOp::LeftShift)),
SHR => Some((c, BinOp::RightShift)), T![>>] => Some((c, BinOp::RightShift)),
CARET => Some((c, BinOp::BitwiseXor)), T![^] => Some((c, BinOp::BitwiseXor)),
PIPE => Some((c, BinOp::BitwiseOr)), T![|] => Some((c, BinOp::BitwiseOr)),
AMP => Some((c, BinOp::BitwiseAnd)), T![&] => Some((c, BinOp::BitwiseAnd)),
DOTDOT => Some((c, BinOp::RangeRightOpen)), T![..] => Some((c, BinOp::RangeRightOpen)),
DOTDOTEQ => Some((c, BinOp::RangeRightClosed)), T![..=] => Some((c, BinOp::RangeRightClosed)),
EQ => Some((c, BinOp::Assignment)), T![=] => Some((c, BinOp::Assignment)),
PLUSEQ => Some((c, BinOp::AddAssign)), T![+=] => Some((c, BinOp::AddAssign)),
SLASHEQ => Some((c, BinOp::DivAssign)), T![/=] => Some((c, BinOp::DivAssign)),
STAREQ => Some((c, BinOp::MulAssign)), T![*=] => Some((c, BinOp::MulAssign)),
PERCENTEQ => Some((c, BinOp::RemAssign)), T![%=] => Some((c, BinOp::RemAssign)),
SHREQ => Some((c, BinOp::ShrAssign)), T![>>=] => Some((c, BinOp::ShrAssign)),
SHLEQ => Some((c, BinOp::ShlAssign)), T![<<=] => Some((c, BinOp::ShlAssign)),
MINUSEQ => Some((c, BinOp::SubAssign)), T![-=] => Some((c, BinOp::SubAssign)),
PIPEEQ => Some((c, BinOp::BitOrAssign)), T![|=] => Some((c, BinOp::BitOrAssign)),
AMPEQ => Some((c, BinOp::BitAndAssign)), T![&=] => Some((c, BinOp::BitAndAssign)),
CARETEQ => Some((c, BinOp::BitXorAssign)), T![^=] => Some((c, BinOp::BitXorAssign)),
_ => None, _ => None,
} }
}) })
@ -211,7 +212,7 @@ impl ast::ArrayExpr {
} }
fn is_repeat(&self) -> bool { fn is_repeat(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == SEMI) self.syntax().children_with_tokens().any(|it| it.kind() == T![;])
} }
} }
@ -258,7 +259,7 @@ impl ast::Literal {
LiteralKind::FloatNumber { suffix: suffix } LiteralKind::FloatNumber { suffix: suffix }
} }
STRING | RAW_STRING => LiteralKind::String, STRING | RAW_STRING => LiteralKind::String,
TRUE_KW | FALSE_KW => LiteralKind::Bool, T![true] | T![false] => LiteralKind::Bool,
BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString, BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString,
CHAR => LiteralKind::Char, CHAR => LiteralKind::Char,
BYTE => LiteralKind::Byte, BYTE => LiteralKind::Byte,

View file

@ -3,7 +3,7 @@
use itertools::Itertools; use itertools::Itertools;
use crate::{SmolStr, SyntaxToken, ast::{self, AstNode, children, child_opt}, SyntaxKind::*, SyntaxElement}; use crate::{SmolStr, SyntaxToken, ast::{self, AstNode, children, child_opt}, SyntaxKind::*, SyntaxElement, T};
use ra_parser::SyntaxKind; use ra_parser::SyntaxKind;
impl ast::Name { impl ast::Name {
@ -32,7 +32,7 @@ impl ast::Attr {
Some(prev) => prev, Some(prev) => prev,
}; };
prev.kind() == EXCL prev.kind() == T![!]
} }
pub fn as_atom(&self) -> Option<SmolStr> { pub fn as_atom(&self) -> Option<SmolStr> {
@ -102,9 +102,9 @@ impl ast::PathSegment {
PathSegmentKind::Name(name_ref) PathSegmentKind::Name(name_ref)
} else { } else {
match self.syntax().first_child_or_token()?.kind() { match self.syntax().first_child_or_token()?.kind() {
SELF_KW => PathSegmentKind::SelfKw, T![self] => PathSegmentKind::SelfKw,
SUPER_KW => PathSegmentKind::SuperKw, T![super] => PathSegmentKind::SuperKw,
CRATE_KW => PathSegmentKind::CrateKw, T![crate] => PathSegmentKind::CrateKw,
_ => return None, _ => return None,
} }
}; };
@ -113,7 +113,7 @@ impl ast::PathSegment {
pub fn has_colon_colon(&self) -> bool { pub fn has_colon_colon(&self) -> bool {
match self.syntax.first_child_or_token().map(|s| s.kind()) { match self.syntax.first_child_or_token().map(|s| s.kind()) {
Some(COLONCOLON) => true, Some(T![::]) => true,
_ => false, _ => false,
} }
} }
@ -129,14 +129,14 @@ impl ast::Module {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() { match self.syntax().last_child_or_token() {
None => false, None => false,
Some(node) => node.kind() == SEMI, Some(node) => node.kind() == T![;],
} }
} }
} }
impl ast::UseTree { impl ast::UseTree {
pub fn has_star(&self) -> bool { pub fn has_star(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == STAR) self.syntax().children_with_tokens().any(|it| it.kind() == T![*])
} }
} }
@ -172,7 +172,7 @@ impl ast::ImplBlock {
} }
pub fn is_negative(&self) -> bool { pub fn is_negative(&self) -> bool {
self.syntax().children_with_tokens().any(|t| t.kind() == EXCL) self.syntax().children_with_tokens().any(|t| t.kind() == T![!])
} }
} }
@ -219,7 +219,7 @@ impl ast::FnDef {
self.syntax() self.syntax()
.last_child_or_token() .last_child_or_token()
.and_then(|it| it.as_token()) .and_then(|it| it.as_token())
.filter(|it| it.kind() == SEMI) .filter(|it| it.kind() == T![;])
} }
} }
@ -227,7 +227,7 @@ impl ast::LetStmt {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() { match self.syntax().last_child_or_token() {
None => false, None => false,
Some(node) => node.kind() == SEMI, Some(node) => node.kind() == T![;],
} }
} }
} }
@ -236,7 +236,7 @@ impl ast::ExprStmt {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() { match self.syntax().last_child_or_token() {
None => false, None => false,
Some(node) => node.kind() == SEMI, Some(node) => node.kind() == T![;],
} }
} }
} }
@ -270,29 +270,29 @@ impl ast::FieldExpr {
impl ast::RefPat { impl ast::RefPat {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
} }
} }
impl ast::BindPat { impl ast::BindPat {
pub fn is_mutable(&self) -> bool { pub fn is_mutable(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
} }
pub fn is_ref(&self) -> bool { pub fn is_ref(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == REF_KW) self.syntax().children_with_tokens().any(|n| n.kind() == T![ref])
} }
} }
impl ast::PointerType { impl ast::PointerType {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
} }
} }
impl ast::ReferenceType { impl ast::ReferenceType {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
} }
} }
@ -311,19 +311,19 @@ impl ast::SelfParam {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.as_token()) .filter_map(|it| it.as_token())
.find(|it| it.kind() == SELF_KW) .find(|it| it.kind() == T![self])
.expect("invalid tree: self param must have self") .expect("invalid tree: self param must have self")
} }
pub fn kind(&self) -> SelfParamKind { pub fn kind(&self) -> SelfParamKind {
let borrowed = self.syntax().children_with_tokens().any(|n| n.kind() == AMP); let borrowed = self.syntax().children_with_tokens().any(|n| n.kind() == T![&]);
if borrowed { if borrowed {
// check for a `mut` coming after the & -- `mut &self` != `&mut self` // check for a `mut` coming after the & -- `mut &self` != `&mut self`
if self if self
.syntax() .syntax()
.children_with_tokens() .children_with_tokens()
.skip_while(|n| n.kind() != AMP) .skip_while(|n| n.kind() != T![&])
.any(|n| n.kind() == MUT_KW) .any(|n| n.kind() == T![mut])
{ {
SelfParamKind::MutRef SelfParamKind::MutRef
} else { } else {
@ -355,6 +355,6 @@ impl ast::WherePred {
impl ast::TraitDef { impl ast::TraitDef {
pub fn is_auto(&self) -> bool { pub fn is_auto(&self) -> bool {
self.syntax().children_with_tokens().any(|t| t.kind() == AUTO_KW) self.syntax().children_with_tokens().any(|t| t.kind() == T![auto])
} }
} }

View file

@ -179,10 +179,7 @@ fn api_walkthrough() {
// There's a bunch of traversal methods on `SyntaxNode`: // There's a bunch of traversal methods on `SyntaxNode`:
assert_eq!(expr_syntax.parent(), Some(block.syntax())); assert_eq!(expr_syntax.parent(), Some(block.syntax()));
assert_eq!( assert_eq!(block.syntax().first_child_or_token().map(|it| it.kind()), Some(T!['{']));
block.syntax().first_child_or_token().map(|it| it.kind()),
Some(SyntaxKind::L_CURLY)
);
assert_eq!( assert_eq!(
expr_syntax.next_sibling_or_token().map(|it| it.kind()), expr_syntax.next_sibling_or_token().map(|it| it.kind()),
Some(SyntaxKind::WHITESPACE) Some(SyntaxKind::WHITESPACE)
@ -191,9 +188,7 @@ fn api_walkthrough() {
// As well as some iterator helpers: // As well as some iterator helpers:
let f = expr_syntax.ancestors().find_map(ast::FnDef::cast); let f = expr_syntax.ancestors().find_map(ast::FnDef::cast);
assert_eq!(f, Some(&*func)); assert_eq!(f, Some(&*func));
assert!(expr_syntax assert!(expr_syntax.siblings_with_tokens(Direction::Next).any(|it| it.kind() == T!['}']));
.siblings_with_tokens(Direction::Next)
.any(|it| it.kind() == SyntaxKind::R_CURLY));
assert_eq!( assert_eq!(
expr_syntax.descendants_with_tokens().count(), expr_syntax.descendants_with_tokens().count(),
8, // 5 tokens `1`, ` `, `+`, ` `, `!` 8, // 5 tokens `1`, ` `, `+`, ` `, `!`

View file

@ -7,6 +7,7 @@ mod strings;
use crate::{ use crate::{
SyntaxKind::{self, *}, SyntaxKind::{self, *},
TextUnit, TextUnit,
T,
}; };
use self::{ use self::{
@ -90,16 +91,16 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
match c { match c {
// Possiblily multi-byte tokens, // Possiblily multi-byte tokens,
// but we only produce single byte token now // but we only produce single byte token now
// DOTDOTDOT, DOTDOT, DOTDOTEQ, DOT // T![...], T![..], T![..=], T![.]
'.' => return DOT, '.' => return T![.],
// COLONCOLON COLON // T![::] T![:]
':' => return COLON, ':' => return T![:],
// EQEQ FATARROW EQ // T![==] FATARROW T![=]
'=' => return EQ, '=' => return T![=],
// NEQ EXCL // T![!=] T![!]
'!' => return EXCL, '!' => return T![!],
// THIN_ARROW MINUS // T![->] T![-]
'-' => return MINUS, '-' => return T![-],
// If the character is an ident start not followed by another single // If the character is an ident start not followed by another single
// quote, then this is a lifetime name: // quote, then this is a lifetime name:
@ -148,8 +149,8 @@ fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind {
ptr.bump(); ptr.bump();
true true
} }
('_', None) => return UNDERSCORE, ('_', None) => return T![_],
('_', Some(c)) if !is_ident_continue(c) => return UNDERSCORE, ('_', Some(c)) if !is_ident_continue(c) => return T![_],
_ => false, _ => false,
}; };
ptr.bump_while(is_ident_continue); ptr.bump_while(is_ident_continue);

View file

@ -17,7 +17,8 @@ use crate::{
text_token_source::TextTokenSource, text_token_source::TextTokenSource,
text_tree_sink::TextTreeSink, text_tree_sink::TextTreeSink,
lexer::{tokenize, Token}, lexer::{tokenize, Token},
} },
T,
}; };
pub(crate) fn incremental_reparse( pub(crate) fn incremental_reparse(
@ -122,16 +123,16 @@ fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxN
fn is_balanced(tokens: &[Token]) -> bool { fn is_balanced(tokens: &[Token]) -> bool {
if tokens.is_empty() if tokens.is_empty()
|| tokens.first().unwrap().kind != L_CURLY || tokens.first().unwrap().kind != T!['{']
|| tokens.last().unwrap().kind != R_CURLY || tokens.last().unwrap().kind != T!['}']
{ {
return false; return false;
} }
let mut balance = 0usize; let mut balance = 0usize;
for t in &tokens[1..tokens.len() - 1] { for t in &tokens[1..tokens.len() - 1] {
match t.kind { match t.kind {
L_CURLY => balance += 1, T!['{'] => balance += 1,
R_CURLY => { T!['}'] => {
balance = match balance.checked_sub(1) { balance = match balance.checked_sub(1) {
Some(b) => b, Some(b) => b,
None => return false, None => return false,

View file

@ -5,9 +5,10 @@ mod field_expr;
use crate::{ use crate::{
SourceFile, SyntaxError, AstNode, SyntaxNode, TextUnit, SourceFile, SyntaxError, AstNode, SyntaxNode, TextUnit,
SyntaxKind::{L_CURLY, R_CURLY, BYTE, BYTE_STRING, STRING, CHAR}, SyntaxKind::{BYTE, BYTE_STRING, STRING, CHAR},
ast, ast,
algo::visit::{visitor_ctx, VisitorCtx}, algo::visit::{visitor_ctx, VisitorCtx},
T,
}; };
pub(crate) use unescape::EscapeError; pub(crate) use unescape::EscapeError;
@ -83,8 +84,8 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) {
let mut stack = Vec::new(); let mut stack = Vec::new();
for node in root.descendants() { for node in root.descendants() {
match node.kind() { match node.kind() {
L_CURLY => stack.push(node), T!['{'] => stack.push(node),
R_CURLY => { T!['}'] => {
if let Some(pair) = stack.pop() { if let Some(pair) = stack.pop() {
assert_eq!( assert_eq!(
node.parent(), node.parent(),