diff --git a/crates/gramatika-macro/Cargo.toml b/crates/gramatika-macro/Cargo.toml index 4686fc7..5f7b6dd 100644 --- a/crates/gramatika-macro/Cargo.toml +++ b/crates/gramatika-macro/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "gramatika-macro" -version = "0.4.3" -edition = "2018" +version = "0.5.0" +edition = "2021" authors = ["Danny McGee "] license = "MIT OR Apache-2.0" readme = "../../README.md" @@ -18,8 +18,8 @@ path = "tests/tests.rs" [dependencies] anyhow = "1" arcstr = "1.1" -convert_case = "0.4" -itertools = "0.10" +convert_case = "0.6" +itertools = "0.12" once_cell = "1" proc-macro2 = "1.0" quote = "1.0" diff --git a/crates/gramatika-macro/src/common.rs b/crates/gramatika-macro/src/common.rs index 3c5bd14..ba0802d 100644 --- a/crates/gramatika-macro/src/common.rs +++ b/crates/gramatika-macro/src/common.rs @@ -29,12 +29,12 @@ impl VariantIdents { | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" - | "move" | "mut" | "pub" - | "ref" | "return" | "static" - | "struct" | "super" | "trait" - | "type" | "union" | "unsafe" - | "use" | "where" | "while" - | "yield" + | "move" | "mut" | "path" + | "pub" | "ref" | "return" + | "static" | "struct" + | "super" | "trait" | "type" + | "union" | "unsafe" | "use" + | "where" | "while" | "yield" ) { format_ident!("{}_", snake) } else { diff --git a/crates/gramatika-macro/src/debug_lisp.rs b/crates/gramatika-macro/src/debug_lisp.rs index bf78163..f9f54d7 100644 --- a/crates/gramatika-macro/src/debug_lisp.rs +++ b/crates/gramatika-macro/src/debug_lisp.rs @@ -43,7 +43,7 @@ fn derive_debug_struct( }; let stream = quote! { - impl#generics ::gramatika::DebugLisp for #ident#generics { + impl #generics ::gramatika::DebugLisp for #ident #generics { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>, indent: usize) -> ::core::fmt::Result { ::gramatika::DebugLispStruct::new(f, indent, stringify!(#ident)) #(#field_method_call)* @@ -51,7 +51,7 @@ fn derive_debug_struct( } } - impl#generics ::core::fmt::Debug for #ident#generics { + impl #generics ::core::fmt::Debug for #ident #generics { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { ::gramatika::DebugLisp::fmt(self, f, 0) } @@ -63,22 +63,25 @@ fn derive_debug_struct( fn derive_debug_enum(ident: &Ident, generics: &Generics, data: &DataEnum) -> TokenStream { let variant_name = data.variants.iter().map(|variant| &variant.ident); - let variant_inner_type = data - .variants - .iter() - .map(|variant| match &variant.fields { - Fields::Unnamed(fields) => fields.unnamed.iter().map(|field| &field.ty), - Fields::Named(_) => { - panic!("`#[derive(DebugLisp)]` is not supported for enum variants with named fields") - } - Fields::Unit => { - panic!("`#[derive(DebugLisp)]` is not supported for unit enum variants") - } - }) - .flatten(); + let variant_inner_type = + data.variants + .iter() + .flat_map(|variant| match &variant.fields { + Fields::Unnamed(fields) => fields.unnamed.iter().map(|field| &field.ty), + Fields::Named(_) => { + panic!( + "`#[derive(DebugLisp)]` is not supported for enum variants with named fields" + ) + } + Fields::Unit => { + panic!( + "`#[derive(DebugLisp)]` is not supported for unit enum variants" + ) + } + }); let stream = quote! { - impl#generics ::gramatika::DebugLisp for #ident#generics { + impl #generics ::gramatika::DebugLisp for #ident #generics { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>, indent: usize) -> ::std::fmt::Result { write!(f, "({}::", stringify!(#ident))?; @@ -93,7 +96,7 @@ fn derive_debug_enum(ident: &Ident, generics: &Generics, data: &DataEnum) -> Tok } } - impl#generics ::core::fmt::Debug for #ident#generics { + impl #generics ::core::fmt::Debug for #ident #generics { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { ::gramatika::DebugLisp::fmt(self, f, 0) } @@ -109,7 +112,7 @@ pub fn derive_token(input: TokenStream) -> TokenStream { let generics = &ast.generics; let stream = quote! { - impl#generics ::gramatika::DebugLisp for #ident#generics { + impl #generics ::gramatika::DebugLisp for #ident #generics { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>, _: usize) -> ::std::fmt::Result { write!( f, diff --git a/crates/gramatika-macro/src/lexer.rs b/crates/gramatika-macro/src/lexer.rs index 884df96..a1aab49 100644 --- a/crates/gramatika-macro/src/lexer.rs +++ b/crates/gramatika-macro/src/lexer.rs @@ -31,6 +31,7 @@ pub fn derive(input: TokenStream) -> TokenStream { >> } + #[allow(non_camel_case_types)] type __TOKEN_CTOR = fn(::gramatika::Substr, ::gramatika::Span) -> #enum_ident; impl ::gramatika::Lexer for #lexer_ident { diff --git a/crates/gramatika-macro/src/token.rs b/crates/gramatika-macro/src/token.rs index 9d03de6..1b1704f 100644 --- a/crates/gramatika-macro/src/token.rs +++ b/crates/gramatika-macro/src/token.rs @@ -100,7 +100,7 @@ pub fn derive(input: pm::TokenStream) -> pm::TokenStream { } } - impl#generics #ident#generics { + impl #generics #ident #generics { pub fn as_inner(&self) -> (::gramatika::Substr, ::gramatika::Span) { match self {#( Self::#variant_ident(lexeme, span) => (lexeme.clone(), *span) @@ -115,7 +115,7 @@ pub fn derive(input: pm::TokenStream) -> pm::TokenStream { } #( - #[macro_export] + #[allow(unused_macros)] macro_rules! #ctor_ident { ($lexeme:literal) => { #ident::#ctor_ident( @@ -130,9 +130,12 @@ pub fn derive(input: pm::TokenStream) -> pm::TokenStream { ) }; } + + #[allow(unused)] + pub(crate) use #ctor_ident; )* - impl#generics ::gramatika::Token for #ident#generics { + impl #generics ::gramatika::Token for #ident #generics { type Kind = #kind_ident; fn lexeme(&self) -> ::gramatika::Substr { @@ -156,13 +159,13 @@ pub fn derive(input: pm::TokenStream) -> pm::TokenStream { } } - impl#generics ::gramatika::Spanned for #ident#generics { + impl #generics ::gramatika::Spanned for #ident #generics { fn span(&self) -> ::gramatika::Span { self.as_inner().1 } } - impl#generics Clone for #ident#generics { + impl #generics Clone for #ident #generics { fn clone(&self) -> Self { match self {#( #ident::#variant_ident(lexeme, span) => #ident::#variant_ident(lexeme.clone(), *span) diff --git a/crates/gramatika-macro/src/traversal/codegen.rs b/crates/gramatika-macro/src/traversal/codegen.rs index f37a89d..c050d7a 100644 --- a/crates/gramatika-macro/src/traversal/codegen.rs +++ b/crates/gramatika-macro/src/traversal/codegen.rs @@ -21,7 +21,7 @@ impl ToTokens for VisitorDef { }); if let Some(Walk) = &self.walker_ident { - if let Some(signature) = self.signatures.get(0) { + if let Some(signature) = self.signatures.first() { let swn = signature.param_type.ownership; let pwn = self.receiver.ownership; let walk = format_ident!("{}", Walk.to_string().to_case(Case::Snake)); diff --git a/crates/gramatika/Cargo.toml b/crates/gramatika/Cargo.toml index 8c0ebc2..1cd6c2e 100644 --- a/crates/gramatika/Cargo.toml +++ b/crates/gramatika/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "gramatika" -version = "0.4.3" -edition = "2018" +version = "0.5.0" +edition = "2021" authors = ["Danny McGee "] license = "MIT OR Apache-2.0" readme = "../../README.md" @@ -17,7 +17,7 @@ once_cell = { version = "1.8", optional = true } regex-automata = { version = "0.1", optional = true } [dependencies.gramatika-macro] -version = "0.4.3" +version = "0.5.0" path = "../gramatika-macro" optional = true diff --git a/crates/gramatika/src/span.rs b/crates/gramatika/src/span.rs index 19d33c2..a386ddf 100644 --- a/crates/gramatika/src/span.rs +++ b/crates/gramatika/src/span.rs @@ -24,19 +24,19 @@ pub struct Position { pub character: usize, } -impl PartialOrd for Position { - fn partial_cmp(&self, other: &Self) -> Option { +impl Ord for Position { + fn cmp(&self, other: &Self) -> Ordering { if self.line == other.line { - Some(self.character.cmp(&other.character)) + self.character.cmp(&other.character) } else { - Some(self.line.cmp(&other.line)) + self.line.cmp(&other.line) } } } -impl Ord for Position { - fn cmp(&self, other: &Self) -> Ordering { - self.partial_cmp(other).unwrap() +impl PartialOrd for Position { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) } } diff --git a/examples/expand/src/main.rs b/examples/expand/src/main.rs index 1df2e09..ec11b8c 100644 --- a/examples/expand/src/main.rs +++ b/examples/expand/src/main.rs @@ -168,14 +168,9 @@ pub struct ExprStmt; pub enum Expr {} -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, Default, PartialEq, Eq)] pub enum FlowControl { + #[default] Continue, Break, } - -impl Default for FlowControl { - fn default() -> Self { - FlowControl::Continue - } -} diff --git a/examples/lox/src/decl.rs b/examples/lox/src/decl.rs index b124e8c..9292a87 100644 --- a/examples/lox/src/decl.rs +++ b/examples/lox/src/decl.rs @@ -1,6 +1,10 @@ use gramatika::{Parse, ParseStreamer, Result, Spanned, SpannedError, Token as _}; -use crate::*; +use crate::{ + expr::{Expr, FunExpr}, + tokens::{brace, operator, punct, Token, TokenKind}, + ParseStream, +}; #[derive(DebugLisp)] pub enum Decl { diff --git a/examples/lox/src/expr.rs b/examples/lox/src/expr.rs index 566a07a..71c7894 100644 --- a/examples/lox/src/expr.rs +++ b/examples/lox/src/expr.rs @@ -1,6 +1,10 @@ use gramatika::{Parse, ParseStreamer, Result, Spanned, SpannedError, Token as _}; -use crate::*; +use crate::{ + stmt::Stmt, + tokens::{brace, keyword, operator, punct, Token, TokenKind}, + ParseStream, +}; #[derive(DebugLisp)] pub enum Expr { diff --git a/examples/lox/src/lib.rs b/examples/lox/src/lib.rs index a06f117..f4d3689 100644 --- a/examples/lox/src/lib.rs +++ b/examples/lox/src/lib.rs @@ -9,12 +9,9 @@ mod expr; mod stmt; mod tokens; -use decl::*; -use expr::*; -use stmt::*; -use tokens::*; - +use stmt::Program; pub use tokens::Lexer; +use tokens::Token; use gramatika::ParseStreamer; diff --git a/examples/lox/src/stmt.rs b/examples/lox/src/stmt.rs index 7652001..3aae6f5 100644 --- a/examples/lox/src/stmt.rs +++ b/examples/lox/src/stmt.rs @@ -1,6 +1,11 @@ use gramatika::{Parse, ParseStreamer, Result, SpannedError, Token as _}; -use crate::*; +use crate::{ + decl::Decl, + expr::Expr, + tokens::{brace, keyword, punct, Token, TokenKind}, + ParseStream, +}; #[derive(DebugLisp)] pub struct Program { diff --git a/examples/lox/src/tests/error.rs b/examples/lox/src/tests/error.rs index 223984d..76729d9 100644 --- a/examples/lox/src/tests/error.rs +++ b/examples/lox/src/tests/error.rs @@ -1,6 +1,6 @@ use gramatika::{ParseStream, ParseStreamer}; -use crate::*; +use crate::stmt::Program; #[test] fn error_formatting() { diff --git a/examples/lox/src/tests/lexer.rs b/examples/lox/src/tests/lexer.rs index bc99691..05b532c 100644 --- a/examples/lox/src/tests/lexer.rs +++ b/examples/lox/src/tests/lexer.rs @@ -1,4 +1,4 @@ -use crate::{Lexer, Token}; +use crate::{tokens::Token, Lexer}; use gramatika::{ArcStr, Lexer as _}; #[test] diff --git a/examples/lox/src/tests/parse.rs b/examples/lox/src/tests/parse.rs index 1c0e3e0..7bb582b 100644 --- a/examples/lox/src/tests/parse.rs +++ b/examples/lox/src/tests/parse.rs @@ -1,6 +1,6 @@ use gramatika::{ParseStream, ParseStreamer}; -use crate::*; +use crate::stmt::Program; #[test] fn print_stmt() { diff --git a/examples/lox_manual_impl/src/decl.rs b/examples/lox_manual_impl/src/decl.rs index 5b01cce..3a08fd7 100644 --- a/examples/lox_manual_impl/src/decl.rs +++ b/examples/lox_manual_impl/src/decl.rs @@ -1,11 +1,9 @@ use gramatika::{Parse, ParseStreamer, Result, SpannedError, Token as _}; use crate::{ - brace, expr::{Expr, FunExpr}, - operator, parse::ParseStream, - punct, + tokens::{brace, operator, punct}, tokens::{Token, TokenKind}, }; diff --git a/examples/lox_manual_impl/src/expr.rs b/examples/lox_manual_impl/src/expr.rs index 10b0f70..98256bb 100644 --- a/examples/lox_manual_impl/src/expr.rs +++ b/examples/lox_manual_impl/src/expr.rs @@ -1,11 +1,9 @@ use gramatika::{Parse, ParseStreamer, Result, SpannedError, Token as _}; use crate::{ - brace, keyword, operator, parse::ParseStream, - punct, stmt::Stmt, - tokens::{Token, TokenKind}, + tokens::{brace, keyword, operator, punct, Token, TokenKind}, }; #[derive(DebugLisp)] diff --git a/examples/lox_manual_impl/src/stmt.rs b/examples/lox_manual_impl/src/stmt.rs index 1ee6ac5..10b2ea3 100644 --- a/examples/lox_manual_impl/src/stmt.rs +++ b/examples/lox_manual_impl/src/stmt.rs @@ -1,13 +1,10 @@ use gramatika::{Parse, ParseStreamer, Result, SpannedError, Token as _}; use crate::{ - brace, decl::Decl, expr::Expr, - keyword, parse::ParseStream, - punct, - tokens::{Token, TokenKind}, + tokens::{brace, keyword, punct, Token, TokenKind}, }; #[derive(DebugLisp)] diff --git a/examples/lox_manual_impl/src/tokens.rs b/examples/lox_manual_impl/src/tokens.rs index 92bf846..51da4c1 100644 --- a/examples/lox_manual_impl/src/tokens.rs +++ b/examples/lox_manual_impl/src/tokens.rs @@ -294,7 +294,7 @@ impl Token { } } -#[macro_export] +#[allow(unused_macros)] macro_rules! brace { ($lexeme:literal) => { Token::brace( @@ -309,7 +309,10 @@ macro_rules! brace { ) }; } -#[macro_export] +#[allow(unused)] +pub(crate) use brace; + +#[allow(unused_macros)] macro_rules! ident { ($lexeme:literal) => { Token::ident( @@ -324,7 +327,10 @@ macro_rules! ident { ) }; } -#[macro_export] +#[allow(unused)] +pub(crate) use ident; + +#[allow(unused_macros)] macro_rules! keyword { ($lexeme:literal) => { Token::keyword( @@ -339,7 +345,10 @@ macro_rules! keyword { ) }; } -#[macro_export] +#[allow(unused)] +pub(crate) use keyword; + +#[allow(unused_macros)] macro_rules! num_lit { ($lexeme:literal) => { Token::num_lit( @@ -354,7 +363,10 @@ macro_rules! num_lit { ) }; } -#[macro_export] +#[allow(unused)] +pub(crate) use num_lit; + +#[allow(unused_macros)] macro_rules! str_lit { ($lexeme:literal) => { Token::str_lit( @@ -369,7 +381,10 @@ macro_rules! str_lit { ) }; } -#[macro_export] +#[allow(unused)] +pub(crate) use str_lit; + +#[allow(unused_macros)] macro_rules! operator { ($lexeme:literal) => { Token::operator( @@ -384,7 +399,10 @@ macro_rules! operator { ) }; } -#[macro_export] +#[allow(unused)] +pub(crate) use operator; + +#[allow(unused_macros)] macro_rules! punct { ($lexeme:literal) => { Token::punct( @@ -399,7 +417,8 @@ macro_rules! punct { ) }; } -pub use {brace, ident, keyword, num_lit, operator, punct, str_lit}; +#[allow(unused)] +pub(crate) use punct; impl Clone for Token { fn clone(&self) -> Self {