From 4cbc902fcc9de79893779582dac01351d1137c7f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 Dec 2018 19:30:35 +0300 Subject: grand module rename --- crates/ra_syntax/src/algo.rs | 26 ++ crates/ra_syntax/src/algo/mod.rs | 26 -- crates/ra_syntax/src/ast.rs | 365 +++++++++++++++++++ crates/ra_syntax/src/ast/mod.rs | 365 ------------------- crates/ra_syntax/src/grammar.rs | 184 ++++++++++ crates/ra_syntax/src/grammar/expressions.rs | 455 ++++++++++++++++++++++++ crates/ra_syntax/src/grammar/expressions/mod.rs | 455 ------------------------ crates/ra_syntax/src/grammar/items.rs | 394 ++++++++++++++++++++ crates/ra_syntax/src/grammar/items/mod.rs | 394 -------------------- crates/ra_syntax/src/grammar/mod.rs | 184 ---------- crates/ra_syntax/src/lexer.rs | 213 +++++++++++ crates/ra_syntax/src/lexer/mod.rs | 213 ----------- crates/ra_syntax/src/parser_impl.rs | 198 +++++++++++ crates/ra_syntax/src/parser_impl/mod.rs | 198 ----------- crates/ra_syntax/src/string_lexing.rs | 13 + crates/ra_syntax/src/string_lexing/mod.rs | 13 - crates/ra_syntax/src/syntax_kinds.rs | 26 ++ crates/ra_syntax/src/syntax_kinds/mod.rs | 26 -- crates/ra_syntax/src/validation.rs | 24 ++ crates/ra_syntax/src/validation/mod.rs | 24 -- crates/ra_syntax/src/yellow.rs | 161 +++++++++ crates/ra_syntax/src/yellow/mod.rs | 161 --------- 22 files changed, 2059 insertions(+), 2059 deletions(-) create mode 100644 crates/ra_syntax/src/algo.rs delete mode 100644 crates/ra_syntax/src/algo/mod.rs create mode 100644 crates/ra_syntax/src/ast.rs delete mode 100644 crates/ra_syntax/src/ast/mod.rs create mode 100644 crates/ra_syntax/src/grammar.rs create mode 100644 crates/ra_syntax/src/grammar/expressions.rs delete mode 100644 crates/ra_syntax/src/grammar/expressions/mod.rs create mode 100644 crates/ra_syntax/src/grammar/items.rs delete mode 100644 crates/ra_syntax/src/grammar/items/mod.rs delete mode 100644 crates/ra_syntax/src/grammar/mod.rs create mode 100644 crates/ra_syntax/src/lexer.rs delete mode 100644 crates/ra_syntax/src/lexer/mod.rs create mode 100644 crates/ra_syntax/src/parser_impl.rs delete mode 100644 crates/ra_syntax/src/parser_impl/mod.rs create mode 100644 crates/ra_syntax/src/string_lexing.rs delete mode 100644 crates/ra_syntax/src/string_lexing/mod.rs create mode 100644 crates/ra_syntax/src/syntax_kinds.rs delete mode 100644 crates/ra_syntax/src/syntax_kinds/mod.rs create mode 100644 crates/ra_syntax/src/validation.rs delete mode 100644 crates/ra_syntax/src/validation/mod.rs create mode 100644 crates/ra_syntax/src/yellow.rs delete mode 100644 crates/ra_syntax/src/yellow/mod.rs (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs new file mode 100644 index 000000000..4b3548ea9 --- /dev/null +++ b/crates/ra_syntax/src/algo.rs @@ -0,0 +1,26 @@ +pub mod visit; + +use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit}; + +pub use rowan::LeafAtOffset; + +pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { + match node.0.leaf_at_offset(offset) { + LeafAtOffset::None => LeafAtOffset::None, + LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)), + LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)), + } +} + +pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { + SyntaxNode(root.0.covering_node(range)) +} + +pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { + ::itertools::unfold(seed, move |slot| { + slot.take().map(|curr| { + *slot = step(&curr); + curr + }) + }) +} diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs deleted file mode 100644 index 4b3548ea9..000000000 --- a/crates/ra_syntax/src/algo/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -pub mod visit; - -use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit}; - -pub use rowan::LeafAtOffset; - -pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { - match node.0.leaf_at_offset(offset) { - LeafAtOffset::None => LeafAtOffset::None, - LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)), - LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)), - } -} - -pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { - SyntaxNode(root.0.covering_node(range)) -} - -pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { - ::itertools::unfold(seed, move |slot| { - slot.take().map(|curr| { - *slot = step(&curr); - curr - }) - }) -} diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs new file mode 100644 index 000000000..91c67119f --- /dev/null +++ b/crates/ra_syntax/src/ast.rs @@ -0,0 +1,365 @@ +mod generated; + +use std::marker::PhantomData; +use std::string::String as RustString; + +use itertools::Itertools; + +pub use self::generated::*; +use crate::{ + yellow::{RefRoot, SyntaxNodeChildren}, + SmolStr, + SyntaxKind::*, + SyntaxNodeRef, +}; + +/// The main trait to go from untyped `SyntaxNode` to a typed ast. The +/// conversion itself has zero runtime cost: ast and syntax nodes have exactly +/// the same representation: a pointer to the tree root and a pointer to the +/// node itself. +pub trait AstNode<'a>: Clone + Copy + 'a { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option + where + Self: Sized; + fn syntax(self) -> SyntaxNodeRef<'a>; +} + +pub trait NameOwner<'a>: AstNode<'a> { + fn name(self) -> Option> { + child_opt(self) + } +} + +pub trait LoopBodyOwner<'a>: AstNode<'a> { + fn loop_body(self) -> Option> { + child_opt(self) + } +} + +pub trait ArgListOwner<'a>: AstNode<'a> { + fn arg_list(self) -> Option> { + child_opt(self) + } +} + +pub trait FnDefOwner<'a>: AstNode<'a> { + fn functions(self) -> AstChildren<'a, FnDef<'a>> { + children(self) + } +} + +pub trait ModuleItemOwner<'a>: AstNode<'a> { + fn items(self) -> AstChildren<'a, ModuleItem<'a>> { + children(self) + } +} + +pub trait TypeParamsOwner<'a>: AstNode<'a> { + fn type_param_list(self) -> Option> { + child_opt(self) + } + + fn where_clause(self) -> Option> { + child_opt(self) + } +} + +pub trait AttrsOwner<'a>: AstNode<'a> { + fn attrs(self) -> AstChildren<'a, Attr<'a>> { + children(self) + } +} + +pub trait DocCommentsOwner<'a>: AstNode<'a> { + fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { + children(self) + } + + /// Returns the textual content of a doc comment block as a single string. + /// That is, strips leading `///` and joins lines + fn doc_comment_text(self) -> RustString { + self.doc_comments() + .map(|comment| { + let prefix = comment.prefix(); + let trimmed = comment + .text() + .as_str() + .trim() + .trim_start_matches(prefix) + .trim_start(); + trimmed.to_owned() + }) + .join("\n") + } +} + +impl<'a> FnDef<'a> { + pub fn has_atom_attr(&self, atom: &str) -> bool { + self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) + } +} + +impl<'a> Attr<'a> { + pub fn as_atom(&self) -> Option { + let tt = self.value()?; + let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; + if attr.kind() == IDENT { + Some(attr.leaf_text().unwrap().clone()) + } else { + None + } + } + + pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { + let tt = self.value()?; + let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; + let args = TokenTree::cast(args)?; + if attr.kind() == IDENT { + Some((attr.leaf_text().unwrap().clone(), args)) + } else { + None + } + } +} + +impl<'a> Lifetime<'a> { + pub fn text(&self) -> SmolStr { + self.syntax().leaf_text().unwrap().clone() + } +} + +impl<'a> Char<'a> { + pub fn text(&self) -> &SmolStr { + &self.syntax().leaf_text().unwrap() + } +} + +impl<'a> Byte<'a> { + pub fn text(&self) -> &SmolStr { + &self.syntax().leaf_text().unwrap() + } +} + +impl<'a> ByteString<'a> { + pub fn text(&self) -> &SmolStr { + &self.syntax().leaf_text().unwrap() + } +} + +impl<'a> String<'a> { + pub fn text(&self) -> &SmolStr { + &self.syntax().leaf_text().unwrap() + } +} + +impl<'a> Comment<'a> { + pub fn text(&self) -> &SmolStr { + self.syntax().leaf_text().unwrap() + } + + pub fn flavor(&self) -> CommentFlavor { + let text = self.text(); + if text.starts_with("///") { + CommentFlavor::Doc + } else if text.starts_with("//!") { + CommentFlavor::ModuleDoc + } else if text.starts_with("//") { + CommentFlavor::Line + } else { + CommentFlavor::Multiline + } + } + + pub fn prefix(&self) -> &'static str { + self.flavor().prefix() + } + + pub fn count_newlines_lazy(&self) -> impl Iterator { + self.text().chars().filter(|&c| c == '\n').map(|_| &()) + } + + pub fn has_newlines(&self) -> bool { + self.count_newlines_lazy().count() > 0 + } +} + +#[derive(Debug, PartialEq, Eq)] +pub enum CommentFlavor { + Line, + Doc, + ModuleDoc, + Multiline, +} + +impl CommentFlavor { + pub fn prefix(&self) -> &'static str { + use self::CommentFlavor::*; + match *self { + Line => "//", + Doc => "///", + ModuleDoc => "//!", + Multiline => "/*", + } + } +} + +impl<'a> Whitespace<'a> { + pub fn text(&self) -> &SmolStr { + &self.syntax().leaf_text().unwrap() + } + + pub fn count_newlines_lazy(&self) -> impl Iterator { + self.text().chars().filter(|&c| c == '\n').map(|_| &()) + } + + pub fn has_newlines(&self) -> bool { + self.count_newlines_lazy().count() > 0 + } +} + +impl<'a> Name<'a> { + pub fn text(&self) -> SmolStr { + let ident = self.syntax().first_child().unwrap(); + ident.leaf_text().unwrap().clone() + } +} + +impl<'a> NameRef<'a> { + pub fn text(&self) -> SmolStr { + let ident = self.syntax().first_child().unwrap(); + ident.leaf_text().unwrap().clone() + } +} + +impl<'a> ImplItem<'a> { + pub fn target_type(self) -> Option> { + match self.target() { + (Some(t), None) | (_, Some(t)) => Some(t), + _ => None, + } + } + + pub fn target_trait(self) -> Option> { + match self.target() { + (Some(t), Some(_)) => Some(t), + _ => None, + } + } + + fn target(self) -> (Option>, Option>) { + let mut types = children(self); + let first = types.next(); + let second = types.next(); + (first, second) + } +} + +impl<'a> Module<'a> { + pub fn has_semi(self) -> bool { + match self.syntax().last_child() { + None => false, + Some(node) => node.kind() == SEMI, + } + } +} + +impl<'a> LetStmt<'a> { + pub fn has_semi(self) -> bool { + match self.syntax().last_child() { + None => false, + Some(node) => node.kind() == SEMI, + } + } +} + +impl<'a> IfExpr<'a> { + pub fn then_branch(self) -> Option> { + self.blocks().nth(0) + } + pub fn else_branch(self) -> Option> { + self.blocks().nth(1) + } + fn blocks(self) -> AstChildren<'a, Block<'a>> { + children(self) + } +} + +#[derive(Debug, Clone, Copy)] +pub enum PathSegmentKind<'a> { + Name(NameRef<'a>), + SelfKw, + SuperKw, + CrateKw, +} + +impl<'a> PathSegment<'a> { + pub fn parent_path(self) -> Path<'a> { + self.syntax() + .parent() + .and_then(Path::cast) + .expect("segments are always nested in paths") + } + + pub fn kind(self) -> Option> { + let res = if let Some(name_ref) = self.name_ref() { + PathSegmentKind::Name(name_ref) + } else { + match self.syntax().first_child()?.kind() { + SELF_KW => PathSegmentKind::SelfKw, + SUPER_KW => PathSegmentKind::SuperKw, + CRATE_KW => PathSegmentKind::CrateKw, + _ => return None, + } + }; + Some(res) + } +} + +impl<'a> UseTree<'a> { + pub fn has_star(self) -> bool { + self.syntax().children().any(|it| it.kind() == STAR) + } +} + +impl<'a> UseTreeList<'a> { + pub fn parent_use_tree(self) -> UseTree<'a> { + self.syntax() + .parent() + .and_then(UseTree::cast) + .expect("UseTreeLists are always nested in UseTrees") + } +} + +fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option { + children(parent).next() +} + +fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> AstChildren<'a, C> { + AstChildren::new(parent.syntax()) +} + +#[derive(Debug)] +pub struct AstChildren<'a, N> { + inner: SyntaxNodeChildren>, + ph: PhantomData, +} + +impl<'a, N> AstChildren<'a, N> { + fn new(parent: SyntaxNodeRef<'a>) -> Self { + AstChildren { + inner: parent.children(), + ph: PhantomData, + } + } +} + +impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { + type Item = N; + fn next(&mut self) -> Option { + loop { + if let Some(n) = N::cast(self.inner.next()?) { + return Some(n); + } + } + } +} diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs deleted file mode 100644 index 91c67119f..000000000 --- a/crates/ra_syntax/src/ast/mod.rs +++ /dev/null @@ -1,365 +0,0 @@ -mod generated; - -use std::marker::PhantomData; -use std::string::String as RustString; - -use itertools::Itertools; - -pub use self::generated::*; -use crate::{ - yellow::{RefRoot, SyntaxNodeChildren}, - SmolStr, - SyntaxKind::*, - SyntaxNodeRef, -}; - -/// The main trait to go from untyped `SyntaxNode` to a typed ast. The -/// conversion itself has zero runtime cost: ast and syntax nodes have exactly -/// the same representation: a pointer to the tree root and a pointer to the -/// node itself. -pub trait AstNode<'a>: Clone + Copy + 'a { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option - where - Self: Sized; - fn syntax(self) -> SyntaxNodeRef<'a>; -} - -pub trait NameOwner<'a>: AstNode<'a> { - fn name(self) -> Option> { - child_opt(self) - } -} - -pub trait LoopBodyOwner<'a>: AstNode<'a> { - fn loop_body(self) -> Option> { - child_opt(self) - } -} - -pub trait ArgListOwner<'a>: AstNode<'a> { - fn arg_list(self) -> Option> { - child_opt(self) - } -} - -pub trait FnDefOwner<'a>: AstNode<'a> { - fn functions(self) -> AstChildren<'a, FnDef<'a>> { - children(self) - } -} - -pub trait ModuleItemOwner<'a>: AstNode<'a> { - fn items(self) -> AstChildren<'a, ModuleItem<'a>> { - children(self) - } -} - -pub trait TypeParamsOwner<'a>: AstNode<'a> { - fn type_param_list(self) -> Option> { - child_opt(self) - } - - fn where_clause(self) -> Option> { - child_opt(self) - } -} - -pub trait AttrsOwner<'a>: AstNode<'a> { - fn attrs(self) -> AstChildren<'a, Attr<'a>> { - children(self) - } -} - -pub trait DocCommentsOwner<'a>: AstNode<'a> { - fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { - children(self) - } - - /// Returns the textual content of a doc comment block as a single string. - /// That is, strips leading `///` and joins lines - fn doc_comment_text(self) -> RustString { - self.doc_comments() - .map(|comment| { - let prefix = comment.prefix(); - let trimmed = comment - .text() - .as_str() - .trim() - .trim_start_matches(prefix) - .trim_start(); - trimmed.to_owned() - }) - .join("\n") - } -} - -impl<'a> FnDef<'a> { - pub fn has_atom_attr(&self, atom: &str) -> bool { - self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) - } -} - -impl<'a> Attr<'a> { - pub fn as_atom(&self) -> Option { - let tt = self.value()?; - let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; - if attr.kind() == IDENT { - Some(attr.leaf_text().unwrap().clone()) - } else { - None - } - } - - pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { - let tt = self.value()?; - let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; - let args = TokenTree::cast(args)?; - if attr.kind() == IDENT { - Some((attr.leaf_text().unwrap().clone(), args)) - } else { - None - } - } -} - -impl<'a> Lifetime<'a> { - pub fn text(&self) -> SmolStr { - self.syntax().leaf_text().unwrap().clone() - } -} - -impl<'a> Char<'a> { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl<'a> Byte<'a> { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl<'a> ByteString<'a> { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl<'a> String<'a> { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl<'a> Comment<'a> { - pub fn text(&self) -> &SmolStr { - self.syntax().leaf_text().unwrap() - } - - pub fn flavor(&self) -> CommentFlavor { - let text = self.text(); - if text.starts_with("///") { - CommentFlavor::Doc - } else if text.starts_with("//!") { - CommentFlavor::ModuleDoc - } else if text.starts_with("//") { - CommentFlavor::Line - } else { - CommentFlavor::Multiline - } - } - - pub fn prefix(&self) -> &'static str { - self.flavor().prefix() - } - - pub fn count_newlines_lazy(&self) -> impl Iterator { - self.text().chars().filter(|&c| c == '\n').map(|_| &()) - } - - pub fn has_newlines(&self) -> bool { - self.count_newlines_lazy().count() > 0 - } -} - -#[derive(Debug, PartialEq, Eq)] -pub enum CommentFlavor { - Line, - Doc, - ModuleDoc, - Multiline, -} - -impl CommentFlavor { - pub fn prefix(&self) -> &'static str { - use self::CommentFlavor::*; - match *self { - Line => "//", - Doc => "///", - ModuleDoc => "//!", - Multiline => "/*", - } - } -} - -impl<'a> Whitespace<'a> { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } - - pub fn count_newlines_lazy(&self) -> impl Iterator { - self.text().chars().filter(|&c| c == '\n').map(|_| &()) - } - - pub fn has_newlines(&self) -> bool { - self.count_newlines_lazy().count() > 0 - } -} - -impl<'a> Name<'a> { - pub fn text(&self) -> SmolStr { - let ident = self.syntax().first_child().unwrap(); - ident.leaf_text().unwrap().clone() - } -} - -impl<'a> NameRef<'a> { - pub fn text(&self) -> SmolStr { - let ident = self.syntax().first_child().unwrap(); - ident.leaf_text().unwrap().clone() - } -} - -impl<'a> ImplItem<'a> { - pub fn target_type(self) -> Option> { - match self.target() { - (Some(t), None) | (_, Some(t)) => Some(t), - _ => None, - } - } - - pub fn target_trait(self) -> Option> { - match self.target() { - (Some(t), Some(_)) => Some(t), - _ => None, - } - } - - fn target(self) -> (Option>, Option>) { - let mut types = children(self); - let first = types.next(); - let second = types.next(); - (first, second) - } -} - -impl<'a> Module<'a> { - pub fn has_semi(self) -> bool { - match self.syntax().last_child() { - None => false, - Some(node) => node.kind() == SEMI, - } - } -} - -impl<'a> LetStmt<'a> { - pub fn has_semi(self) -> bool { - match self.syntax().last_child() { - None => false, - Some(node) => node.kind() == SEMI, - } - } -} - -impl<'a> IfExpr<'a> { - pub fn then_branch(self) -> Option> { - self.blocks().nth(0) - } - pub fn else_branch(self) -> Option> { - self.blocks().nth(1) - } - fn blocks(self) -> AstChildren<'a, Block<'a>> { - children(self) - } -} - -#[derive(Debug, Clone, Copy)] -pub enum PathSegmentKind<'a> { - Name(NameRef<'a>), - SelfKw, - SuperKw, - CrateKw, -} - -impl<'a> PathSegment<'a> { - pub fn parent_path(self) -> Path<'a> { - self.syntax() - .parent() - .and_then(Path::cast) - .expect("segments are always nested in paths") - } - - pub fn kind(self) -> Option> { - let res = if let Some(name_ref) = self.name_ref() { - PathSegmentKind::Name(name_ref) - } else { - match self.syntax().first_child()?.kind() { - SELF_KW => PathSegmentKind::SelfKw, - SUPER_KW => PathSegmentKind::SuperKw, - CRATE_KW => PathSegmentKind::CrateKw, - _ => return None, - } - }; - Some(res) - } -} - -impl<'a> UseTree<'a> { - pub fn has_star(self) -> bool { - self.syntax().children().any(|it| it.kind() == STAR) - } -} - -impl<'a> UseTreeList<'a> { - pub fn parent_use_tree(self) -> UseTree<'a> { - self.syntax() - .parent() - .and_then(UseTree::cast) - .expect("UseTreeLists are always nested in UseTrees") - } -} - -fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option { - children(parent).next() -} - -fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> AstChildren<'a, C> { - AstChildren::new(parent.syntax()) -} - -#[derive(Debug)] -pub struct AstChildren<'a, N> { - inner: SyntaxNodeChildren>, - ph: PhantomData, -} - -impl<'a, N> AstChildren<'a, N> { - fn new(parent: SyntaxNodeRef<'a>) -> Self { - AstChildren { - inner: parent.children(), - ph: PhantomData, - } - } -} - -impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { - type Item = N; - fn next(&mut self) -> Option { - loop { - if let Some(n) = N::cast(self.inner.next()?) { - return Some(n); - } - } - } -} diff --git a/crates/ra_syntax/src/grammar.rs b/crates/ra_syntax/src/grammar.rs new file mode 100644 index 000000000..06a37d648 --- /dev/null +++ b/crates/ra_syntax/src/grammar.rs @@ -0,0 +1,184 @@ +//! This is the actual "grammar" of the Rust language. +//! +//! Each function in this module and its children corresponds +//! to a production of the format grammar. Submodules roughly +//! correspond to different *areas* of the grammar. By convention, +//! each submodule starts with `use super::*` import and exports +//! "public" productions via `pub(super)`. +//! +//! See docs for `Parser` to learn about API, available to the grammar, +//! and see docs for `Event` to learn how this actually manages to +//! produce parse trees. +//! +//! Code in this module also contains inline tests, which start with +//! `// test name-of-the-test` comment and look like this: +//! +//! ``` +//! // test function_with_zero_parameters +//! // fn foo() {} +//! ``` +//! +//! After adding a new inline-test, run `cargo collect-tests` to extract +//! it as a standalone text-fixture into `tests/data/parser/inline`, and +//! run `cargo test` once to create the "gold" value. +//! +//! Coding convention: rules like `where_clause` always produce either a +//! node or an error, rules like `opt_where_clause` may produce nothing. +//! Non-opt rules typically start with `assert!(p.at(FIRST_TOKEN))`, the +//! caller is responsible for branching on the first token. +mod attributes; +mod expressions; +mod items; +mod params; +mod paths; +mod patterns; +mod type_args; +mod type_params; +mod types; + +pub(crate) use self::{ + expressions::block, + items::{ + enum_variant_list, extern_item_list, impl_item_list, match_arm_list, mod_item_list, + named_field_def_list, named_field_list, token_tree, trait_item_list, use_tree_list, + }, +}; +use crate::{ + parser_api::{CompletedMarker, Marker, Parser}, + token_set::TokenSet, + SyntaxKind::{self, *}, +}; + +pub(crate) fn root(p: &mut Parser) { + let m = p.start(); + p.eat(SHEBANG); + items::mod_contents(p, false); + m.complete(p, SOURCE_FILE); +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum BlockLike { + Block, + NotBlock, +} + +impl BlockLike { + fn is_block(self) -> bool { + self == BlockLike::Block + } +} + +fn opt_visibility(p: &mut Parser) { + match p.current() { + PUB_KW => { + let m = p.start(); + p.bump(); + if p.at(L_PAREN) { + match p.nth(1) { + // test crate_visibility + // pub(crate) struct S; + // pub(self) struct S; + // pub(self) struct S; + // pub(self) struct S; + CRATE_KW | SELF_KW | SUPER_KW => { + p.bump(); + p.bump(); + p.expect(R_PAREN); + } + IN_KW => { + p.bump(); + p.bump(); + paths::use_path(p); + p.expect(R_PAREN); + } + _ => (), + } + } + m.complete(p, VISIBILITY); + } + // test crate_keyword_vis + // crate fn main() { } + CRATE_KW => { + let m = p.start(); + p.bump(); + m.complete(p, VISIBILITY); + } + _ => (), + } +} + +fn opt_alias(p: &mut Parser) { + if p.at(AS_KW) { + let m = p.start(); + p.bump(); + name(p); + m.complete(p, ALIAS); + } +} + +fn abi(p: &mut Parser) { + assert!(p.at(EXTERN_KW)); + let abi = p.start(); + p.bump(); + match p.current() { + STRING | RAW_STRING => p.bump(), + _ => (), + } + abi.complete(p, ABI); +} + +fn opt_fn_ret_type(p: &mut Parser) -> bool { + if p.at(THIN_ARROW) { + let m = p.start(); + p.bump(); + types::type_(p); + m.complete(p, RET_TYPE); + true + } else { + false + } +} + +fn name_r(p: &mut Parser, recovery: TokenSet) { + if p.at(IDENT) { + let m = p.start(); + p.bump(); + m.complete(p, NAME); + } else { + p.err_recover("expected a name", recovery); + } +} + +fn name(p: &mut Parser) { + name_r(p, TokenSet::EMPTY) +} + +fn name_ref(p: &mut Parser) { + if p.at(IDENT) { + let m = p.start(); + p.bump(); + m.complete(p, NAME_REF); + } else { + p.err_and_bump("expected identifier"); + } +} + +fn error_block(p: &mut Parser, message: &str) { + go(p, Some(message)); + fn go(p: &mut Parser, message: Option<&str>) { + assert!(p.at(L_CURLY)); + let m = p.start(); + if let Some(message) = message { + p.error(message); + } + p.bump(); + while !p.at(EOF) && !p.at(R_CURLY) { + match p.current() { + L_CURLY => go(p, None), + _ => p.bump(), + } + } + p.eat(R_CURLY); + m.complete(p, ERROR); + } +} diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs new file mode 100644 index 000000000..60c8602f9 --- /dev/null +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -0,0 +1,455 @@ +mod atom; + +pub(crate) use self::atom::match_arm_list; +pub(super) use self::atom::{literal, LITERAL_FIRST}; +use super::*; + +const EXPR_FIRST: TokenSet = LHS_FIRST; + +pub(super) fn expr(p: &mut Parser) -> BlockLike { + let r = Restrictions { + forbid_structs: false, + prefer_stmt: false, + }; + expr_bp(p, r, 1) +} + +pub(super) fn expr_stmt(p: &mut Parser) -> BlockLike { + let r = Restrictions { + forbid_structs: false, + prefer_stmt: true, + }; + expr_bp(p, r, 1) +} + +fn expr_no_struct(p: &mut Parser) { + let r = Restrictions { + forbid_structs: true, + prefer_stmt: false, + }; + expr_bp(p, r, 1); +} + +// test block +// fn a() {} +// fn b() { let _ = 1; } +// fn c() { 1; 2; } +// fn d() { 1; 2 } +pub(crate) fn block(p: &mut Parser) { + if !p.at(L_CURLY) { + p.error("expected a block"); + return; + } + let m = p.start(); + p.bump(); + while !p.at(EOF) && !p.at(R_CURLY) { + match p.current() { + LET_KW => let_stmt(p), + _ => { + // test block_items + // fn a() { fn b() {} } + let m = p.start(); + match items::maybe_item(p, items::ItemFlavor::Mod) { + items::MaybeItem::Item(kind) => { + m.complete(p, kind); + } + items::MaybeItem::Modifiers => { + m.abandon(p); + p.error("expected an item"); + } + // test pub_expr + // fn foo() { pub 92; } //FIXME + items::MaybeItem::None => { + let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; + if p.at(R_CURLY) { + m.abandon(p); + } else { + if is_blocklike { + p.eat(SEMI); + } else { + p.expect(SEMI); + } + m.complete(p, EXPR_STMT); + } + } + } + } + } + } + p.expect(R_CURLY); + m.complete(p, BLOCK); + + // test let_stmt; + // fn foo() { + // let a; + // let b: i32; + // let c = 92; + // let d: i32 = 92; + // } + fn let_stmt(p: &mut Parser) { + assert!(p.at(LET_KW)); + let m = p.start(); + p.bump(); + patterns::pattern(p); + if p.at(COLON) { + types::ascription(p); + } + if p.eat(EQ) { + expressions::expr(p); + } + p.expect(SEMI); + m.complete(p, LET_STMT); + } +} + +#[derive(Clone, Copy)] +struct Restrictions { + forbid_structs: bool, + prefer_stmt: bool, +} + +enum Op { + Simple, + Composite(SyntaxKind, u8), +} + +fn current_op(p: &Parser) -> (u8, Op) { + if let Some(t) = p.next3() { + match t { + (L_ANGLE, L_ANGLE, EQ) => return (1, Op::Composite(SHLEQ, 3)), + (R_ANGLE, R_ANGLE, EQ) => return (1, Op::Composite(SHREQ, 3)), + _ => (), + } + } + + if let Some(t) = p.next2() { + match t { + (PLUS, EQ) => return (1, Op::Composite(PLUSEQ, 2)), + (MINUS, EQ) => return (1, Op::Composite(MINUSEQ, 2)), + (STAR, EQ) => return (1, Op::Composite(STAREQ, 2)), + (SLASH, EQ) => return (1, Op::Composite(SLASHEQ, 2)), + (PIPE, EQ) => return (1, Op::Composite(PIPEEQ, 2)), + (AMP, EQ) => return (1, Op::Composite(AMPEQ, 2)), + (CARET, EQ) => return (1, Op::Composite(CARETEQ, 2)), + (PIPE, PIPE) => return (3, Op::Composite(PIPEPIPE, 2)), + (AMP, AMP) => return (4, Op::Composite(AMPAMP, 2)), + (L_ANGLE, EQ) => return (5, Op::Composite(LTEQ, 2)), + (R_ANGLE, EQ) => return (5, Op::Composite(GTEQ, 2)), + (L_ANGLE, L_ANGLE) => return (9, Op::Composite(SHL, 2)), + (R_ANGLE, R_ANGLE) => return (9, Op::Composite(SHR, 2)), + _ => (), + } + } + + let bp = match p.current() { + EQ => 1, + DOTDOT => 2, + EQEQ | NEQ | L_ANGLE | R_ANGLE => 5, + PIPE => 6, + CARET => 7, + AMP => 8, + MINUS | PLUS => 10, + STAR | SLASH | PERCENT => 11, + _ => 0, + }; + (bp, Op::Simple) +} + +// Parses expression with binding power of at least bp. +fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { + let mut lhs = match lhs(p, r) { + Some(lhs) => { + // test stmt_bin_expr_ambiguity + // fn foo() { + // let _ = {1} & 2; + // {1} &2; + // } + if r.prefer_stmt && is_block(lhs.kind()) { + return BlockLike::Block; + } + lhs + } + None => return BlockLike::NotBlock, + }; + + loop { + let is_range = p.current() == DOTDOT; + let (op_bp, op) = current_op(p); + if op_bp < bp { + break; + } + let m = lhs.precede(p); + match op { + Op::Simple => p.bump(), + Op::Composite(kind, n) => { + p.bump_compound(kind, n); + } + } + expr_bp(p, r, op_bp + 1); + lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR }); + } + BlockLike::NotBlock +} + +// test no_semi_after_block +// fn foo() { +// if true {} +// loop {} +// match () {} +// while true {} +// for _ in () {} +// {} +// {} +// } +fn is_block(kind: SyntaxKind) -> bool { + match kind { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true, + _ => false, + } +} + +const LHS_FIRST: TokenSet = token_set_union![ + token_set![AMP, STAR, EXCL, DOTDOT, MINUS], + atom::ATOM_EXPR_FIRST, +]; + +fn lhs(p: &mut Parser, r: Restrictions) -> Option { + let m; + let kind = match p.current() { + // test ref_expr + // fn foo() { + // let _ = &1; + // let _ = &mut &f(); + // } + AMP => { + m = p.start(); + p.bump(); + p.eat(MUT_KW); + REF_EXPR + } + // test unary_expr + // fn foo() { + // **&1; + // !!true; + // --1; + // } + STAR | EXCL | MINUS => { + m = p.start(); + p.bump(); + PREFIX_EXPR + } + // test full_range_expr + // fn foo() { xs[..]; } + DOTDOT => { + m = p.start(); + p.bump(); + if p.at_ts(EXPR_FIRST) { + expr_bp(p, r, 2); + } + return Some(m.complete(p, RANGE_EXPR)); + } + _ => { + let lhs = atom::atom_expr(p, r)?; + return Some(postfix_expr(p, r, lhs)); + } + }; + expr_bp(p, r, 255); + Some(m.complete(p, kind)) +} + +fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { + let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); + loop { + lhs = match p.current() { + // test stmt_postfix_expr_ambiguity + // fn foo() { + // match () { + // _ => {} + // () => {} + // [] => {} + // } + // } + L_PAREN if allow_calls => call_expr(p, lhs), + L_BRACK if allow_calls => index_expr(p, lhs), + DOT if p.nth(1) == IDENT => { + if p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON { + method_call_expr(p, lhs) + } else { + field_expr(p, lhs) + } + } + DOT if p.nth(1) == INT_NUMBER => field_expr(p, lhs), + // test postfix_range + // fn foo() { let x = 1..; } + DOTDOT if !EXPR_FIRST.contains(p.nth(1)) => { + let m = lhs.precede(p); + p.bump(); + m.complete(p, RANGE_EXPR) + } + QUESTION => try_expr(p, lhs), + AS_KW => cast_expr(p, lhs), + _ => break, + }; + allow_calls = true + } + lhs +} + +// test call_expr +// fn foo() { +// let _ = f(); +// let _ = f()(1)(1, 2,); +// } +fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { + assert!(p.at(L_PAREN)); + let m = lhs.precede(p); + arg_list(p); + m.complete(p, CALL_EXPR) +} + +// test index_expr +// fn foo() { +// x[1][2]; +// } +fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { + assert!(p.at(L_BRACK)); + let m = lhs.precede(p); + p.bump(); + expr(p); + p.expect(R_BRACK); + m.complete(p, INDEX_EXPR) +} + +// test method_call_expr +// fn foo() { +// x.foo(); +// y.bar::(1, 2,); +// } +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)); + let m = lhs.precede(p); + p.bump(); + name_ref(p); + type_args::opt_type_arg_list(p, true); + if p.at(L_PAREN) { + arg_list(p); + } + m.complete(p, METHOD_CALL_EXPR) +} + +// test field_expr +// fn foo() { +// x.foo; +// x.0.bar; +// } +fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { + assert!(p.at(DOT) && (p.nth(1) == IDENT || p.nth(1) == INT_NUMBER)); + let m = lhs.precede(p); + p.bump(); + if p.at(IDENT) { + name_ref(p) + } else { + p.bump() + } + m.complete(p, FIELD_EXPR) +} + +// test try_expr +// fn foo() { +// x?; +// } +fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { + assert!(p.at(QUESTION)); + let m = lhs.precede(p); + p.bump(); + m.complete(p, TRY_EXPR) +} + +// test cast_expr +// fn foo() { +// 82 as i32; +// } +fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { + assert!(p.at(AS_KW)); + let m = lhs.precede(p); + p.bump(); + types::type_(p); + m.complete(p, CAST_EXPR) +} + +fn arg_list(p: &mut Parser) { + assert!(p.at(L_PAREN)); + let m = p.start(); + p.bump(); + while !p.at(R_PAREN) && !p.at(EOF) { + if !p.at_ts(EXPR_FIRST) { + p.error("expected expression"); + break; + } + expr(p); + if !p.at(R_PAREN) && !p.expect(COMMA) { + break; + } + } + p.eat(R_PAREN); + m.complete(p, ARG_LIST); +} + +// test path_expr +// fn foo() { +// let _ = a; +// let _ = a::b; +// let _ = ::a::; +// let _ = format!(); +// } +fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { + assert!(paths::is_path_start(p) || p.at(L_ANGLE)); + let m = p.start(); + paths::expr_path(p); + match p.current() { + L_CURLY if !r.forbid_structs => { + named_field_list(p); + m.complete(p, STRUCT_LIT) + } + EXCL => { + items::macro_call_after_excl(p); + m.complete(p, MACRO_CALL) + } + _ => m.complete(p, PATH_EXPR), + } +} + +// test struct_lit +// fn foo() { +// S {}; +// S { x, y: 32, }; +// S { x, y: 32, ..Default::default() }; +// } +pub(crate) fn named_field_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); + while !p.at(EOF) && !p.at(R_CURLY) { + match p.current() { + IDENT => { + let m = p.start(); + name_ref(p); + if p.eat(COLON) { + expr(p); + } + m.complete(p, NAMED_FIELD); + } + DOTDOT => { + p.bump(); + expr(p); + } + L_CURLY => error_block(p, "expected a field"), + _ => p.err_and_bump("expected identifier"), + } + if !p.at(R_CURLY) { + p.expect(COMMA); + } + } + p.expect(R_CURLY); + m.complete(p, NAMED_FIELD_LIST); +} diff --git a/crates/ra_syntax/src/grammar/expressions/mod.rs b/crates/ra_syntax/src/grammar/expressions/mod.rs deleted file mode 100644 index 60c8602f9..000000000 --- a/crates/ra_syntax/src/grammar/expressions/mod.rs +++ /dev/null @@ -1,455 +0,0 @@ -mod atom; - -pub(crate) use self::atom::match_arm_list; -pub(super) use self::atom::{literal, LITERAL_FIRST}; -use super::*; - -const EXPR_FIRST: TokenSet = LHS_FIRST; - -pub(super) fn expr(p: &mut Parser) -> BlockLike { - let r = Restrictions { - forbid_structs: false, - prefer_stmt: false, - }; - expr_bp(p, r, 1) -} - -pub(super) fn expr_stmt(p: &mut Parser) -> BlockLike { - let r = Restrictions { - forbid_structs: false, - prefer_stmt: true, - }; - expr_bp(p, r, 1) -} - -fn expr_no_struct(p: &mut Parser) { - let r = Restrictions { - forbid_structs: true, - prefer_stmt: false, - }; - expr_bp(p, r, 1); -} - -// test block -// fn a() {} -// fn b() { let _ = 1; } -// fn c() { 1; 2; } -// fn d() { 1; 2 } -pub(crate) fn block(p: &mut Parser) { - if !p.at(L_CURLY) { - p.error("expected a block"); - return; - } - let m = p.start(); - p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { - match p.current() { - LET_KW => let_stmt(p), - _ => { - // test block_items - // fn a() { fn b() {} } - let m = p.start(); - match items::maybe_item(p, items::ItemFlavor::Mod) { - items::MaybeItem::Item(kind) => { - m.complete(p, kind); - } - items::MaybeItem::Modifiers => { - m.abandon(p); - p.error("expected an item"); - } - // test pub_expr - // fn foo() { pub 92; } //FIXME - items::MaybeItem::None => { - let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; - if p.at(R_CURLY) { - m.abandon(p); - } else { - if is_blocklike { - p.eat(SEMI); - } else { - p.expect(SEMI); - } - m.complete(p, EXPR_STMT); - } - } - } - } - } - } - p.expect(R_CURLY); - m.complete(p, BLOCK); - - // test let_stmt; - // fn foo() { - // let a; - // let b: i32; - // let c = 92; - // let d: i32 = 92; - // } - fn let_stmt(p: &mut Parser) { - assert!(p.at(LET_KW)); - let m = p.start(); - p.bump(); - patterns::pattern(p); - if p.at(COLON) { - types::ascription(p); - } - if p.eat(EQ) { - expressions::expr(p); - } - p.expect(SEMI); - m.complete(p, LET_STMT); - } -} - -#[derive(Clone, Copy)] -struct Restrictions { - forbid_structs: bool, - prefer_stmt: bool, -} - -enum Op { - Simple, - Composite(SyntaxKind, u8), -} - -fn current_op(p: &Parser) -> (u8, Op) { - if let Some(t) = p.next3() { - match t { - (L_ANGLE, L_ANGLE, EQ) => return (1, Op::Composite(SHLEQ, 3)), - (R_ANGLE, R_ANGLE, EQ) => return (1, Op::Composite(SHREQ, 3)), - _ => (), - } - } - - if let Some(t) = p.next2() { - match t { - (PLUS, EQ) => return (1, Op::Composite(PLUSEQ, 2)), - (MINUS, EQ) => return (1, Op::Composite(MINUSEQ, 2)), - (STAR, EQ) => return (1, Op::Composite(STAREQ, 2)), - (SLASH, EQ) => return (1, Op::Composite(SLASHEQ, 2)), - (PIPE, EQ) => return (1, Op::Composite(PIPEEQ, 2)), - (AMP, EQ) => return (1, Op::Composite(AMPEQ, 2)), - (CARET, EQ) => return (1, Op::Composite(CARETEQ, 2)), - (PIPE, PIPE) => return (3, Op::Composite(PIPEPIPE, 2)), - (AMP, AMP) => return (4, Op::Composite(AMPAMP, 2)), - (L_ANGLE, EQ) => return (5, Op::Composite(LTEQ, 2)), - (R_ANGLE, EQ) => return (5, Op::Composite(GTEQ, 2)), - (L_ANGLE, L_ANGLE) => return (9, Op::Composite(SHL, 2)), - (R_ANGLE, R_ANGLE) => return (9, Op::Composite(SHR, 2)), - _ => (), - } - } - - let bp = match p.current() { - EQ => 1, - DOTDOT => 2, - EQEQ | NEQ | L_ANGLE | R_ANGLE => 5, - PIPE => 6, - CARET => 7, - AMP => 8, - MINUS | PLUS => 10, - STAR | SLASH | PERCENT => 11, - _ => 0, - }; - (bp, Op::Simple) -} - -// Parses expression with binding power of at least bp. -fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { - let mut lhs = match lhs(p, r) { - Some(lhs) => { - // test stmt_bin_expr_ambiguity - // fn foo() { - // let _ = {1} & 2; - // {1} &2; - // } - if r.prefer_stmt && is_block(lhs.kind()) { - return BlockLike::Block; - } - lhs - } - None => return BlockLike::NotBlock, - }; - - loop { - let is_range = p.current() == DOTDOT; - let (op_bp, op) = current_op(p); - if op_bp < bp { - break; - } - let m = lhs.precede(p); - match op { - Op::Simple => p.bump(), - Op::Composite(kind, n) => { - p.bump_compound(kind, n); - } - } - expr_bp(p, r, op_bp + 1); - lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR }); - } - BlockLike::NotBlock -} - -// test no_semi_after_block -// fn foo() { -// if true {} -// loop {} -// match () {} -// while true {} -// for _ in () {} -// {} -// {} -// } -fn is_block(kind: SyntaxKind) -> bool { - match kind { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true, - _ => false, - } -} - -const LHS_FIRST: TokenSet = token_set_union![ - token_set![AMP, STAR, EXCL, DOTDOT, MINUS], - atom::ATOM_EXPR_FIRST, -]; - -fn lhs(p: &mut Parser, r: Restrictions) -> Option { - let m; - let kind = match p.current() { - // test ref_expr - // fn foo() { - // let _ = &1; - // let _ = &mut &f(); - // } - AMP => { - m = p.start(); - p.bump(); - p.eat(MUT_KW); - REF_EXPR - } - // test unary_expr - // fn foo() { - // **&1; - // !!true; - // --1; - // } - STAR | EXCL | MINUS => { - m = p.start(); - p.bump(); - PREFIX_EXPR - } - // test full_range_expr - // fn foo() { xs[..]; } - DOTDOT => { - m = p.start(); - p.bump(); - if p.at_ts(EXPR_FIRST) { - expr_bp(p, r, 2); - } - return Some(m.complete(p, RANGE_EXPR)); - } - _ => { - let lhs = atom::atom_expr(p, r)?; - return Some(postfix_expr(p, r, lhs)); - } - }; - expr_bp(p, r, 255); - Some(m.complete(p, kind)) -} - -fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { - let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); - loop { - lhs = match p.current() { - // test stmt_postfix_expr_ambiguity - // fn foo() { - // match () { - // _ => {} - // () => {} - // [] => {} - // } - // } - L_PAREN if allow_calls => call_expr(p, lhs), - L_BRACK if allow_calls => index_expr(p, lhs), - DOT if p.nth(1) == IDENT => { - if p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON { - method_call_expr(p, lhs) - } else { - field_expr(p, lhs) - } - } - DOT if p.nth(1) == INT_NUMBER => field_expr(p, lhs), - // test postfix_range - // fn foo() { let x = 1..; } - DOTDOT if !EXPR_FIRST.contains(p.nth(1)) => { - let m = lhs.precede(p); - p.bump(); - m.complete(p, RANGE_EXPR) - } - QUESTION => try_expr(p, lhs), - AS_KW => cast_expr(p, lhs), - _ => break, - }; - allow_calls = true - } - lhs -} - -// test call_expr -// fn foo() { -// let _ = f(); -// let _ = f()(1)(1, 2,); -// } -fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(L_PAREN)); - let m = lhs.precede(p); - arg_list(p); - m.complete(p, CALL_EXPR) -} - -// test index_expr -// fn foo() { -// x[1][2]; -// } -fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(L_BRACK)); - let m = lhs.precede(p); - p.bump(); - expr(p); - p.expect(R_BRACK); - m.complete(p, INDEX_EXPR) -} - -// test method_call_expr -// fn foo() { -// x.foo(); -// y.bar::(1, 2,); -// } -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)); - let m = lhs.precede(p); - p.bump(); - name_ref(p); - type_args::opt_type_arg_list(p, true); - if p.at(L_PAREN) { - arg_list(p); - } - m.complete(p, METHOD_CALL_EXPR) -} - -// test field_expr -// fn foo() { -// x.foo; -// x.0.bar; -// } -fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(DOT) && (p.nth(1) == IDENT || p.nth(1) == INT_NUMBER)); - let m = lhs.precede(p); - p.bump(); - if p.at(IDENT) { - name_ref(p) - } else { - p.bump() - } - m.complete(p, FIELD_EXPR) -} - -// test try_expr -// fn foo() { -// x?; -// } -fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(QUESTION)); - let m = lhs.precede(p); - p.bump(); - m.complete(p, TRY_EXPR) -} - -// test cast_expr -// fn foo() { -// 82 as i32; -// } -fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(AS_KW)); - let m = lhs.precede(p); - p.bump(); - types::type_(p); - m.complete(p, CAST_EXPR) -} - -fn arg_list(p: &mut Parser) { - assert!(p.at(L_PAREN)); - let m = p.start(); - p.bump(); - while !p.at(R_PAREN) && !p.at(EOF) { - if !p.at_ts(EXPR_FIRST) { - p.error("expected expression"); - break; - } - expr(p); - if !p.at(R_PAREN) && !p.expect(COMMA) { - break; - } - } - p.eat(R_PAREN); - m.complete(p, ARG_LIST); -} - -// test path_expr -// fn foo() { -// let _ = a; -// let _ = a::b; -// let _ = ::a::; -// let _ = format!(); -// } -fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { - assert!(paths::is_path_start(p) || p.at(L_ANGLE)); - let m = p.start(); - paths::expr_path(p); - match p.current() { - L_CURLY if !r.forbid_structs => { - named_field_list(p); - m.complete(p, STRUCT_LIT) - } - EXCL => { - items::macro_call_after_excl(p); - m.complete(p, MACRO_CALL) - } - _ => m.complete(p, PATH_EXPR), - } -} - -// test struct_lit -// fn foo() { -// S {}; -// S { x, y: 32, }; -// S { x, y: 32, ..Default::default() }; -// } -pub(crate) fn named_field_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); - let m = p.start(); - p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { - match p.current() { - IDENT => { - let m = p.start(); - name_ref(p); - if p.eat(COLON) { - expr(p); - } - m.complete(p, NAMED_FIELD); - } - DOTDOT => { - p.bump(); - expr(p); - } - L_CURLY => error_block(p, "expected a field"), - _ => p.err_and_bump("expected identifier"), - } - if !p.at(R_CURLY) { - p.expect(COMMA); - } - } - p.expect(R_CURLY); - m.complete(p, NAMED_FIELD_LIST); -} diff --git a/crates/ra_syntax/src/grammar/items.rs b/crates/ra_syntax/src/grammar/items.rs new file mode 100644 index 000000000..4473c2fab --- /dev/null +++ b/crates/ra_syntax/src/grammar/items.rs @@ -0,0 +1,394 @@ +mod consts; +mod nominal; +mod traits; +mod use_item; + +pub(crate) use self::{ + expressions::{match_arm_list, named_field_list}, + nominal::{enum_variant_list, named_field_def_list}, + traits::{impl_item_list, trait_item_list}, + use_item::use_tree_list, +}; +use super::*; + +// test mod_contents +// fn foo() {} +// macro_rules! foo {} +// foo::bar!(); +// super::baz! {} +// struct S; +pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { + attributes::inner_attributes(p); + while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { + item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) + } +} + +pub(super) enum ItemFlavor { + Mod, + Trait, +} + +pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ + FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW, + CRATE_KW +]; + +pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { + let m = p.start(); + match maybe_item(p, flavor) { + MaybeItem::Item(kind) => { + m.complete(p, kind); + } + MaybeItem::None => { + if paths::is_path_start(p) { + match macro_call(p) { + BlockLike::Block => (), + BlockLike::NotBlock => { + p.expect(SEMI); + } + } + m.complete(p, MACRO_CALL); + } else { + m.abandon(p); + if p.at(L_CURLY) { + error_block(p, "expected an item"); + } else if p.at(R_CURLY) && !stop_on_r_curly { + let e = p.start(); + p.error("unmatched `}`"); + p.bump(); + e.complete(p, ERROR); + } else if !p.at(EOF) && !p.at(R_CURLY) { + p.err_and_bump("expected an item"); + } else { + p.error("expected an item"); + } + } + } + MaybeItem::Modifiers => { + p.error("expected fn, trait or impl"); + m.complete(p, ERROR); + } + } +} + +pub(super) enum MaybeItem { + None, + Item(SyntaxKind), + Modifiers, +} + +pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { + attributes::outer_attributes(p); + opt_visibility(p); + if let Some(kind) = items_without_modifiers(p) { + return MaybeItem::Item(kind); + } + + let mut has_mods = false; + // modifiers + has_mods |= p.eat(CONST_KW); + + // test unsafe_block_in_mod + // fn foo(){} unsafe { } fn bar(){} + if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY { + p.eat(UNSAFE_KW); + has_mods = true; + } + if p.at(EXTERN_KW) { + has_mods = true; + abi(p); + } + if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW { + p.bump_remap(AUTO_KW); + has_mods = true; + } + if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW { + p.bump_remap(DEFAULT_KW); + has_mods = true; + } + + // items + let kind = match p.current() { + // test extern_fn + // extern fn foo() {} + + // test const_fn + // const fn foo() {} + + // test const_unsafe_fn + // const unsafe fn foo() {} + + // test unsafe_extern_fn + // unsafe extern "C" fn foo() {} + + // test unsafe_fn + // unsafe fn foo() {} + FN_KW => { + fn_def(p, flavor); + FN_DEF + } + + // test unsafe_trait + // unsafe trait T {} + + // test auto_trait + // auto trait T {} + + // test unsafe_auto_trait + // unsafe auto trait T {} + TRAIT_KW => { + traits::trait_def(p); + TRAIT_DEF + } + + // test unsafe_impl + // unsafe impl Foo {} + + // test default_impl + // default impl Foo {} + + // test unsafe_default_impl + // unsafe default impl Foo {} + IMPL_KW => { + traits::impl_item(p); + IMPL_ITEM + } + _ => { + return if has_mods { + MaybeItem::Modifiers + } else { + MaybeItem::None + }; + } + }; + + MaybeItem::Item(kind) +} + +fn items_without_modifiers(p: &mut Parser) -> Option { + let la = p.nth(1); + let kind = match p.current() { + // test extern_crate + // extern crate foo; + EXTERN_KW if la == CRATE_KW => { + extern_crate_item(p); + EXTERN_CRATE_ITEM + } + TYPE_KW => { + type_def(p); + TYPE_DEF + } + MOD_KW => { + mod_item(p); + MODULE + } + STRUCT_KW => { + // test struct_items + // struct Foo; + // struct Foo {} + // struct Foo(); + // struct Foo(String, usize); + // struct Foo { + // a: i32, + // b: f32, + // } + nominal::struct_def(p, STRUCT_KW); + if p.at(SEMI) { + p.err_and_bump( + "expected item, found `;`\n\ + consider removing this semicolon", + ); + } + STRUCT_DEF + } + IDENT if p.at_contextual_kw("union") => { + // test union_items + // union Foo {} + // union Foo { + // a: i32, + // b: f32, + // } + nominal::struct_def(p, UNION_KW); + STRUCT_DEF + } + ENUM_KW => { + nominal::enum_def(p); + ENUM_DEF + } + USE_KW => { + use_item::use_item(p); + USE_ITEM + } + CONST_KW if (la == IDENT || la == MUT_KW) => { + consts::const_def(p); + CONST_DEF + } + STATIC_KW => { + consts::static_def(p); + STATIC_DEF + } + // test extern_block + // extern {} + EXTERN_KW + if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => + { + abi(p); + extern_item_list(p); + EXTERN_BLOCK + } + _ => return None, + }; + Some(kind) +} + +fn extern_crate_item(p: &mut Parser) { + assert!(p.at(EXTERN_KW)); + p.bump(); + assert!(p.at(CRATE_KW)); + p.bump(); + name(p); + opt_alias(p); + p.expect(SEMI); +} + +pub(crate) fn extern_item_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); + mod_contents(p, true); + p.expect(R_CURLY); + m.complete(p, EXTERN_ITEM_LIST); +} + +fn fn_def(p: &mut Parser, flavor: ItemFlavor) { + assert!(p.at(FN_KW)); + p.bump(); + + name_r(p, ITEM_RECOVERY_SET); + // test function_type_params + // fn foo(){} + type_params::opt_type_param_list(p); + + if p.at(L_PAREN) { + match flavor { + ItemFlavor::Mod => params::param_list(p), + ItemFlavor::Trait => params::param_list_opt_patterns(p), + } + } else { + p.error("expected function arguments"); + } + // test function_ret_type + // fn foo() {} + // fn bar() -> () {} + opt_fn_ret_type(p); + + // test function_where_clause + // fn foo() where T: Copy {} + type_params::opt_where_clause(p); + + // test fn_decl + // trait T { fn foo(); } + if p.at(SEMI) { + p.bump(); + } else { + expressions::block(p) + } +} + +// test type_item +// type Foo = Bar; +fn type_def(p: &mut Parser) { + assert!(p.at(TYPE_KW)); + p.bump(); + + name(p); + + // test type_item_type_params + // type Result = (); + type_params::opt_type_param_list(p); + + if p.at(COLON) { + type_params::bounds(p); + } + + // test type_item_where_clause + // type Foo where Foo: Copy = (); + type_params::opt_where_clause(p); + + if p.eat(EQ) { + types::type_(p); + } + p.expect(SEMI); +} + +pub(crate) fn mod_item(p: &mut Parser) { + assert!(p.at(MOD_KW)); + p.bump(); + + name(p); + if p.at(L_CURLY) { + mod_item_list(p); + } else if !p.eat(SEMI) { + p.error("expected `;` or `{`"); + } +} + +pub(crate) fn mod_item_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); + mod_contents(p, true); + p.expect(R_CURLY); + m.complete(p, ITEM_LIST); +} + +fn macro_call(p: &mut Parser) -> BlockLike { + assert!(paths::is_path_start(p)); + paths::use_path(p); + macro_call_after_excl(p) +} + +pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { + p.expect(EXCL); + p.eat(IDENT); + match p.current() { + L_CURLY => { + token_tree(p); + BlockLike::Block + } + L_PAREN | L_BRACK => { + token_tree(p); + BlockLike::NotBlock + } + _ => { + p.error("expected `{`, `[`, `(`"); + BlockLike::NotBlock + } + } +} + +pub(crate) fn token_tree(p: &mut Parser) { + let closing_paren_kind = match p.current() { + L_CURLY => R_CURLY, + L_PAREN => R_PAREN, + L_BRACK => R_BRACK, + _ => unreachable!(), + }; + let m = p.start(); + p.bump(); + while !p.at(EOF) && !p.at(closing_paren_kind) { + match p.current() { + L_CURLY | L_PAREN | L_BRACK => token_tree(p), + R_CURLY => { + p.error("unmatched `}`"); + m.complete(p, TOKEN_TREE); + return; + } + R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"), + _ => p.bump(), + } + } + p.expect(closing_paren_kind); + m.complete(p, TOKEN_TREE); +} diff --git a/crates/ra_syntax/src/grammar/items/mod.rs b/crates/ra_syntax/src/grammar/items/mod.rs deleted file mode 100644 index 4473c2fab..000000000 --- a/crates/ra_syntax/src/grammar/items/mod.rs +++ /dev/null @@ -1,394 +0,0 @@ -mod consts; -mod nominal; -mod traits; -mod use_item; - -pub(crate) use self::{ - expressions::{match_arm_list, named_field_list}, - nominal::{enum_variant_list, named_field_def_list}, - traits::{impl_item_list, trait_item_list}, - use_item::use_tree_list, -}; -use super::*; - -// test mod_contents -// fn foo() {} -// macro_rules! foo {} -// foo::bar!(); -// super::baz! {} -// struct S; -pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { - attributes::inner_attributes(p); - while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { - item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) - } -} - -pub(super) enum ItemFlavor { - Mod, - Trait, -} - -pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ - FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW, - CRATE_KW -]; - -pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { - let m = p.start(); - match maybe_item(p, flavor) { - MaybeItem::Item(kind) => { - m.complete(p, kind); - } - MaybeItem::None => { - if paths::is_path_start(p) { - match macro_call(p) { - BlockLike::Block => (), - BlockLike::NotBlock => { - p.expect(SEMI); - } - } - m.complete(p, MACRO_CALL); - } else { - m.abandon(p); - if p.at(L_CURLY) { - error_block(p, "expected an item"); - } else if p.at(R_CURLY) && !stop_on_r_curly { - let e = p.start(); - p.error("unmatched `}`"); - p.bump(); - e.complete(p, ERROR); - } else if !p.at(EOF) && !p.at(R_CURLY) { - p.err_and_bump("expected an item"); - } else { - p.error("expected an item"); - } - } - } - MaybeItem::Modifiers => { - p.error("expected fn, trait or impl"); - m.complete(p, ERROR); - } - } -} - -pub(super) enum MaybeItem { - None, - Item(SyntaxKind), - Modifiers, -} - -pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { - attributes::outer_attributes(p); - opt_visibility(p); - if let Some(kind) = items_without_modifiers(p) { - return MaybeItem::Item(kind); - } - - let mut has_mods = false; - // modifiers - has_mods |= p.eat(CONST_KW); - - // test unsafe_block_in_mod - // fn foo(){} unsafe { } fn bar(){} - if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY { - p.eat(UNSAFE_KW); - has_mods = true; - } - if p.at(EXTERN_KW) { - has_mods = true; - abi(p); - } - if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW { - p.bump_remap(AUTO_KW); - has_mods = true; - } - if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW { - p.bump_remap(DEFAULT_KW); - has_mods = true; - } - - // items - let kind = match p.current() { - // test extern_fn - // extern fn foo() {} - - // test const_fn - // const fn foo() {} - - // test const_unsafe_fn - // const unsafe fn foo() {} - - // test unsafe_extern_fn - // unsafe extern "C" fn foo() {} - - // test unsafe_fn - // unsafe fn foo() {} - FN_KW => { - fn_def(p, flavor); - FN_DEF - } - - // test unsafe_trait - // unsafe trait T {} - - // test auto_trait - // auto trait T {} - - // test unsafe_auto_trait - // unsafe auto trait T {} - TRAIT_KW => { - traits::trait_def(p); - TRAIT_DEF - } - - // test unsafe_impl - // unsafe impl Foo {} - - // test default_impl - // default impl Foo {} - - // test unsafe_default_impl - // unsafe default impl Foo {} - IMPL_KW => { - traits::impl_item(p); - IMPL_ITEM - } - _ => { - return if has_mods { - MaybeItem::Modifiers - } else { - MaybeItem::None - }; - } - }; - - MaybeItem::Item(kind) -} - -fn items_without_modifiers(p: &mut Parser) -> Option { - let la = p.nth(1); - let kind = match p.current() { - // test extern_crate - // extern crate foo; - EXTERN_KW if la == CRATE_KW => { - extern_crate_item(p); - EXTERN_CRATE_ITEM - } - TYPE_KW => { - type_def(p); - TYPE_DEF - } - MOD_KW => { - mod_item(p); - MODULE - } - STRUCT_KW => { - // test struct_items - // struct Foo; - // struct Foo {} - // struct Foo(); - // struct Foo(String, usize); - // struct Foo { - // a: i32, - // b: f32, - // } - nominal::struct_def(p, STRUCT_KW); - if p.at(SEMI) { - p.err_and_bump( - "expected item, found `;`\n\ - consider removing this semicolon", - ); - } - STRUCT_DEF - } - IDENT if p.at_contextual_kw("union") => { - // test union_items - // union Foo {} - // union Foo { - // a: i32, - // b: f32, - // } - nominal::struct_def(p, UNION_KW); - STRUCT_DEF - } - ENUM_KW => { - nominal::enum_def(p); - ENUM_DEF - } - USE_KW => { - use_item::use_item(p); - USE_ITEM - } - CONST_KW if (la == IDENT || la == MUT_KW) => { - consts::const_def(p); - CONST_DEF - } - STATIC_KW => { - consts::static_def(p); - STATIC_DEF - } - // test extern_block - // extern {} - EXTERN_KW - if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => - { - abi(p); - extern_item_list(p); - EXTERN_BLOCK - } - _ => return None, - }; - Some(kind) -} - -fn extern_crate_item(p: &mut Parser) { - assert!(p.at(EXTERN_KW)); - p.bump(); - assert!(p.at(CRATE_KW)); - p.bump(); - name(p); - opt_alias(p); - p.expect(SEMI); -} - -pub(crate) fn extern_item_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); - let m = p.start(); - p.bump(); - mod_contents(p, true); - p.expect(R_CURLY); - m.complete(p, EXTERN_ITEM_LIST); -} - -fn fn_def(p: &mut Parser, flavor: ItemFlavor) { - assert!(p.at(FN_KW)); - p.bump(); - - name_r(p, ITEM_RECOVERY_SET); - // test function_type_params - // fn foo(){} - type_params::opt_type_param_list(p); - - if p.at(L_PAREN) { - match flavor { - ItemFlavor::Mod => params::param_list(p), - ItemFlavor::Trait => params::param_list_opt_patterns(p), - } - } else { - p.error("expected function arguments"); - } - // test function_ret_type - // fn foo() {} - // fn bar() -> () {} - opt_fn_ret_type(p); - - // test function_where_clause - // fn foo() where T: Copy {} - type_params::opt_where_clause(p); - - // test fn_decl - // trait T { fn foo(); } - if p.at(SEMI) { - p.bump(); - } else { - expressions::block(p) - } -} - -// test type_item -// type Foo = Bar; -fn type_def(p: &mut Parser) { - assert!(p.at(TYPE_KW)); - p.bump(); - - name(p); - - // test type_item_type_params - // type Result = (); - type_params::opt_type_param_list(p); - - if p.at(COLON) { - type_params::bounds(p); - } - - // test type_item_where_clause - // type Foo where Foo: Copy = (); - type_params::opt_where_clause(p); - - if p.eat(EQ) { - types::type_(p); - } - p.expect(SEMI); -} - -pub(crate) fn mod_item(p: &mut Parser) { - assert!(p.at(MOD_KW)); - p.bump(); - - name(p); - if p.at(L_CURLY) { - mod_item_list(p); - } else if !p.eat(SEMI) { - p.error("expected `;` or `{`"); - } -} - -pub(crate) fn mod_item_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); - let m = p.start(); - p.bump(); - mod_contents(p, true); - p.expect(R_CURLY); - m.complete(p, ITEM_LIST); -} - -fn macro_call(p: &mut Parser) -> BlockLike { - assert!(paths::is_path_start(p)); - paths::use_path(p); - macro_call_after_excl(p) -} - -pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { - p.expect(EXCL); - p.eat(IDENT); - match p.current() { - L_CURLY => { - token_tree(p); - BlockLike::Block - } - L_PAREN | L_BRACK => { - token_tree(p); - BlockLike::NotBlock - } - _ => { - p.error("expected `{`, `[`, `(`"); - BlockLike::NotBlock - } - } -} - -pub(crate) fn token_tree(p: &mut Parser) { - let closing_paren_kind = match p.current() { - L_CURLY => R_CURLY, - L_PAREN => R_PAREN, - L_BRACK => R_BRACK, - _ => unreachable!(), - }; - let m = p.start(); - p.bump(); - while !p.at(EOF) && !p.at(closing_paren_kind) { - match p.current() { - L_CURLY | L_PAREN | L_BRACK => token_tree(p), - R_CURLY => { - p.error("unmatched `}`"); - m.complete(p, TOKEN_TREE); - return; - } - R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"), - _ => p.bump(), - } - } - p.expect(closing_paren_kind); - m.complete(p, TOKEN_TREE); -} diff --git a/crates/ra_syntax/src/grammar/mod.rs b/crates/ra_syntax/src/grammar/mod.rs deleted file mode 100644 index 06a37d648..000000000 --- a/crates/ra_syntax/src/grammar/mod.rs +++ /dev/null @@ -1,184 +0,0 @@ -//! This is the actual "grammar" of the Rust language. -//! -//! Each function in this module and its children corresponds -//! to a production of the format grammar. Submodules roughly -//! correspond to different *areas* of the grammar. By convention, -//! each submodule starts with `use super::*` import and exports -//! "public" productions via `pub(super)`. -//! -//! See docs for `Parser` to learn about API, available to the grammar, -//! and see docs for `Event` to learn how this actually manages to -//! produce parse trees. -//! -//! Code in this module also contains inline tests, which start with -//! `// test name-of-the-test` comment and look like this: -//! -//! ``` -//! // test function_with_zero_parameters -//! // fn foo() {} -//! ``` -//! -//! After adding a new inline-test, run `cargo collect-tests` to extract -//! it as a standalone text-fixture into `tests/data/parser/inline`, and -//! run `cargo test` once to create the "gold" value. -//! -//! Coding convention: rules like `where_clause` always produce either a -//! node or an error, rules like `opt_where_clause` may produce nothing. -//! Non-opt rules typically start with `assert!(p.at(FIRST_TOKEN))`, the -//! caller is responsible for branching on the first token. -mod attributes; -mod expressions; -mod items; -mod params; -mod paths; -mod patterns; -mod type_args; -mod type_params; -mod types; - -pub(crate) use self::{ - expressions::block, - items::{ - enum_variant_list, extern_item_list, impl_item_list, match_arm_list, mod_item_list, - named_field_def_list, named_field_list, token_tree, trait_item_list, use_tree_list, - }, -}; -use crate::{ - parser_api::{CompletedMarker, Marker, Parser}, - token_set::TokenSet, - SyntaxKind::{self, *}, -}; - -pub(crate) fn root(p: &mut Parser) { - let m = p.start(); - p.eat(SHEBANG); - items::mod_contents(p, false); - m.complete(p, SOURCE_FILE); -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum BlockLike { - Block, - NotBlock, -} - -impl BlockLike { - fn is_block(self) -> bool { - self == BlockLike::Block - } -} - -fn opt_visibility(p: &mut Parser) { - match p.current() { - PUB_KW => { - let m = p.start(); - p.bump(); - if p.at(L_PAREN) { - match p.nth(1) { - // test crate_visibility - // pub(crate) struct S; - // pub(self) struct S; - // pub(self) struct S; - // pub(self) struct S; - CRATE_KW | SELF_KW | SUPER_KW => { - p.bump(); - p.bump(); - p.expect(R_PAREN); - } - IN_KW => { - p.bump(); - p.bump(); - paths::use_path(p); - p.expect(R_PAREN); - } - _ => (), - } - } - m.complete(p, VISIBILITY); - } - // test crate_keyword_vis - // crate fn main() { } - CRATE_KW => { - let m = p.start(); - p.bump(); - m.complete(p, VISIBILITY); - } - _ => (), - } -} - -fn opt_alias(p: &mut Parser) { - if p.at(AS_KW) { - let m = p.start(); - p.bump(); - name(p); - m.complete(p, ALIAS); - } -} - -fn abi(p: &mut Parser) { - assert!(p.at(EXTERN_KW)); - let abi = p.start(); - p.bump(); - match p.current() { - STRING | RAW_STRING => p.bump(), - _ => (), - } - abi.complete(p, ABI); -} - -fn opt_fn_ret_type(p: &mut Parser) -> bool { - if p.at(THIN_ARROW) { - let m = p.start(); - p.bump(); - types::type_(p); - m.complete(p, RET_TYPE); - true - } else { - false - } -} - -fn name_r(p: &mut Parser, recovery: TokenSet) { - if p.at(IDENT) { - let m = p.start(); - p.bump(); - m.complete(p, NAME); - } else { - p.err_recover("expected a name", recovery); - } -} - -fn name(p: &mut Parser) { - name_r(p, TokenSet::EMPTY) -} - -fn name_ref(p: &mut Parser) { - if p.at(IDENT) { - let m = p.start(); - p.bump(); - m.complete(p, NAME_REF); - } else { - p.err_and_bump("expected identifier"); - } -} - -fn error_block(p: &mut Parser, message: &str) { - go(p, Some(message)); - fn go(p: &mut Parser, message: Option<&str>) { - assert!(p.at(L_CURLY)); - let m = p.start(); - if let Some(message) = message { - p.error(message); - } - p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { - match p.current() { - L_CURLY => go(p, None), - _ => p.bump(), - } - } - p.eat(R_CURLY); - m.complete(p, ERROR); - } -} diff --git a/crates/ra_syntax/src/lexer.rs b/crates/ra_syntax/src/lexer.rs new file mode 100644 index 000000000..f388da273 --- /dev/null +++ b/crates/ra_syntax/src/lexer.rs @@ -0,0 +1,213 @@ +mod classes; +mod comments; +mod numbers; +mod ptr; +mod strings; + +use crate::{ + SyntaxKind::{self, *}, + TextUnit, +}; + +use self::{ + classes::*, + comments::{scan_comment, scan_shebang}, + numbers::scan_number, + ptr::Ptr, + strings::{ + is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string, scan_string, + }, +}; + +/// A token of Rust source. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Token { + /// The kind of token. + pub kind: SyntaxKind, + /// The length of the token. + pub len: TextUnit, +} + +/// Break a string up into its component tokens +pub fn tokenize(text: &str) -> Vec { + let mut text = text; + let mut acc = Vec::new(); + while !text.is_empty() { + let token = next_token(text); + acc.push(token); + let len: u32 = token.len.into(); + text = &text[len as usize..]; + } + acc +} + +/// Get the next token from a string +pub fn next_token(text: &str) -> Token { + assert!(!text.is_empty()); + let mut ptr = Ptr::new(text); + let c = ptr.bump().unwrap(); + let kind = next_token_inner(c, &mut ptr); + let len = ptr.into_len(); + Token { kind, len } +} + +fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { + if is_whitespace(c) { + ptr.bump_while(is_whitespace); + return WHITESPACE; + } + + match c { + '#' => { + if scan_shebang(ptr) { + return SHEBANG; + } + } + '/' => { + if let Some(kind) = scan_comment(ptr) { + return kind; + } + } + _ => (), + } + + let ident_start = is_ident_start(c) && !is_string_literal_start(c, ptr.current(), ptr.nth(1)); + if ident_start { + return scan_ident(c, ptr); + } + + if is_dec_digit(c) { + let kind = scan_number(c, ptr); + scan_literal_suffix(ptr); + return kind; + } + + // One-byte tokens. + if let Some(kind) = SyntaxKind::from_char(c) { + return kind; + } + + match c { + // Multi-byte tokens. + '.' => { + return match (ptr.current(), ptr.nth(1)) { + (Some('.'), Some('.')) => { + ptr.bump(); + ptr.bump(); + DOTDOTDOT + } + (Some('.'), Some('=')) => { + ptr.bump(); + ptr.bump(); + DOTDOTEQ + } + (Some('.'), _) => { + ptr.bump(); + DOTDOT + } + _ => DOT, + }; + } + ':' => { + return match ptr.current() { + Some(':') => { + ptr.bump(); + COLONCOLON + } + _ => COLON, + }; + } + '=' => { + return match ptr.current() { + Some('=') => { + ptr.bump(); + EQEQ + } + Some('>') => { + ptr.bump(); + FAT_ARROW + } + _ => EQ, + }; + } + '!' => { + return match ptr.current() { + Some('=') => { + ptr.bump(); + NEQ + } + _ => EXCL, + }; + } + '-' => { + return if ptr.at('>') { + ptr.bump(); + THIN_ARROW + } else { + MINUS + }; + } + + // If the character is an ident start not followed by another single + // quote, then this is a lifetime name: + '\'' => { + return if ptr.at_p(is_ident_start) && !ptr.at_str("''") { + ptr.bump(); + while ptr.at_p(is_ident_continue) { + ptr.bump(); + } + // lifetimes shouldn't end with a single quote + // if we find one, then this is an invalid character literal + if ptr.at('\'') { + ptr.bump(); + return CHAR; // TODO: error reporting + } + LIFETIME + } else { + scan_char(ptr); + scan_literal_suffix(ptr); + CHAR + }; + } + 'b' => { + let kind = scan_byte_char_or_string(ptr); + scan_literal_suffix(ptr); + return kind; + } + '"' => { + scan_string(ptr); + scan_literal_suffix(ptr); + return STRING; + } + 'r' => { + scan_raw_string(ptr); + scan_literal_suffix(ptr); + return RAW_STRING; + } + _ => (), + } + ERROR +} + +fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind { + let is_single_letter = match ptr.current() { + None => true, + Some(c) if !is_ident_continue(c) => true, + _ => false, + }; + if is_single_letter { + return if c == '_' { UNDERSCORE } else { IDENT }; + } + ptr.bump_while(is_ident_continue); + if let Some(kind) = SyntaxKind::from_keyword(ptr.current_token_text()) { + return kind; + } + IDENT +} + +fn scan_literal_suffix(ptr: &mut Ptr) { + if ptr.at_p(is_ident_start) { + ptr.bump(); + } + ptr.bump_while(is_ident_continue); +} diff --git a/crates/ra_syntax/src/lexer/mod.rs b/crates/ra_syntax/src/lexer/mod.rs deleted file mode 100644 index f388da273..000000000 --- a/crates/ra_syntax/src/lexer/mod.rs +++ /dev/null @@ -1,213 +0,0 @@ -mod classes; -mod comments; -mod numbers; -mod ptr; -mod strings; - -use crate::{ - SyntaxKind::{self, *}, - TextUnit, -}; - -use self::{ - classes::*, - comments::{scan_comment, scan_shebang}, - numbers::scan_number, - ptr::Ptr, - strings::{ - is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string, scan_string, - }, -}; - -/// A token of Rust source. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Token { - /// The kind of token. - pub kind: SyntaxKind, - /// The length of the token. - pub len: TextUnit, -} - -/// Break a string up into its component tokens -pub fn tokenize(text: &str) -> Vec { - let mut text = text; - let mut acc = Vec::new(); - while !text.is_empty() { - let token = next_token(text); - acc.push(token); - let len: u32 = token.len.into(); - text = &text[len as usize..]; - } - acc -} - -/// Get the next token from a string -pub fn next_token(text: &str) -> Token { - assert!(!text.is_empty()); - let mut ptr = Ptr::new(text); - let c = ptr.bump().unwrap(); - let kind = next_token_inner(c, &mut ptr); - let len = ptr.into_len(); - Token { kind, len } -} - -fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { - if is_whitespace(c) { - ptr.bump_while(is_whitespace); - return WHITESPACE; - } - - match c { - '#' => { - if scan_shebang(ptr) { - return SHEBANG; - } - } - '/' => { - if let Some(kind) = scan_comment(ptr) { - return kind; - } - } - _ => (), - } - - let ident_start = is_ident_start(c) && !is_string_literal_start(c, ptr.current(), ptr.nth(1)); - if ident_start { - return scan_ident(c, ptr); - } - - if is_dec_digit(c) { - let kind = scan_number(c, ptr); - scan_literal_suffix(ptr); - return kind; - } - - // One-byte tokens. - if let Some(kind) = SyntaxKind::from_char(c) { - return kind; - } - - match c { - // Multi-byte tokens. - '.' => { - return match (ptr.current(), ptr.nth(1)) { - (Some('.'), Some('.')) => { - ptr.bump(); - ptr.bump(); - DOTDOTDOT - } - (Some('.'), Some('=')) => { - ptr.bump(); - ptr.bump(); - DOTDOTEQ - } - (Some('.'), _) => { - ptr.bump(); - DOTDOT - } - _ => DOT, - }; - } - ':' => { - return match ptr.current() { - Some(':') => { - ptr.bump(); - COLONCOLON - } - _ => COLON, - }; - } - '=' => { - return match ptr.current() { - Some('=') => { - ptr.bump(); - EQEQ - } - Some('>') => { - ptr.bump(); - FAT_ARROW - } - _ => EQ, - }; - } - '!' => { - return match ptr.current() { - Some('=') => { - ptr.bump(); - NEQ - } - _ => EXCL, - }; - } - '-' => { - return if ptr.at('>') { - ptr.bump(); - THIN_ARROW - } else { - MINUS - }; - } - - // If the character is an ident start not followed by another single - // quote, then this is a lifetime name: - '\'' => { - return if ptr.at_p(is_ident_start) && !ptr.at_str("''") { - ptr.bump(); - while ptr.at_p(is_ident_continue) { - ptr.bump(); - } - // lifetimes shouldn't end with a single quote - // if we find one, then this is an invalid character literal - if ptr.at('\'') { - ptr.bump(); - return CHAR; // TODO: error reporting - } - LIFETIME - } else { - scan_char(ptr); - scan_literal_suffix(ptr); - CHAR - }; - } - 'b' => { - let kind = scan_byte_char_or_string(ptr); - scan_literal_suffix(ptr); - return kind; - } - '"' => { - scan_string(ptr); - scan_literal_suffix(ptr); - return STRING; - } - 'r' => { - scan_raw_string(ptr); - scan_literal_suffix(ptr); - return RAW_STRING; - } - _ => (), - } - ERROR -} - -fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind { - let is_single_letter = match ptr.current() { - None => true, - Some(c) if !is_ident_continue(c) => true, - _ => false, - }; - if is_single_letter { - return if c == '_' { UNDERSCORE } else { IDENT }; - } - ptr.bump_while(is_ident_continue); - if let Some(kind) = SyntaxKind::from_keyword(ptr.current_token_text()) { - return kind; - } - IDENT -} - -fn scan_literal_suffix(ptr: &mut Ptr) { - if ptr.at_p(is_ident_start) { - ptr.bump(); - } - ptr.bump_while(is_ident_continue); -} diff --git a/crates/ra_syntax/src/parser_impl.rs b/crates/ra_syntax/src/parser_impl.rs new file mode 100644 index 000000000..cb6e370ac --- /dev/null +++ b/crates/ra_syntax/src/parser_impl.rs @@ -0,0 +1,198 @@ +mod event; +mod input; + +use std::cell::Cell; + +use crate::{ + lexer::Token, + parser_api::Parser, + parser_impl::{ + event::{Event, EventProcessor}, + input::{InputPosition, ParserInput}, + }, + SmolStr, + yellow::syntax_error::{ + ParseError, + SyntaxError, + }, +}; + +use crate::SyntaxKind::{self, EOF, TOMBSTONE}; + +pub(crate) trait Sink { + type Tree; + + fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); + fn start_internal(&mut self, kind: SyntaxKind); + fn finish_internal(&mut self); + fn error(&mut self, error: SyntaxError); + fn finish(self) -> Self::Tree; +} + +/// Parse a sequence of tokens into the representative node tree +pub(crate) fn parse_with( + sink: S, + text: &str, + tokens: &[Token], + parser: fn(&mut Parser), +) -> S::Tree { + let mut events = { + let input = input::ParserInput::new(text, tokens); + let parser_impl = ParserImpl::new(&input); + let mut parser_api = Parser(parser_impl); + parser(&mut parser_api); + parser_api.0.into_events() + }; + EventProcessor::new(sink, text, tokens, &mut events) + .process() + .finish() +} + +/// Implementation details of `Parser`, extracted +/// to a separate struct in order not to pollute +/// the public API of the `Parser`. +pub(crate) struct ParserImpl<'t> { + inp: &'t ParserInput<'t>, + + pos: InputPosition, + events: Vec, + steps: Cell, +} + +impl<'t> ParserImpl<'t> { + pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { + ParserImpl { + inp, + + pos: InputPosition::new(), + events: Vec::new(), + steps: Cell::new(0), + } + } + + pub(crate) fn into_events(self) -> Vec { + assert_eq!(self.nth(0), EOF); + self.events + } + + pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { + let c1 = self.inp.kind(self.pos); + let c2 = self.inp.kind(self.pos + 1); + if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) { + Some((c1, c2)) + } else { + None + } + } + + pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { + let c1 = self.inp.kind(self.pos); + let c2 = self.inp.kind(self.pos + 1); + let c3 = self.inp.kind(self.pos + 2); + if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) + && self.inp.start(self.pos + 2) + == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1) + { + Some((c1, c2, c3)) + } else { + None + } + } + + pub(super) fn nth(&self, n: u32) -> SyntaxKind { + let steps = self.steps.get(); + if steps > 10_000_000 { + panic!("the parser seems stuck"); + } + self.steps.set(steps + 1); + + self.inp.kind(self.pos + n) + } + + pub(super) fn at_kw(&self, t: &str) -> bool { + self.inp.text(self.pos) == t + } + + pub(super) fn start(&mut self) -> u32 { + let pos = self.events.len() as u32; + self.event(Event::Start { + kind: TOMBSTONE, + forward_parent: None, + }); + pos + } + + pub(super) fn bump(&mut self) { + let kind = self.nth(0); + if kind == EOF { + return; + } + self.do_bump(kind, 1); + } + + pub(super) fn bump_remap(&mut self, kind: SyntaxKind) { + if self.nth(0) == EOF { + // TODO: panic!? + return; + } + self.do_bump(kind, 1); + } + + pub(super) fn bump_compound(&mut self, kind: SyntaxKind, n: u8) { + self.do_bump(kind, n); + } + + fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { + self.pos += u32::from(n_raw_tokens); + self.event(Event::Token { kind, n_raw_tokens }); + } + + pub(super) fn error(&mut self, msg: String) { + self.event(Event::Error { + msg: ParseError(msg), + }) + } + + pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) { + match self.events[pos as usize] { + Event::Start { + kind: ref mut slot, .. + } => { + *slot = kind; + } + _ => unreachable!(), + } + self.event(Event::Finish); + } + + pub(super) fn abandon(&mut self, pos: u32) { + let idx = pos as usize; + if idx == self.events.len() - 1 { + match self.events.pop() { + Some(Event::Start { + kind: TOMBSTONE, + forward_parent: None, + }) => (), + _ => unreachable!(), + } + } + } + + pub(super) fn precede(&mut self, pos: u32) -> u32 { + let new_pos = self.start(); + match self.events[pos as usize] { + Event::Start { + ref mut forward_parent, + .. + } => { + *forward_parent = Some(new_pos - pos); + } + _ => unreachable!(), + } + new_pos + } + + fn event(&mut self, event: Event) { + self.events.push(event) + } +} diff --git a/crates/ra_syntax/src/parser_impl/mod.rs b/crates/ra_syntax/src/parser_impl/mod.rs deleted file mode 100644 index cb6e370ac..000000000 --- a/crates/ra_syntax/src/parser_impl/mod.rs +++ /dev/null @@ -1,198 +0,0 @@ -mod event; -mod input; - -use std::cell::Cell; - -use crate::{ - lexer::Token, - parser_api::Parser, - parser_impl::{ - event::{Event, EventProcessor}, - input::{InputPosition, ParserInput}, - }, - SmolStr, - yellow::syntax_error::{ - ParseError, - SyntaxError, - }, -}; - -use crate::SyntaxKind::{self, EOF, TOMBSTONE}; - -pub(crate) trait Sink { - type Tree; - - fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); - fn start_internal(&mut self, kind: SyntaxKind); - fn finish_internal(&mut self); - fn error(&mut self, error: SyntaxError); - fn finish(self) -> Self::Tree; -} - -/// Parse a sequence of tokens into the representative node tree -pub(crate) fn parse_with( - sink: S, - text: &str, - tokens: &[Token], - parser: fn(&mut Parser), -) -> S::Tree { - let mut events = { - let input = input::ParserInput::new(text, tokens); - let parser_impl = ParserImpl::new(&input); - let mut parser_api = Parser(parser_impl); - parser(&mut parser_api); - parser_api.0.into_events() - }; - EventProcessor::new(sink, text, tokens, &mut events) - .process() - .finish() -} - -/// Implementation details of `Parser`, extracted -/// to a separate struct in order not to pollute -/// the public API of the `Parser`. -pub(crate) struct ParserImpl<'t> { - inp: &'t ParserInput<'t>, - - pos: InputPosition, - events: Vec, - steps: Cell, -} - -impl<'t> ParserImpl<'t> { - pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { - ParserImpl { - inp, - - pos: InputPosition::new(), - events: Vec::new(), - steps: Cell::new(0), - } - } - - pub(crate) fn into_events(self) -> Vec { - assert_eq!(self.nth(0), EOF); - self.events - } - - pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { - let c1 = self.inp.kind(self.pos); - let c2 = self.inp.kind(self.pos + 1); - if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) { - Some((c1, c2)) - } else { - None - } - } - - pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { - let c1 = self.inp.kind(self.pos); - let c2 = self.inp.kind(self.pos + 1); - let c3 = self.inp.kind(self.pos + 2); - if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) - && self.inp.start(self.pos + 2) - == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1) - { - Some((c1, c2, c3)) - } else { - None - } - } - - pub(super) fn nth(&self, n: u32) -> SyntaxKind { - let steps = self.steps.get(); - if steps > 10_000_000 { - panic!("the parser seems stuck"); - } - self.steps.set(steps + 1); - - self.inp.kind(self.pos + n) - } - - pub(super) fn at_kw(&self, t: &str) -> bool { - self.inp.text(self.pos) == t - } - - pub(super) fn start(&mut self) -> u32 { - let pos = self.events.len() as u32; - self.event(Event::Start { - kind: TOMBSTONE, - forward_parent: None, - }); - pos - } - - pub(super) fn bump(&mut self) { - let kind = self.nth(0); - if kind == EOF { - return; - } - self.do_bump(kind, 1); - } - - pub(super) fn bump_remap(&mut self, kind: SyntaxKind) { - if self.nth(0) == EOF { - // TODO: panic!? - return; - } - self.do_bump(kind, 1); - } - - pub(super) fn bump_compound(&mut self, kind: SyntaxKind, n: u8) { - self.do_bump(kind, n); - } - - fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { - self.pos += u32::from(n_raw_tokens); - self.event(Event::Token { kind, n_raw_tokens }); - } - - pub(super) fn error(&mut self, msg: String) { - self.event(Event::Error { - msg: ParseError(msg), - }) - } - - pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) { - match self.events[pos as usize] { - Event::Start { - kind: ref mut slot, .. - } => { - *slot = kind; - } - _ => unreachable!(), - } - self.event(Event::Finish); - } - - pub(super) fn abandon(&mut self, pos: u32) { - let idx = pos as usize; - if idx == self.events.len() - 1 { - match self.events.pop() { - Some(Event::Start { - kind: TOMBSTONE, - forward_parent: None, - }) => (), - _ => unreachable!(), - } - } - } - - pub(super) fn precede(&mut self, pos: u32) -> u32 { - let new_pos = self.start(); - match self.events[pos as usize] { - Event::Start { - ref mut forward_parent, - .. - } => { - *forward_parent = Some(new_pos - pos); - } - _ => unreachable!(), - } - new_pos - } - - fn event(&mut self, event: Event) { - self.events.push(event) - } -} diff --git a/crates/ra_syntax/src/string_lexing.rs b/crates/ra_syntax/src/string_lexing.rs new file mode 100644 index 000000000..94853331f --- /dev/null +++ b/crates/ra_syntax/src/string_lexing.rs @@ -0,0 +1,13 @@ +mod parser; +mod byte; +mod byte_string; +mod char; +mod string; + +pub use self::{ + byte::parse_byte_literal, + byte_string::parse_byte_string_literal, + char::parse_char_literal, + parser::{CharComponent, CharComponentKind, StringComponent, StringComponentKind}, + string::parse_string_literal, +}; diff --git a/crates/ra_syntax/src/string_lexing/mod.rs b/crates/ra_syntax/src/string_lexing/mod.rs deleted file mode 100644 index 94853331f..000000000 --- a/crates/ra_syntax/src/string_lexing/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod parser; -mod byte; -mod byte_string; -mod char; -mod string; - -pub use self::{ - byte::parse_byte_literal, - byte_string::parse_byte_string_literal, - char::parse_char_literal, - parser::{CharComponent, CharComponentKind, StringComponent, StringComponentKind}, - string::parse_string_literal, -}; diff --git a/crates/ra_syntax/src/syntax_kinds.rs b/crates/ra_syntax/src/syntax_kinds.rs new file mode 100644 index 000000000..d53886676 --- /dev/null +++ b/crates/ra_syntax/src/syntax_kinds.rs @@ -0,0 +1,26 @@ +mod generated; + +use crate::SyntaxKind::*; +use std::fmt; + +pub use self::generated::SyntaxKind; + +impl fmt::Debug for SyntaxKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let name = self.info().name; + f.write_str(name) + } +} + +pub(crate) struct SyntaxInfo { + pub name: &'static str, +} + +impl SyntaxKind { + pub fn is_trivia(self) -> bool { + match self { + WHITESPACE | COMMENT => true, + _ => false, + } + } +} diff --git a/crates/ra_syntax/src/syntax_kinds/mod.rs b/crates/ra_syntax/src/syntax_kinds/mod.rs deleted file mode 100644 index d53886676..000000000 --- a/crates/ra_syntax/src/syntax_kinds/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -mod generated; - -use crate::SyntaxKind::*; -use std::fmt; - -pub use self::generated::SyntaxKind; - -impl fmt::Debug for SyntaxKind { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let name = self.info().name; - f.write_str(name) - } -} - -pub(crate) struct SyntaxInfo { - pub name: &'static str, -} - -impl SyntaxKind { - pub fn is_trivia(self) -> bool { - match self { - WHITESPACE | COMMENT => true, - _ => false, - } - } -} diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs new file mode 100644 index 000000000..bdee8120c --- /dev/null +++ b/crates/ra_syntax/src/validation.rs @@ -0,0 +1,24 @@ +use crate::{ + algo::visit::{visitor_ctx, VisitorCtx}, + ast, + SourceFileNode, + yellow::SyntaxError, +}; + +mod byte; +mod byte_string; +mod char; +mod string; + +pub(crate) fn validate(file: &SourceFileNode) -> Vec { + let mut errors = Vec::new(); + for node in file.syntax().descendants() { + let _ = visitor_ctx(&mut errors) + .visit::(self::byte::validate_byte_node) + .visit::(self::byte_string::validate_byte_string_node) + .visit::(self::char::validate_char_node) + .visit::(self::string::validate_string_node) + .accept(node); + } + errors +} diff --git a/crates/ra_syntax/src/validation/mod.rs b/crates/ra_syntax/src/validation/mod.rs deleted file mode 100644 index bdee8120c..000000000 --- a/crates/ra_syntax/src/validation/mod.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::{ - algo::visit::{visitor_ctx, VisitorCtx}, - ast, - SourceFileNode, - yellow::SyntaxError, -}; - -mod byte; -mod byte_string; -mod char; -mod string; - -pub(crate) fn validate(file: &SourceFileNode) -> Vec { - let mut errors = Vec::new(); - for node in file.syntax().descendants() { - let _ = visitor_ctx(&mut errors) - .visit::(self::byte::validate_byte_node) - .visit::(self::byte_string::validate_byte_string_node) - .visit::(self::char::validate_char_node) - .visit::(self::string::validate_string_node) - .accept(node); - } - errors -} diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs new file mode 100644 index 000000000..cacd89dc8 --- /dev/null +++ b/crates/ra_syntax/src/yellow.rs @@ -0,0 +1,161 @@ +mod builder; +pub mod syntax_error; +mod syntax_text; + +use self::syntax_text::SyntaxText; +use crate::{SmolStr, SyntaxKind, TextRange}; +use rowan::Types; +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +pub(crate) use self::builder::GreenBuilder; +pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; +pub use rowan::{TreeRoot, WalkEvent}; + +#[derive(Debug, Clone, Copy)] +pub enum RaTypes {} +impl Types for RaTypes { + type Kind = SyntaxKind; + type RootData = Vec; +} + +pub type OwnedRoot = ::rowan::OwnedRoot; +pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; + +pub type GreenNode = ::rowan::GreenNode; + +#[derive(Clone, Copy)] +pub struct SyntaxNode = OwnedRoot>(pub(crate) ::rowan::SyntaxNode); +pub type SyntaxNodeRef<'a> = SyntaxNode>; + +impl PartialEq> for SyntaxNode +where + R1: TreeRoot, + R2: TreeRoot, +{ + fn eq(&self, other: &SyntaxNode) -> bool { + self.0 == other.0 + } +} + +impl> Eq for SyntaxNode {} +impl> Hash for SyntaxNode { + fn hash(&self, state: &mut H) { + self.0.hash(state) + } +} + +impl SyntaxNode { + pub(crate) fn new(green: GreenNode, errors: Vec) -> SyntaxNode { + SyntaxNode(::rowan::SyntaxNode::new(green, errors)) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Direction { + Next, + Prev, +} + +impl<'a> SyntaxNodeRef<'a> { + pub fn leaf_text(self) -> Option<&'a SmolStr> { + self.0.leaf_text() + } + pub fn ancestors(self) -> impl Iterator> { + crate::algo::generate(Some(self), |&node| node.parent()) + } + pub fn descendants(self) -> impl Iterator> { + self.preorder().filter_map(|event| match event { + WalkEvent::Enter(node) => Some(node), + WalkEvent::Leave(_) => None, + }) + } + pub fn siblings(self, direction: Direction) -> impl Iterator> { + crate::algo::generate(Some(self), move |&node| match direction { + Direction::Next => node.next_sibling(), + Direction::Prev => node.prev_sibling(), + }) + } + pub fn preorder(self) -> impl Iterator>> { + self.0.preorder().map(|event| match event { + WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), + WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), + }) + } +} + +impl> SyntaxNode { + pub(crate) fn root_data(&self) -> &Vec { + self.0.root_data() + } + pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { + self.0.replace_with(replacement) + } + pub fn borrowed<'a>(&'a self) -> SyntaxNode> { + SyntaxNode(self.0.borrowed()) + } + pub fn owned(&self) -> SyntaxNode { + SyntaxNode(self.0.owned()) + } + pub fn kind(&self) -> SyntaxKind { + self.0.kind() + } + pub fn range(&self) -> TextRange { + self.0.range() + } + pub fn text(&self) -> SyntaxText { + SyntaxText::new(self.borrowed()) + } + pub fn is_leaf(&self) -> bool { + self.0.is_leaf() + } + pub fn parent(&self) -> Option> { + self.0.parent().map(SyntaxNode) + } + pub fn first_child(&self) -> Option> { + self.0.first_child().map(SyntaxNode) + } + pub fn last_child(&self) -> Option> { + self.0.last_child().map(SyntaxNode) + } + pub fn next_sibling(&self) -> Option> { + self.0.next_sibling().map(SyntaxNode) + } + pub fn prev_sibling(&self) -> Option> { + self.0.prev_sibling().map(SyntaxNode) + } + pub fn children(&self) -> SyntaxNodeChildren { + SyntaxNodeChildren(self.0.children()) + } +} + +impl> fmt::Debug for SyntaxNode { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; + if has_short_text(self.kind()) { + write!(fmt, " \"{}\"", self.text())?; + } + Ok(()) + } +} + +#[derive(Debug)] +pub struct SyntaxNodeChildren>(::rowan::SyntaxNodeChildren); + +impl> Iterator for SyntaxNodeChildren { + type Item = SyntaxNode; + + fn next(&mut self) -> Option> { + self.0.next().map(SyntaxNode) + } +} + +fn has_short_text(kind: SyntaxKind) -> bool { + use crate::SyntaxKind::*; + match kind { + IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true, + _ => false, + } +} diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs deleted file mode 100644 index cacd89dc8..000000000 --- a/crates/ra_syntax/src/yellow/mod.rs +++ /dev/null @@ -1,161 +0,0 @@ -mod builder; -pub mod syntax_error; -mod syntax_text; - -use self::syntax_text::SyntaxText; -use crate::{SmolStr, SyntaxKind, TextRange}; -use rowan::Types; -use std::{ - fmt, - hash::{Hash, Hasher}, -}; - -pub(crate) use self::builder::GreenBuilder; -pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; -pub use rowan::{TreeRoot, WalkEvent}; - -#[derive(Debug, Clone, Copy)] -pub enum RaTypes {} -impl Types for RaTypes { - type Kind = SyntaxKind; - type RootData = Vec; -} - -pub type OwnedRoot = ::rowan::OwnedRoot; -pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; - -pub type GreenNode = ::rowan::GreenNode; - -#[derive(Clone, Copy)] -pub struct SyntaxNode = OwnedRoot>(pub(crate) ::rowan::SyntaxNode); -pub type SyntaxNodeRef<'a> = SyntaxNode>; - -impl PartialEq> for SyntaxNode -where - R1: TreeRoot, - R2: TreeRoot, -{ - fn eq(&self, other: &SyntaxNode) -> bool { - self.0 == other.0 - } -} - -impl> Eq for SyntaxNode {} -impl> Hash for SyntaxNode { - fn hash(&self, state: &mut H) { - self.0.hash(state) - } -} - -impl SyntaxNode { - pub(crate) fn new(green: GreenNode, errors: Vec) -> SyntaxNode { - SyntaxNode(::rowan::SyntaxNode::new(green, errors)) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Direction { - Next, - Prev, -} - -impl<'a> SyntaxNodeRef<'a> { - pub fn leaf_text(self) -> Option<&'a SmolStr> { - self.0.leaf_text() - } - pub fn ancestors(self) -> impl Iterator> { - crate::algo::generate(Some(self), |&node| node.parent()) - } - pub fn descendants(self) -> impl Iterator> { - self.preorder().filter_map(|event| match event { - WalkEvent::Enter(node) => Some(node), - WalkEvent::Leave(_) => None, - }) - } - pub fn siblings(self, direction: Direction) -> impl Iterator> { - crate::algo::generate(Some(self), move |&node| match direction { - Direction::Next => node.next_sibling(), - Direction::Prev => node.prev_sibling(), - }) - } - pub fn preorder(self) -> impl Iterator>> { - self.0.preorder().map(|event| match event { - WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), - WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), - }) - } -} - -impl> SyntaxNode { - pub(crate) fn root_data(&self) -> &Vec { - self.0.root_data() - } - pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { - self.0.replace_with(replacement) - } - pub fn borrowed<'a>(&'a self) -> SyntaxNode> { - SyntaxNode(self.0.borrowed()) - } - pub fn owned(&self) -> SyntaxNode { - SyntaxNode(self.0.owned()) - } - pub fn kind(&self) -> SyntaxKind { - self.0.kind() - } - pub fn range(&self) -> TextRange { - self.0.range() - } - pub fn text(&self) -> SyntaxText { - SyntaxText::new(self.borrowed()) - } - pub fn is_leaf(&self) -> bool { - self.0.is_leaf() - } - pub fn parent(&self) -> Option> { - self.0.parent().map(SyntaxNode) - } - pub fn first_child(&self) -> Option> { - self.0.first_child().map(SyntaxNode) - } - pub fn last_child(&self) -> Option> { - self.0.last_child().map(SyntaxNode) - } - pub fn next_sibling(&self) -> Option> { - self.0.next_sibling().map(SyntaxNode) - } - pub fn prev_sibling(&self) -> Option> { - self.0.prev_sibling().map(SyntaxNode) - } - pub fn children(&self) -> SyntaxNodeChildren { - SyntaxNodeChildren(self.0.children()) - } -} - -impl> fmt::Debug for SyntaxNode { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; - if has_short_text(self.kind()) { - write!(fmt, " \"{}\"", self.text())?; - } - Ok(()) - } -} - -#[derive(Debug)] -pub struct SyntaxNodeChildren>(::rowan::SyntaxNodeChildren); - -impl> Iterator for SyntaxNodeChildren { - type Item = SyntaxNode; - - fn next(&mut self) -> Option> { - self.0.next().map(SyntaxNode) - } -} - -fn has_short_text(kind: SyntaxKind) -> bool { - use crate::SyntaxKind::*; - match kind { - IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true, - _ => false, - } -} -- cgit v1.2.3