From 9c75e9fa7d2738c206a43c57297c719d6cc62382 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 14 Apr 2024 15:29:01 +0200 Subject: [PATCH] Deduplicate Edition enum --- Cargo.lock | 1 + crates/parser/src/edition.rs | 55 ++++++++++++++++++++++++++++++++++++ crates/parser/src/lib.rs | 3 +- crates/parser/src/parser.rs | 8 +----- crates/span/Cargo.toml | 1 + crates/span/src/lib.rs | 53 ++-------------------------------- crates/span/src/map.rs | 4 +-- crates/syntax/src/lib.rs | 2 +- 8 files changed, 65 insertions(+), 62 deletions(-) create mode 100644 crates/parser/src/edition.rs diff --git a/Cargo.lock b/Cargo.lock index 0de4ae46df4..486f78c5ab7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1823,6 +1823,7 @@ dependencies = [ "salsa", "stdx", "syntax", + "text-size", "vfs", ] diff --git a/crates/parser/src/edition.rs b/crates/parser/src/edition.rs new file mode 100644 index 00000000000..26178544f9b --- /dev/null +++ b/crates/parser/src/edition.rs @@ -0,0 +1,55 @@ +//! The edition of the Rust language used in a crate. +// Ideally this would be defined in the span crate, but the dependency chain is all over the place +// wrt to span, parser and syntax. +use std::fmt; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Edition { + Edition2015, + Edition2018, + Edition2021, + Edition2024, +} + +impl Edition { + pub const CURRENT: Edition = Edition::Edition2021; + pub const DEFAULT: Edition = Edition::Edition2015; +} + +#[derive(Debug)] +pub struct ParseEditionError { + invalid_input: String, +} + +impl std::error::Error for ParseEditionError {} +impl fmt::Display for ParseEditionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "invalid edition: {:?}", self.invalid_input) + } +} + +impl std::str::FromStr for Edition { + type Err = ParseEditionError; + + fn from_str(s: &str) -> Result { + let res = match s { + "2015" => Edition::Edition2015, + "2018" => Edition::Edition2018, + "2021" => Edition::Edition2021, + "2024" => Edition::Edition2024, + _ => return Err(ParseEditionError { invalid_input: s.to_owned() }), + }; + Ok(res) + } +} + +impl fmt::Display for Edition { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Edition::Edition2015 => "2015", + Edition::Edition2018 => "2018", + Edition::Edition2021 => "2021", + Edition::Edition2024 => "2024", + }) + } +} diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index f29fba08dd3..c7ad025f6b0 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -26,6 +26,7 @@ #[cfg(feature = "in-rust-tree")] extern crate rustc_lexer; +mod edition; mod event; mod grammar; mod input; @@ -42,10 +43,10 @@ pub(crate) use token_set::TokenSet; pub use crate::{ + edition::Edition, input::Input, lexed_str::LexedStr, output::{Output, Step}, - parser::Edition, shortcuts::StrStep, syntax_kind::SyntaxKind, }; diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs index f48073701e4..5b901f911dc 100644 --- a/crates/parser/src/parser.rs +++ b/crates/parser/src/parser.rs @@ -8,6 +8,7 @@ use crate::{ event::Event, input::Input, + Edition, SyntaxKind::{self, EOF, ERROR, TOMBSTONE}, TokenSet, T, }; @@ -29,13 +30,6 @@ pub(crate) struct Parser<'t> { _edition: Edition, } -#[non_exhaustive] -pub enum Edition { - Edition2015, - Edition2018, - Edition2021, -} - static PARSER_STEP_LIMIT: Limit = Limit::new(15_000_000); impl<'t> Parser<'t> { diff --git a/crates/span/Cargo.toml b/crates/span/Cargo.toml index cbda91f0a59..9f85f0107cc 100644 --- a/crates/span/Cargo.toml +++ b/crates/span/Cargo.toml @@ -14,6 +14,7 @@ la-arena.workspace = true salsa.workspace = true rustc-hash.workspace = true hashbrown.workspace = true +text-size.workspace = true # local deps vfs.workspace = true diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs index c9109c72d0d..8ca7bc2d38a 100644 --- a/crates/span/src/lib.rs +++ b/crates/span/src/lib.rs @@ -13,59 +13,10 @@ map::{RealSpanMap, SpanMap}, }; -pub use syntax::{TextRange, TextSize}; +pub use syntax::Edition; +pub use text_size::{TextRange, TextSize}; pub use vfs::FileId; -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Edition { - Edition2015, - Edition2018, - Edition2021, - Edition2024, -} - -impl Edition { - pub const CURRENT: Edition = Edition::Edition2021; - pub const DEFAULT: Edition = Edition::Edition2015; -} - -#[derive(Debug)] -pub struct ParseEditionError { - invalid_input: String, -} - -impl std::error::Error for ParseEditionError {} -impl fmt::Display for ParseEditionError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "invalid edition: {:?}", self.invalid_input) - } -} - -impl std::str::FromStr for Edition { - type Err = ParseEditionError; - - fn from_str(s: &str) -> Result { - let res = match s { - "2015" => Edition::Edition2015, - "2018" => Edition::Edition2018, - "2021" => Edition::Edition2021, - "2024" => Edition::Edition2024, - _ => return Err(ParseEditionError { invalid_input: s.to_owned() }), - }; - Ok(res) - } -} - -impl fmt::Display for Edition { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(match self { - Edition::Edition2015 => "2015", - Edition::Edition2018 => "2018", - Edition::Edition2021 => "2021", - Edition::Edition2024 => "2024", - }) - } -} #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct FilePosition { pub file_id: FileId, diff --git a/crates/span/src/map.rs b/crates/span/src/map.rs index 1f396a1e97b..6d8c9c30fb5 100644 --- a/crates/span/src/map.rs +++ b/crates/span/src/map.rs @@ -4,11 +4,11 @@ use std::{fmt, hash::Hash}; use stdx::{always, itertools::Itertools}; -use syntax::{TextRange, TextSize}; use vfs::FileId; use crate::{ - ErasedFileAstId, Span, SpanAnchor, SpanData, SyntaxContextId, ROOT_ERASED_FILE_AST_ID, + ErasedFileAstId, Span, SpanAnchor, SpanData, SyntaxContextId, TextRange, TextSize, + ROOT_ERASED_FILE_AST_ID, }; /// Maps absolute text ranges for the corresponding file to the relevant span data. diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 1c628a948c2..a8a91bc412d 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -60,7 +60,7 @@ }, token_text::TokenText, }; -pub use parser::{SyntaxKind, T}; +pub use parser::{Edition, SyntaxKind, T}; pub use rowan::{ api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, TokenAtOffset, WalkEvent,